Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.happyseal.zenplayer.LogMusic
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import org.json.JSONArray

private val Context.queueDataStore: DataStore<Preferences> by preferencesDataStore(
name = "zenplayer_queue_snapshot",
Expand All @@ -33,7 +34,7 @@ class QueueSnapshotDataStore(
*/
suspend fun saveQueueSnapshot(snapshot: QueueSnapshot) {
dataStore.edit { prefs ->
prefs[KEY_TRACK_URIS] = snapshot.trackUris.joinToString(SEPARATOR)
prefs[KEY_TRACK_URIS] = JSONArray(snapshot.trackUris).toString()
prefs[KEY_CURRENT_INDEX] = snapshot.currentIndex
prefs[KEY_POSITION_MS] = snapshot.positionMs
}
Expand All @@ -50,7 +51,8 @@ class QueueSnapshotDataStore(
}.map { prefs ->
val urisRaw = prefs[KEY_TRACK_URIS] ?: return@map null
if (urisRaw.isBlank()) return@map null
val uris = urisRaw.split(SEPARATOR).filter { it.isNotBlank() }
val jsonArray = JSONArray(urisRaw)
val uris = List(jsonArray.length()) { jsonArray.getString(it) }.filter { it.isNotBlank() }
if (uris.isEmpty()) return@map null

QueueSnapshot(
Expand All @@ -76,7 +78,6 @@ class QueueSnapshotDataStore(
private val KEY_TRACK_URIS = stringPreferencesKey("queue_track_uris")
private val KEY_CURRENT_INDEX = intPreferencesKey("queue_current_index")
private val KEY_POSITION_MS = longPreferencesKey("queue_position_ms")
private const val SEPARATOR = "|||"
}
}

Expand Down
Loading