Aperture: Use the view model for views' rotation

Change-Id: I97f5895e182b7d63dc44b2b7cd24ede933d8bf0d
diff --git a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
index 4722b3f..cbd6684 100644
--- a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
@@ -528,6 +528,10 @@
         cameraMode = overrideInitialCameraMode() ?: sharedPreferences.lastCameraMode
         initialCameraFacing = sharedPreferences.lastCameraFacing
 
+        // Pass the view model to the views
+        capturePreviewLayout.cameraViewModel = model
+        countDownView.cameraViewModel = model
+
         // Restore settings from shared preferences
         gridMode = sharedPreferences.lastGridMode
         timerMode = sharedPreferences.timerMode
@@ -2011,12 +2015,6 @@
         exposureLevel.screenRotation = screenRotation
         zoomLevel.screenRotation = screenRotation
 
-        // Rotate countdown
-        countDownView.screenRotation = screenRotation
-
-        // Rotate capture preview buttons
-        capturePreviewLayout.screenRotation = screenRotation
-
         // Rotate secondary top bar buttons
         ConstraintLayout::class.safeCast(
             secondaryTopBarLayout.getChildAt(0)
diff --git a/app/src/main/java/org/lineageos/aperture/ui/CapturePreviewLayout.kt b/app/src/main/java/org/lineageos/aperture/ui/CapturePreviewLayout.kt
index 4f0fc3b..6799fc2 100644
--- a/app/src/main/java/org/lineageos/aperture/ui/CapturePreviewLayout.kt
+++ b/app/src/main/java/org/lineageos/aperture/ui/CapturePreviewLayout.kt
@@ -13,10 +13,12 @@
 import android.widget.ImageView
 import androidx.constraintlayout.widget.ConstraintLayout
 import androidx.core.view.isVisible
+import androidx.lifecycle.findViewTreeLifecycleOwner
 import androidx.media3.common.MediaItem
 import androidx.media3.exoplayer.ExoPlayer
 import androidx.media3.ui.PlayerView
 import org.lineageos.aperture.R
+import org.lineageos.aperture.camera.CameraViewModel
 import org.lineageos.aperture.ext.*
 import org.lineageos.aperture.utils.ExifUtils
 import org.lineageos.aperture.utils.MediaType
@@ -46,11 +48,25 @@
      */
     internal var onChoiceCallback: (input: Any?) -> Unit = {}
 
-    internal var screenRotation = Rotation.ROTATION_0
+    internal var cameraViewModel: CameraViewModel? = null
         set(value) {
+            val lifecycleOwner = findViewTreeLifecycleOwner() ?: return
+
+            // Unregister
+            field?.screenRotation?.removeObservers(lifecycleOwner)
+
             field = value
-            updateViewsRotation()
+
+            value?.let { cameraViewModel ->
+                cameraViewModel.screenRotation.observe(lifecycleOwner) {
+                    val screenRotation = it ?: return@observe
+
+                    updateViewsRotation(screenRotation)
+                }
+            }
         }
+    private val screenRotation
+        get() = cameraViewModel?.screenRotation?.value ?: Rotation.ROTATION_0
 
     override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
         super.onLayout(changed, left, top, right, bottom)
@@ -133,7 +149,7 @@
         }
     }
 
-    private fun updateViewsRotation() {
+    private fun updateViewsRotation(screenRotation: Rotation) {
         val compensationValue = screenRotation.compensationValue.toFloat()
 
         cancelButton.smoothRotate(compensationValue)
diff --git a/app/src/main/java/org/lineageos/aperture/ui/CountDownView.kt b/app/src/main/java/org/lineageos/aperture/ui/CountDownView.kt
index 05f3c6b..5f04e17 100644
--- a/app/src/main/java/org/lineageos/aperture/ui/CountDownView.kt
+++ b/app/src/main/java/org/lineageos/aperture/ui/CountDownView.kt
@@ -17,7 +17,9 @@
 import androidx.annotation.IntRange
 import androidx.core.view.isInvisible
 import androidx.core.view.isVisible
+import androidx.lifecycle.findViewTreeLifecycleOwner
 import org.lineageos.aperture.R
+import org.lineageos.aperture.camera.CameraViewModel
 import org.lineageos.aperture.ext.*
 import org.lineageos.aperture.utils.Rotation
 
@@ -49,10 +51,22 @@
     private val isCountingDown: Boolean
         get() = remainingSeconds > 0
 
-    internal var screenRotation = Rotation.ROTATION_0
+    internal var cameraViewModel: CameraViewModel? = null
         set(value) {
+            val lifecycleOwner = findViewTreeLifecycleOwner() ?: return
+
+            // Unregister
+            field?.screenRotation?.removeObservers(lifecycleOwner)
+
             field = value
-            updateViewsRotation()
+
+            value?.let { cameraViewModel ->
+                cameraViewModel.screenRotation.observe(lifecycleOwner) {
+                    val screenRotation = it ?: return@observe
+
+                    updateViewsRotation(screenRotation)
+                }
+            }
         }
 
     /**
@@ -125,7 +139,7 @@
         return false
     }
 
-    private fun updateViewsRotation() {
+    private fun updateViewsRotation(screenRotation: Rotation) {
         val compensationValue = screenRotation.compensationValue.toFloat()
 
         remainingSecondsView.smoothRotate(compensationValue)