Merge pull request #5127 from Bnyro/master

refactor: remove unnecessary repeatOnLifecycle calls
This commit is contained in:
Bnyro 2023-11-07 17:16:33 +01:00 committed by GitHub
commit 2afdb1f359
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 352 additions and 382 deletions

View File

@ -10,9 +10,7 @@ import android.widget.ArrayAdapter
import android.widget.Toast import android.widget.Toast
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.PipedStream 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.parcelable.DownloadData
import com.github.libretube.util.TextUtils import com.github.libretube.util.TextUtils
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.io.IOException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException
class DownloadDialog : DialogFragment() { class DownloadDialog : DialogFragment() {
private lateinit var videoId: String private lateinit var videoId: String
@ -80,7 +78,6 @@ class DownloadDialog : DialogFragment() {
private fun fetchAvailableSources(binding: DialogDownloadBinding) { private fun fetchAvailableSources(binding: DialogDownloadBinding) {
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try { val response = try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
RetrofitInstance.api.getStreams(videoId) RetrofitInstance.api.getStreams(videoId)
@ -89,16 +86,15 @@ class DownloadDialog : DialogFragment() {
println(e) println(e)
Log.e(TAG(), "IOException, you might not have internet connection") Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@repeatOnLifecycle return@launch
} catch (e: HttpException) { } catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response") Log.e(TAG(), "HttpException, unexpected response")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@repeatOnLifecycle return@launch
} }
initDownloadOptions(binding, response) initDownloadOptions(binding, response)
} }
} }
}
private fun initDownloadOptions(binding: DialogDownloadBinding, streams: Streams) { private fun initDownloadOptions(binding: DialogDownloadBinding, streams: Streams) {
binding.fileName.setText(streams.title) 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.isGone
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance 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.dialogs.ShareDialog
import com.github.libretube.ui.extensions.setupSubscriptionButton import com.github.libretube.ui.extensions.setupSubscriptionButton
import com.github.libretube.util.deArrow import com.github.libretube.util.deArrow
import java.io.IOException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException
class ChannelFragment : Fragment() { class ChannelFragment : Fragment() {
private var _binding: FragmentChannelBinding? = null private var _binding: FragmentChannelBinding? = null
@ -113,7 +111,6 @@ class ChannelFragment : Fragment() {
private fun fetchChannel() { private fun fetchChannel() {
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try { val response = try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
if (channelId != null) { if (channelId != null) {
@ -127,13 +124,13 @@ class ChannelFragment : Fragment() {
} catch (e: IOException) { } catch (e: IOException) {
_binding?.channelRefresh?.isRefreshing = false _binding?.channelRefresh?.isRefreshing = false
Log.e(TAG(), "IOException, you might not have internet connection") Log.e(TAG(), "IOException, you might not have internet connection")
return@repeatOnLifecycle return@launch
} catch (e: HttpException) { } catch (e: HttpException) {
_binding?.channelRefresh?.isRefreshing = false _binding?.channelRefresh?.isRefreshing = false
Log.e(TAG(), "HttpException, unexpected response") 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 // needed if the channel gets loaded by the ID
channelId = response.id channelId = response.id
@ -144,10 +141,10 @@ class ChannelFragment : Fragment() {
fetchChannelNextPage() fetchChannelNextPage()
} }
val channelId = channelId ?: return@repeatOnLifecycle val channelId = channelId ?: return@launch
// fetch and update the subscription status // fetch and update the subscription status
isSubscribed = SubscriptionHelper.isSubscribed(channelId) isSubscribed = SubscriptionHelper.isSubscribed(channelId)
if (isSubscribed == null) return@repeatOnLifecycle if (isSubscribed == null) return@launch
binding.channelSubscribe.setupSubscriptionButton( binding.channelSubscribe.setupSubscriptionButton(
channelId, channelId,
@ -223,7 +220,6 @@ class ChannelFragment : Fragment() {
setupTabs(response.tabs) setupTabs(response.tabs)
} }
} }
}
private fun setupTabs(tabs: List<ChannelTab>) { private fun setupTabs(tabs: List<ChannelTab>) {
binding.tabChips.children.forEach { chip -> binding.tabChips.children.forEach { chip ->
@ -292,7 +288,6 @@ class ChannelFragment : Fragment() {
binding.channelRefresh.isRefreshing = true binding.channelRefresh.isRefreshing = true
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try { val response = try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
RetrofitInstance.api.getChannelNextPage(channelId!!, nextPage!!).apply { RetrofitInstance.api.getChannelNextPage(channelId!!, nextPage!!).apply {
@ -302,13 +297,13 @@ class ChannelFragment : Fragment() {
} catch (e: IOException) { } catch (e: IOException) {
_binding?.channelRefresh?.isRefreshing = false _binding?.channelRefresh?.isRefreshing = false
Log.e(TAG(), "IOException, you might not have internet connection") Log.e(TAG(), "IOException, you might not have internet connection")
return@repeatOnLifecycle return@launch
} catch (e: HttpException) { } catch (e: HttpException) {
_binding?.channelRefresh?.isRefreshing = false _binding?.channelRefresh?.isRefreshing = false
Log.e(TAG(), "HttpException, unexpected response," + e.response()) 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 nextPage = response.nextpage
channelAdapter?.insertItems(response.relatedStreams) channelAdapter?.insertItems(response.relatedStreams)
@ -316,7 +311,6 @@ class ChannelFragment : Fragment() {
binding.channelRefresh.isRefreshing = false binding.channelRefresh.isRefreshing = false
} }
} }
}
private fun fetchTabNextPage( private fun fetchTabNextPage(
nextPage: String, nextPage: String,

View File

@ -12,9 +12,7 @@ import androidx.core.view.isVisible
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -122,16 +120,15 @@ class PlaylistFragment : Fragment() {
private fun fetchPlaylist() { private fun fetchPlaylist() {
binding.playlistScrollview.isGone = true binding.playlistScrollview.isGone = true
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try { val response = try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
PlaylistsHelper.getPlaylist(playlistId!!) PlaylistsHelper.getPlaylist(playlistId!!)
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
return@repeatOnLifecycle return@launch
} }
val binding = _binding ?: return@repeatOnLifecycle val binding = _binding ?: return@launch
playlistFeed = response.relatedStreams.toMutableList() playlistFeed = response.relatedStreams.toMutableList()
binding.playlistScrollview.isVisible = true binding.playlistScrollview.isVisible = true
@ -263,7 +260,6 @@ class PlaylistFragment : Fragment() {
updatePlaylistBookmark(response) updatePlaylistBookmark(response)
} }
} }
}
/** /**
* If the playlist is bookmarked, update its content if modified by the uploader * If the playlist is bookmarked, update its content if modified by the uploader
@ -380,7 +376,6 @@ class PlaylistFragment : Fragment() {
isLoading = true isLoading = true
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try { val response = try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
// load locally stored playlists with the auth api // load locally stored playlists with the auth api
@ -393,7 +388,7 @@ class PlaylistFragment : Fragment() {
} catch (e: Exception) { } catch (e: Exception) {
context?.toastFromMainDispatcher(e.localizedMessage.orEmpty()) context?.toastFromMainDispatcher(e.localizedMessage.orEmpty())
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
return@repeatOnLifecycle return@launch
} }
nextPage = response.nextpage 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.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
@ -74,14 +72,13 @@ class SearchFragment : Fragment() {
private fun fetchSuggestions(query: String) { private fun fetchSuggestions(query: String) {
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val response = try { val response = try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
RetrofitInstance.api.getSuggestions(query) RetrofitInstance.api.getSuggestions(query)
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
return@repeatOnLifecycle return@launch
} }
// only load the suggestions if the input field didn't get cleared yet // only load the suggestions if the input field didn't get cleared yet
val suggestionsAdapter = SearchSuggestionsAdapter( val suggestionsAdapter = SearchSuggestionsAdapter(
@ -93,7 +90,6 @@ class SearchFragment : Fragment() {
} }
} }
} }
}
private fun showHistory() { private fun showHistory() {
lifecycleScope.launch { lifecycleScope.launch {

View File

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

View File

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