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

Update timetable to next day if there is no more lessons today (#1551)

This commit is contained in:
Mateusz Idziejczak 2021-12-25 06:46:24 +01:00 committed by GitHub
parent e26860ea5a
commit 497083be97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 37 deletions

View File

@ -2,14 +2,6 @@
<code_scheme name="Project" version="173"> <code_scheme name="Project" version="173">
<option name="LINE_SEPARATOR" value="&#10;" /> <option name="LINE_SEPARATOR" value="&#10;" />
<JetCodeStyleSettings> <JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value>
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
</value>
</option>
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
<option name="WRAP_ELVIS_EXPRESSIONS" value="0" />
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" /> <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings> </JetCodeStyleSettings>
<codeStyleSettings language="XML"> <codeStyleSettings language="XML">
@ -126,13 +118,6 @@
</codeStyleSettings> </codeStyleSettings>
<codeStyleSettings language="kotlin"> <codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" /> <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
<option name="KEEP_BLANK_LINES_IN_DECLARATIONS" value="1" />
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="0" />
<option name="ALIGN_MULTILINE_PARAMETERS" value="false" />
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
</indentOptions>
</codeStyleSettings> </codeStyleSettings>
</code_scheme> </code_scheme>
</component> </component>

View File

@ -22,12 +22,14 @@ import io.github.wulkanowy.data.repositories.TimetableRepository
import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getCurrentThemeWidgetKey import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getCurrentThemeWidgetKey
import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getDateWidgetKey import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getDateWidgetKey
import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getStudentWidgetKey import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getStudentWidgetKey
import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getTodayLastLessonEndDateTimeWidgetKey
import io.github.wulkanowy.utils.getCompatColor import io.github.wulkanowy.utils.getCompatColor
import io.github.wulkanowy.utils.toFirstResult import io.github.wulkanowy.utils.toFirstResult
import io.github.wulkanowy.utils.toFormattedString import io.github.wulkanowy.utils.toFormattedString
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import timber.log.Timber import timber.log.Timber
import java.time.LocalDate import java.time.LocalDate
import java.time.ZoneOffset
class TimetableWidgetFactory( class TimetableWidgetFactory(
private val timetableRepository: TimetableRepository, private val timetableRepository: TimetableRepository,
@ -70,6 +72,16 @@ class TimetableWidgetFactory(
updateTheme(appWidgetId) updateTheme(appWidgetId)
lessons = getLessons(date, studentId) lessons = getLessons(date, studentId)
if (date == LocalDate.now()) {
val todayLastLessonEndTimestamp =
lessons.maxOf { it.end }.toEpochSecond(ZoneOffset.UTC)
sharedPref.putLong(
getTodayLastLessonEndDateTimeWidgetKey(appWidgetId),
todayLastLessonEndTimestamp,
true
)
}
} }
} }

View File

@ -3,10 +3,7 @@ package io.github.wulkanowy.ui.modules.timetablewidget
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.PendingIntent import android.app.PendingIntent
import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_DELETED import android.appwidget.AppWidgetManager.*
import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_UPDATE
import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID
import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_IDS
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK import android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK
@ -25,22 +22,14 @@ import io.github.wulkanowy.services.HiltBroadcastReceiver
import io.github.wulkanowy.services.widgets.TimetableWidgetService import io.github.wulkanowy.services.widgets.TimetableWidgetService
import io.github.wulkanowy.ui.modules.Destination import io.github.wulkanowy.ui.modules.Destination
import io.github.wulkanowy.ui.modules.splash.SplashActivity import io.github.wulkanowy.ui.modules.splash.SplashActivity
import io.github.wulkanowy.utils.AnalyticsHelper import io.github.wulkanowy.utils.*
import io.github.wulkanowy.utils.PendingIntentCompat
import io.github.wulkanowy.utils.capitalise
import io.github.wulkanowy.utils.createNameInitialsDrawable
import io.github.wulkanowy.utils.getCompatColor
import io.github.wulkanowy.utils.nextOrSameSchoolDay
import io.github.wulkanowy.utils.nextSchoolDay
import io.github.wulkanowy.utils.nickOrName
import io.github.wulkanowy.utils.previousSchoolDay
import io.github.wulkanowy.utils.toFormattedString
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import java.time.LocalDate import java.time.LocalDate
import java.time.LocalDate.now import java.time.LocalDateTime
import java.time.ZoneOffset
import javax.inject.Inject import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
@ -76,6 +65,9 @@ class TimetableWidgetProvider : HiltBroadcastReceiver() {
fun getDateWidgetKey(appWidgetId: Int) = "timetable_widget_date_$appWidgetId" fun getDateWidgetKey(appWidgetId: Int) = "timetable_widget_date_$appWidgetId"
fun getTodayLastLessonEndDateTimeWidgetKey(appWidgetId: Int) =
"timetable_widget_today_last_lesson_end_date_time_$appWidgetId"
fun getStudentWidgetKey(appWidgetId: Int) = "timetable_widget_student_$appWidgetId" fun getStudentWidgetKey(appWidgetId: Int) = "timetable_widget_student_$appWidgetId"
fun getThemeWidgetKey(appWidgetId: Int) = "timetable_widget_theme_$appWidgetId" fun getThemeWidgetKey(appWidgetId: Int) = "timetable_widget_theme_$appWidgetId"
@ -100,7 +92,8 @@ class TimetableWidgetProvider : HiltBroadcastReceiver() {
intent.getIntArrayExtra(EXTRA_APPWIDGET_IDS)?.forEach { appWidgetId -> intent.getIntArrayExtra(EXTRA_APPWIDGET_IDS)?.forEach { appWidgetId ->
val student = val student =
getStudent(sharedPref.getLong(getStudentWidgetKey(appWidgetId), 0), appWidgetId) getStudent(sharedPref.getLong(getStudentWidgetKey(appWidgetId), 0), appWidgetId)
updateWidget(context, appWidgetId, now().nextOrSameSchoolDay, student)
updateWidget(context, appWidgetId, getWidgetDateToLoad(appWidgetId), student)
} }
} else { } else {
val buttonType = intent.getStringExtra(EXTRA_BUTTON_TYPE) val buttonType = intent.getStringExtra(EXTRA_BUTTON_TYPE)
@ -112,15 +105,17 @@ class TimetableWidgetProvider : HiltBroadcastReceiver() {
val savedDate = val savedDate =
LocalDate.ofEpochDay(sharedPref.getLong(getDateWidgetKey(toggledWidgetId), 0)) LocalDate.ofEpochDay(sharedPref.getLong(getDateWidgetKey(toggledWidgetId), 0))
val date = when (buttonType) { val date = when (buttonType) {
BUTTON_RESET -> now().nextOrSameSchoolDay BUTTON_RESET -> getWidgetDateToLoad(toggledWidgetId)
BUTTON_NEXT -> savedDate.nextSchoolDay BUTTON_NEXT -> savedDate.nextSchoolDay
BUTTON_PREV -> savedDate.previousSchoolDay BUTTON_PREV -> savedDate.previousSchoolDay
else -> now().nextOrSameSchoolDay else -> getWidgetDateToLoad(toggledWidgetId)
} }
if (!buttonType.isNullOrBlank()) analytics.logEvent( if (!buttonType.isNullOrBlank()) {
analytics.logEvent(
"changed_timetable_widget_day", "changed_timetable_widget_day",
"button" to buttonType "button" to buttonType
) )
}
updateWidget(context, toggledWidgetId, date, student) updateWidget(context, toggledWidgetId, date, student)
} }
} }
@ -278,4 +273,21 @@ class TimetableWidgetProvider : HiltBroadcastReceiver() {
avatarDrawable.draw(canvas) avatarDrawable.draw(canvas)
return avatarBitmap return avatarBitmap
} }
private fun getWidgetDateToLoad(appWidgetId: Int): LocalDate {
val lastLessonEndTimestamp =
sharedPref.getLong(getTodayLastLessonEndDateTimeWidgetKey(appWidgetId), 0)
val lastLessonEndDateTime =
LocalDateTime.ofEpochSecond(lastLessonEndTimestamp, 0, ZoneOffset.UTC)
val todayDate = LocalDate.now()
val isLastLessonEndDateNow = lastLessonEndDateTime.toLocalDate() == todayDate
val isLastLessonEndDateAfterNowTime = LocalDateTime.now() > lastLessonEndDateTime
return if (isLastLessonEndDateNow && isLastLessonEndDateAfterNowTime) {
todayDate.nextSchoolDay
} else {
todayDate.nextOrSameSchoolDay
}
}
} }