Update playback system

Tweak some things in PlaybackService and PlaybackStateManager to make them more elegant.
This commit is contained in:
OxygenCobalt 2021-03-05 14:57:44 -07:00
parent 466629e43d
commit 391ca70822
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
7 changed files with 27 additions and 44 deletions

View file

@ -30,13 +30,10 @@ class MainActivity : AppCompatActivity() {
)
val settingsManager = SettingsManager.getInstance()
val newAccent = Accent.set(settingsManager.accent)
AppCompatDelegate.setDefaultNightMode(settingsManager.theme)
val accent = Accent.set(settingsManager.accent)
// Apply the theme
setTheme(accent.theme)
setTheme(newAccent.theme)
if (isEdgeOn()) {
doEdgeToEdgeSetup(binding)

View file

@ -28,7 +28,7 @@ class MusicStore private constructor() {
val songs: List<Song> get() = mSongs
/** All parent models (ex Albums, Artists) loaded by Auxio */
val parents: MutableList<Parent> by lazy {
val parents: List<Parent> by lazy {
mutableListOf<Parent>().apply {
addAll(mGenres)
addAll(mArtists)
@ -50,7 +50,7 @@ class MusicStore private constructor() {
}
/**
* Do the internal music loading process.
* Do the actual music loading process internally.
*/
private fun loadMusicInternal(context: Context): Response {
logD("Starting initial music load...")

View file

@ -183,9 +183,7 @@ class PlaybackStateManager private constructor() {
mMode = mode
clearLoopMode()
updatePlayback(song)
// Keep shuffle on, if enabled
setShuffling(settingsManager.keepShuffle && mIsShuffling, keepSong = true)
}
@ -217,7 +215,6 @@ class PlaybackStateManager private constructor() {
}
}
clearLoopMode()
setShuffling(shuffled, keepSong = false)
updatePlayback(mQueue[0])
}
@ -232,8 +229,6 @@ class PlaybackStateManager private constructor() {
setShuffling(true, keepSong = false)
updatePlayback(mQueue[0])
// FIXME: Add clearLoopMode here?
}
/**
@ -248,6 +243,8 @@ class PlaybackStateManager private constructor() {
if (!mIsPlaying) {
setPlaying(true)
}
clearLoopMode()
}
// --- QUEUE FUNCTIONS ---
@ -256,8 +253,6 @@ class PlaybackStateManager private constructor() {
* Go to the next song, along with doing all the checks that entails.
*/
fun next() {
clearLoopMode()
// If there's anything in the user queue, go to the first song in there instead
// of incrementing the index.
if (mUserQueue.isNotEmpty()) {
@ -298,7 +293,6 @@ class PlaybackStateManager private constructor() {
mIndex = mIndex.dec()
}
clearLoopMode()
updatePlayback(mQueue[mIndex])
forceQueueUpdate()
}
@ -315,6 +309,7 @@ class PlaybackStateManager private constructor() {
mIsInUserQueue = false
mSong = mQueue[0]
clearLoopMode()
setPlaying(false)
forceQueueUpdate()
}
@ -623,33 +618,30 @@ class PlaybackStateManager private constructor() {
suspend fun restoreFromDatabase(context: Context) {
logD("Getting state from DB.")
val now: Long
val start: Long
val playbackState: PlaybackState?
val queueItems: List<QueueItem>
// The coroutine call is locked at queueItems so that this function does not
// go ahead until EVERYTHING is read.
// TODO: Improve this
val queueItems = withContext(Dispatchers.IO) {
now = System.currentTimeMillis()
withContext(Dispatchers.IO) {
start = System.currentTimeMillis()
val database = PlaybackStateDatabase.getInstance(context)
playbackState = database.readState()
database.readQueue()
queueItems = database.readQueue()
}
// Get off the IO coroutine since it will cause LiveData updates to throw an exception
if (playbackState != null) {
logD("Found playback state $playbackState")
logD("Found queue size ${queueItems.size}")
logD("Found playback state $playbackState with queue size ${queueItems.size}")
unpackFromPlaybackState(playbackState)
unpackQueue(queueItems)
doParentSanityCheck()
}
logD("Restore finished in ${System.currentTimeMillis() - now}ms")
logD("Restore finished in ${System.currentTimeMillis() - start}ms")
markRestored()
}
@ -717,9 +709,9 @@ class PlaybackStateManager private constructor() {
// When unpacking, first traverse albums and then traverse album songs to reduce
// the amount of comparisons in large queues.
queueItems.forEach { item ->
musicStore.albums.find {
it.name == item.albumName
}?.songs?.find { it.name == item.songName }?.let { song ->
val album = musicStore.albums.find { it.name == item.albumName }
album?.songs?.find { it.name == item.songName }?.let { song ->
if (item.isUserQueue) {
mUserQueue.add(song)
} else {

View file

@ -220,8 +220,6 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
this, song, settingsManager.colorizeNotif, ::startForegroundOrNotify
)
logD("Song Status: $song")
return
}

View file

@ -155,7 +155,6 @@ class SettingsListFragment : PreferenceFragmentCompat() {
val recycler = RecyclerView(requireContext()).apply {
adapter = AccentAdapter { accent ->
if (accent != Accent.get()) {
// TODO: Move this to Accent.set?
settingsManager.accent = accent
requireActivity().recreate()

View file

@ -24,8 +24,7 @@ import org.oxycblt.auxio.ui.inflater
/**
* A slimmed-down variant of [com.reddit.indicatorfastscroll.FastScrollerThumbView] designed
* specifically for Auxio. Also fixes a memory leak that occurs from a bug fix they
* added.
* specifically for Auxio. Also fixes a memory leak that occurs from a bug fix they added.
* @author OxygenCobalt
*/
class CobaltScrollThumb @JvmOverloads constructor(
@ -88,16 +87,18 @@ class CobaltScrollThumb @JvmOverloads constructor(
}
@Suppress("ClickableViewAccessibility")
scrollView.setOnTouchListener { v, event ->
scrollView.setOnTouchListener { _, event ->
scrollView.onTouchEvent(event)
scrollView.performClick()
val action = event.actionMasked
val actionValid = action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_CANCEL
// If we arent deselecting the scroll view, determine if we are selecting an item.
isActivated = if (
action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_CANCEL
) isPointerOnItem(scrollView, event.y.toInt()) else false
isActivated = if (actionValid) {
isPointerOnItem(scrollView, event.y.toInt())
} else {
false
}
true
}

View file

@ -9,7 +9,6 @@ import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.core.text.HtmlCompat
import org.oxycblt.auxio.R
import org.oxycblt.auxio.settings.SettingsManager
import java.util.Locale
/**
@ -71,6 +70,7 @@ data class Accent(@ColorRes val color: Int, @StyleRes val theme: Int, @StringRes
/**
* Get the current accent.
* @return The current accent
* @throws IllegalStateException When the accent has not been set.
*/
fun get(): Accent {
val cur = current
@ -79,11 +79,7 @@ data class Accent(@ColorRes val color: Int, @StyleRes val theme: Int, @StringRes
return cur
}
synchronized(this) {
val newCur = SettingsManager.getInstance().accent
current = newCur
return newCur
}
error("Accent must be set before retrieving it.")
}
/**