LibreTube/app/src/main/java/com/github/libretube/util/NavigationHelper.kt

146 lines
4.8 KiB
Kotlin
Raw Normal View History

2022-07-20 01:01:56 +05:30
package com.github.libretube.util
2022-09-26 22:11:41 +05:30
import android.app.NotificationManager
2022-07-20 01:01:56 +05:30
import android.content.Context
2022-11-26 22:55:07 +05:30
import android.content.ContextWrapper
2022-09-26 22:11:41 +05:30
import android.content.Intent
import android.content.pm.PackageManager
2023-01-14 21:29:21 +05:30
import android.os.Handler
import android.os.Looper
2022-07-20 01:01:56 +05:30
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.core.os.postDelayed
2022-07-20 01:01:56 +05:30
import com.github.libretube.R
2022-09-08 23:49:44 +05:30
import com.github.libretube.constants.IntentData
2023-01-14 21:29:21 +05:30
import com.github.libretube.constants.PreferenceKeys
2022-11-20 20:24:55 +05:30
import com.github.libretube.enums.PlaylistType
2022-08-27 18:43:24 +05:30
import com.github.libretube.extensions.toID
2022-09-20 23:30:51 +05:30
import com.github.libretube.ui.activities.MainActivity
2022-09-20 23:23:34 +05:30
import com.github.libretube.ui.fragments.PlayerFragment
2022-10-06 01:17:09 +05:30
import com.github.libretube.ui.views.SingleViewTouchableMotionLayout
2022-07-20 01:01:56 +05:30
object NavigationHelper {
2023-01-14 21:29:21 +05:30
private val handler = Handler(Looper.getMainLooper())
2022-08-08 18:52:08 +05:30
fun navigateChannel(
context: Context,
channelId: String?
) {
2022-10-06 01:17:09 +05:30
if (channelId == null) return
2022-11-26 22:55:07 +05:30
val activity = unwrap(context)
2022-10-06 01:17:09 +05:30
val bundle = bundleOf(IntentData.channelId to channelId)
activity.navController.navigate(R.id.channelFragment, bundle)
try {
if (activity.binding.mainMotionLayout.progress == 0.toFloat()) {
activity.binding.mainMotionLayout.transitionToEnd()
activity.findViewById<SingleViewTouchableMotionLayout>(R.id.playerMotionLayout)
.transitionToEnd()
2022-07-20 01:01:56 +05:30
}
2022-10-06 01:17:09 +05:30
} catch (e: Exception) {
e.printStackTrace()
2022-07-20 01:01:56 +05:30
}
}
2022-11-26 22:55:07 +05:30
private fun unwrap(context: Context): MainActivity {
var correctContext: Context? = context
while (correctContext !is MainActivity && correctContext is ContextWrapper) {
correctContext = correctContext.baseContext
}
return correctContext as MainActivity
}
2023-01-14 21:29:21 +05:30
/**
* Navigate to the given video using the other provided parameters as well
* If the audio only mode is enabled, play it in the background, else as a normal video
*/
2022-08-08 18:52:08 +05:30
fun navigateVideo(
context: Context,
videoId: String?,
playlistId: String? = null,
channelId: String? = null,
2023-01-14 21:29:21 +05:30
keepQueue: Boolean = false,
timeStamp: Long? = null,
forceVideo: Boolean = false
2022-08-08 18:52:08 +05:30
) {
2022-10-06 01:17:09 +05:30
if (videoId == null) return
if (PreferenceHelper.getBoolean(PreferenceKeys.AUDIO_ONLY_MODE, false) && !forceVideo) {
2023-01-14 21:29:21 +05:30
BackgroundHelper.stopBackgroundPlay(context)
2023-01-14 21:31:44 +05:30
BackgroundHelper.playOnBackground(
context,
videoId.toID(),
timeStamp,
playlistId,
channelId,
keepQueue
)
handler.postDelayed(500) {
2023-01-14 21:29:21 +05:30
startAudioPlayer(context)
}
2023-01-14 21:29:21 +05:30
return
}
2023-01-20 09:53:12 +05:30
val bundle = bundleOf(
IntentData.videoId to videoId.toID(),
IntentData.playlistId to playlistId,
IntentData.channelId to channelId,
IntentData.keepQueue to keepQueue,
IntentData.timeStamp to timeStamp
)
2022-10-06 01:17:09 +05:30
val activity = context as AppCompatActivity
activity.supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
activity.supportFragmentManager.beginTransaction()
.replace(
R.id.container,
PlayerFragment().apply {
arguments = bundle
}
)
2022-10-06 01:17:09 +05:30
.commitNow()
2022-07-20 01:01:56 +05:30
}
2022-08-08 18:52:08 +05:30
fun navigatePlaylist(
context: Context,
playlistId: String?,
2022-11-20 20:24:55 +05:30
playlistType: PlaylistType
2022-08-08 18:52:08 +05:30
) {
2022-10-06 01:17:09 +05:30
if (playlistId == null) return
2022-11-26 22:55:07 +05:30
val activity = unwrap(context)
2023-01-20 09:53:12 +05:30
val bundle = bundleOf(
IntentData.playlistId to playlistId,
IntentData.playlistType to playlistType
)
2022-10-06 01:17:09 +05:30
activity.navController.navigate(R.id.playlistFragment, bundle)
2022-07-20 01:01:56 +05:30
}
2022-09-26 22:11:41 +05:30
2023-01-14 21:29:21 +05:30
/**
* Start the audio player fragment
*/
fun startAudioPlayer(context: Context) {
val activity = unwrap(context)
activity.navController.navigate(R.id.audioPlayerFragment)
}
2022-09-26 22:11:41 +05:30
/**
* Needed due to different MainActivity Aliases because of the app icons
*/
fun restartMainActivity(context: Context) {
// kill player notification
val nManager = context
.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
nManager.cancelAll()
// start a new Intent of the app
val pm: PackageManager = context.packageManager
val intent = pm.getLaunchIntentForPackage(context.packageName)
intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(intent)
// kill the old application
android.os.Process.killProcess(android.os.Process.myPid())
}
2022-07-20 01:01:56 +05:30
}