fix: observe livedata once in onViewCreated (#6955)

This commit is contained in:
Thomas W. 2025-01-15 15:09:44 +01:00 committed by GitHub
parent aef23e26e5
commit eb7df283eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -54,6 +54,15 @@ class HomeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
with(homeViewModel) {
trending.observe(viewLifecycleOwner, ::showTrending)
feed.observe(viewLifecycleOwner, ::showFeed)
bookmarks.observe(viewLifecycleOwner, ::showBookmarks)
playlists.observe(viewLifecycleOwner, ::showPlaylists)
continueWatching.observe(viewLifecycleOwner, ::showContinueWatching)
isLoading.observe(viewLifecycleOwner, ::updateLoading)
}
binding.featuredTV.setOnClickListener {
findNavController().navigate(R.id.subscriptionsFragment)
}
@ -94,7 +103,6 @@ class HomeFragment : Fragment() {
override fun onResume() {
super.onResume()
observeChanges()
// Avoid re-fetching when re-entering the screen if it was loaded successfully
if (homeViewModel.loadedSuccessfully.value == false) {
@ -102,33 +110,6 @@ class HomeFragment : Fragment() {
}
}
private fun observeChanges() {
with(homeViewModel) {
trending.observe(viewLifecycleOwner, ::showTrending)
feed.observe(viewLifecycleOwner, ::showFeed)
bookmarks.observe(viewLifecycleOwner, ::showBookmarks)
playlists.observe(viewLifecycleOwner, ::showPlaylists)
continueWatching.observe(viewLifecycleOwner, ::showContinueWatching)
isLoading.observe(viewLifecycleOwner, ::updateLoading)
}
}
override fun onPause() {
super.onPause()
stopObservingChanges()
}
private fun stopObservingChanges() {
with(homeViewModel) {
trending.removeObserver(::showTrending)
feed.removeObserver(::showFeed)
bookmarks.removeObserver(::showBookmarks)
playlists.removeObserver(::showPlaylists)
continueWatching.removeObserver(::showContinueWatching)
isLoading.removeObserver(::updateLoading)
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null