@@ -4,6 +4,8 @@ import android.app.PendingIntent
44import android.content.Context
55import android.content.Intent
66import android.os.Bundle
7+ import android.os.Handler
8+ import android.os.Looper
79import android.view.View
810import android.widget.Toast
911import androidx.activity.viewModels
@@ -17,6 +19,7 @@ import me.alllexey123.itmowidgets.ItmoWidgetsApp
1719import me.alllexey123.itmowidgets.R
1820import me.alllexey123.itmowidgets.ui.qr.QrCodeActivity
1921import me.alllexey123.itmowidgets.ui.settings.SettingsActivity
22+ import java.time.Duration
2023import java.time.LocalDate
2124
2225class ScheduleActivity : AppCompatActivity () {
@@ -31,6 +34,9 @@ class ScheduleActivity : AppCompatActivity() {
3134
3235 private val snapHelper = PagerSnapHelper ()
3336
37+ private val handler = Handler (Looper .getMainLooper())
38+ private lateinit var updateTimeRunnable: Runnable
39+
3440 private val scheduleViewModel: ScheduleViewModel by viewModels {
3541 val appContainer = (application as ItmoWidgetsApp ).appContainer
3642 ScheduleViewModelFactory (appContainer.scheduleRepository)
@@ -54,6 +60,16 @@ class ScheduleActivity : AppCompatActivity() {
5460 scheduleViewModel.fetchScheduleData(forceRefresh = false )
5561 }
5662
63+ override fun onResume () {
64+ super .onResume()
65+ startLessonStateUpdater()
66+ }
67+
68+ override fun onPause () {
69+ super .onPause()
70+ stopLessonStateUpdater()
71+ }
72+
5773 private fun setupButtons () {
5874 fabSettings = findViewById(R .id.fab_settings)
5975 fabSettings.setOnClickListener { view ->
@@ -168,4 +184,18 @@ class ScheduleActivity : AppCompatActivity() {
168184 }
169185 }
170186
187+ private fun startLessonStateUpdater () {
188+ updateTimeRunnable = object : Runnable {
189+ override fun run () {
190+ dayScheduleAdapter.updateLessonStates()
191+ handler.postDelayed(this , Duration .ofMinutes(1 ).toMillis())
192+ }
193+ }
194+ handler.post(updateTimeRunnable)
195+ }
196+
197+ private fun stopLessonStateUpdater () {
198+ handler.removeCallbacks(updateTimeRunnable)
199+ }
200+
171201}
0 commit comments