From 788ef6f01f046a56ecece5db04a595d2d1a9f7ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Wed, 6 Sep 2023 09:58:24 +0200 Subject: [PATCH] Add radio button for seconds for custom sleep timer --- .../flashlight/activities/MainActivity.kt | 10 +++++- .../dialogs/SleepTimerCustomDialog.kt | 28 ++++++++++++---- .../dialog_custom_sleep_timer_picker.xml | 33 ++++++++++++++++--- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt index e66347f..e5c6d1b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt @@ -319,7 +319,15 @@ class MainActivity : SimpleActivity() { if (items.none { it.id == config.lastSleepTimerSeconds }) { val lastSleepTimerMinutes = config.lastSleepTimerSeconds / 60 - val text = resources.getQuantityString(R.plurals.minutes, lastSleepTimerMinutes, lastSleepTimerMinutes) + val lastSleepTimerSeconds = config.lastSleepTimerSeconds % 60 + val parts = mutableListOf() + if (lastSleepTimerMinutes != 0) { + parts.add(resources.getQuantityString(R.plurals.minutes, lastSleepTimerMinutes, lastSleepTimerMinutes)) + } + if (lastSleepTimerSeconds != 0) { + parts.add(resources.getQuantityString(R.plurals.seconds, lastSleepTimerSeconds, lastSleepTimerSeconds)) + } + val text = parts.joinToString(separator = " ") items.add(RadioItem(config.lastSleepTimerSeconds, text)) } diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/dialogs/SleepTimerCustomDialog.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/dialogs/SleepTimerCustomDialog.kt index 269fc5f..ce6b989 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/dialogs/SleepTimerCustomDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/dialogs/SleepTimerCustomDialog.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.flashlight.dialogs import android.app.Activity +import android.view.inputmethod.EditorInfo import androidx.appcompat.app.AlertDialog import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.flashlight.R @@ -11,23 +12,38 @@ class SleepTimerCustomDialog(val activity: Activity, val callback: (seconds: Int private val binding = DialogCustomSleepTimerPickerBinding.inflate(activity.layoutInflater) init { - binding.minutesHint.hint = activity.getString(R.string.minutes_raw).replaceFirstChar { it.uppercaseChar() } + binding.dialogRadioView.check(R.id.dialog_radio_minutes) + binding.timerValue.setOnEditorActionListener { _, actionId, _ -> + if (actionId == EditorInfo.IME_ACTION_DONE) { + dialogConfirmed() + return@setOnEditorActionListener true + } + return@setOnEditorActionListener false + } + activity.getAlertDialogBuilder() - .setPositiveButton(R.string.ok) { dialog, which -> dialogConfirmed() } + .setPositiveButton(R.string.ok) { _, _ -> dialogConfirmed() } .setNegativeButton(R.string.cancel, null) .apply { activity.setupDialogStuff(binding.root, this, R.string.sleep_timer) { alertDialog -> dialog = alertDialog - alertDialog.showKeyboard(binding.minutes) + alertDialog.showKeyboard(binding.timerValue) } } } private fun dialogConfirmed() { - val value = binding.minutes.value - val minutes = Integer.valueOf(if (value.isEmpty()) "0" else value) - callback(minutes * 60) + val value = binding.timerValue.value + val minutes = Integer.valueOf(value.ifEmpty { "0" }) + val multiplier = getMultiplier(binding.dialogRadioView.checkedRadioButtonId) + callback(minutes * multiplier) activity.hideKeyboard() dialog?.dismiss() } + + private fun getMultiplier(id: Int) = when (id) { + R.id.dialog_radio_seconds -> 1 + R.id.dialog_radio_minutes -> 60 + else -> 60 + } } diff --git a/app/src/main/res/layout/dialog_custom_sleep_timer_picker.xml b/app/src/main/res/layout/dialog_custom_sleep_timer_picker.xml index 91bd078..14a0446 100644 --- a/app/src/main/res/layout/dialog_custom_sleep_timer_picker.xml +++ b/app/src/main/res/layout/dialog_custom_sleep_timer_picker.xml @@ -3,19 +3,19 @@ android:id="@+id/dialog_custom_sleep_timer_holder" android:layout_width="match_parent" android:layout_height="wrap_content" - android:orientation="horizontal" + android:orientation="vertical" android:paddingLeft="@dimen/activity_margin" android:paddingTop="@dimen/activity_margin" android:paddingRight="@dimen/activity_margin"> + android:hint="@string/value"> + + + + + + + +