[Dialog/BellSync] Add coroutine timer and make some small changes.

This commit is contained in:
Kacper Ziubryniewicz 2019-12-20 23:48:47 +01:00
parent f242c30476
commit 2d838e7003
5 changed files with 31 additions and 13 deletions

View File

@ -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<Time, Int>
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)
}
}
}
}

View File

@ -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() }

View File

@ -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" />
</LinearLayout>
</layout>

View File

@ -76,9 +76,9 @@
<string name="bell_sync_adjust_error">Incorrect format</string>
<string name="bell_sync_cannot_now">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.</string>
<string name="bell_sync_current_dialog">\n\nCurrent calibration: %s</string>
<string name="bell_sync_howto">Click OK, when the bell rings. The counter\'s time will be calibrated to the bell time.\n\nPlanned bell time is %s</string>
<string name="bell_sync_howto">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</string>
<string name="bell_sync_reset_confirm">Do you want to reset the calibration?</string>
<string name="bell_sync_results">The bell is inexact by %s%s</string>
<string name="bell_sync_results">The bell is inexact by %s</string>
<string name="bell_sync_title">Calibrate with the school bell</string>
<string name="bell_sync_time_title">Time of the bell to synchronize</string>
<string name="bell_sync_lesson_item">lesson %s (%s)</string>
@ -873,4 +873,5 @@
<string name="login_summary_account_child">(child)</string>
<string name="login_summary_account_parent">(parent)</string>
<string name="toolbar_subtitle_syncing">Syncing...</string>
<string name="bell_sync_choose_howto">Choose the nearest bell to synchronize.</string>
</resources>

View File

@ -98,10 +98,10 @@
<string name="bell_sync_adjust_error">Nieprawidłowy format</string>
<string name="bell_sync_cannot_now">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.</string>
<string name="bell_sync_current_dialog">"\n\nAktualna kalibracja to %s"</string>
<string name="bell_sync_howto">Kliknij OK, kiedy dzwonek zadzwoni. Licznik czasu zostanie zsynchronizowany z czasem dzwonka.\n\nCzas według planu to %s</string>
<string name="bell_sync_howto">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</string>
<string name="bell_sync_choose_howto">Wybierz najbliższy dzwonek, abyś mógł go zsynchronizować z aplikacją.</string>
<string name="bell_sync_reset_confirm">Czy na pewno zresetować synchronizację?</string>
<string name="bell_sync_results">Dzwonek jest niedokładny o %s%s</string>
<string name="bell_sync_results">Dzwonek jest niedokładny o %s</string>
<string name="bell_sync_title">Synchronizacja z dzwonkiem</string>
<string name="bell_sync_time_title">Godzina dzwonka do synchronizacji</string>
<string name="bell_sync_lesson_item">lekcja %s (%s)</string>