Skip to content

Commit

Permalink
Merge pull request #6611 from Bnyro/master
Browse files Browse the repository at this point in the history
fix: hide go to video action when in no internet mode
  • Loading branch information
Bnyro authored Oct 11, 2024
2 parents 69185bb + 4109048 commit a0970ff
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) }
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/github/libretube/helpers/ContextHelper.kt
Original file line number Diff line number Diff line change
@@ -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)!!
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
)
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit a0970ff

Please sign in to comment.