Untitled

mail@pastecode.io avatar
unknown
kotlin
3 years ago
3.2 kB
2
Indexable
Never
package com.example.appsem5.pakiety

import android.app.*
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.icu.util.Calendar
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.example.appsem5.AlarmReceiver
import com.example.appsem5.R

class Sport : AppCompatActivity() {

    lateinit var alarmManager: AlarmManager
    lateinit var sharedPreferences: SharedPreferences
    lateinit var notificationManager: NotificationManager
    private var isPackageActive = false



    @RequiresApi(Build.VERSION_CODES.O)
    @Suppress("DEPRECATION")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.sport)
        val title: TextView = findViewById(R.id.Package1textView1)
        val description: TextView = findViewById(R.id.Package1textView2)
        val activity1: TextView = findViewById(R.id.Package1textView3)
        val activity2: TextView = findViewById(R.id.Package1textView4)
        val activity3: TextView = findViewById(R.id.Package1textView5)
        val activity4: TextView = findViewById(R.id.Package1textView6)
        val activeButton = findViewById<Button>(R.id.Package1button1)
        val calendar = Calendar.getInstance()

        notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        sharedPreferences = getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
        title.setText("Sport")
        description.setText("Ten pakiet pozwoli Ci utrzymac dobra forme")
        activity1.setText("15 minut rozciagania")
        activity2.setText("10 pompek")
        activity3.setText("15 przysiadow")
        activity4.setText("Padnij powstan przez 30 sekund")


        activeButton.setOnClickListener {
            isPackageActive = true
            alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
            intent = Intent(this, AlarmReceiver("Sport", "Czas na 10 pompek")::class.java)
            val pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)

            sharedPreferences.edit().putBoolean("PACK1AC", isPackageActive).apply()
            calendar.timeInMillis = System.currentTimeMillis()
            calendar.set(Calendar.HOUR_OF_DAY, 10)
            calendar.set(Calendar.MINUTE, 51)
            calendar.set(Calendar.SECOND, 0)

            // Starts the alarm manager
            alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP,
                calendar.timeInMillis,
                AlarmManager.INTERVAL_DAY,
                pendingIntent
            )
            startActivity(Intent(this, WidokAktywnychPakietow::class.java))
        }

        val deactiveButton = findViewById<Button>(R.id.Package1button2)
        deactiveButton.setOnClickListener {
            isPackageActive = false
            sharedPreferences.edit().putBoolean("PACK1AC", isPackageActive).apply()
            startActivity(Intent(this, WidokAktywnychPakietow::class.java))
        }
    }
}