Merge pull request #766 from Bnyro/master

cleanup
This commit is contained in:
Bnyro 2022-07-12 15:29:28 +00:00 committed by GitHub
commit 2c5c6654b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 30 deletions

View File

@ -0,0 +1,7 @@
package com.github.libretube
object Globals {
var isFullScreen = false
var isMiniPlayerVisible = false
var isCurrentViewMainSettings = true
}

View File

@ -25,11 +25,11 @@ import androidx.fragment.app.Fragment
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController import androidx.navigation.ui.setupWithNavController
import com.github.libretube.Globals
import com.github.libretube.PIPED_API_URL import com.github.libretube.PIPED_API_URL
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.ActivityMainBinding import com.github.libretube.databinding.ActivityMainBinding
import com.github.libretube.fragments.PlayerFragment import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.fragments.isFullScreen
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.services.ClosingService import com.github.libretube.services.ClosingService
import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.ConnectionHelper
@ -308,7 +308,7 @@ class MainActivity : AppCompatActivity() {
enableTransition(R.id.yt_transition, true) enableTransition(R.id.yt_transition, true)
} }
findViewById<LinearLayout>(R.id.linLayout).visibility = View.VISIBLE findViewById<LinearLayout>(R.id.linLayout).visibility = View.VISIBLE
isFullScreen = false Globals.isFullScreen = false
} }
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {

View File

@ -3,14 +3,13 @@ package com.github.libretube.activities
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.github.libretube.Globals
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.ActivitySettingsBinding import com.github.libretube.databinding.ActivitySettingsBinding
import com.github.libretube.preferences.MainSettings import com.github.libretube.preferences.MainSettings
import com.github.libretube.util.ThemeHelper import com.github.libretube.util.ThemeHelper
import com.google.android.material.color.DynamicColors import com.google.android.material.color.DynamicColors
var isCurrentViewMainSettings = true
class SettingsActivity : AppCompatActivity() { class SettingsActivity : AppCompatActivity() {
val TAG = "SettingsActivity" val TAG = "SettingsActivity"
lateinit var binding: ActivitySettingsBinding lateinit var binding: ActivitySettingsBinding
@ -46,11 +45,11 @@ class SettingsActivity : AppCompatActivity() {
} }
override fun onBackPressed() { override fun onBackPressed() {
if (isCurrentViewMainSettings) { if (Globals.isCurrentViewMainSettings) {
super.onBackPressed() super.onBackPressed()
finishAndRemoveTask() finishAndRemoveTask()
} else { } else {
isCurrentViewMainSettings = true Globals.isCurrentViewMainSettings = true
supportFragmentManager supportFragmentManager
.beginTransaction() .beginTransaction()
.replace(R.id.settings, MainSettings()) .replace(R.id.settings, MainSettings())

View File

@ -10,6 +10,7 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.Globals
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.adapters.PlaylistsAdapter import com.github.libretube.adapters.PlaylistsAdapter
import com.github.libretube.databinding.FragmentLibraryBinding import com.github.libretube.databinding.FragmentLibraryBinding
@ -77,7 +78,7 @@ class LibraryFragment : Fragment() {
override fun onResume() { override fun onResume() {
// optimize CreatePlaylistFab bottom margin if miniPlayer active // optimize CreatePlaylistFab bottom margin if miniPlayer active
val layoutParams = binding.createPlaylist.layoutParams as ViewGroup.MarginLayoutParams val layoutParams = binding.createPlaylist.layoutParams as ViewGroup.MarginLayoutParams
layoutParams.bottomMargin = if (isMiniPlayerVisible) 180 else 64 layoutParams.bottomMargin = if (Globals.isMiniPlayerVisible) 180 else 64
binding.createPlaylist.layoutParams = layoutParams binding.createPlaylist.layoutParams = layoutParams
super.onResume() super.onResume()
} }

View File

@ -32,6 +32,7 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.Globals
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.activities.MainActivity import com.github.libretube.activities.MainActivity
import com.github.libretube.activities.hideKeyboard import com.github.libretube.activities.hideKeyboard
@ -95,9 +96,6 @@ import java.io.IOException
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.abs import kotlin.math.abs
var isFullScreen = false
var isMiniPlayerVisible = false
class PlayerFragment : Fragment() { class PlayerFragment : Fragment() {
private val TAG = "PlayerFragment" private val TAG = "PlayerFragment"
@ -208,11 +206,11 @@ class PlayerFragment : Fragment() {
val mainMotionLayout = val mainMotionLayout =
mainActivity.binding.mainMotionLayout mainActivity.binding.mainMotionLayout
if (currentId == eId) { if (currentId == eId) {
isMiniPlayerVisible = true Globals.isMiniPlayerVisible = true
exoPlayerView.useController = false exoPlayerView.useController = false
mainMotionLayout.progress = 1F mainMotionLayout.progress = 1F
} else if (currentId == sId) { } else if (currentId == sId) {
isMiniPlayerVisible = false Globals.isMiniPlayerVisible = false
exoPlayerView.useController = true exoPlayerView.useController = true
mainMotionLayout.progress = 0F mainMotionLayout.progress = 0F
} }
@ -231,7 +229,7 @@ class PlayerFragment : Fragment() {
binding.playerMotionLayout.transitionToStart() binding.playerMotionLayout.transitionToStart()
binding.closeImageView.setOnClickListener { binding.closeImageView.setOnClickListener {
isMiniPlayerVisible = false Globals.isMiniPlayerVisible = false
binding.playerMotionLayout.transitionToEnd() binding.playerMotionLayout.transitionToEnd()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
@ -240,7 +238,7 @@ class PlayerFragment : Fragment() {
.commit() .commit()
} }
playerBinding.closeImageButton.setOnClickListener { playerBinding.closeImageButton.setOnClickListener {
isMiniPlayerVisible = false Globals.isMiniPlayerVisible = false
binding.playerMotionLayout.transitionToEnd() binding.playerMotionLayout.transitionToEnd()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
@ -273,7 +271,7 @@ class PlayerFragment : Fragment() {
playerBinding.fullscreen.setOnClickListener { playerBinding.fullscreen.setOnClickListener {
// hide player controller // hide player controller
exoPlayerView.hideController() exoPlayerView.hideController()
if (!isFullScreen) { if (!Globals.isFullScreen) {
// go to fullscreen mode // go to fullscreen mode
setFullscreen() setFullscreen()
} else { } else {
@ -358,7 +356,7 @@ class PlayerFragment : Fragment() {
} }
mainActivity.requestedOrientation = orientation mainActivity.requestedOrientation = orientation
isFullScreen = true Globals.isFullScreen = true
} }
private fun unsetFullscreen() { private fun unsetFullscreen() {
@ -378,7 +376,7 @@ class PlayerFragment : Fragment() {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
isFullScreen = false Globals.isFullScreen = false
} }
private fun scaleControls(scaleFactor: Float) { private fun scaleControls(scaleFactor: Float) {
@ -508,7 +506,7 @@ class PlayerFragment : Fragment() {
runOnUiThread { runOnUiThread {
// set media sources for the player // set media sources for the player
setResolutionAndSubtitles(view, response) setResolutionAndSubtitles(response)
prepareExoPlayerView() prepareExoPlayerView()
initializePlayerView(view, response) initializePlayerView(view, response)
seekToWatchPosition() seekToWatchPosition()
@ -981,10 +979,15 @@ class PlayerFragment : Fragment() {
} }
override fun onScrubMove(timeBar: TimeBar, position: Long) { override fun onScrubMove(timeBar: TimeBar, position: Long) {
exoPlayer.seekTo(position) val minTimeDiff = 10 * 1000 // 10s
// get the difference between the new and the old position
val diff = abs(exoPlayer.currentPosition - position)
// seek only when the difference is greater than 10 seconds
if (diff >= minTimeDiff) exoPlayer.seekTo(position)
} }
override fun onScrubStop(timeBar: TimeBar, position: Long, canceled: Boolean) { override fun onScrubStop(timeBar: TimeBar, position: Long, canceled: Boolean) {
exoPlayer.seekTo(position)
exoPlayer.play() exoPlayer.play()
Handler(Looper.getMainLooper()).postDelayed({ Handler(Looper.getMainLooper()).postDelayed({
exoPlayerView.hideController() exoPlayerView.hideController()
@ -1012,7 +1015,7 @@ class PlayerFragment : Fragment() {
} }
playerBinding.chapterLL.visibility = View.VISIBLE playerBinding.chapterLL.visibility = View.VISIBLE
playerBinding.chapterLL.setOnClickListener { playerBinding.chapterLL.setOnClickListener {
if (isFullScreen) { if (Globals.isFullScreen) {
MaterialAlertDialogBuilder(requireContext()) MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.chapters) .setTitle(R.string.chapters)
.setItems(titles.toTypedArray()) { _, index -> .setItems(titles.toTypedArray()) { _, index ->
@ -1033,7 +1036,7 @@ class PlayerFragment : Fragment() {
// call the function again in 100ms // call the function again in 100ms
exoPlayerView.postDelayed(this::setCurrentChapterName, 100) exoPlayerView.postDelayed(this::setCurrentChapterName, 100)
var chapterName = getCurrentChapterName() val chapterName = getCurrentChapterName()
// change the chapter name textView text to the chapterName // change the chapter name textView text to the chapterName
if (chapterName != null && chapterName != playerBinding.chapterName.text) { if (chapterName != null && chapterName != playerBinding.chapterName.text) {
@ -1078,7 +1081,7 @@ class PlayerFragment : Fragment() {
exoPlayer.setMediaSource(mergeSource) exoPlayer.setMediaSource(mergeSource)
} }
private fun setResolutionAndSubtitles(view: View, response: Streams) { private fun setResolutionAndSubtitles(response: Streams) {
val videoFormatPreference = val videoFormatPreference =
PreferenceHelper.getString(requireContext(), "player_video_format", "WEBM") PreferenceHelper.getString(requireContext(), "player_video_format", "WEBM")
val defres = PreferenceHelper.getString(requireContext(), "default_res", "")!! val defres = PreferenceHelper.getString(requireContext(), "default_res", "")!!
@ -1312,7 +1315,7 @@ class PlayerFragment : Fragment() {
private fun subscribe(channel_id: String) { private fun subscribe(channel_id: String) {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.authApi.subscribe( RetrofitInstance.authApi.subscribe(
token, token,
@ -1335,7 +1338,7 @@ class PlayerFragment : Fragment() {
private fun unsubscribe(channel_id: String) { private fun unsubscribe(channel_id: String) {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.authApi.unsubscribe( RetrofitInstance.authApi.unsubscribe(
token, token,
@ -1435,7 +1438,7 @@ class PlayerFragment : Fragment() {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
isFullScreen = false Globals.isFullScreen = false
} else { } else {
// enable exoPlayer controls again // enable exoPlayer controls again
exoPlayerView.useController = true exoPlayerView.useController = true
@ -1450,7 +1453,7 @@ class PlayerFragment : Fragment() {
binding.playerScrollView.getHitRect(bounds) binding.playerScrollView.getHitRect(bounds)
if (SDK_INT >= Build.VERSION_CODES.O && if (SDK_INT >= Build.VERSION_CODES.O &&
exoPlayer.isPlaying && (binding.playerScrollView.getLocalVisibleRect(bounds) || isFullScreen) exoPlayer.isPlaying && (binding.playerScrollView.getLocalVisibleRect(bounds) || Globals.isFullScreen)
) { ) {
activity?.enterPictureInPictureMode(updatePipParams()) activity?.enterPictureInPictureMode(updatePipParams())
} }

View File

@ -37,7 +37,7 @@ object ThemeHelper {
"L" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) "L" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
"D" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) "D" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
"O" -> { "O" -> {
context.setTheme(R.style.OLED) context.setTheme(R.style.Black)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
} }
} }

View File

@ -5,7 +5,7 @@ import android.os.Looper
import android.view.View import android.view.View
class DoubleClickListener( class DoubleClickListener(
private val doubleClickTimeLimitMills: Long = 300, private val doubleClickTimeLimitMills: Long = 200,
private val callback: Callback private val callback: Callback
) : View.OnClickListener { ) : View.OnClickListener {
private var lastClicked: Long = -1L private var lastClicked: Long = -1L

View File

@ -15,7 +15,7 @@
</style> </style>
<style name="OLED"> <style name="Black">
<item name="android:colorBackground">@android:color/black</item> <item name="android:colorBackground">@android:color/black</item>
<item name="colorSurface">@android:color/black</item> <item name="colorSurface">@android:color/black</item>
@ -116,7 +116,7 @@
<style name="PlayerButton"> <style name="PlayerButton">
<item name="android:padding">6dp</item> <item name="android:padding">9dp</item>
<item name="android:layout_height">36dp</item> <item name="android:layout_height">36dp</item>
<item name="android:layout_width">36dp</item> <item name="android:layout_width">36dp</item>
<item name="android:layout_gravity">center</item> <item name="android:layout_gravity">center</item>