extend fragment

This commit is contained in:
Bnyro 2022-08-08 11:49:19 +02:00
parent 5f498e2e39
commit 1cb0c8f7b5
14 changed files with 45 additions and 73 deletions

View File

@ -55,8 +55,9 @@ class PlaylistOptionsDialog(
// play the playlist in the background
context?.getString(R.string.playOnBackground) -> {
runBlocking {
val playlist = if (isOwner) RetrofitInstance.authApi.getPlaylist(playlistId)
else RetrofitInstance.api.getPlaylist(playlistId)
val playlist =
if (isOwner) RetrofitInstance.authApi.getPlaylist(playlistId)
else RetrofitInstance.api.getPlaylist(playlistId)
BackgroundHelper.playOnBackground(
context = requireContext(),
videoId = playlist.relatedStreams!![0].url.toID(),

View File

@ -55,7 +55,10 @@ class VideoOptionsDialog(
val bundle = Bundle()
bundle.putString("videoId", videoId)
newFragment.arguments = bundle
newFragment.show(parentFragmentManager, AddToPlaylistDialog::class.java.name)
newFragment.show(
parentFragmentManager,
AddToPlaylistDialog::class.java.name
)
} else {
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
}

View File

@ -0,0 +1,10 @@
package com.github.libretube.extensions
import androidx.fragment.app.Fragment
open class BaseFragment : Fragment() {
fun runOnUiThread(action: () -> Unit) {
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}

View File

@ -5,12 +5,12 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.adapters.ChannelAdapter
import com.github.libretube.databinding.FragmentChannelBinding
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.SubscriptionHelper
@ -19,7 +19,7 @@ import com.github.libretube.util.toID
import retrofit2.HttpException
import java.io.IOException
class ChannelFragment : Fragment() {
class ChannelFragment : BaseFragment() {
private val TAG = "ChannelFragment"
private lateinit var binding: FragmentChannelBinding
@ -184,10 +184,4 @@ class ChannelFragment : Fragment() {
}
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}

View File

@ -6,12 +6,12 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.github.libretube.R
import com.github.libretube.adapters.TrendingAdapter
import com.github.libretube.databinding.FragmentHomeBinding
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.LocaleHelper
@ -19,7 +19,7 @@ import com.github.libretube.util.RetrofitInstance
import retrofit2.HttpException
import java.io.IOException
class HomeFragment : Fragment() {
class HomeFragment : BaseFragment() {
private val TAG = "HomeFragment"
private lateinit var binding: FragmentHomeBinding
private lateinit var region: String
@ -88,10 +88,4 @@ class HomeFragment : Fragment() {
}
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}

View File

@ -6,7 +6,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
@ -15,13 +14,14 @@ import com.github.libretube.R
import com.github.libretube.adapters.PlaylistsAdapter
import com.github.libretube.databinding.FragmentLibraryBinding
import com.github.libretube.dialogs.CreatePlaylistDialog
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.RetrofitInstance
import retrofit2.HttpException
import java.io.IOException
class LibraryFragment : Fragment() {
class LibraryFragment : BaseFragment() {
private val TAG = "LibraryFragment"
lateinit var token: String
@ -129,10 +129,4 @@ class LibraryFragment : Fragment() {
}
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}

View File

@ -28,7 +28,6 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
@ -45,6 +44,7 @@ import com.github.libretube.databinding.FragmentPlayerBinding
import com.github.libretube.dialogs.AddToPlaylistDialog
import com.github.libretube.dialogs.DownloadDialog
import com.github.libretube.dialogs.ShareDialog
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.obj.ChapterSegment
import com.github.libretube.obj.Segment
import com.github.libretube.obj.Segments
@ -88,6 +88,7 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSource
import com.google.android.exoplayer2.util.RepeatModeUtil
import com.google.android.exoplayer2.video.VideoSize
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlin.math.abs
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -95,9 +96,8 @@ import org.chromium.net.CronetEngine
import retrofit2.HttpException
import java.io.IOException
import java.util.concurrent.Executors
import kotlin.math.abs
class PlayerFragment : Fragment() {
class PlayerFragment : BaseFragment() {
private val TAG = "PlayerFragment"
private lateinit var binding: FragmentPlayerBinding
@ -1509,12 +1509,6 @@ class PlayerFragment : Fragment() {
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
private fun fetchComments() {
lifecycleScope.launchWhenCreated {
val commentsResponse = try {

View File

@ -5,7 +5,6 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
@ -14,13 +13,14 @@ import com.github.libretube.R
import com.github.libretube.adapters.PlaylistAdapter
import com.github.libretube.databinding.FragmentPlaylistBinding
import com.github.libretube.dialogs.PlaylistOptionsDialog
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID
import retrofit2.HttpException
import java.io.IOException
class PlaylistFragment : Fragment() {
class PlaylistFragment : BaseFragment() {
private val TAG = "PlaylistFragment"
private lateinit var binding: FragmentPlaylistBinding
@ -87,7 +87,10 @@ class PlaylistFragment : Fragment() {
binding.optionsMenu.setOnClickListener {
val optionsDialog =
PlaylistOptionsDialog(playlistId!!, isOwner)
optionsDialog.show(childFragmentManager, PlaylistOptionsDialog::class.java.name)
optionsDialog.show(
childFragmentManager,
PlaylistOptionsDialog::class.java.name
)
}
playlistAdapter = PlaylistAdapter(
@ -171,10 +174,4 @@ class PlaylistFragment : Fragment() {
}
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}

View File

@ -5,7 +5,6 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
@ -13,13 +12,14 @@ import com.github.libretube.activities.MainActivity
import com.github.libretube.adapters.SearchHistoryAdapter
import com.github.libretube.adapters.SearchSuggestionsAdapter
import com.github.libretube.databinding.FragmentSearchBinding
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.models.SearchViewModel
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.RetrofitInstance
import retrofit2.HttpException
import java.io.IOException
class SearchFragment() : Fragment() {
class SearchFragment() : BaseFragment() {
private val TAG = "SearchFragment"
private lateinit var binding: FragmentSearchBinding
private val viewModel: SearchViewModel by activityViewModels()
@ -101,10 +101,4 @@ class SearchFragment() : Fragment() {
binding.historyEmpty.visibility = View.VISIBLE
}
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}

View File

@ -5,12 +5,12 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.adapters.SearchAdapter
import com.github.libretube.databinding.FragmentSearchResultBinding
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.RetrofitInstance
@ -18,7 +18,7 @@ import com.github.libretube.util.hideKeyboard
import retrofit2.HttpException
import java.io.IOException
class SearchResultFragment : Fragment() {
class SearchResultFragment : BaseFragment() {
private val TAG = "SearchResultFragment"
private lateinit var binding: FragmentSearchResultBinding
@ -135,10 +135,4 @@ class SearchResultFragment : Fragment() {
PreferenceHelper.saveToSearchHistory(query)
}
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}

View File

@ -7,7 +7,6 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
@ -15,6 +14,7 @@ import com.github.libretube.R
import com.github.libretube.adapters.SubscriptionChannelAdapter
import com.github.libretube.adapters.TrendingAdapter
import com.github.libretube.databinding.FragmentSubscriptionsBinding
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.obj.StreamItem
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
@ -25,7 +25,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import retrofit2.HttpException
import java.io.IOException
class SubscriptionsFragment : Fragment() {
class SubscriptionsFragment : BaseFragment() {
val TAG = "SubFragment"
private lateinit var binding: FragmentSubscriptionsBinding
@ -199,10 +199,4 @@ class SubscriptionsFragment : Fragment() {
}
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}

View File

@ -4,13 +4,13 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.adapters.WatchHistoryAdapter
import com.github.libretube.databinding.FragmentWatchHistoryBinding
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.preferences.PreferenceHelper
class WatchHistoryFragment : Fragment() {
class WatchHistoryFragment : BaseFragment() {
private val TAG = "WatchHistoryFragment"
private lateinit var binding: FragmentWatchHistoryBinding

View File

@ -25,7 +25,10 @@ class AutoPlayHelper(
// fetch the playlists or its nextPage's videos
val playlist =
if (playlistNextPage == null) RetrofitInstance.authApi.getPlaylist(playlistId)
else RetrofitInstance.authApi.getPlaylistNextPage(playlistId, playlistNextPage!!)
else RetrofitInstance.authApi.getPlaylistNextPage(
playlistId,
playlistNextPage!!
)
// save the playlist urls to the list
playlistStreamIds += playlist.relatedStreams!!.map { it.url.toID() }
// save playlistNextPage for usage if video is not contained

View File

@ -1,7 +1,7 @@
package com.github.libretube.util
import java.util.*
import kotlin.reflect.KProperty
import java.util.*
class ResettableLazyManager {
// we synchronize to make sure the timing of a reset() call and new inits do not collide