From 7c08838d1c518cfa4b9b4b956bd23f1cacb8143f Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 27 Apr 2023 09:03:53 +0200 Subject: [PATCH] Fix various crashes caused by view bindings --- .../libretube/ui/fragments/ChannelFragment.kt | 13 +++++++++---- .../libretube/ui/fragments/LibraryFragment.kt | 2 +- .../github/libretube/ui/fragments/PlayerFragment.kt | 8 ++++---- .../libretube/ui/fragments/PlaylistFragment.kt | 2 ++ .../libretube/ui/fragments/SearchResultFragment.kt | 2 ++ 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt index 1e326fa38..c0fc4e490 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt @@ -118,14 +118,16 @@ class ChannelFragment : Fragment() { } } } catch (e: IOException) { - binding.channelRefresh.isRefreshing = false + _binding?.channelRefresh?.isRefreshing = false Log.e(TAG(), "IOException, you might not have internet connection") return@repeatOnLifecycle } catch (e: HttpException) { - binding.channelRefresh.isRefreshing = false + _binding?.channelRefresh?.isRefreshing = false Log.e(TAG(), "HttpException, unexpected response") return@repeatOnLifecycle } + val binding = _binding ?: return@repeatOnLifecycle + // needed if the channel gets loaded by the ID channelId = response.id channelName = response.name @@ -240,6 +242,7 @@ class ChannelFragment : Fragment() { } catch (e: Exception) { return@launch } + val binding = _binding ?: return@launch val adapter = SearchAdapter() binding.channelRecView.adapter = adapter @@ -268,14 +271,16 @@ class ChannelFragment : Fragment() { RetrofitInstance.api.getChannelNextPage(channelId!!, nextPage!!) } } catch (e: IOException) { - binding.channelRefresh.isRefreshing = false + _binding?.channelRefresh?.isRefreshing = false Log.e(TAG(), "IOException, you might not have internet connection") return@repeatOnLifecycle } catch (e: HttpException) { - binding.channelRefresh.isRefreshing = false + _binding?.channelRefresh?.isRefreshing = false Log.e(TAG(), "HttpException, unexpected response," + e.response()) return@repeatOnLifecycle } + val binding = _binding ?: return@repeatOnLifecycle + nextPage = response.nextpage channelAdapter?.insertItems(response.relatedStreams) isLoading = false diff --git a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt index 33a31c617..e486a324e 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt @@ -125,7 +125,7 @@ class LibraryFragment : Fragment() { } private fun fetchPlaylists() { - binding.playlistRefresh.isRefreshing = true + _binding?.playlistRefresh?.isRefreshing = true lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.CREATED) { var playlists = try { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index fc3239dcc..293726a98 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -110,6 +110,10 @@ import com.google.android.exoplayer2.trackselection.DefaultTrackSelector import com.google.android.exoplayer2.upstream.DefaultDataSource import com.google.android.exoplayer2.util.MimeTypes import com.google.android.material.dialog.MaterialAlertDialogBuilder +import java.io.IOException +import java.util.* +import java.util.concurrent.Executors +import kotlin.math.abs import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -118,10 +122,6 @@ import kotlinx.coroutines.withContext import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import retrofit2.HttpException -import java.io.IOException -import java.util.* -import java.util.concurrent.Executors -import kotlin.math.abs class PlayerFragment : Fragment(), OnlinePlayerOptions { private var _binding: FragmentPlayerBinding? = null diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt index 2f1cbd312..dee1de33a 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlaylistFragment.kt @@ -121,6 +121,8 @@ class PlaylistFragment : Fragment() { Log.e(TAG(), e.toString()) return@repeatOnLifecycle } + val binding = _binding ?: return@repeatOnLifecycle + playlistFeed = response.relatedStreams.toMutableList() binding.playlistScrollview.visibility = View.VISIBLE nextPage = response.nextpage diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt index 0cf3b077b..57a76a3b2 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt @@ -104,6 +104,8 @@ class SearchResultFragment : Fragment() { Log.e(TAG(), "HttpException, unexpected response") return@repeatOnLifecycle } + + val binding = _binding ?: return@repeatOnLifecycle searchAdapter = SearchAdapter() binding.searchRecycler.adapter = searchAdapter searchAdapter.submitList(response.items)