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-09-26 22:11:41 +05:30
|
|
|
import android.content.Intent
|
|
|
|
import android.content.pm.PackageManager
|
2022-07-20 01:01:56 +05:30
|
|
|
import android.os.Bundle
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import androidx.core.os.bundleOf
|
|
|
|
import com.github.libretube.R
|
2022-09-08 23:49:44 +05:30
|
|
|
import com.github.libretube.constants.IntentData
|
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 {
|
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
|
|
|
|
|
|
|
|
val activity = context as MainActivity
|
|
|
|
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-08-08 18:52:08 +05:30
|
|
|
fun navigateVideo(
|
|
|
|
context: Context,
|
|
|
|
videoId: String?,
|
|
|
|
playlistId: String? = null
|
|
|
|
) {
|
2022-10-06 01:17:09 +05:30
|
|
|
if (videoId == null) return
|
|
|
|
|
|
|
|
val bundle = Bundle()
|
|
|
|
bundle.putString(IntentData.videoId, videoId.toID())
|
|
|
|
if (playlistId != null) bundle.putString(IntentData.playlistId, playlistId)
|
|
|
|
val frag = PlayerFragment()
|
|
|
|
frag.arguments = bundle
|
|
|
|
val activity = context as AppCompatActivity
|
|
|
|
activity.supportFragmentManager.beginTransaction()
|
|
|
|
.remove(PlayerFragment())
|
|
|
|
.commit()
|
|
|
|
activity.supportFragmentManager.beginTransaction()
|
|
|
|
.replace(R.id.container, frag)
|
|
|
|
.commitNow()
|
2022-07-20 01:01:56 +05:30
|
|
|
}
|
|
|
|
|
2022-08-08 18:52:08 +05:30
|
|
|
fun navigatePlaylist(
|
|
|
|
context: Context,
|
|
|
|
playlistId: String?,
|
|
|
|
isOwner: Boolean
|
|
|
|
) {
|
2022-10-06 01:17:09 +05:30
|
|
|
if (playlistId == null) return
|
|
|
|
|
|
|
|
val activity = context as MainActivity
|
|
|
|
val bundle = Bundle()
|
|
|
|
bundle.putString(IntentData.playlistId, playlistId)
|
|
|
|
bundle.putBoolean("isOwner", isOwner)
|
|
|
|
activity.navController.navigate(R.id.playlistFragment, bundle)
|
2022-07-20 01:01:56 +05:30
|
|
|
}
|
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
|
|
|
}
|