LibreTube/app/src/main/java/com/github/libretube/MainActivity.kt

178 lines
7.2 KiB
Kotlin
Raw Normal View History

2022-02-01 21:22:06 +05:30
package com.github.libretube
2021-12-09 18:01:40 +05:30
2022-02-10 13:52:05 +05:30
import android.content.pm.ActivityInfo
2021-12-16 03:54:40 +05:30
import android.content.res.Configuration
import android.content.res.Resources
2022-02-10 13:45:09 +05:30
import android.os.Build
2021-12-09 18:31:47 +05:30
import androidx.appcompat.app.AppCompatActivity
2021-12-09 18:01:40 +05:30
import android.os.Bundle
2022-02-02 21:40:37 +05:30
import android.util.Log
2022-02-10 13:45:09 +05:30
import android.view.*
import android.widget.FrameLayout
2022-02-10 16:39:34 +05:30
import android.widget.LinearLayout
import androidx.appcompat.widget.Toolbar
2021-12-16 03:54:40 +05:30
import androidx.constraintlayout.motion.widget.MotionLayout
2022-02-10 16:39:34 +05:30
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.text.HtmlCompat
2022-02-10 13:45:09 +05:30
import androidx.core.view.ViewCompat
import androidx.core.view.ViewCompat.getWindowInsetsController
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.core.view.WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
import androidx.fragment.app.Fragment
2022-02-02 21:40:37 +05:30
import androidx.navigation.NavController
2021-12-28 01:37:07 +05:30
import androidx.navigation.Navigation
2021-12-09 18:31:47 +05:30
import com.google.android.material.bottomnavigation.BottomNavigationView
2021-12-09 18:25:32 +05:30
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
2021-12-28 01:37:07 +05:30
import androidx.navigation.ui.NavigationUI.onNavDestinationSelected
2021-12-09 18:25:32 +05:30
import androidx.navigation.ui.setupWithNavController
2022-02-08 19:57:13 +05:30
import androidx.preference.PreferenceManager
2021-12-14 21:45:53 +05:30
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.material.color.DynamicColors
2022-02-02 23:58:24 +05:30
import java.lang.Exception
2021-12-09 18:01:40 +05:30
class MainActivity : AppCompatActivity() {
2021-12-28 01:37:07 +05:30
lateinit var bottomNavigationView: BottomNavigationView
lateinit var toolbar: Toolbar
2022-02-02 21:40:37 +05:30
lateinit var navController : NavController
2021-12-09 18:01:40 +05:30
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2022-02-08 19:57:13 +05:30
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
RetrofitInstance.url=sharedPreferences.getString("instance", "https://pipedapi.kavin.rocks/")!!
DynamicColors.applyToActivitiesIfAvailable(application)
2021-12-09 18:01:40 +05:30
setContentView(R.layout.activity_main)
2022-02-10 13:52:05 +05:30
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
2022-02-02 21:40:37 +05:30
2022-02-08 20:47:49 +05:30
bottomNavigationView = findViewById(R.id.bottomNav)
2022-02-02 21:40:37 +05:30
navController = findNavController(R.id.fragment)
2021-12-09 18:25:32 +05:30
bottomNavigationView.setupWithNavController(navController)
2021-12-28 01:37:07 +05:30
2022-02-08 20:47:49 +05:30
bottomNavigationView.setOnItemSelectedListener {
when(it.itemId){
R.id.home2 -> {
2022-02-10 00:08:41 +05:30
navController.backQueue.clear()
2022-02-08 20:47:49 +05:30
navController.navigate(R.id.home2)
true
}
R.id.subscriptions -> {
2022-02-10 00:08:41 +05:30
//navController.backQueue.clear()
2022-02-08 20:47:49 +05:30
navController.navigate(R.id.subscriptions)
true
}
R.id.library -> {
2022-02-10 00:08:41 +05:30
//navController.backQueue.clear()
2022-02-08 20:47:49 +05:30
navController.navigate(R.id.library)
true
}
}
false
}
toolbar = findViewById(R.id.toolbar)
val hexColor = String.format("#%06X", 0xFFFFFF and 0xcc322d)
val appName = HtmlCompat.fromHtml(
"Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT
)
2022-02-02 22:18:54 +05:30
toolbar.title= appName
toolbar.setNavigationOnClickListener{
2022-02-02 21:40:37 +05:30
//settings fragment stuff
2022-02-08 14:58:50 +05:30
navController.navigate(R.id.settings)
true
}
toolbar.setOnMenuItemClickListener{
when (it.itemId){
R.id.action_search -> {
navController.navigate(R.id.searchFragment)
true
}
}
false
2021-12-28 01:37:07 +05:30
}
2021-12-09 18:01:40 +05:30
}
2021-12-16 03:54:40 +05:30
2022-02-10 00:08:41 +05:30
2022-01-28 23:16:11 +05:30
override fun onBackPressed() {
2022-02-02 23:58:24 +05:30
try{
val mainMotionLayout = findViewById<MotionLayout>(R.id.mainMotionLayout)
if (mainMotionLayout.progress == 0.toFloat()){
mainMotionLayout.transitionToEnd()
2022-02-10 16:39:34 +05:30
findViewById<ConstraintLayout>(R.id.main_container).isClickable=false
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout.transitionToEnd()
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
with(motionLayout) {
getConstraintSet(R.id.start).constrainHeight(R.id.player, 0)
enableTransition(R.id.yt_transition,true)
}
findViewById<LinearLayout>(R.id.linLayout).visibility=View.VISIBLE
isFullScreen=false
2022-02-02 23:58:24 +05:30
}else{
navController.popBackStack()
if (navController.currentBackStackEntry == null){
2022-02-10 00:08:41 +05:30
super.onBackPressed()
2022-02-10 16:39:34 +05:30
}
}
2022-02-02 23:58:24 +05:30
}catch (e: Exception){
navController.popBackStack()
if (navController.currentBackStackEntry == null){
2022-02-10 00:08:41 +05:30
super.onBackPressed()
2022-02-02 23:58:24 +05:30
}
}
2022-01-28 23:16:11 +05:30
}
2021-12-16 03:54:40 +05:30
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
val orientation = newConfig.orientation
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
2021-12-16 03:54:40 +05:30
println("Portrait")
2022-02-13 17:23:04 +05:30
unsetFullscreen()
2021-12-16 03:54:40 +05:30
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
println("Landscape")
2022-02-13 17:23:04 +05:30
setFullscreen()
2022-02-10 13:45:09 +05:30
}
}
private fun setFullscreen() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
window.insetsController?.apply {
hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
2022-01-29 18:41:00 +05:30
}
2022-02-10 13:45:09 +05:30
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@Suppress("DEPRECATION")
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
}
}
private fun unsetFullscreen(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
}
2021-12-16 03:54:40 +05:30
2022-02-10 13:45:09 +05:30
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
window.insetsController?.apply {
show(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@Suppress("DEPRECATION")
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_VISIBLE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
2021-12-16 03:54:40 +05:30
}
}
}