Skip to content

Commit

Permalink
fix(schedule): missing recomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Info committed Oct 26, 2023
1 parent 8837eaf commit 628a6ba
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ fun ClassSchedule(
startPadding = 30.dp,
) {
val today = dateTime.dayOfWeek

DayOfWeek.entries.forEachIndexed { index, dayOfWeek ->
val dayCourses = remember(courses) {
courses.filter { it.dayOfWeek == dayOfWeek }.toImmutableList()
Expand All @@ -134,6 +133,7 @@ fun ClassSchedule(
}
}


// Left section bar
Surface(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,14 @@ private fun ClassScheduleScreen(
.verticalScroll(rememberScrollState()),
) {
Box(contentAlignment = Alignment.TopCenter) {
ClassSchedule(
modifier = Modifier.padding(10.dp),
courses = courses,
highlight = highlight,
)
// Force recompose when courses changed
key(courses) {
ClassSchedule(
modifier = Modifier.padding(10.dp),
courses = courses,
highlight = highlight,
)
}
key(refreshing) {
PullRefreshIndicator(
refreshing = refreshing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.work.WorkManager
import com.zjutjh.ijh.data.repository.CourseRepository
import com.zjutjh.ijh.data.repository.WeJhInfoRepository
import com.zjutjh.ijh.work.ScheduleWidgetUpdater
import com.zjutjh.ijh.work.WidgetRefreshWorker
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
Expand Down Expand Up @@ -38,7 +37,7 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() {
val request = PeriodicWorkRequestBuilder<ScheduleWidgetUpdater>(
Duration.ofHours(6), Duration.ofMinutes(30)
).setConstraints(
Constraints(requiresBatteryNotLow = true, requiresDeviceIdle = true)
Constraints(requiresBatteryNotLow = true)
).build()

manager.enqueueUniquePeriodicWork(
Expand All @@ -47,8 +46,6 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() {
request
)

WidgetRefreshWorker.enqueue<ScheduleWidget>(context)

Log.i("ScheduleWidget", "Updater enqueued.")
}

Expand All @@ -58,8 +55,6 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() {
val manager = WorkManager.getInstance(context)
manager.cancelUniqueWork(ScheduleWidgetUpdater.PERIODIC_UNIQUE_NAME)

WidgetRefreshWorker.cancel<ScheduleWidget>(context)

Log.i("ScheduleWidget", "Updater canceled.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ScheduleWidgetUpdater(
val info = entryPoint.weJhInfoRepository.sync()
entryPoint.courseRepository.sync(info.first, info.second)

Log.i("ScheduleWidget", "Synced.")
Result.success()
} catch (e: ApiResponseException) {
// Not logged in, stop the worker
Expand All @@ -59,13 +60,13 @@ class ScheduleWidgetUpdater(
}

else -> {
Log.e("ScheduleWidget", "Failed to sync: $e")
Log.e("ScheduleWidget", "Failed to sync.", e)
}
}

Result.failure()
} catch (e: Exception) {
Log.e("ScheduleWidget", "Failed to sync: $e")
Log.e("ScheduleWidget", "Failed to sync", e)

Result.failure()
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/kotlin/com/zjutjh/ijh/work/WidgetRefreshWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ class WidgetRefreshWorker(
/**
* Enqueue a [WidgetRefreshWorker] for the given [GlanceAppWidget].
* Do nothing if the worker is already enqueued.
* Default refresh interval is 20 minutes.
* Default refresh interval is 15 minutes.
*/
inline fun <reified T : GlanceAppWidget> enqueue(context: Context) {
val manager = WorkManager.getInstance(context)
val request = PeriodicWorkRequestBuilder<WidgetRefreshWorker>(
Duration.ofMinutes(20), Duration.ofMinutes(5)
Duration.ofMinutes(15), Duration.ofMinutes(5)
).setConstraints(
Constraints(requiresBatteryNotLow = true, requiresDeviceIdle = true)
Constraints(requiresBatteryNotLow = true)
).setInputData(
workDataOf(INPUT_CLASS_NAME to T::class.java.name)
).build()
)
.build()

manager.enqueueUniquePeriodicWork(
getUniqueWorkName<T>(),
Expand Down Expand Up @@ -70,6 +71,7 @@ class WidgetRefreshWorker(

obj.updateAll(context)

Log.i("WidgetRefreshWorker", "Widget refreshed: $className")
Result.success()
} catch (e: Exception) {
Log.e("WidgetRefreshWorker", "Error when refreshing widget.", e)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/app_widget_schedule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
android:resizeMode="horizontal|vertical"
android:targetCellWidth="3"
android:targetCellHeight="3"
android:updatePeriodMillis="0"
android:updatePeriodMillis="1800000"
android:widgetCategory="home_screen"
tools:targetApi="s" />

0 comments on commit 628a6ba

Please sign in to comment.