1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-09-20 00:29:09 -05:00

Fix timetable widget day reset (#1862)

Co-authored-by: Rafał Borcz <RafalBO99@outlook.com>
This commit is contained in:
Michael 2022-06-26 13:06:43 +02:00 committed by GitHub
parent 0fb55bd6c6
commit c808bf2e61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View File

@ -1,8 +1,6 @@
package io.github.wulkanowy.ui.modules.timetablewidget
import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_UPDATE
import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID
import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_IDS
import android.appwidget.AppWidgetManager.*
import android.content.Intent
import android.os.Build
import android.os.Bundle
@ -17,6 +15,7 @@ import io.github.wulkanowy.databinding.ActivityWidgetConfigureBinding
import io.github.wulkanowy.ui.base.BaseActivity
import io.github.wulkanowy.ui.base.WidgetConfigureAdapter
import io.github.wulkanowy.ui.modules.login.LoginActivity
import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.EXTRA_FROM_CONFIGURE
import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.EXTRA_FROM_PROVIDER
import io.github.wulkanowy.utils.AppInfo
import javax.inject.Inject
@ -92,6 +91,7 @@ class TimetableWidgetConfigureActivity :
.apply {
action = ACTION_APPWIDGET_UPDATE
putExtra(EXTRA_APPWIDGET_IDS, intArrayOf(widgetId))
putExtra(EXTRA_FROM_CONFIGURE, true)
})
}

View File

@ -60,6 +60,8 @@ class TimetableWidgetProvider : BroadcastReceiver() {
private const val BUTTON_RESET = "buttonReset"
const val EXTRA_FROM_CONFIGURE = "extraFromConfigure"
const val EXTRA_FROM_PROVIDER = "extraFromProvider"
fun getDateWidgetKey(appWidgetId: Int) = "timetable_widget_date_$appWidgetId"
@ -86,12 +88,22 @@ class TimetableWidgetProvider : BroadcastReceiver() {
}
private suspend fun onUpdate(context: Context, intent: Intent) {
if (intent.getStringExtra(EXTRA_BUTTON_TYPE) === null) {
intent.getIntArrayExtra(EXTRA_APPWIDGET_IDS)?.forEach { appWidgetId ->
if (intent.getStringExtra(EXTRA_BUTTON_TYPE) == null) {
val isFromConfigure = intent.getBooleanExtra(EXTRA_FROM_CONFIGURE, false)
val appWidgetIds = intent.getIntArrayExtra(EXTRA_APPWIDGET_IDS) ?: return
appWidgetIds.forEach { appWidgetId ->
val student =
getStudent(sharedPref.getLong(getStudentWidgetKey(appWidgetId), 0), appWidgetId)
val savedDataEpochDay = sharedPref.getLong(getDateWidgetKey(appWidgetId), 0)
updateWidget(context, appWidgetId, getWidgetDateToLoad(appWidgetId), student)
val dateToLoad = if (isFromConfigure && savedDataEpochDay != 0L) {
LocalDate.ofEpochDay(savedDataEpochDay)
} else {
getWidgetDefaultDateToLoad(appWidgetId)
}
updateWidget(context, appWidgetId, dateToLoad, student)
}
} else {
val buttonType = intent.getStringExtra(EXTRA_BUTTON_TYPE)
@ -103,10 +115,10 @@ class TimetableWidgetProvider : BroadcastReceiver() {
val savedDate =
LocalDate.ofEpochDay(sharedPref.getLong(getDateWidgetKey(toggledWidgetId), 0))
val date = when (buttonType) {
BUTTON_RESET -> getWidgetDateToLoad(toggledWidgetId)
BUTTON_RESET -> getWidgetDefaultDateToLoad(toggledWidgetId)
BUTTON_NEXT -> savedDate.nextSchoolDay
BUTTON_PREV -> savedDate.previousSchoolDay
else -> getWidgetDateToLoad(toggledWidgetId)
else -> getWidgetDefaultDateToLoad(toggledWidgetId)
}
if (!buttonType.isNullOrBlank()) {
analytics.logEvent(
@ -271,7 +283,7 @@ class TimetableWidgetProvider : BroadcastReceiver() {
return avatarBitmap
}
private fun getWidgetDateToLoad(appWidgetId: Int): LocalDate {
private fun getWidgetDefaultDateToLoad(appWidgetId: Int): LocalDate {
val lastLessonEndTimestamp =
sharedPref.getLong(getTodayLastLessonEndDateTimeWidgetKey(appWidgetId), 0)
val lastLessonEndDateTime =