get rid of globals

This commit is contained in:
Bnyro 2022-09-19 20:07:55 +02:00
parent ba21ebb99e
commit d99386bbc2
7 changed files with 48 additions and 36 deletions

View File

@ -1,12 +0,0 @@
package com.github.libretube
/**
* Global variables can be stored here
*/
object Globals {
// for downloads
var IS_DOWNLOAD_RUNNING = false
// history of played videos in the current lifecycle
val playingQueue = mutableListOf<String>()
}

View File

@ -35,7 +35,6 @@ import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.activities.MainActivity
import com.github.libretube.adapters.ChaptersAdapter
@ -67,12 +66,14 @@ import com.github.libretube.obj.Segment
import com.github.libretube.obj.Segments
import com.github.libretube.obj.Streams
import com.github.libretube.services.BackgroundMode
import com.github.libretube.services.DownloadService
import com.github.libretube.util.AutoPlayHelper
import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.ImageHelper
import com.github.libretube.util.NetworkHelper
import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayerHelper
import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.PreferenceHelper
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.DefaultLoadControl
@ -210,7 +211,7 @@ class PlayerFragment : BaseFragment() {
context?.hideKeyboard(view)
// clear the playing queue
Globals.playingQueue.clear()
PlayingQueue.clear()
setUserPrefs()
@ -752,7 +753,7 @@ class PlayerFragment : BaseFragment() {
}
private fun playVideo() {
Globals.playingQueue += videoId!!
PlayingQueue.add(videoId!!)
lifecycleScope.launchWhenCreated {
streams = try {
RetrofitInstance.api.getStreams(videoId!!)
@ -1023,7 +1024,7 @@ class PlayerFragment : BaseFragment() {
if (response.duration > 0) {
// download clicked
binding.relPlayerDownload.setOnClickListener {
if (!Globals.IS_DOWNLOAD_RUNNING) {
if (!DownloadService.IS_DOWNLOAD_RUNNING) {
val newFragment = DownloadDialog(videoId!!)
newFragment.show(childFragmentManager, DownloadDialog::class.java.name)
} else {
@ -1105,7 +1106,7 @@ class PlayerFragment : BaseFragment() {
// next and previous buttons
playerBinding.skipPrev.visibility = if (
skipButtonsEnabled && Globals.playingQueue.indexOf(videoId!!) != 0
skipButtonsEnabled && PlayingQueue.queue.indexOf(videoId!!) != 0
) {
View.VISIBLE
} else {
@ -1114,8 +1115,8 @@ class PlayerFragment : BaseFragment() {
playerBinding.skipNext.visibility = if (skipButtonsEnabled) View.VISIBLE else View.INVISIBLE
playerBinding.skipPrev.setOnClickListener {
val index = Globals.playingQueue.indexOf(videoId!!) - 1
videoId = Globals.playingQueue[index]
val index = PlayingQueue.queue.indexOf(videoId!!) - 1
videoId = PlayingQueue.queue[index]
playVideo()
}

View File

@ -11,7 +11,6 @@ import android.os.IBinder
import android.os.Looper
import android.widget.Toast
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
@ -25,6 +24,7 @@ import com.github.libretube.obj.Streams
import com.github.libretube.util.AutoPlayHelper
import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayerHelper
import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.PreferenceHelper
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.ExoPlayer
@ -119,7 +119,7 @@ class BackgroundMode : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
try {
// clear the playing queue
Globals.playingQueue.clear()
PlayingQueue.clear()
// get the intent arguments
videoId = intent?.getStringExtra(IntentData.videoId)!!
@ -145,7 +145,7 @@ class BackgroundMode : Service() {
seekToPosition: Long = 0
) {
// append the video to the playing queue
Globals.playingQueue += videoId
PlayingQueue.add(videoId)
CoroutineScope(Dispatchers.IO).launch {
try {
streams = RetrofitInstance.api.getStreams(videoId)

View File

@ -11,7 +11,6 @@ import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
import com.github.libretube.constants.DOWNLOAD_FAILURE_NOTIFICATION_ID
@ -34,7 +33,7 @@ class DownloadService : Service() {
override fun onCreate() {
super.onCreate()
Globals.IS_DOWNLOAD_RUNNING = true
IS_DOWNLOAD_RUNNING = true
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@ -175,11 +174,16 @@ class DownloadService : Service() {
try {
unregisterReceiver(onDownloadComplete)
} catch (e: Exception) {
e.printStackTrace()
}
Globals.IS_DOWNLOAD_RUNNING = false
IS_DOWNLOAD_RUNNING = false
stopService(Intent(this, DownloadService::class.java))
super.onDestroy()
}
companion object {
var IS_DOWNLOAD_RUNNING = false
}
}

View File

@ -5,7 +5,6 @@ import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
@ -13,6 +12,7 @@ import com.github.libretube.dialogs.AddToPlaylistDialog
import com.github.libretube.dialogs.DownloadDialog
import com.github.libretube.dialogs.ShareDialog
import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.PreferenceHelper
import com.github.libretube.views.BottomSheet
@ -90,7 +90,7 @@ class VideoOptionsBottomSheet(
shareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
}
context?.getString(R.string.add_to_queue) -> {
Globals.playingQueue += videoId
PlayingQueue.add(videoId)
}
}
}

View File

@ -1,6 +1,5 @@
package com.github.libretube.util
import com.github.libretube.Globals
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.extensions.toID
import com.github.libretube.obj.StreamItem
@ -21,9 +20,9 @@ class AutoPlayHelper(
currentVideoId: String,
relatedStreams: List<StreamItem>?
): String? {
return if (Globals.playingQueue.last() != currentVideoId) {
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
Globals.playingQueue[currentVideoIndex + 1]
return if (PlayingQueue.queue.last() != currentVideoId) {
val currentVideoIndex = PlayingQueue.queue.indexOf(currentVideoId)
PlayingQueue.queue[currentVideoIndex + 1]
} else if (playlistId == null) {
getNextTrendingVideoId(
currentVideoId,
@ -49,8 +48,8 @@ class AutoPlayHelper(
var nextStreamId: String? = null
while (nextStreamId == null ||
(
Globals.playingQueue.contains(nextStreamId) &&
Globals.playingQueue.indexOf(videoId) > Globals.playingQueue.indexOf(
PlayingQueue.queue.contains(nextStreamId) &&
PlayingQueue.queue.indexOf(videoId) > PlayingQueue.queue.indexOf(
nextStreamId
)
)
@ -110,9 +109,9 @@ class AutoPlayHelper(
fun getNextPlayingQueueVideoId(
currentVideoId: String
): String? {
return if (Globals.playingQueue.last() != currentVideoId) {
val currentVideoIndex = Globals.playingQueue.indexOf(currentVideoId)
Globals.playingQueue[currentVideoIndex + 1]
return if (PlayingQueue.queue.last() != currentVideoId) {
val currentVideoIndex = PlayingQueue.queue.indexOf(currentVideoId)
PlayingQueue.queue[currentVideoIndex + 1]
} else {
null
}

View File

@ -0,0 +1,20 @@
package com.github.libretube.util
object PlayingQueue {
val queue = mutableListOf<String>()
fun clear() {
queue.clear()
}
fun add(videoId: String) {
queue.add(videoId)
}
fun playNext(currentVideoId: String, nextVideoId: String) {
queue.add(
queue.indexOf(currentVideoId),
nextVideoId
)
}
}