From c2315c3f32646686d41c58090fdb1e8c5a49fac2 Mon Sep 17 00:00:00 2001 From: I-Info Date: Wed, 25 Oct 2023 00:53:56 +0800 Subject: [PATCH] fix: Retrofit problem with R8 minification --- .../com/zjutjh/ijh/widget/ScheduleWidget.kt | 97 ++++++++++++++----- .../ijh/widget/ScheduleWidgetReceiver.kt | 2 +- app/src/main/res/xml/app_widget_schedule.xml | 6 +- core/network/build.gradle | 2 + core/network/consumer-rules.pro | 9 ++ 5 files changed, 86 insertions(+), 30 deletions(-) create mode 100644 core/network/consumer-rules.pro diff --git a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt index 6875638..fbf815a 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt @@ -10,17 +10,22 @@ import androidx.glance.Button import androidx.glance.GlanceId import androidx.glance.GlanceModifier import androidx.glance.GlanceTheme +import androidx.glance.Image +import androidx.glance.ImageProvider +import androidx.glance.action.clickable import androidx.glance.appwidget.GlanceAppWidget import androidx.glance.appwidget.appWidgetBackground import androidx.glance.appwidget.cornerRadius import androidx.glance.appwidget.provideContent import androidx.glance.background +import androidx.glance.layout.Alignment import androidx.glance.layout.Box import androidx.glance.layout.Column import androidx.glance.layout.Row import androidx.glance.layout.Spacer import androidx.glance.layout.fillMaxSize import androidx.glance.layout.fillMaxWidth +import androidx.glance.layout.height import androidx.glance.layout.padding import androidx.glance.material3.ColorProviders import com.zjutjh.ijh.model.Course @@ -28,12 +33,14 @@ import com.zjutjh.ijh.ui.component.shortTime import com.zjutjh.ijh.ui.model.toTermDayState import com.zjutjh.ijh.ui.theme.DarkColorScheme import com.zjutjh.ijh.ui.theme.LightColorScheme +import com.zjutjh.ijh.util.toLocalizedString import com.zjutjh.ijh.work.ScheduleWidgetUpdateWorker import dagger.hilt.android.EntryPointAccessors import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map +import java.time.Duration import java.time.ZonedDateTime // TODO: Unfinished yet, in early stages @@ -51,6 +58,11 @@ class ScheduleWidget : GlanceAppWidget() { } else flow { emit(emptyList()) } val syncTime = entryPoint.courseRepository.lastSyncTimeStream + .map { + if (it == null) null else { + Duration.between(it, ZonedDateTime.now()).toLocalizedString(context) + } + } provideContent { Content(courses, syncTime) { @@ -62,7 +74,7 @@ class ScheduleWidget : GlanceAppWidget() { @Composable fun Content( coursesFlow: Flow>, - syncTimeFlow: Flow, + syncTimeFlow: Flow, onUpdate: () -> Unit ) { val courses by coursesFlow.collectAsState(null) @@ -76,19 +88,32 @@ class ScheduleWidget : GlanceAppWidget() { .appWidgetBackground() .padding(10.dp) ) { - if (courses != null) { - Column { - Row(modifier = GlanceModifier.fillMaxWidth()) { - if (syncTime != null) - GText( - text = "Last sync: ${syncTime!!.toLocalTime()}", - ) - else - GText(text = "Never") + Column { + Row( + modifier = GlanceModifier.fillMaxWidth(), + verticalAlignment = Alignment.Vertical.CenterVertically + ) { + if (syncTime != null) + GText( + text = syncTime!!, + ) + else + GText(text = "Never") - Spacer(GlanceModifier.defaultWeight()) - Button(text = "Update", onClick = onUpdate, maxLines = 1) - } + Spacer(GlanceModifier.defaultWeight()) + Button(text = "Update", onClick = onUpdate, maxLines = 1) + } + Box( + GlanceModifier.padding(vertical = 10.dp) + ) { + Spacer( + GlanceModifier + .fillMaxWidth() + .height(1.dp) + .background(GlanceTheme.colors.outline) + ) + } + if (courses != null) { if (courses!!.isEmpty()) { GText("No course.") } else { @@ -102,25 +127,45 @@ class ScheduleWidget : GlanceAppWidget() { } } } + } else { + GText("Loading...") } - } else { - GText("Loading") } } } } +} - @Composable - private fun CourseItem(course: Course) { - Column( - modifier = GlanceModifier - .fillMaxWidth() - .padding(10.dp) - ) { - GText("${course.name}-${course.teacherName}") - GText("${course.shortTime()} | ${course.place}") - } +// TODO: Impl icon button +@Composable +private fun IconButton( + provider: ImageProvider, + onClick: () -> Unit +) { + Box( + modifier = GlanceModifier + .background(GlanceTheme.colors.primaryContainer) + .height(50.dp) + .cornerRadius(10.dp) + .clickable(onClick) + ) { + Image( + provider = provider, + contentDescription = "Image in button", + modifier = GlanceModifier.fillMaxSize() + ) } +} +@Composable +private fun CourseItem(course: Course) { + Column( + modifier = GlanceModifier + .fillMaxWidth() + .padding(10.dp) + ) { + GText("${course.name}-${course.teacherName}") + GText("${course.shortTime()} | ${course.place}") + } +} -} \ No newline at end of file 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 cef9320..7542e4b 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt @@ -33,7 +33,7 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() { // Enter relevant functionality for when the first widget is created val manager = WorkManager.getInstance(context) val request = PeriodicWorkRequestBuilder( - Duration.ofHours(1), Duration.ofMinutes(15) + Duration.ofHours(6), Duration.ofMinutes(30) ).setConstraints( Constraints(requiresBatteryNotLow = true, requiresDeviceIdle = true) ).build() diff --git a/app/src/main/res/xml/app_widget_schedule.xml b/app/src/main/res/xml/app_widget_schedule.xml index 7686e18..51191a0 100644 --- a/app/src/main/res/xml/app_widget_schedule.xml +++ b/app/src/main/res/xml/app_widget_schedule.xml @@ -4,14 +4,14 @@ android:description="@string/app_widget_schedule_description" android:initialKeyguardLayout="@layout/widget_loading" android:initialLayout="@layout/widget_loading" - android:minWidth="160dp" + android:minWidth="200dp" android:minHeight="160dp" - android:minResizeWidth="160dp" + android:minResizeWidth="200dp" android:minResizeHeight="160dp" android:previewLayout="@layout/widget_loading" 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 diff --git a/core/network/build.gradle b/core/network/build.gradle index 58be1f3..91b25cd 100644 --- a/core/network/build.gradle +++ b/core/network/build.gradle @@ -13,6 +13,8 @@ android { defaultConfig { minSdk min_sdk + + consumerProguardFiles "consumer-rules.pro" } buildTypes { diff --git a/core/network/consumer-rules.pro b/core/network/consumer-rules.pro new file mode 100644 index 0000000..b0b58bb --- /dev/null +++ b/core/network/consumer-rules.pro @@ -0,0 +1,9 @@ + # TODO: remove when new retrofit version release. (Current 2.9.0) + # Keep generic signature of Call, Response (R8 full mode strips signatures from non-kept items). + -keep,allowobfuscation,allowshrinking interface retrofit2.Call + -keep,allowobfuscation,allowshrinking class retrofit2.Response + + # With R8 full mode generic signatures are stripped for classes that are not + # kept. Suspend functions are wrapped in continuations where the type argument + # is used. + -keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation \ No newline at end of file