Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reuse existing player when selecting other video while playing #6821

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.os.Process
import androidx.annotation.OptIn
import androidx.core.content.getSystemService
import androidx.core.os.bundleOf
import androidx.core.os.postDelayed
import androidx.fragment.app.commitNow
import androidx.fragment.app.replace
import androidx.media3.common.util.UnstableApi
import com.github.libretube.NavDirections
import com.github.libretube.R
import com.github.libretube.constants.IntentData
Expand Down Expand Up @@ -61,30 +63,56 @@ object NavigationHelper {
if (videoUrlOrId == null) return

if (PreferenceHelper.getBoolean(PreferenceKeys.AUDIO_ONLY_MODE, false) && !forceVideo) {
BackgroundHelper.playOnBackground(
context,
videoUrlOrId.toID(),
timestamp,
playlistId,
channelId,
keepQueue
)
handler.postDelayed(500) {
startAudioPlayer(context)
}
navigateAudio(context, videoUrlOrId.toID(), playlistId, channelId, keepQueue, timestamp)
return
}

val activity = ContextHelper.unwrapActivity<MainActivity>(context)
val attachedToRunningPlayer = activity.runOnPlayerFragment {
this.playNextVideo(videoUrlOrId.toID())
true
}
if (attachedToRunningPlayer) return

val playerData =
PlayerData(videoUrlOrId.toID(), playlistId, channelId, keepQueue, timestamp)
val bundle = bundleOf(IntentData.playerData to playerData)

val activity = ContextHelper.unwrapActivity<MainActivity>(context)
activity.supportFragmentManager.commitNow {
replace<PlayerFragment>(R.id.container, args = bundle)
}
}

@OptIn(UnstableApi::class)
fun navigateAudio(
context: Context,
videoId: String,
playlistId: String? = null,
channelId: String? = null,
keepQueue: Boolean = false,
timestamp: Long = 0,
minimizeByDefault: Boolean = false
) {
val activity = ContextHelper.unwrapActivity<MainActivity>(context)
val attachedToRunningPlayer = activity.runOnAudioPlayerFragment {
this.playNextVideo(videoId)
true
}
if (attachedToRunningPlayer) return

BackgroundHelper.playOnBackground(
context,
videoId,
timestamp,
playlistId,
channelId,
keepQueue
)

handler.postDelayed(500) {
openAudioPlayerFragment(context, minimizeByDefault = minimizeByDefault)
}
}

fun navigatePlaylist(context: Context, playlistUrlOrId: String?, playlistType: PlaylistType) {
if (playlistUrlOrId == null) return

Expand All @@ -97,7 +125,11 @@ object NavigationHelper {
/**
* Start the audio player fragment
*/
fun startAudioPlayer(context: Context, offlinePlayer: Boolean = false, minimizeByDefault: Boolean = false) {
fun openAudioPlayerFragment(
context: Context,
offlinePlayer: Boolean = false,
minimizeByDefault: Boolean = false
) {
val activity = ContextHelper.unwrapActivity<BaseActivity>(context)
activity.supportFragmentManager.commitNow {
val args = bundleOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ class MainActivity : BaseActivity() {

if (intent?.getBooleanExtra(IntentData.openAudioPlayer, false) == true) {
val offlinePlayer = intent!!.getBooleanExtra(IntentData.offlinePlayer, false)
NavigationHelper.startAudioPlayer(this, offlinePlayer = offlinePlayer)
NavigationHelper.openAudioPlayerFragment(this, offlinePlayer = offlinePlayer)
return
}

Expand Down Expand Up @@ -639,10 +639,17 @@ class MainActivity : BaseActivity() {
* Attempt to run code on the player fragment if running
* Returns true if a running player fragment was found and the action got consumed, else false
*/
private fun runOnPlayerFragment(action: PlayerFragment.() -> Boolean): Boolean {
fun runOnPlayerFragment(action: PlayerFragment.() -> Boolean): Boolean {
return supportFragmentManager.fragments.filterIsInstance<PlayerFragment>()
.firstOrNull()
?.let(action)
?: false
}

fun runOnAudioPlayerFragment(action: AudioPlayerFragment.() -> Boolean): Boolean {
return supportFragmentManager.fragments.filterIsInstance<AudioPlayerFragment>()
.firstOrNull()
?.let(action)
?: false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class NoInternetActivity : BaseActivity() {
super.onNewIntent(intent)

if (intent.getBooleanExtra(IntentData.openAudioPlayer, false)) {
NavigationHelper.startAudioPlayer(this, offlinePlayer = true)
NavigationHelper.openAudioPlayerFragment(this, offlinePlayer = true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class DownloadsAdapter(
root.context.startActivity(intent)
} else {
BackgroundHelper.playOnBackgroundOffline(root.context, download.videoId, downloadTab)
NavigationHelper.startAudioPlayer(root.context, offlinePlayer = true)
NavigationHelper.openAudioPlayerFragment(root.context, offlinePlayer = true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
}
}

fun playNextVideo(videoId: String) {
playerController?.navigateVideo(videoId)
}

@SuppressLint("ClickableViewAccessibility")
private fun initializeTransitionLayout() {
if (mainActivity == null) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment() {
shuffle = true
)

NavigationHelper.startAudioPlayer(requireContext(), offlinePlayer = true)
NavigationHelper.openAudioPlayerFragment(requireContext(), offlinePlayer = true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
keepVideoPlayerAlive = true
)
killPlayerFragment()
NavigationHelper.startAudioPlayer(requireContext())
NavigationHelper.openAudioPlayerFragment(requireContext())
}

private fun updateFullscreenOrientation() {
Expand Down Expand Up @@ -1020,7 +1020,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
/**
* Manually skip to another video.
*/
private fun playNextVideo(nextId: String) {
fun playNextVideo(nextId: String) {
playerController.sendCustomCommand(
AbstractPlayerService.runPlayerActionCommand,
bundleOf(PlayerCommand.PLAY_VIDEO_BY_ID.name to nextId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.parcelable
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.DownloadHelper
import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.helpers.PlayerHelper
Expand Down Expand Up @@ -58,8 +57,7 @@ class VideoOptionsBottomSheet : BaseBottomSheet() {
when (optionsList[which]) {
// Start the background mode
R.string.playOnBackground -> {
BackgroundHelper.playOnBackground(requireContext(), videoId)
NavigationHelper.startAudioPlayer(requireContext(), minimizeByDefault = true)
NavigationHelper.navigateAudio(requireContext(), videoId, minimizeByDefault = true)
}
// Add Video to Playlist Dialog
R.string.addToPlaylist -> {
Expand Down
Loading