Preference DataStore | Jetpack Compose

MainActivity.kt

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.Button
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.neco_desarrollo.datastorelesson.ui.theme.Blue
import com.neco_desarrollo.datastorelesson.ui.theme.DataStoreLessonTheme
import com.neco_desarrollo.datastorelesson.ui.theme.Green
import com.neco_desarrollo.datastorelesson.ui.theme.Red
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val dataStoreManager = DataStoreManager(this)
setContent {
DataStoreLessonTheme {
val bgColorState = remember {
mutableStateOf(Red.value)
}
val textSizeState = remember {
mutableStateOf(40)
}
LaunchedEffect(key1 = true){
dataStoreManager.getSettings().collect{ settings ->
bgColorState.value = settings.bgColor.toULong()
textSizeState.value = settings.textSize
}
}
Surface(
modifier = Modifier.fillMaxSize(),
color = Color(bgColorState.value)
) {
MainScreen(dataStoreManager, textSizeState)
}
}
}
}
}

@Composable
fun MainScreen(
dataStoreManager: DataStoreManager,
textSizeState: MutableState<Int>
) {
val coroutine = rememberCoroutineScope()

Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Box(
modifier = Modifier
.fillMaxSize(0.5f)
.wrapContentWidth(align = Alignment.CenterHorizontally)
.wrapContentHeight(align = Alignment.CenterVertically)
){
Text(
text = "Some Text",
color = Color.White,
fontSize = textSizeState.value.sp
)
}
Button(onClick = {
coroutine.launch {
dataStoreManager.saveSettings(
SettingsData(
10,
Blue.value.toLong()
)
)
}
}) {
Text(text = "Blue")
}
Spacer(modifier = Modifier.height(10.dp))
Button(onClick = {
coroutine.launch {
dataStoreManager.saveSettings(
SettingsData(
30,
Red.value.toLong()
)
)
}
}) {
Text(text = "Red")
}
Spacer(modifier = Modifier.height(10.dp))
Button(onClick = {
coroutine.launch {
dataStoreManager.saveSettings(
SettingsData(
50,
Green.value.toLong()
)
)
}
}) {
Text(text = "Green")
}
Spacer(modifier = Modifier.height(10.dp))
}
}

DataStoreManager.kt

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.intPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.neco_desarrollo.datastorelesson.ui.theme.Red
import kotlinx.coroutines.flow.map

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore("data_store")

class DataStoreManager(val context: Context) {

suspend fun saveSettings(settingsData: SettingsData){
context.dataStore.edit { pref ->
pref[intPreferencesKey("text_size")] = settingsData.textSize
pref[longPreferencesKey("bg_color")] = settingsData.bgColor
}
}

fun getSettings() = context.dataStore.data.map { pref ->
return@map SettingsData(
pref[intPreferencesKey("text_size")] ?: 40,
pref[longPreferencesKey("bg_color")] ?: Red.value.toLong()
)
}
}

Color.kt


import androidx.compose.ui.graphics.Color

val Purple80 = Color(0xFFD0BCFF)
val PurpleGrey80 = Color(0xFFCCC2DC)
val Pink80 = Color(0xFFEFB8C8)

val Purple40 = Color(0xFF6650a4)
val PurpleGrey40 = Color(0xFF625b71)
val Pink40 = Color(0xFF7D5260)

val Red = Color(0xFFCF1656)
val Green = Color(0xFF068D4F)
val Blue = Color(0xFF08329B)

SettingsData.kt

data class SettingsData(
val textSize: Int,
val bgColor: Long
)

DataStore зависимость:

implementation 'androidx.datastore:datastore-preferences:1.0.0'

Добавить комментарий

Ваш адрес email не будет опубликован.