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.core.view.updatePadding
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController import androidx.navigation.ui.setupWithNavController
@ -64,9 +66,11 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
requireActivity().finish() requireActivity().finish()
} else { } else {
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
mediaViewModel.setBucketId(album.id) viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
mediaViewModel.mediaForAlbum.collectLatest { data -> mediaViewModel.setBucketId(album.id)
thumbnailAdapter.data = data.toTypedArray() mediaViewModel.mediaForAlbum.collectLatest { data ->
thumbnailAdapter.data = data.toTypedArray()
}
} }
} }
} }
@ -123,8 +127,10 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
} else { } else {
mediaViewModel.setBucketId(album.id) mediaViewModel.setBucketId(album.id)
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
mediaViewModel.mediaForAlbum.collectLatest { data -> viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
thumbnailAdapter.data = data.toTypedArray() 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.core.view.updatePadding
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -74,8 +76,10 @@ class AlbumsFragment : Fragment() {
} }
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
mediaViewModel.albums.collectLatest { viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
albumThumbnailAdapter.data = it.toTypedArray() 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.core.view.updateLayoutParams
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.media3.common.C import androidx.media3.common.C
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.ExoPlayer
@ -80,8 +82,10 @@ class MediaViewerFragment : Fragment(R.layout.fragment_media_viewer) {
requireActivity().finish() requireActivity().finish()
} else { } else {
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
mediaViewModel.setBucketId(album?.id) viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
mediaViewModel.mediaForAlbum.collectLatest(::initData) mediaViewModel.setBucketId(album?.id)
mediaViewModel.mediaForAlbum.collectLatest(::initData)
}
} }
} }
} }
@ -302,8 +306,10 @@ class MediaViewerFragment : Fragment(R.layout.fragment_media_viewer) {
mainPermissionsRequestLauncher.launch(PermissionsUtils.mainPermissions) mainPermissionsRequestLauncher.launch(PermissionsUtils.mainPermissions)
} else { } else {
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
mediaViewModel.setBucketId(album?.id) viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
mediaViewModel.mediaForAlbum.collectLatest(::initData) 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.core.view.updatePadding
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
@ -60,8 +62,10 @@ class ReelsFragment : Fragment(R.layout.fragment_reels) {
requireActivity().finish() requireActivity().finish()
} else { } else {
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
mediaViewModel.media.collectLatest { data -> viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
thumbnailAdapter.data = data.toTypedArray() mediaViewModel.media.collectLatest { data ->
thumbnailAdapter.data = data.toTypedArray()
}
} }
} }
permissionsUtils.showManageMediaPermissionDialogIfNeeded() permissionsUtils.showManageMediaPermissionDialogIfNeeded()
@ -105,8 +109,10 @@ class ReelsFragment : Fragment(R.layout.fragment_reels) {
mainPermissionsRequestLauncher.launch(PermissionsUtils.mainPermissions) mainPermissionsRequestLauncher.launch(PermissionsUtils.mainPermissions)
} else { } else {
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
mediaViewModel.media.collectLatest { data -> viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
thumbnailAdapter.data = data.toTypedArray() mediaViewModel.media.collectLatest { data ->
thumbnailAdapter.data = data.toTypedArray()
}
} }
} }
permissionsUtils.showManageMediaPermissionDialogIfNeeded() permissionsUtils.showManageMediaPermissionDialogIfNeeded()