mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-06-05 21:59:19 +02:00
Add radio button for seconds for custom sleep timer
This commit is contained in:
@ -319,7 +319,15 @@ class MainActivity : SimpleActivity() {
|
|||||||
|
|
||||||
if (items.none { it.id == config.lastSleepTimerSeconds }) {
|
if (items.none { it.id == config.lastSleepTimerSeconds }) {
|
||||||
val lastSleepTimerMinutes = config.lastSleepTimerSeconds / 60
|
val lastSleepTimerMinutes = config.lastSleepTimerSeconds / 60
|
||||||
val text = resources.getQuantityString(R.plurals.minutes, lastSleepTimerMinutes, lastSleepTimerMinutes)
|
val lastSleepTimerSeconds = config.lastSleepTimerSeconds % 60
|
||||||
|
val parts = mutableListOf<String>()
|
||||||
|
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))
|
items.add(RadioItem(config.lastSleepTimerSeconds, text))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.simplemobiletools.flashlight.dialogs
|
package com.simplemobiletools.flashlight.dialogs
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
|
import android.view.inputmethod.EditorInfo
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.*
|
||||||
import com.simplemobiletools.flashlight.R
|
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)
|
private val binding = DialogCustomSleepTimerPickerBinding.inflate(activity.layoutInflater)
|
||||||
|
|
||||||
init {
|
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()
|
activity.getAlertDialogBuilder()
|
||||||
.setPositiveButton(R.string.ok) { dialog, which -> dialogConfirmed() }
|
.setPositiveButton(R.string.ok) { _, _ -> dialogConfirmed() }
|
||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
.apply {
|
.apply {
|
||||||
activity.setupDialogStuff(binding.root, this, R.string.sleep_timer) { alertDialog ->
|
activity.setupDialogStuff(binding.root, this, R.string.sleep_timer) { alertDialog ->
|
||||||
dialog = alertDialog
|
dialog = alertDialog
|
||||||
alertDialog.showKeyboard(binding.minutes)
|
alertDialog.showKeyboard(binding.timerValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun dialogConfirmed() {
|
private fun dialogConfirmed() {
|
||||||
val value = binding.minutes.value
|
val value = binding.timerValue.value
|
||||||
val minutes = Integer.valueOf(if (value.isEmpty()) "0" else value)
|
val minutes = Integer.valueOf(value.ifEmpty { "0" })
|
||||||
callback(minutes * 60)
|
val multiplier = getMultiplier(binding.dialogRadioView.checkedRadioButtonId)
|
||||||
|
callback(minutes * multiplier)
|
||||||
activity.hideKeyboard()
|
activity.hideKeyboard()
|
||||||
dialog?.dismiss()
|
dialog?.dismiss()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getMultiplier(id: Int) = when (id) {
|
||||||
|
R.id.dialog_radio_seconds -> 1
|
||||||
|
R.id.dialog_radio_minutes -> 60
|
||||||
|
else -> 60
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,19 +3,19 @@
|
|||||||
android:id="@+id/dialog_custom_sleep_timer_holder"
|
android:id="@+id/dialog_custom_sleep_timer_holder"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal"
|
android:orientation="vertical"
|
||||||
android:paddingLeft="@dimen/activity_margin"
|
android:paddingLeft="@dimen/activity_margin"
|
||||||
android:paddingTop="@dimen/activity_margin"
|
android:paddingTop="@dimen/activity_margin"
|
||||||
android:paddingRight="@dimen/activity_margin">
|
android:paddingRight="@dimen/activity_margin">
|
||||||
|
|
||||||
<com.simplemobiletools.commons.views.MyTextInputLayout
|
<com.simplemobiletools.commons.views.MyTextInputLayout
|
||||||
android:id="@+id/minutes_hint"
|
android:id="@+id/value_hint"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:hint="@string/minutes_raw">
|
android:hint="@string/value">
|
||||||
|
|
||||||
<com.google.android.material.textfield.TextInputEditText
|
<com.google.android.material.textfield.TextInputEditText
|
||||||
android:id="@+id/minutes"
|
android:id="@+id/timer_value"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginBottom="@dimen/activity_margin"
|
android:layout_marginBottom="@dimen/activity_margin"
|
||||||
@ -24,7 +24,32 @@
|
|||||||
android:maxLength="5"
|
android:maxLength="5"
|
||||||
android:singleLine="true"
|
android:singleLine="true"
|
||||||
android:textCursorDrawable="@null"
|
android:textCursorDrawable="@null"
|
||||||
|
android:imeOptions="actionDone"
|
||||||
android:textSize="@dimen/normal_text_size" />
|
android:textSize="@dimen/normal_text_size" />
|
||||||
|
|
||||||
</com.simplemobiletools.commons.views.MyTextInputLayout>
|
</com.simplemobiletools.commons.views.MyTextInputLayout>
|
||||||
|
|
||||||
|
<RadioGroup
|
||||||
|
android:id="@+id/dialog_radio_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingTop="@dimen/normal_margin">
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||||
|
android:id="@+id/dialog_radio_minutes"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingTop="@dimen/normal_margin"
|
||||||
|
android:paddingBottom="@dimen/normal_margin"
|
||||||
|
android:text="@string/minutes_raw" />
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||||
|
android:id="@+id/dialog_radio_seconds"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingTop="@dimen/normal_margin"
|
||||||
|
android:paddingBottom="@dimen/normal_margin"
|
||||||
|
android:text="@string/seconds_raw" />
|
||||||
|
|
||||||
|
</RadioGroup>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
Reference in New Issue
Block a user