Glimpse: Collect on resume

ContentObservers are tied to the lifecycle of the application
and must be started/stopped onResume to ensure proper callbacks

Change-Id: I63c1a6ebc3e734a84aa729f36296d570801e6c70
diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/AlbumFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/AlbumFragment.kt
index 58a1449..4eee023 100644
--- a/app/src/main/java/org/lineageos/glimpse/fragments/AlbumFragment.kt
+++ b/app/src/main/java/org/lineageos/glimpse/fragments/AlbumFragment.kt
@@ -18,7 +18,9 @@
 import androidx.core.view.updatePadding
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.viewModels
+import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.repeatOnLifecycle
 import androidx.navigation.fragment.findNavController
 import androidx.navigation.ui.AppBarConfiguration
 import androidx.navigation.ui.setupWithNavController
@@ -64,9 +66,11 @@
                 requireActivity().finish()
             } else {
                 viewLifecycleOwner.lifecycleScope.launch {
-                    mediaViewModel.setBucketId(album.id)
-                    mediaViewModel.mediaForAlbum.collectLatest { data ->
-                        thumbnailAdapter.data = data.toTypedArray()
+                    viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
+                        mediaViewModel.setBucketId(album.id)
+                        mediaViewModel.mediaForAlbum.collectLatest { data ->
+                            thumbnailAdapter.data = data.toTypedArray()
+                        }
                     }
                 }
             }
@@ -123,8 +127,10 @@
         } else {
             mediaViewModel.setBucketId(album.id)
             viewLifecycleOwner.lifecycleScope.launch {
-                mediaViewModel.mediaForAlbum.collectLatest { data ->
-                    thumbnailAdapter.data = data.toTypedArray()
+                viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
+                    mediaViewModel.mediaForAlbum.collectLatest { data ->
+                        thumbnailAdapter.data = data.toTypedArray()
+                    }
                 }
             }
         }
diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt
index 37398e1..2a8e70e 100644
--- a/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt
+++ b/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt
@@ -17,7 +17,9 @@
 import androidx.core.view.updatePadding
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.viewModels
+import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.repeatOnLifecycle
 import androidx.navigation.fragment.findNavController
 import androidx.recyclerview.widget.GridLayoutManager
 import androidx.recyclerview.widget.RecyclerView
@@ -74,8 +76,10 @@
         }
 
         viewLifecycleOwner.lifecycleScope.launch {
-            mediaViewModel.albums.collectLatest {
-                albumThumbnailAdapter.data = it.toTypedArray()
+            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
+                mediaViewModel.albums.collectLatest {
+                    albumThumbnailAdapter.data = it.toTypedArray()
+                }
             }
         }
     }
diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt
index 381595a..e27b09e 100644
--- a/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt
+++ b/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt
@@ -23,7 +23,9 @@
 import androidx.core.view.updateLayoutParams
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.viewModels
+import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.repeatOnLifecycle
 import androidx.media3.common.C
 import androidx.media3.common.MediaItem
 import androidx.media3.exoplayer.ExoPlayer
@@ -80,8 +82,10 @@
                 requireActivity().finish()
             } else {
                 viewLifecycleOwner.lifecycleScope.launch {
-                    mediaViewModel.setBucketId(album?.id)
-                    mediaViewModel.mediaForAlbum.collectLatest(::initData)
+                    viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
+                        mediaViewModel.setBucketId(album?.id)
+                        mediaViewModel.mediaForAlbum.collectLatest(::initData)
+                    }
                 }
             }
         }
@@ -302,8 +306,10 @@
             mainPermissionsRequestLauncher.launch(PermissionsUtils.mainPermissions)
         } else {
             viewLifecycleOwner.lifecycleScope.launch {
-                mediaViewModel.setBucketId(album?.id)
-                mediaViewModel.mediaForAlbum.collectLatest(::initData)
+                viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
+                    mediaViewModel.setBucketId(album?.id)
+                    mediaViewModel.mediaForAlbum.collectLatest(::initData)
+                }
             }
         }
     }
diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt
index 7f67801..12a0259 100644
--- a/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt
+++ b/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt
@@ -18,7 +18,9 @@
 import androidx.core.view.updatePadding
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.viewModels
+import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.repeatOnLifecycle
 import androidx.navigation.fragment.findNavController
 import androidx.recyclerview.widget.RecyclerView
 import kotlinx.coroutines.flow.collectLatest
@@ -60,8 +62,10 @@
                 requireActivity().finish()
             } else {
                 viewLifecycleOwner.lifecycleScope.launch {
-                    mediaViewModel.media.collectLatest { data ->
-                        thumbnailAdapter.data = data.toTypedArray()
+                    viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
+                        mediaViewModel.media.collectLatest { data ->
+                            thumbnailAdapter.data = data.toTypedArray()
+                        }
                     }
                 }
                 permissionsUtils.showManageMediaPermissionDialogIfNeeded()
@@ -105,8 +109,10 @@
             mainPermissionsRequestLauncher.launch(PermissionsUtils.mainPermissions)
         } else {
             viewLifecycleOwner.lifecycleScope.launch {
-                mediaViewModel.media.collectLatest { data ->
-                    thumbnailAdapter.data = data.toTypedArray()
+                viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
+                    mediaViewModel.media.collectLatest { data ->
+                        thumbnailAdapter.data = data.toTypedArray()
+                    }
                 }
             }
             permissionsUtils.showManageMediaPermissionDialogIfNeeded()