mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-11 07:29:14 +01:00
add preferences, paging reads, repositories and sync status
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.subtracks.data.prefs
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
enum class AlbumSort { Name, Artist, Year, RecentlyAdded }
|
||||
|
||||
private val Context.preferences: DataStore<Preferences> by preferencesDataStore("user_prefs")
|
||||
|
||||
class UserPreferences(
|
||||
private val store: DataStore<Preferences>,
|
||||
) {
|
||||
val albumSort: Flow<AlbumSort> =
|
||||
store.data.map { prefs ->
|
||||
prefs[ALBUM_SORT]?.let { stored -> AlbumSort.entries.firstOrNull { it.name == stored } } ?: AlbumSort.Name
|
||||
}
|
||||
|
||||
suspend fun setAlbumSort(sort: AlbumSort) {
|
||||
store.edit { prefs -> prefs[ALBUM_SORT] = sort.name }
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val ALBUM_SORT = stringPreferencesKey("album_sort")
|
||||
}
|
||||
}
|
||||
|
||||
fun createUserPreferences(context: Context): UserPreferences = UserPreferences(context.preferences)
|
||||
Reference in New Issue
Block a user