From 2d838e700314dbd10819ab8e3cc8c6a220a43a00 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Fri, 20 Dec 2019 23:48:47 +0100 Subject: [PATCH] [Dialog/BellSync] Add coroutine timer and make some small changes. --- .../ui/dialogs/bell/BellSyncDialog.kt | 31 ++++++++++++++----- .../dialogs/bell/BellSyncTimeChooseDialog.kt | 2 +- app/src/main/res/layout/dialog_bell_sync.xml | 2 +- app/src/main/res/values-en/strings.xml | 5 +-- app/src/main/res/values/strings.xml | 4 +-- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/bell/BellSyncDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/bell/BellSyncDialog.kt index 7dd3619d..85d531ba 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/bell/BellSyncDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/bell/BellSyncDialog.kt @@ -10,10 +10,12 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job +import kotlinx.coroutines.launch import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.MainActivity import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.databinding.DialogBellSyncBinding +import pl.szczodrzynski.edziennik.startCoroutineTimer import pl.szczodrzynski.edziennik.utils.models.Time import kotlin.coroutines.CoroutineContext @@ -37,6 +39,16 @@ class BellSyncDialog( private val app by lazy { activity.application as App } + private var counterJob: Job? = null + + private val actualBellDiff: Pair + get() { + val now = Time.getNow() + val bellDiff = Time.diff(now, bellTime) + val multiplier = if (bellTime > now) -1 else 1 + return Pair(bellDiff, multiplier) + } + init { apply { if (activity.isFinishing) return@apply @@ -52,18 +64,15 @@ class BellSyncDialog( }} private fun initView() { - b.bellSyncHowto.text = app.getString(R.string.bell_sync_howto, bellTime.stringHM) - b.bellSyncButton.setOnClickListener { - val now = Time.getNow() - val bellDiff = Time.diff(now, bellTime) - val multiplier = if (bellTime > now) -1 else 1 + val (bellDiff, multiplier) = actualBellDiff + val bellDiffText = (if (multiplier == -1) '-' else '+') + bellDiff.stringHMS app.config.timetable.bellSyncDiff = bellDiff app.config.timetable.bellSyncMultiplier = multiplier MaterialAlertDialogBuilder(activity) .setTitle(R.string.bell_sync_title) - .setMessage(app.getString(R.string.bell_sync_results, if (multiplier == -1) '-' else '+', bellDiff.stringHMS)) + .setMessage(app.getString(R.string.bell_sync_results, bellDiffText)) .setPositiveButton(R.string.ok) { resultsDialog, _ -> resultsDialog.dismiss() dialog.dismiss() @@ -72,8 +81,16 @@ class BellSyncDialog( .show() } - if (Time.diff(Time.getNow(), bellTime) > Time(0, 10, 0)) { // Easter egg ^^ + if (Time.diff(Time.getNow(), bellTime) > Time(2, 0, 0)) { // Easter egg ^^ b.bellSyncButton.setImageDrawable(app.resources.getDrawable(R.drawable.ic_bell_wtf)) // wtf } + + launch { + counterJob = startCoroutineTimer(repeatMillis = 1000) { + val (bellDiff, multiplier) = actualBellDiff + val bellDiffText = (if (multiplier == -1) '-' else '+') + bellDiff.stringHMS + b.bellSyncHowto.text = app.getString(R.string.bell_sync_howto, bellTime.stringHM, bellDiffText) + } + } } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/bell/BellSyncTimeChooseDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/bell/BellSyncTimeChooseDialog.kt index 3bc88aa9..33e35e59 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/bell/BellSyncTimeChooseDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/bell/BellSyncTimeChooseDialog.kt @@ -129,7 +129,7 @@ class BellSyncTimeChooseDialog( app.config.timetable.bellSyncMultiplier = 0 confirmDialog.dismiss() - dialog.dismiss() + initView() if (activity is MainActivity) activity.reloadTarget() } .setNegativeButton(R.string.no) { dialog, _ -> dialog.dismiss() } diff --git a/app/src/main/res/layout/dialog_bell_sync.xml b/app/src/main/res/layout/dialog_bell_sync.xml index e8838cc6..3af9dbed 100644 --- a/app/src/main/res/layout/dialog_bell_sync.xml +++ b/app/src/main/res/layout/dialog_bell_sync.xml @@ -25,7 +25,7 @@ android:layout_height="128dp" android:layout_gravity="center" android:layout_margin="32dp" - android:background="?android:attr/selectableItemBackground" + android:background="?attr/selectableItemBackgroundBorderless" app:srcCompat="@drawable/ic_bell" /> diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml index fd3d8ae4..decfedb0 100644 --- a/app/src/main/res/values-en/strings.xml +++ b/app/src/main/res/values-en/strings.xml @@ -76,9 +76,9 @@ Incorrect format Calibration is impossible, because there are no lessons now. Try again, i.e. in the end of the lesson or break. Remember that you should run this even before planned bell time. \n\nCurrent calibration: %s - Click OK, when the bell rings. The counter\'s time will be calibrated to the bell time.\n\nPlanned bell time is %s + Click the bell icon, when the bell rings. The counter\'s time will be calibrated to the bell time.\n\nPlanned bell time is %s\n\nThe bell time difference at the moment is %s Do you want to reset the calibration? - The bell is inexact by %s%s + The bell is inexact by %s Calibrate with the school bell Time of the bell to synchronize lesson %s (%s) @@ -873,4 +873,5 @@ (child) (parent) Syncing... + Choose the nearest bell to synchronize. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0e4003c6..43d3df29 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -98,10 +98,10 @@ Nieprawidłowy format Synchronizacja jest niemożliwa, ponieważ teraz nie ma żadnych lekcji. Spróbuj jeszcze raz np. pod koniec lekcji albo przerwy. Pamiętaj, że powinieneś to uruchomić jeszcze przed planowanym czasem dzwonka. "\n\nAktualna kalibracja to %s" - Kliknij OK, kiedy dzwonek zadzwoni. Licznik czasu zostanie zsynchronizowany z czasem dzwonka.\n\nCzas według planu to %s + Kliknij w ikonę dzwonka, kiedy dzwonek zadzwoni. Licznik czasu zostanie zsynchronizowany z czasem dzwonka.\n\nCzas według planu to %s\n\nAktualna różnica wynosi %s Wybierz najbliższy dzwonek, abyś mógł go zsynchronizować z aplikacją. Czy na pewno zresetować synchronizację? - Dzwonek jest niedokładny o %s%s + Dzwonek jest niedokładny o %s Synchronizacja z dzwonkiem Godzina dzwonka do synchronizacji lekcja %s (%s)