From 2a0cd2f0dc98b400fd1018c6eef585cad72f97da Mon Sep 17 00:00:00 2001 From: sadorowo Date: Thu, 13 Jun 2024 19:17:18 +0200 Subject: [PATCH] adapt code to updated dependencies + align lessons (based by szkolny-eu/szkolny-android#196) --- .../main/java/pl/szczodrzynski/edziennik/App.kt | 3 ++- .../edziennik/config/DelegateConfig.kt | 2 +- .../data/api/edziennik/vulcan/data/VulcanHebe.kt | 4 +--- .../pl/szczodrzynski/edziennik/sync/WorkerUtils.kt | 12 ++++++++---- .../edziennik/ui/debug/LabProfileFragment.kt | 2 +- .../edziennik/ui/home/cards/HomeTimetableCard.kt | 8 +++++++- .../edziennik/ui/timetable/TimetableFragment.kt | 14 ++++++++++++-- 7 files changed, 32 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/App.kt b/app/src/main/java/pl/szczodrzynski/edziennik/App.kt index 74e23d88..a52b958b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/App.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/App.kt @@ -124,7 +124,8 @@ class App : MultiDexApplication(), Configuration.Provider, CoroutineScope { private val job = Job() override val coroutineContext: CoroutineContext get() = job + Dispatchers.Main - override fun getWorkManagerConfiguration() = Configuration.Builder() + + override val workManagerConfiguration: Configuration = Configuration.Builder() .setMinimumLoggingLevel(Log.VERBOSE) .build() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/config/DelegateConfig.kt b/app/src/main/java/pl/szczodrzynski/edziennik/config/DelegateConfig.kt index 91dac668..74880afb 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/config/DelegateConfig.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/config/DelegateConfig.kt @@ -144,7 +144,7 @@ class ConfigDelegate( java.lang.Float::class.java -> value.toFloatOrNull() // enums, maps & collections else -> when { - Enum::class.java.isAssignableFrom(type) -> value.toIntOrNull()?.toEnum(type) as Enum<*> + Enum::class.java.isAssignableFrom(type) -> value.toIntOrNull()?.toEnum(type) as Enum Collection::class.java.isAssignableFrom(type) -> { val array = value.toJsonArray() val genericType = getGenericType() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt index 2a11386c..cf7272d5 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt @@ -391,7 +391,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { fun apiGetList( tag: String, endpoint: String, - filterType: HebeFilterType? = null, + filterType: HebeFilterType = HebeFilterType.BY_PUPIL, dateFrom: Date? = null, dateTo: Date? = null, lastSync: Long? = null, @@ -424,8 +424,6 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { HebeFilterType.BY_MESSAGEBOX -> { query["box"] = messageBox ?: data.messageBoxKey ?: "" } - - null -> TODO() } if (dateFrom != null) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/sync/WorkerUtils.kt b/app/src/main/java/pl/szczodrzynski/edziennik/sync/WorkerUtils.kt index e8266a64..c54d699d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/sync/WorkerUtils.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/sync/WorkerUtils.kt @@ -23,20 +23,24 @@ object WorkerUtils { inline fun scheduleNext(app: App, rescheduleIfFailedFound: Boolean = true, crossinline onReschedule: () -> Unit) { AsyncTask.execute { val workManager = WorkManager.getInstance(app) as WorkManagerImpl - val scheduledWork = workManager.workDatabase.workSpecDao().scheduledWork + val scheduledWork = workManager.workDatabase.workSpecDao().getScheduledWork() as MutableList; + scheduledWork.forEach { - Utils.d("WorkerUtils", "Work: ${it.id} at ${(it.periodStartTime + it.initialDelay).formatDate()}. State = ${it.state} (finished = ${it.state.isFinished})") + Utils.d("WorkerUtils", "Work: ${it.id} at ${it.calculateNextRunTime().formatDate()}. State = ${it.state} (finished = ${it.state.isFinished})") } + // remove finished work and other than SyncWorker scheduledWork.removeAll { it.workerClassName != SyncWorker::class.java.canonicalName || it.isPeriodic || it.state.isFinished } Utils.d("WorkerUtils", "Found ${scheduledWork.size} unfinished work") + // remove all enqueued work that had to (but didn't) run at some point in the past (at least 1min ago) - val failedWork = scheduledWork.filter { it.state == WorkInfo.State.ENQUEUED && it.periodStartTime + it.initialDelay < System.currentTimeMillis() - 1 * MINUTE * 1000 } + val failedWork = scheduledWork.filter { it.state == WorkInfo.State.ENQUEUED && it.calculateNextRunTime() < System.currentTimeMillis() - 1 * MINUTE * 1000 } Utils.d("WorkerUtils", "${failedWork.size} work requests failed to start (out of ${scheduledWork.size} requests)") + if (rescheduleIfFailedFound) { if (failedWork.isNotEmpty()) { Utils.d("WorkerUtils", "App Manager detected!") - EventBus.getDefault().postSticky(AppManagerDetectedEvent(failedWork.map { it.periodStartTime + it.initialDelay })) + EventBus.getDefault().postSticky(AppManagerDetectedEvent(failedWork.map { it.calculateNextRunTime() })) } if (scheduledWork.size - failedWork.size < 1) { Utils.d("WorkerUtils", "No pending work found, scheduling next:") diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/debug/LabProfileFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/debug/LabProfileFragment.kt index f4267a25..83e1703f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/debug/LabProfileFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/debug/LabProfileFragment.kt @@ -129,7 +129,7 @@ class LabProfileFragment : LazyFragment(), CoroutineScope { is String -> input is Long -> input.toLong() is Double -> input.toDouble() - is Enum<*> -> input.toInt().toEnum(objVal::class.java) + is Enum<*> -> input.toInt().toEnum(objVal::class.java) as Enum else -> input } field.set(parent, newVal) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/home/cards/HomeTimetableCard.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/home/cards/HomeTimetableCard.kt index fa2d0381..2d8109b9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/home/cards/HomeTimetableCard.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/home/cards/HomeTimetableCard.kt @@ -328,7 +328,7 @@ class HomeTimetableCard( for (lesson in nextLessons) { text += listOf( - lesson.displayStartTime?.stringHM, + adjustTimeWidth(lesson.displayStartTime?.stringHM), lesson.subjectSpannable ).concat(" ") } @@ -337,6 +337,12 @@ class HomeTimetableCard( b.nextLessons.text = text.concat("\n") }} + private fun adjustTimeWidth(time: String?) = when { + time == null -> "" + time.length == 4 -> " $time " + else -> "$time " + } + private val LessonFull?.subjectSpannable: CharSequence get() = if (this == null) "?" else when { hasReplacingNotes() -> getNoteSubstituteText(showNotes = true) ?: "?" diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/timetable/TimetableFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/timetable/TimetableFragment.kt index d4e710ef..925dfd24 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/timetable/TimetableFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/timetable/TimetableFragment.kt @@ -4,11 +4,13 @@ package pl.szczodrzynski.edziennik.ui.timetable +import android.annotation.SuppressLint import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter import android.os.AsyncTask +import android.os.Build import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -90,8 +92,16 @@ class TimetableFragment : Fragment(), CoroutineScope { } override fun onResume() { super.onResume() - activity.registerReceiver(broadcastReceiver, IntentFilter(ACTION_SCROLL_TO_DATE)) - activity.registerReceiver(broadcastReceiver, IntentFilter(ACTION_RELOAD_PAGES)) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + activity.registerReceiver(broadcastReceiver, IntentFilter(ACTION_SCROLL_TO_DATE ), + Context.RECEIVER_NOT_EXPORTED) + activity.registerReceiver(broadcastReceiver, IntentFilter(ACTION_RELOAD_PAGES), + Context.RECEIVER_NOT_EXPORTED) + } else @Suppress("UnspecifiedRegisterReceiverFlag") { + activity.registerReceiver(broadcastReceiver, IntentFilter(ACTION_SCROLL_TO_DATE)) + activity.registerReceiver(broadcastReceiver, IntentFilter(ACTION_RELOAD_PAGES)) + } } override fun onPause() { super.onPause()