fix: hide go to video action when in no internet mode

This commit is contained in:
Bnyro 2024-10-11 12:36:20 +02:00
parent 7e12df799b
commit 410904848c
6 changed files with 25 additions and 12 deletions

View File

@ -10,6 +10,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.parcelable.PlayerData
import com.github.libretube.services.OfflinePlayerService
import com.github.libretube.services.OnlinePlayerService
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.fragments.DownloadTab
import com.github.libretube.ui.fragments.PlayerFragment
@ -33,7 +34,7 @@ object BackgroundHelper {
) {
// close the previous video player if open
if (!keepVideoPlayerAlive) {
val fragmentManager = ContextHelper.unwrapActivity(context).supportFragmentManager
val fragmentManager = ContextHelper.unwrapActivity<MainActivity>(context).supportFragmentManager
fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let {
fragmentManager.commit { remove(it) }
}

View File

@ -1,15 +1,17 @@
package com.github.libretube.helpers
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import com.github.libretube.ui.activities.MainActivity
object ContextHelper {
fun unwrapActivity(context: Context): MainActivity {
inline fun <reified T : Activity> tryUnwrapActivity(context: Context): T? {
var correctContext: Context? = context
while (correctContext !is MainActivity && correctContext is ContextWrapper) {
while (correctContext !is T && correctContext is ContextWrapper) {
correctContext = correctContext.baseContext
}
return correctContext as MainActivity
return correctContext as? T
}
inline fun <reified T : Activity> unwrapActivity(context: Context): T = tryUnwrapActivity(context)!!
}

View File

@ -18,6 +18,7 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.toID
import com.github.libretube.parcelable.PlayerData
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.activities.ZoomableImageActivity
import com.github.libretube.ui.fragments.AudioPlayerFragment
import com.github.libretube.ui.fragments.PlayerFragment
@ -29,7 +30,7 @@ object NavigationHelper {
fun navigateChannel(context: Context, channelUrlOrId: String?) {
if (channelUrlOrId == null) return
val activity = ContextHelper.unwrapActivity(context)
val activity = ContextHelper.unwrapActivity<MainActivity>(context)
activity.navController.navigate(NavDirections.openChannel(channelUrlOrId.toID()))
try {
// minimize player if currently expanded
@ -78,7 +79,7 @@ object NavigationHelper {
PlayerData(videoUrlOrId.toID(), playlistId, channelId, keepQueue, timestamp)
val bundle = bundleOf(IntentData.playerData to playerData)
val activity = ContextHelper.unwrapActivity(context)
val activity = ContextHelper.unwrapActivity<MainActivity>(context)
activity.supportFragmentManager.commitNow {
replace<PlayerFragment>(R.id.container, args = bundle)
}
@ -87,7 +88,7 @@ object NavigationHelper {
fun navigatePlaylist(context: Context, playlistUrlOrId: String?, playlistType: PlaylistType) {
if (playlistUrlOrId == null) return
val activity = ContextHelper.unwrapActivity(context)
val activity = ContextHelper.unwrapActivity<MainActivity>(context)
activity.navController.navigate(
NavDirections.openPlaylist(playlistUrlOrId.toID(), playlistType)
)
@ -97,7 +98,7 @@ object NavigationHelper {
* Start the audio player fragment
*/
fun startAudioPlayer(context: Context, offlinePlayer: Boolean = false, minimizeByDefault: Boolean = false) {
val activity = ContextHelper.unwrapActivity(context)
val activity = ContextHelper.unwrapActivity<MainActivity>(context)
activity.supportFragmentManager.commitNow {
val args = bundleOf(
IntentData.minimizeByDefault to minimizeByDefault,

View File

@ -8,8 +8,11 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.serializable
import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.ContextHelper
import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.obj.ShareData
import com.github.libretube.ui.activities.DownloadActivity
import com.github.libretube.ui.activities.NoInternetActivity
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.ui.fragments.DownloadTab
@ -18,13 +21,18 @@ class DownloadOptionsBottomSheet : BaseBottomSheet() {
val videoId = arguments?.getString(IntentData.videoId)!!
val downloadTab = arguments?.serializable<DownloadTab>(IntentData.downloadTab)!!
val options = listOf(
val options = mutableListOf(
R.string.playOnBackground,
R.string.go_to_video,
R.string.share,
R.string.delete
)
// can't navigate to video while in offline activity
if (ContextHelper.tryUnwrapActivity<NoInternetActivity>(requireContext()) != null) {
options.remove(R.string.go_to_video)
}
setSimpleItems(options.map { getString(it) }) { which ->
when (options[which]) {
R.string.playOnBackground -> {

View File

@ -63,7 +63,7 @@ class AutoplayCountdownView(
}
// don't show cancel and play next buttons in PiP mode
val context = ContextHelper.unwrapActivity(context)
val context = ContextHelper.unwrapActivity<MainActivity>(context)
val isInPipMode = PictureInPictureCompat.isInPictureInPictureMode(context)
binding.cancel.isVisible = !isInPipMode
binding.playNext.isVisible = !isInPipMode

View File

@ -56,6 +56,7 @@ import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.WindowHelper
import com.github.libretube.obj.BottomSheetItem
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.extensions.toggleSystemBars
import com.github.libretube.ui.fragments.PlayerFragment
@ -848,7 +849,7 @@ abstract class CustomExoPlayerView(
}
KeyEvent.KEYCODE_F -> {
val fragmentManager = ContextHelper.unwrapActivity(context).supportFragmentManager
val fragmentManager = ContextHelper.unwrapActivity<MainActivity>(context).supportFragmentManager
fragmentManager.fragments.filterIsInstance<PlayerFragment>().firstOrNull()
?.toggleFullscreen()
}