diff --git a/app/src/main/kotlin/com/zjutjh/ijh/ui/component/ClassSchedule.kt b/app/src/main/kotlin/com/zjutjh/ijh/ui/component/ClassSchedule.kt index c517468..c1a6114 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/ui/component/ClassSchedule.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/ui/component/ClassSchedule.kt @@ -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() @@ -134,6 +133,7 @@ fun ClassSchedule( } } + // Left section bar Surface( modifier = Modifier diff --git a/app/src/main/kotlin/com/zjutjh/ijh/ui/screen/ClassScheduleScreen.kt b/app/src/main/kotlin/com/zjutjh/ijh/ui/screen/ClassScheduleScreen.kt index 1828d20..d60fdb8 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/ui/screen/ClassScheduleScreen.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/ui/screen/ClassScheduleScreen.kt @@ -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, diff --git a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt index c4ed9f6..773c694 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt @@ -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 @@ -38,7 +37,7 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() { val request = PeriodicWorkRequestBuilder( Duration.ofHours(6), Duration.ofMinutes(30) ).setConstraints( - Constraints(requiresBatteryNotLow = true, requiresDeviceIdle = true) + Constraints(requiresBatteryNotLow = true) ).build() manager.enqueueUniquePeriodicWork( @@ -47,8 +46,6 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() { request ) - WidgetRefreshWorker.enqueue(context) - Log.i("ScheduleWidget", "Updater enqueued.") } @@ -58,8 +55,6 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() { val manager = WorkManager.getInstance(context) manager.cancelUniqueWork(ScheduleWidgetUpdater.PERIODIC_UNIQUE_NAME) - WidgetRefreshWorker.cancel(context) - Log.i("ScheduleWidget", "Updater canceled.") } } diff --git a/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt b/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt index b56d94e..a4dc95a 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt @@ -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 @@ -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() } diff --git a/app/src/main/kotlin/com/zjutjh/ijh/work/WidgetRefreshWorker.kt b/app/src/main/kotlin/com/zjutjh/ijh/work/WidgetRefreshWorker.kt index 761600d..a2011ae 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/work/WidgetRefreshWorker.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/work/WidgetRefreshWorker.kt @@ -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 enqueue(context: Context) { val manager = WorkManager.getInstance(context) val request = PeriodicWorkRequestBuilder( - 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(), @@ -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) diff --git a/app/src/main/res/xml/app_widget_schedule.xml b/app/src/main/res/xml/app_widget_schedule.xml index 1e87da7..51191a0 100644 --- a/app/src/main/res/xml/app_widget_schedule.xml +++ b/app/src/main/res/xml/app_widget_schedule.xml @@ -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" /> \ No newline at end of file