Add option to rewind before skipping back

Add an option to rewind before skipping to prevous song, depending on the user-defined threshhold.
This commit is contained in:
OxygenCobalt 2020-12-12 15:45:51 -07:00
parent aaa1ad1a3d
commit 6882722201
7 changed files with 75 additions and 14 deletions

View file

@ -14,6 +14,7 @@ import org.oxycblt.auxio.music.Genre
import org.oxycblt.auxio.music.Header import org.oxycblt.auxio.music.Header
import org.oxycblt.auxio.music.MusicStore import org.oxycblt.auxio.music.MusicStore
import org.oxycblt.auxio.music.Song import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.settings.SettingsManager
import kotlin.random.Random import kotlin.random.Random
/** /**
@ -100,6 +101,8 @@ class PlaybackStateManager private constructor() {
val isRestored: Boolean get() = mIsRestored val isRestored: Boolean get() = mIsRestored
val hasPlayed: Boolean get() = mHasPlayed val hasPlayed: Boolean get() = mHasPlayed
private val settingsManager = SettingsManager.getInstance()
// --- CALLBACKS --- // --- CALLBACKS ---
private val callbacks = mutableListOf<Callback>() private val callbacks = mutableListOf<Callback>()
@ -252,7 +255,7 @@ class PlaybackStateManager private constructor() {
forceUserQueueUpdate() forceUserQueueUpdate()
} else { } else {
// If not in the user queue, then increment the current index // Increment the index.
// If it cant be incremented anymore, end playback or loop depending on the setting. // If it cant be incremented anymore, end playback or loop depending on the setting.
if (mIndex < mQueue.lastIndex) { if (mIndex < mQueue.lastIndex) {
mIndex = mIndex.inc() mIndex = mIndex.inc()
@ -271,18 +274,24 @@ class PlaybackStateManager private constructor() {
/** /**
* Go to the previous song, doing any checks that are needed. * Go to the previous song, doing any checks that are needed.
* TODO: Implement option to rewind before skipping back
*/ */
fun prev() { fun prev() {
if (mIndex > 0 && !mIsInUserQueue) { // If enabled, rewind before skipping back if the position is past the threshold set.
mIndex = mIndex.dec() if (settingsManager.rewindWithPrev && mPosition >= settingsManager.rewindThreshold) {
seekTo(0)
} else {
// Only decrement the index if there's a song to move back to AND if we are not exiting
// the user queue.
if (mIndex > 0 && !mIsInUserQueue) {
mIndex = mIndex.dec()
}
resetLoopMode()
updatePlayback(mQueue[mIndex])
forceQueueUpdate()
} }
resetLoopMode()
updatePlayback(mQueue[mIndex])
forceQueueUpdate()
} }
fun removeQueueItem(index: Int): Boolean { fun removeQueueItem(index: Int): Boolean {

View file

@ -73,6 +73,12 @@ class SettingsManager private constructor(context: Context) :
) )
) )
val rewindWithPrev: Boolean
get() = sharedPrefs.getBoolean(Keys.KEY_PREV_REWIND, true)
val rewindThreshold: Long
get() = (sharedPrefs.getInt(Keys.KEY_REWIND_THRESHOLD, 5) * 1000).toLong()
var librarySortMode: SortMode var librarySortMode: SortMode
get() { get() {
return SortMode.fromInt( return SortMode.fromInt(
@ -145,7 +151,7 @@ class SettingsManager private constructor(context: Context) :
*/ */
fun getInstance(): SettingsManager { fun getInstance(): SettingsManager {
check(::INSTANCE.isInitialized) { check(::INSTANCE.isInitialized) {
"PrefsManager must be initialized with init() before getting its instance." "SettingsManager must be initialized with init() before getting its instance."
} }
return INSTANCE return INSTANCE
} }
@ -161,6 +167,8 @@ class SettingsManager private constructor(context: Context) :
const val KEY_AUDIO_FOCUS = "KEY_AUDIO_FOCUS" const val KEY_AUDIO_FOCUS = "KEY_AUDIO_FOCUS"
const val KEY_PLUG_MANAGEMENT = "KEY_PLUG_MGT" const val KEY_PLUG_MANAGEMENT = "KEY_PLUG_MGT"
const val KEY_SONG_PLAYBACK_MODE = "KEY_SONG_PLAY_MODE" const val KEY_SONG_PLAYBACK_MODE = "KEY_SONG_PLAY_MODE"
const val KEY_PREV_REWIND = "KEY_PREV_REWIND"
const val KEY_REWIND_THRESHOLD = "KEY_REWIND_THRESHOLD"
const val KEY_LIBRARY_SORT_MODE = "KEY_LIBRARY_SORT_MODE" const val KEY_LIBRARY_SORT_MODE = "KEY_LIBRARY_SORT_MODE"
} }

View file

@ -8,7 +8,6 @@ import android.os.Build
import android.text.SpannableString import android.text.SpannableString
import android.text.Spanned import android.text.Spanned
import android.text.style.ForegroundColorSpan import android.text.style.ForegroundColorSpan
import android.util.Log
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.Window import android.view.Window
@ -150,8 +149,6 @@ fun PopupMenu.setupSongActions(song: Song, context: Context, playbackModel: Play
else -> -1 else -> -1
} }
Log.d(this::class.simpleName, (idToRemove == R.id.action_play_album).toString())
menu.findItem(idToRemove)?.isVisible = false menu.findItem(idToRemove)?.isVisible = false
} }

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="rewind_threshold_min">1</integer>
<integer name="rewind_threshold_max">30</integer>
<integer name="rewind_threshold_default">5</integer>
</resources>

View file

@ -71,6 +71,11 @@
<string name="setting_behavior">Behavior</string> <string name="setting_behavior">Behavior</string>
<string name="setting_behavior_song_play">When a song is selected</string> <string name="setting_behavior_song_play">When a song is selected</string>
<string name="setting_behavior_rewind_prev">Rewind before skipping back</string>
<string name="setting_behavior_rewind_prev_desc">Rewind before skipping to the previous song</string>
<string name="setting_behavior_rewind_threshold">Rewind threshold</string>
<string name="setting_behavior_rewind_threshold_desc">Progress at which a rewind should occur (seconds)</string>
<!-- Debug Namespace | Debug labels --> <!-- Debug Namespace | Debug labels -->
<string name="debug_state_saved">State saved</string> <string name="debug_state_saved">State saved</string>

View file

@ -93,5 +93,23 @@
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
<SwitchPreference
app:key="KEY_PREV_REWIND"
android:title="@string/setting_behavior_rewind_prev"
app:summary="@string/setting_behavior_rewind_prev_desc"
app:iconSpaceReserved="false"
app:defaultValue="true" />
<SeekBarPreference
app:key="KEY_REWIND_THRESHOLD"
android:title="@string/setting_behavior_rewind_threshold"
android:defaultValue="@integer/rewind_threshold_default"
android:max="@integer/rewind_threshold_max"
app:min="@integer/rewind_threshold_min"
app:summary="@string/setting_behavior_rewind_threshold_desc"
app:showSeekBarValue="true"
app:iconSpaceReserved="false"
app:dependency="KEY_PREV_REWIND" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View file

@ -84,5 +84,23 @@
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
<SwitchPreference
app:key="KEY_PREV_REWIND"
android:title="@string/setting_behavior_rewind_prev"
app:summary="@string/setting_behavior_rewind_prev_desc"
app:iconSpaceReserved="false"
app:defaultValue="true" />
<SeekBarPreference
app:key="KEY_REWIND_THRESHOLD"
android:title="@string/setting_behavior_rewind_threshold"
android:defaultValue="@integer/rewind_threshold_default"
app:summary="@string/setting_behavior_rewind_threshold_desc"
app:showSeekBarValue="true"
android:max="@integer/rewind_threshold_max"
app:min="@integer/rewind_threshold_min"
app:iconSpaceReserved="false"
app:dependency="KEY_PREV_REWIND" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>