Make save playback state setting public

Make the ability to force-save the playback state public in the settings menu instead of in the debug menu.
This commit is contained in:
OxygenCobalt
2021-02-19 11:19:45 -07:00
parent 0a7fbeca6d
commit e631ddd730
8 changed files with 54 additions and 55 deletions
@@ -73,6 +73,16 @@ class LoadingFragment : Fragment() {
return binding.root
}
override fun onResume() {
super.onResume()
if (MusicStore.getInstance().loaded) {
findNavController().navigate(
LoadingFragmentDirections.actionToMain()
)
}
}
// --- PERMISSIONS ---
private fun noPermissions(): Boolean {
@@ -62,8 +62,6 @@ class MusicLoader(private val app: Application) {
Genres.DEFAULT_SORT_ORDER
)
val genrePlaceholder = app.getString(R.string.placeholder_genre)
// And then process those into Genre objects
genreCursor?.use { cursor ->
val idIndex = cursor.getColumnIndexOrThrow(Genres._ID)
@@ -71,7 +69,7 @@ class MusicLoader(private val app: Application) {
while (cursor.moveToNext()) {
val id = cursor.getLong(idIndex)
val name = cursor.getStringOrNull(nameIndex) ?: genrePlaceholder
val name = cursor.getStringOrNull(nameIndex) ?: continue // No non-broken genre would be missing a name
genres.add(Genre(id, name))
}
@@ -119,12 +117,7 @@ class MusicLoader(private val app: Application) {
artistName = artistPlaceholder
}
albums.add(
Album(
id = id, name = name, artistName = artistName,
coverUri = coverUri, year = year
)
)
albums.add(Album(id, name, artistName, coverUri, year))
}
cursor.close()
@@ -170,12 +163,7 @@ class MusicLoader(private val app: Application) {
val track = cursor.getInt(trackIndex)
val duration = cursor.getLong(durationIndex)
songs.add(
Song(
id, title, albumId,
track, duration
)
)
songs.add(Song(id, title, albumId, track, duration))
}
cursor.close()
@@ -187,6 +175,7 @@ class MusicLoader(private val app: Application) {
// Then try to associate any genres with their respective songs
// This is stupidly inefficient, but I don't have another choice really.
// Blame the android devs for deciding to design MediaStore this way.
for (genre in genres) {
val songGenreCursor = resolver.query(
Genres.Members.getContentUri("external", genre.id),
@@ -207,6 +196,23 @@ class MusicLoader(private val app: Application) {
}
}
/*
// Fix that will group songs w/o genres into an unknown genre
// Currently disabled until it would actually benefit someone, otherwise its just
// a performance deadweight.
val songsWithoutGenres = songs.filter { it.genre == null }
if (songsWithoutGenres.isNotEmpty()) {
val unknownGenre = Genre(name = app.getString(R.string.placeholder_genre))
songsWithoutGenres.forEach {
unknownGenre.addSong(it)
}
genres.add(unknownGenre)
}
*/
logD("Song search finished with ${songs.size} found")
}
}
@@ -298,6 +298,18 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
// --- SAVE/RESTORE FUNCTIONS ---
/**
* Force save the current [PlaybackStateManager] state to the database. Called by SettingsListFragment.
* @param context [Context] required.
*/
fun savePlaybackState(context: Context) {
viewModelScope.launch {
playbackManager.saveStateToDatabase(context)
context.getString(R.string.debug_state_saved).createToast(context)
}
}
/**
* Get [PlaybackStateManager] to restore its state from the database, if needed. Called by MainFragment.
* @param context [Context] required.
@@ -310,18 +322,6 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
}
}
/**
* Force save the current [PlaybackStateManager] state to the database. Called by SettingsListFragment.
* @param context [Context] required.
*/
fun save(context: Context) {
viewModelScope.launch {
playbackManager.saveStateToDatabase(context)
context.getString(R.string.debug_state_saved).createToast(context)
}
}
/** Attempt to restore the current playback state from an existing [PlaybackStateManager] instance */
private fun restorePlaybackState() {
logD("Attempting to restore playback state.")
@@ -14,7 +14,6 @@ import coil.Coil
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.utils.invalidateDividers
import org.oxycblt.auxio.BuildConfig
import org.oxycblt.auxio.R
import org.oxycblt.auxio.logD
import org.oxycblt.auxio.playback.PlaybackViewModel
@@ -48,13 +47,6 @@ class SettingsListFragment : PreferenceFragmentCompat() {
private fun recursivelyHandleChildren(pref: Preference) {
if (pref is PreferenceCategory) {
// Show the debug category if this build is a debug build
if (pref.title == getString(R.string.debug_title) && BuildConfig.DEBUG) {
logD("Showing debug category.")
pref.isVisible = true
}
// If this preference is a category of its own, handle its own children
pref.children.forEach { recursivelyHandleChildren(it) }
} else {
@@ -133,7 +125,7 @@ class SettingsListFragment : PreferenceFragmentCompat() {
SettingsManager.Keys.KEY_DEBUG_SAVE -> {
onPreferenceClickListener = Preference.OnPreferenceClickListener {
playbackModel.save(requireContext())
playbackModel.savePlaybackState(requireContext())
true
}