Skip to content

Commit 788ef6f

Browse files
committed
Add radio button for seconds for custom sleep timer
1 parent c00d4cc commit 788ef6f

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,15 @@ class MainActivity : SimpleActivity() {
319319

320320
if (items.none { it.id == config.lastSleepTimerSeconds }) {
321321
val lastSleepTimerMinutes = config.lastSleepTimerSeconds / 60
322-
val text = resources.getQuantityString(R.plurals.minutes, lastSleepTimerMinutes, lastSleepTimerMinutes)
322+
val lastSleepTimerSeconds = config.lastSleepTimerSeconds % 60
323+
val parts = mutableListOf<String>()
324+
if (lastSleepTimerMinutes != 0) {
325+
parts.add(resources.getQuantityString(R.plurals.minutes, lastSleepTimerMinutes, lastSleepTimerMinutes))
326+
}
327+
if (lastSleepTimerSeconds != 0) {
328+
parts.add(resources.getQuantityString(R.plurals.seconds, lastSleepTimerSeconds, lastSleepTimerSeconds))
329+
}
330+
val text = parts.joinToString(separator = " ")
323331
items.add(RadioItem(config.lastSleepTimerSeconds, text))
324332
}
325333

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.simplemobiletools.flashlight.dialogs
22

33
import android.app.Activity
4+
import android.view.inputmethod.EditorInfo
45
import androidx.appcompat.app.AlertDialog
56
import com.simplemobiletools.commons.extensions.*
67
import com.simplemobiletools.flashlight.R
@@ -11,23 +12,38 @@ class SleepTimerCustomDialog(val activity: Activity, val callback: (seconds: Int
1112
private val binding = DialogCustomSleepTimerPickerBinding.inflate(activity.layoutInflater)
1213

1314
init {
14-
binding.minutesHint.hint = activity.getString(R.string.minutes_raw).replaceFirstChar { it.uppercaseChar() }
15+
binding.dialogRadioView.check(R.id.dialog_radio_minutes)
16+
binding.timerValue.setOnEditorActionListener { _, actionId, _ ->
17+
if (actionId == EditorInfo.IME_ACTION_DONE) {
18+
dialogConfirmed()
19+
return@setOnEditorActionListener true
20+
}
21+
return@setOnEditorActionListener false
22+
}
23+
1524
activity.getAlertDialogBuilder()
16-
.setPositiveButton(R.string.ok) { dialog, which -> dialogConfirmed() }
25+
.setPositiveButton(R.string.ok) { _, _ -> dialogConfirmed() }
1726
.setNegativeButton(R.string.cancel, null)
1827
.apply {
1928
activity.setupDialogStuff(binding.root, this, R.string.sleep_timer) { alertDialog ->
2029
dialog = alertDialog
21-
alertDialog.showKeyboard(binding.minutes)
30+
alertDialog.showKeyboard(binding.timerValue)
2231
}
2332
}
2433
}
2534

2635
private fun dialogConfirmed() {
27-
val value = binding.minutes.value
28-
val minutes = Integer.valueOf(if (value.isEmpty()) "0" else value)
29-
callback(minutes * 60)
36+
val value = binding.timerValue.value
37+
val minutes = Integer.valueOf(value.ifEmpty { "0" })
38+
val multiplier = getMultiplier(binding.dialogRadioView.checkedRadioButtonId)
39+
callback(minutes * multiplier)
3040
activity.hideKeyboard()
3141
dialog?.dismiss()
3242
}
43+
44+
private fun getMultiplier(id: Int) = when (id) {
45+
R.id.dialog_radio_seconds -> 1
46+
R.id.dialog_radio_minutes -> 60
47+
else -> 60
48+
}
3349
}

app/src/main/res/layout/dialog_custom_sleep_timer_picker.xml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
android:id="@+id/dialog_custom_sleep_timer_holder"
44
android:layout_width="match_parent"
55
android:layout_height="wrap_content"
6-
android:orientation="horizontal"
6+
android:orientation="vertical"
77
android:paddingLeft="@dimen/activity_margin"
88
android:paddingTop="@dimen/activity_margin"
99
android:paddingRight="@dimen/activity_margin">
1010

1111
<com.simplemobiletools.commons.views.MyTextInputLayout
12-
android:id="@+id/minutes_hint"
12+
android:id="@+id/value_hint"
1313
android:layout_width="match_parent"
1414
android:layout_height="wrap_content"
15-
android:hint="@string/minutes_raw">
15+
android:hint="@string/value">
1616

1717
<com.google.android.material.textfield.TextInputEditText
18-
android:id="@+id/minutes"
18+
android:id="@+id/timer_value"
1919
android:layout_width="match_parent"
2020
android:layout_height="wrap_content"
2121
android:layout_marginBottom="@dimen/activity_margin"
@@ -24,7 +24,32 @@
2424
android:maxLength="5"
2525
android:singleLine="true"
2626
android:textCursorDrawable="@null"
27+
android:imeOptions="actionDone"
2728
android:textSize="@dimen/normal_text_size" />
2829

2930
</com.simplemobiletools.commons.views.MyTextInputLayout>
31+
32+
<RadioGroup
33+
android:id="@+id/dialog_radio_view"
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:paddingTop="@dimen/normal_margin">
37+
38+
<com.simplemobiletools.commons.views.MyCompatRadioButton
39+
android:id="@+id/dialog_radio_minutes"
40+
android:layout_width="match_parent"
41+
android:layout_height="wrap_content"
42+
android:paddingTop="@dimen/normal_margin"
43+
android:paddingBottom="@dimen/normal_margin"
44+
android:text="@string/minutes_raw" />
45+
46+
<com.simplemobiletools.commons.views.MyCompatRadioButton
47+
android:id="@+id/dialog_radio_seconds"
48+
android:layout_width="match_parent"
49+
android:layout_height="wrap_content"
50+
android:paddingTop="@dimen/normal_margin"
51+
android:paddingBottom="@dimen/normal_margin"
52+
android:text="@string/seconds_raw" />
53+
54+
</RadioGroup>
3055
</LinearLayout>

0 commit comments

Comments
 (0)