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
This commit is contained in:
Luca Stefani 2023-08-15 20:08:41 +02:00 committed by Sebastiano Barezzi
parent f5b020e292
commit 23ae63d0f3
No known key found for this signature in database
GPG Key ID: 763BD3AE91A7A13F
4 changed files with 37 additions and 15 deletions

View File

@ -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()
}
}
}
}

View File

@ -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()
}
}
}
}

View File

@ -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)
}
}
}
}

View File

@ -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()