Aperture: Use .next() where possible

Change-Id: Ife78738040a77e72ee72424e59bd714e1be735ad
diff --git a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
index 6df449d..32e3e35 100644
--- a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
@@ -91,6 +91,7 @@
 import org.lineageos.aperture.utils.StabilizationMode
 import org.lineageos.aperture.utils.StorageUtils
 import org.lineageos.aperture.utils.TimeUtils
+import org.lineageos.aperture.utils.TimerMode
 import java.io.FileNotFoundException
 import java.util.concurrent.ExecutorService
 import kotlin.math.abs
@@ -1227,12 +1228,7 @@
      * Set the specified grid mode, also updating the icon
      */
     private fun cycleGridMode() {
-        sharedPreferences.lastGridMode = when (sharedPreferences.lastGridMode) {
-            GridMode.OFF -> GridMode.ON_3
-            GridMode.ON_3 -> GridMode.ON_4
-            GridMode.ON_4 -> GridMode.ON_GOLDENRATIO
-            GridMode.ON_GOLDENRATIO -> GridMode.OFF
-        }
+        sharedPreferences.lastGridMode = sharedPreferences.lastGridMode.next()
         setGridMode(sharedPreferences.lastGridMode)
     }
 
@@ -1249,18 +1245,18 @@
             timerButton.setCompoundDrawablesWithIntrinsicBounds(
                 0,
                 when (it) {
-                    3 -> R.drawable.ic_timer_3
-                    10 -> R.drawable.ic_timer_10
-                    else -> R.drawable.ic_timer_off
+                    TimerMode.OFF -> R.drawable.ic_timer_off
+                    TimerMode.ON_3S -> R.drawable.ic_timer_3
+                    TimerMode.ON_10S -> R.drawable.ic_timer_10
                 },
                 0,
                 0
             )
             timerButton.text = resources.getText(
                 when (it) {
-                    3 -> R.string.timer_3
-                    10 -> R.string.timer_10
-                    else -> R.string.timer_off
+                    TimerMode.OFF -> R.string.timer_off
+                    TimerMode.ON_3S -> R.string.timer_3
+                    TimerMode.ON_10S -> R.string.timer_10
                 }
             )
         }
@@ -1270,11 +1266,7 @@
      * Toggle timer mode
      */
     private fun toggleTimerMode() {
-        sharedPreferences.timerMode = when (sharedPreferences.timerMode) {
-            0 -> 3
-            3 -> 10
-            else -> 0
-        }
+        sharedPreferences.timerMode = sharedPreferences.timerMode.next()
         updateTimerModeIcon()
     }
 
@@ -1664,7 +1656,7 @@
     }
 
     private fun startTimerAndRun(runnable: () -> Unit) {
-        if (sharedPreferences.timerMode <= 0 || !canRestartCamera()) {
+        if (sharedPreferences.timerMode == TimerMode.OFF || !canRestartCamera()) {
             runnable()
             return
         }
@@ -1674,7 +1666,7 @@
         countDownView.onPreviewAreaChanged(Rect().apply {
             viewFinder.getGlobalVisibleRect(this)
         })
-        countDownView.startCountDown(sharedPreferences.timerMode) {
+        countDownView.startCountDown(sharedPreferences.timerMode.seconds) {
             shutterButton.isEnabled = true
             runnable()
         }
diff --git a/app/src/main/java/org/lineageos/aperture/SharedPreferencesExt.kt b/app/src/main/java/org/lineageos/aperture/SharedPreferencesExt.kt
index bdb68a3..6b26772 100644
--- a/app/src/main/java/org/lineageos/aperture/SharedPreferencesExt.kt
+++ b/app/src/main/java/org/lineageos/aperture/SharedPreferencesExt.kt
@@ -18,6 +18,7 @@
 import org.lineageos.aperture.utils.Framerate
 import org.lineageos.aperture.utils.GridMode
 import org.lineageos.aperture.utils.StabilizationMode
+import org.lineageos.aperture.utils.TimerMode
 
 // Generic prefs
 private const val LAST_CAMERA_FACING_KEY = "last_camera_facing"
@@ -233,10 +234,10 @@
 private const val TIMER_MODE_KEY = "timer_mode"
 private const val TIMER_MODE_DEFAULT = 0
 
-internal var SharedPreferences.timerMode: Int
-    get() = getInt(TIMER_MODE_KEY, TIMER_MODE_DEFAULT)
+internal var SharedPreferences.timerMode: TimerMode
+    get() = TimerMode.fromSeconds(getInt(TIMER_MODE_KEY, TIMER_MODE_DEFAULT)) ?: TimerMode.OFF
     set(value) = edit {
-        putInt(TIMER_MODE_KEY, value)
+        putInt(TIMER_MODE_KEY, value.seconds)
     }
 
 // Aspect ratio
diff --git a/app/src/main/java/org/lineageos/aperture/utils/GridMode.kt b/app/src/main/java/org/lineageos/aperture/utils/GridMode.kt
index 16d48af..635bb8a 100644
--- a/app/src/main/java/org/lineageos/aperture/utils/GridMode.kt
+++ b/app/src/main/java/org/lineageos/aperture/utils/GridMode.kt
@@ -5,9 +5,16 @@
 
 package org.lineageos.aperture.utils
 
+import org.lineageos.aperture.next
+
 enum class GridMode {
     OFF,
     ON_3,
     ON_4,
-    ON_GOLDENRATIO,
+    ON_GOLDENRATIO;
+
+    /**
+     * Get the next mode.
+     */
+    fun next() = values().next(this)
 }
diff --git a/app/src/main/java/org/lineageos/aperture/utils/TimerMode.kt b/app/src/main/java/org/lineageos/aperture/utils/TimerMode.kt
new file mode 100644
index 0000000..c486b0e
--- /dev/null
+++ b/app/src/main/java/org/lineageos/aperture/utils/TimerMode.kt
@@ -0,0 +1,23 @@
+/*
+ * SPDX-FileCopyrightText: 2022 The LineageOS Project
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.lineageos.aperture.utils
+
+import org.lineageos.aperture.next
+
+enum class TimerMode(val seconds: Int) {
+    OFF(0),
+    ON_3S(3),
+    ON_10S(10);
+
+    /**
+     * Get the next mode.
+     */
+    fun next() = values().next(this)
+
+    companion object {
+        fun fromSeconds(seconds: Int) = values().firstOrNull { it.seconds == seconds }
+    }
+}