@@ -16,6 +16,7 @@ import com.hifnawy.caffeinate.view.CheckBoxItem
1616import com.hifnawy.caffeinate.view.Widget
1717import java.util.Locale
1818import kotlin.time.Duration
19+ import kotlin.time.Duration.Companion.seconds
1920import timber.log.Timber as Log
2021
2122/* *
@@ -53,6 +54,41 @@ class CaffeinateApplication : Application() {
5354 */
5455 private val sharedPreferences by lazy { SharedPrefsManager (this ) }
5556
57+ /* *
58+ * Returns the current locale set by the user in the system settings.
59+ *
60+ * If the device is running Android 13 (API level 33) or later, the current locale is retrieved
61+ * from the [resources.configuration.locales][android.content.res.Configuration.getLocales] list. Otherwise, the current locale is retrieved
62+ * from the [getDefault][Locale.getDefault] method.
63+ *
64+ * @return the current locale set by the user in the system settings.
65+ *
66+ * @see android.content.res.Configuration
67+ * @see Locale.getDefault
68+ */
69+ private val currentLocale: Locale
70+ get() = when {
71+ Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU -> resources.configuration.locales[0 ]
72+ else -> Locale .getDefault()
73+ }
74+
75+ /* *
76+ * A list of [CheckBoxItem]s that represent the timeout durations the user can select from.
77+ *
78+ * This list is stored in the app's SharedPreferences and is loaded when the app is launched.
79+ *
80+ * The list of timeout durations is used to populate the [RecyclerView][androidx.recyclerview.widget.RecyclerView] in the "Choose timeout" dialog,
81+ * which is shown when the user clicks the "Choose timeout" button in the app's UI.
82+ *
83+ * The list is also used to determine the next timeout duration to use when the current timeout duration is finished.
84+ *
85+ * @see CheckBoxItem
86+ * @see android.content.SharedPreferences
87+ * @see androidx.recyclerview.widget.RecyclerView
88+ */
89+ val timeoutCheckBoxes
90+ get() = sharedPreferences.timeoutCheckBoxes
91+
5692 /* *
5793 * The first timeout duration that was selected by the user.
5894 *
@@ -62,7 +98,7 @@ class CaffeinateApplication : Application() {
6298 *
6399 * @return [Duration] the first timeout duration that was selected by the user, or [Duration.INFINITE] if no timeout duration was selected.
64100 */
65- val firstTimeout: Duration
101+ val firstTimeout
66102 get() = timeoutCheckBoxes.first { checkBoxItem -> checkBoxItem.isChecked }.duration
67103
68104 /* *
@@ -73,7 +109,7 @@ class CaffeinateApplication : Application() {
73109 *
74110 * @return [Duration] the last timeout duration that was selected by the user.
75111 */
76- val lastTimeout: Duration
112+ val lastTimeout
77113 get() = timeoutCheckBoxes.last { checkBoxItem -> checkBoxItem.isChecked }.duration
78114
79115 /* *
@@ -84,15 +120,14 @@ class CaffeinateApplication : Application() {
84120 *
85121 * @return [Duration] the previously selected timeout duration.
86122 */
87- val prevTimeout: Duration
88- get() {
89- val timeoutCheckBox = timeoutCheckBoxes.first { timeoutCheckBox -> timeoutCheckBox.duration == timeout }
123+ val prevTimeout
124+ get() = timeoutCheckBoxes.first { timeoutCheckBox -> timeoutCheckBox.duration == timeout }.let { timeoutCheckBox ->
90125 val index = timeoutCheckBoxes.indexOf(timeoutCheckBox)
91126 var prevIndex = (index - 1 + timeoutCheckBoxes.size) % timeoutCheckBoxes.size
92127
93128 while (! timeoutCheckBoxes[prevIndex].isChecked) prevIndex = (prevIndex - 1 + timeoutCheckBoxes.size) % timeoutCheckBoxes.size
94129
95- return timeoutCheckBoxes[prevIndex].duration
130+ timeoutCheckBoxes[prevIndex].duration
96131 }
97132
98133 /* *
@@ -104,15 +139,14 @@ class CaffeinateApplication : Application() {
104139 *
105140 * @return [Duration] the next timeout duration that will be used when the KeepAwakeService is running.
106141 */
107- val nextTimeout: Duration
108- get() {
109- val timeoutCheckBox = timeoutCheckBoxes.first { timeoutCheckBox -> timeoutCheckBox.duration == timeout }
142+ val nextTimeout
143+ get() = timeoutCheckBoxes.first { timeoutCheckBox -> timeoutCheckBox.duration == timeout }.let { timeoutCheckBox ->
110144 val index = timeoutCheckBoxes.indexOf(timeoutCheckBox)
111145 var nextIndex = (index + 1 ) % timeoutCheckBoxes.size
112146
113147 while (! timeoutCheckBoxes[nextIndex].isChecked) nextIndex = (nextIndex + 1 ) % timeoutCheckBoxes.size
114148
115- return timeoutCheckBoxes[nextIndex].duration
149+ timeoutCheckBoxes[nextIndex].duration
116150 }
117151
118152 /* *
@@ -126,7 +160,7 @@ class CaffeinateApplication : Application() {
126160 * @see com.hifnawy.caffeinate.controller.KeepAwakeService
127161 * @see SharedPrefsManager.timeouts
128162 */
129- var timeout: Duration = sharedPreferences.timeouts.first()
163+ var timeout = 0 .seconds
130164
131165 /* *
132166 * A list of observers that are notified whenever the status of the KeepAwakeService changes.
@@ -178,23 +212,6 @@ class CaffeinateApplication : Application() {
178212 }
179213 }
180214
181- /* *
182- * A list of [CheckBoxItem]s that represent the timeout durations the user can select from.
183- *
184- * This list is stored in the app's SharedPreferences and is loaded when the app is launched.
185- *
186- * The list of timeout durations is used to populate the [RecyclerView][androidx.recyclerview.widget.RecyclerView] in the "Choose timeout" dialog,
187- * which is shown when the user clicks the "Choose timeout" button in the app's UI.
188- *
189- * The list is also used to determine the next timeout duration to use when the current timeout duration is finished.
190- *
191- * @see CheckBoxItem
192- * @see android.content.SharedPreferences
193- * @see androidx.recyclerview.widget.RecyclerView
194- */
195- lateinit var timeoutCheckBoxes: MutableList <CheckBoxItem >
196- private set
197-
198215 /* *
199216 * The context of the application localized to the user's current locale.
200217 *
@@ -237,24 +254,6 @@ class CaffeinateApplication : Application() {
237254 Log .d(" observers notified!" )
238255 }
239256
240- /* *
241- * Returns the current locale set by the user in the system settings.
242- *
243- * If the device is running Android 13 (API level 33) or later, the current locale is retrieved
244- * from the [resources.configuration.locales][android.content.res.Configuration.getLocales] list. Otherwise, the current locale is retrieved
245- * from the [getDefault][Locale.getDefault] method.
246- *
247- * @return the current locale set by the user in the system settings.
248- *
249- * @see android.content.res.Configuration
250- * @see Locale.getDefault
251- */
252- private val currentLocale: Locale
253- get() = when {
254- Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU -> resources.configuration.locales[0 ]
255- else -> Locale .getDefault()
256- }
257-
258257 /* *
259258 * Applies the locale configuration set by the user.
260259 *
@@ -274,8 +273,6 @@ class CaffeinateApplication : Application() {
274273 @Suppress(" AppBundleLocaleChanges" )
275274 configuration.setLocale(currentLocale)
276275 localizedApplicationContext = createConfigurationContext(configuration)
277-
278- timeoutCheckBoxes = sharedPreferences.timeoutCheckBoxes
279276 }
280277
281278 /* *
0 commit comments