Skip to content

Commit 94c8c8e

Browse files
author
alllexey
committed
Trigger update every minute
1 parent 08b6c69 commit 94c8c8e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

app/src/main/java/me/alllexey123/itmowidgets/ui/schedule/DayScheduleAdapter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ class DayScheduleAdapter(private var schedules: List<Schedule>) :
6363
if (oldDaySchedules != newDaySchedules) notifyDataSetChanged()
6464
}
6565

66+
fun updateLessonStates() {
67+
notifyDataSetChanged()
68+
}
69+
6670
override fun getItemCount(): Int = schedules.size
6771

6872
inner class DayViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

app/src/main/java/me/alllexey123/itmowidgets/ui/schedule/ScheduleActivity.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import android.app.PendingIntent
44
import android.content.Context
55
import android.content.Intent
66
import android.os.Bundle
7+
import android.os.Handler
8+
import android.os.Looper
79
import android.view.View
810
import android.widget.Toast
911
import androidx.activity.viewModels
@@ -17,6 +19,7 @@ import me.alllexey123.itmowidgets.ItmoWidgetsApp
1719
import me.alllexey123.itmowidgets.R
1820
import me.alllexey123.itmowidgets.ui.qr.QrCodeActivity
1921
import me.alllexey123.itmowidgets.ui.settings.SettingsActivity
22+
import java.time.Duration
2023
import java.time.LocalDate
2124

2225
class 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

Comments
 (0)