From 23ae63d0f3ae7db67eebf582e772e60377ac9908 Mon Sep 17 00:00:00 2001 From: Luca Stefani Date: Tue, 15 Aug 2023 20:08:41 +0200 Subject: [PATCH] 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 --- .../lineageos/glimpse/fragments/AlbumFragment.kt | 16 +++++++++++----- .../glimpse/fragments/AlbumsFragment.kt | 8 ++++++-- .../glimpse/fragments/MediaViewerFragment.kt | 14 ++++++++++---- .../lineageos/glimpse/fragments/ReelsFragment.kt | 14 ++++++++++---- 4 files changed, 37 insertions(+), 15 deletions(-) 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.updateLayoutParams 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 @@ class AlbumFragment : Fragment(R.layout.fragment_album) { 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 @@ class AlbumFragment : Fragment(R.layout.fragment_album) { } 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.updateLayoutParams 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 @@ class AlbumsFragment : Fragment() { } 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.WindowInsetsCompat 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 @@ class MediaViewerFragment : Fragment(R.layout.fragment_media_viewer) { 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 @@ class MediaViewerFragment : Fragment(R.layout.fragment_media_viewer) { 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.updateLayoutParams 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 @@ class ReelsFragment : Fragment(R.layout.fragment_reels) { 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 @@ class ReelsFragment : Fragment(R.layout.fragment_reels) { 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()