Fix crashes caused by view bindings

This commit is contained in:
Bnyro 2023-04-10 13:31:58 +02:00
parent a42f7ddb50
commit c0241222ee
2 changed files with 6 additions and 2 deletions

View File

@ -132,6 +132,7 @@ class HomeFragment : Fragment() {
val bookmarkedPlaylists = withContext(Dispatchers.IO) { val bookmarkedPlaylists = withContext(Dispatchers.IO) {
DatabaseHolder.Database.playlistBookmarkDao().getAll() DatabaseHolder.Database.playlistBookmarkDao().getAll()
}.takeIf { it.isNotEmpty() } ?: return }.takeIf { it.isNotEmpty() } ?: return
val binding = _binding ?: return
makeVisible(binding.bookmarksTV, binding.bookmarksRV) makeVisible(binding.bookmarksTV, binding.bookmarksRV)
binding.bookmarksRV.layoutManager = LinearLayoutManager( binding.bookmarksRV.layoutManager = LinearLayoutManager(
@ -151,6 +152,7 @@ class HomeFragment : Fragment() {
PlaylistsHelper.getPlaylists().take(20) PlaylistsHelper.getPlaylists().take(20)
} }
}.getOrNull()?.takeIf { it.isNotEmpty() } ?: return }.getOrNull()?.takeIf { it.isNotEmpty() } ?: return
val binding = _binding ?: return
makeVisible(binding.playlistsRV, binding.playlistsTV) makeVisible(binding.playlistsRV, binding.playlistsTV)
binding.playlistsRV.layoutManager = LinearLayoutManager(context) binding.playlistsRV.layoutManager = LinearLayoutManager(context)

View File

@ -33,6 +33,7 @@ class WatchHistoryFragment : Fragment() {
private var _binding: FragmentWatchHistoryBinding? = null private var _binding: FragmentWatchHistoryBinding? = null
private val binding get() = _binding!! private val binding get() = _binding!!
private val handler = Handler(Looper.getMainLooper())
private val playerViewModel: PlayerViewModel by activityViewModels() private val playerViewModel: PlayerViewModel by activityViewModels()
private var isLoading = false private var isLoading = false
@ -49,7 +50,7 @@ class WatchHistoryFragment : Fragment() {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
playerViewModel.isMiniPlayerVisible.observe(viewLifecycleOwner) { playerViewModel.isMiniPlayerVisible.observe(viewLifecycleOwner) {
binding.watchHistoryRecView.updatePadding( _binding?.watchHistoryRecView?.updatePadding(
bottom = if (it) (64).dpToPx().toInt() else 0 bottom = if (it) (64).dpToPx().toInt() else 0
) )
} }
@ -148,7 +149,8 @@ class WatchHistoryFragment : Fragment() {
}) })
// add a listener for scroll end, delay needed to prevent loading new ones the first time // add a listener for scroll end, delay needed to prevent loading new ones the first time
Handler(Looper.getMainLooper()).postDelayed(200) { handler.postDelayed(200) {
if (_binding == null) return@postDelayed
binding.historyScrollView.viewTreeObserver.addOnScrollChangedListener { binding.historyScrollView.viewTreeObserver.addOnScrollChangedListener {
if (_binding?.historyScrollView?.canScrollVertically(1) == false && if (_binding?.historyScrollView?.canScrollVertically(1) == false &&
!isLoading !isLoading