Migrate to DataStore

This commit is contained in:
vfsfitvnm 2022-06-05 17:34:56 +02:00
parent 68edfe9b9f
commit c808b2a482
9 changed files with 23 additions and 29 deletions

View file

@ -88,6 +88,8 @@ dependencies {
implementation(libs.android.media)
implementation(libs.bundles.media3)
implementation(libs.datastore)
implementation(libs.room)
kapt(libs.room.compiler)

View file

@ -1,6 +1,7 @@
package it.vfsfitvnm.vimusic
import android.content.ComponentName
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
@ -20,15 +21,13 @@ import androidx.compose.material.ripple.LocalRippleTheme
import androidx.compose.material.ripple.RippleAlpha
import androidx.compose.material.ripple.RippleTheme
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.datastore.preferences.preferencesDataStore
import androidx.media3.session.MediaController
import androidx.media3.session.SessionToken
import com.google.accompanist.systemuicontroller.rememberSystemUiController
@ -41,13 +40,13 @@ import it.vfsfitvnm.vimusic.ui.components.BottomSheetMenu
import it.vfsfitvnm.vimusic.ui.components.LocalMenuState
import it.vfsfitvnm.vimusic.ui.components.rememberMenuState
import it.vfsfitvnm.vimusic.ui.screens.HomeScreen
import it.vfsfitvnm.vimusic.ui.screens.SettingsScreen
import it.vfsfitvnm.vimusic.ui.styling.LocalColorPalette
import it.vfsfitvnm.vimusic.ui.styling.LocalTypography
import it.vfsfitvnm.vimusic.ui.styling.rememberColorPalette
import it.vfsfitvnm.vimusic.ui.styling.rememberTypography
import it.vfsfitvnm.vimusic.utils.*
private val Context.dataStore by preferencesDataStore(name = "preferences")
@ExperimentalAnimationApi
@ExperimentalFoundationApi
@ -64,7 +63,7 @@ class MainActivity : ComponentActivity() {
val intentVideoId = intent?.data?.getQueryParameter("v")
setContent {
val preferences = rememberPreferences()
val preferences by rememberPreferences(dataStore)
val systemUiController = rememberSystemUiController()
val isDarkTheme = when (preferences.colorPaletteMode) {
@ -121,10 +120,9 @@ class MainActivity : ComponentActivity() {
LocalColorPalette provides colorPalette,
LocalShimmerTheme provides shimmerTheme,
LocalTypography provides rememberTypography(colorPalette.text),
LocalYoutubePlayer provides rememberYoutubePlayer(
mediaControllerFuture,
preferences.repeatMode
),
LocalYoutubePlayer provides rememberYoutubePlayer(mediaControllerFuture) {
it.repeatMode = preferences.repeatMode
},
LocalMenuState provides rememberMenuState(),
LocalHapticFeedback provides rememberHapticFeedback()
) {
@ -133,7 +131,6 @@ class MainActivity : ComponentActivity() {
.fillMaxSize()
.background(LocalColorPalette.current.background)
) {
// SettingsScreen()
HomeScreen(intentVideoId = intentVideoId)
BottomSheetMenu(
@ -151,3 +148,4 @@ class MainActivity : ComponentActivity() {
super.onDestroy()
}
}

View file

@ -270,6 +270,8 @@ fun HomeScreen(intentVideoId: String?) {
}
}
if (!preferences.isReady) return@LazyColumn
item {
Row(
verticalAlignment = Alignment.CenterVertically,
@ -302,9 +304,7 @@ fun HomeScreen(intentVideoId: String?) {
modifier = Modifier
.clickable {
val values = SongCollection.values()
preferences.homePageSongCollection =
values[(preferences.homePageSongCollection.ordinal + 1) % values.size]
preferences.onHomePageSongCollectionChange(values[(preferences.homePageSongCollection.ordinal + 1) % values.size])
}
.padding(horizontal = 8.dp, vertical = 8.dp)
.size(16.dp)

View file

@ -51,7 +51,6 @@ fun LocalPlaylistScreen(
Database.playlistWithSongs(playlistId).map { it ?: PlaylistWithSongs.NotFound }
}.collectAsState(initial = PlaylistWithSongs.Empty, context = Dispatchers.IO)
val lazyListState = rememberLazyListState()
val albumRoute = rememberAlbumRoute()

View file

@ -183,9 +183,7 @@ fun SearchResultScreen(
selectedTextStyle = typography.xs.medium.color(colorPalette.onPrimaryContainer),
unselectedTextStyle = typography.xs.medium,
shape = RoundedCornerShape(36.dp),
onValueChanged = { filter ->
preferences.searchFilter = filter
},
onValueChanged = preferences.onSearchFilterChange,
modifier = Modifier
.padding(vertical = 8.dp)
.padding(horizontal = 16.dp)

View file

@ -112,17 +112,13 @@ fun SettingsScreen() {
EnumValueSelectorEntry(
title = "Theme mode",
selectedValue = preferences.colorPaletteMode,
onValueSelected = {
preferences.colorPaletteMode = it
}
onValueSelected = preferences.onColorPaletteModeChange
)
EnumValueSelectorEntry(
title = "Thumbnail roundness",
selectedValue = preferences.thumbnailRoundness,
onValueSelected = {
preferences.thumbnailRoundness = it
}
onValueSelected = preferences.onThumbnailRoundnessChange
)
}
}

View file

@ -449,7 +449,8 @@ fun PlayerView(
.clickable {
player.mediaController.repeatMode =
(player.mediaController.repeatMode + 2) % 3
preferences.repeatMode = player.mediaController.repeatMode
preferences.onRepeatModeChange(player.mediaController.repeatMode)
}
.padding(horizontal = 16.dp)
.size(28.dp)

View file

@ -110,12 +110,10 @@ val LocalYoutubePlayer = compositionLocalOf<YoutubePlayer?> { null }
@Composable
fun rememberYoutubePlayer(
mediaControllerFuture: ListenableFuture<MediaController>,
repeatMode: Int
block: (MediaController) -> Unit,
): YoutubePlayer? {
val mediaController by produceState<MediaController?>(initialValue = null) {
value = mediaControllerFuture.await().also {
it.repeatMode = repeatMode
}
value = mediaControllerFuture.await().also(block)
}
val playerState = remember(mediaController) {

View file

@ -32,6 +32,8 @@ dependencyResolutionManagement {
alias("accompanist-systemuicontroller").to("com.google.accompanist", "accompanist-systemuicontroller").versionRef("accompanist")
alias("accompanist-flowlayout").to("com.google.accompanist", "accompanist-flowlayout").versionRef("accompanist")
alias("datastore").to("androidx.datastore", "datastore-preferences").version("1.0.0")
version("room", "2.5.0-alpha01")
alias("room").to("androidx.room", "room-ktx").versionRef("room")
alias("room-compiler").to("androidx.room", "room-compiler").versionRef("room")