refactor: remove unnecessary repeatOnLifecycle calls

This commit is contained in:
Bnyro 2023-11-07 17:16:15 +01:00
parent d791e12b6f
commit 992ba1f78d
6 changed files with 352 additions and 382 deletions

View File

@ -10,9 +10,7 @@ import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.core.view.isGone
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.PipedStream
@ -27,11 +25,11 @@ import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.parcelable.DownloadData
import com.github.libretube.util.TextUtils
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.io.IOException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.HttpException
import java.io.IOException
class DownloadDialog : DialogFragment() {
private lateinit var videoId: String
@ -80,7 +78,6 @@ class DownloadDialog : DialogFragment() {
private fun fetchAvailableSources(binding: DialogDownloadBinding) {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try {
withContext(Dispatchers.IO) {
RetrofitInstance.api.getStreams(videoId)
@ -89,16 +86,15 @@ class DownloadDialog : DialogFragment() {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@repeatOnLifecycle
return@launch
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@repeatOnLifecycle
return@launch
}
initDownloadOptions(binding, response)
}
}
}
private fun initDownloadOptions(binding: DialogDownloadBinding, streams: Streams) {
binding.fileName.setText(streams.title)

View File

@ -11,9 +11,7 @@ import androidx.core.view.children
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
@ -34,11 +32,11 @@ import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.ui.extensions.setupSubscriptionButton
import com.github.libretube.util.deArrow
import java.io.IOException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.HttpException
import java.io.IOException
class ChannelFragment : Fragment() {
private var _binding: FragmentChannelBinding? = null
@ -113,7 +111,6 @@ class ChannelFragment : Fragment() {
private fun fetchChannel() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try {
withContext(Dispatchers.IO) {
if (channelId != null) {
@ -127,13 +124,13 @@ class ChannelFragment : Fragment() {
} catch (e: IOException) {
_binding?.channelRefresh?.isRefreshing = false
Log.e(TAG(), "IOException, you might not have internet connection")
return@repeatOnLifecycle
return@launch
} catch (e: HttpException) {
_binding?.channelRefresh?.isRefreshing = false
Log.e(TAG(), "HttpException, unexpected response")
return@repeatOnLifecycle
return@launch
}
val binding = _binding ?: return@repeatOnLifecycle
val binding = _binding ?: return@launch
// needed if the channel gets loaded by the ID
channelId = response.id
@ -144,10 +141,10 @@ class ChannelFragment : Fragment() {
fetchChannelNextPage()
}
val channelId = channelId ?: return@repeatOnLifecycle
val channelId = channelId ?: return@launch
// fetch and update the subscription status
isSubscribed = SubscriptionHelper.isSubscribed(channelId)
if (isSubscribed == null) return@repeatOnLifecycle
if (isSubscribed == null) return@launch
binding.channelSubscribe.setupSubscriptionButton(
channelId,
@ -223,7 +220,6 @@ class ChannelFragment : Fragment() {
setupTabs(response.tabs)
}
}
}
private fun setupTabs(tabs: List<ChannelTab>) {
binding.tabChips.children.forEach { chip ->
@ -292,7 +288,6 @@ class ChannelFragment : Fragment() {
binding.channelRefresh.isRefreshing = true
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try {
withContext(Dispatchers.IO) {
RetrofitInstance.api.getChannelNextPage(channelId!!, nextPage!!).apply {
@ -302,13 +297,13 @@ class ChannelFragment : Fragment() {
} catch (e: IOException) {
_binding?.channelRefresh?.isRefreshing = false
Log.e(TAG(), "IOException, you might not have internet connection")
return@repeatOnLifecycle
return@launch
} catch (e: HttpException) {
_binding?.channelRefresh?.isRefreshing = false
Log.e(TAG(), "HttpException, unexpected response," + e.response())
return@repeatOnLifecycle
return@launch
}
val binding = _binding ?: return@repeatOnLifecycle
val binding = _binding ?: return@launch
nextPage = response.nextpage
channelAdapter?.insertItems(response.relatedStreams)
@ -316,7 +311,6 @@ class ChannelFragment : Fragment() {
binding.channelRefresh.isRefreshing = false
}
}
}
private fun fetchTabNextPage(
nextPage: String,

View File

@ -12,9 +12,7 @@ import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -122,16 +120,15 @@ class PlaylistFragment : Fragment() {
private fun fetchPlaylist() {
binding.playlistScrollview.isGone = true
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try {
withContext(Dispatchers.IO) {
PlaylistsHelper.getPlaylist(playlistId!!)
}
} catch (e: Exception) {
Log.e(TAG(), e.toString())
return@repeatOnLifecycle
return@launch
}
val binding = _binding ?: return@repeatOnLifecycle
val binding = _binding ?: return@launch
playlistFeed = response.relatedStreams.toMutableList()
binding.playlistScrollview.isVisible = true
@ -263,7 +260,6 @@ class PlaylistFragment : Fragment() {
updatePlaylistBookmark(response)
}
}
}
/**
* If the playlist is bookmarked, update its content if modified by the uploader
@ -380,7 +376,6 @@ class PlaylistFragment : Fragment() {
isLoading = true
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try {
withContext(Dispatchers.IO) {
// load locally stored playlists with the auth api
@ -393,7 +388,7 @@ class PlaylistFragment : Fragment() {
} catch (e: Exception) {
context?.toastFromMainDispatcher(e.localizedMessage.orEmpty())
Log.e(TAG(), e.toString())
return@repeatOnLifecycle
return@launch
}
nextPage = response.nextpage
@ -402,4 +397,3 @@ class PlaylistFragment : Fragment() {
}
}
}
}

View File

@ -9,9 +9,7 @@ import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.IntentData
@ -74,14 +72,13 @@ class SearchFragment : Fragment() {
private fun fetchSuggestions(query: String) {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try {
withContext(Dispatchers.IO) {
RetrofitInstance.api.getSuggestions(query)
}
} catch (e: Exception) {
Log.e(TAG(), e.toString())
return@repeatOnLifecycle
return@launch
}
// only load the suggestions if the input field didn't get cleared yet
val suggestionsAdapter = SearchSuggestionsAdapter(
@ -93,7 +90,6 @@ class SearchFragment : Fragment() {
}
}
}
}
private fun showHistory() {
lifecycleScope.launch {

View File

@ -8,9 +8,7 @@ import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
@ -114,7 +112,6 @@ class SearchResultFragment : Fragment() {
"${ShareDialog.YOUTUBE_FRONTEND_URL}/watch?v=$videoId"
} ?: query
repeatOnLifecycle(Lifecycle.State.CREATED) {
view?.let { context?.hideKeyboard(it) }
val response = try {
withContext(Dispatchers.IO) {
@ -125,10 +122,10 @@ class SearchResultFragment : Fragment() {
} catch (e: Exception) {
Log.e(TAG(), e.toString())
context?.toastFromMainDispatcher(R.string.unknown_error)
return@repeatOnLifecycle
return@launch
}
val binding = _binding ?: return@repeatOnLifecycle
val binding = _binding ?: return@launch
searchAdapter = SearchAdapter(timeStamp = timeStamp ?: 0)
binding.searchRecycler.adapter = searchAdapter
searchAdapter.submitList(response.items)
@ -140,11 +137,9 @@ class SearchResultFragment : Fragment() {
nextPage = response.nextpage
}
}
}
private fun fetchNextSearchItems() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try {
withContext(Dispatchers.IO) {
RetrofitInstance.api.getSearchResultsNextPage(
@ -157,7 +152,7 @@ class SearchResultFragment : Fragment() {
}
} catch (e: Exception) {
Log.e(TAG(), e.toString())
return@repeatOnLifecycle
return@launch
}
nextPage = response.nextpage
if (response.items.isNotEmpty()) {
@ -165,7 +160,6 @@ class SearchResultFragment : Fragment() {
}
}
}
}
private fun addToHistory(query: String) {
val searchHistoryEnabled =

View File

@ -9,9 +9,7 @@ import android.view.ViewGroup
import android.widget.Toast
import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.databinding.FragmentTrendsBinding
@ -21,11 +19,11 @@ import com.github.libretube.ui.activities.SettingsActivity
import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.util.deArrow
import com.google.android.material.snackbar.Snackbar
import java.io.IOException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.HttpException
import java.io.IOException
class TrendsFragment : Fragment() {
private var _binding: FragmentTrendsBinding? = null
@ -57,7 +55,6 @@ class TrendsFragment : Fragment() {
private fun fetchTrending() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try {
withContext(Dispatchers.IO) {
val region = LocaleHelper.getTrendingRegion(requireContext())
@ -67,14 +64,14 @@ class TrendsFragment : Fragment() {
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@repeatOnLifecycle
return@launch
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@repeatOnLifecycle
return@launch
}
val binding = _binding ?: return@repeatOnLifecycle
val binding = _binding ?: return@launch
binding.homeRefresh.isRefreshing = false
binding.progressBar.isGone = true
@ -86,7 +83,7 @@ class TrendsFragment : Fragment() {
startActivity(settingsIntent)
}
.show()
return@repeatOnLifecycle
return@launch
}
binding.recview.adapter = VideosAdapter(response.toMutableList())
@ -94,4 +91,3 @@ class TrendsFragment : Fragment() {
}
}
}
}