Proto 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.collectAsState
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 = ProtoDataStoreManager(this)
setContent {
DataStoreLessonTheme {
val settingState = dataStoreManager.getSettings().collectAsState(initial = SettingsData())

Surface(
modifier = Modifier.fillMaxSize(),
color = Color(settingState.value.bgColor.toULong())
) {
MainScreen(dataStoreManager, settingState.value.textSize)
}
}
}
}
}

@Composable
fun MainScreen(
dataStoreManager: ProtoDataStoreManager,
textSize: 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 = textSize.sp
)
}
Button(onClick = {
coroutine.launch {
dataStoreManager.saveSettings(
SettingsData(
10,
Blue.value
)
)
}
}) {
Text(text = "Blue")
}
Spacer(modifier = Modifier.height(10.dp))
Button(onClick = {
coroutine.launch {
dataStoreManager.saveSettings(
SettingsData(
30,
Red.value
)
)
}
}) {
Text(text = "Red")
}
Spacer(modifier = Modifier.height(10.dp))
Button(onClick = {
coroutine.launch {
dataStoreManager.saveSettings(
SettingsData(
50,
Green.value
)
)
}
}) {
Text(text = "Green")
}
Spacer(modifier = Modifier.height(10.dp))
}
}

ProtoDataStoreManger.kt

import android.content.Context
import androidx.datastore.dataStore

private val Context.protoDataStore by dataStore("settings.json", SettingsSerializer)
class ProtoDataStoreManager(val context: Context) {

suspend fun saveColor(color: ULong){
context.protoDataStore.updateData { data ->
data.copy(bgColor = color)
}
}

suspend fun saveTextSize(size: Int){
context.protoDataStore.updateData { data ->
data.copy(textSize = size)
}
}

suspend fun saveSettings(settingsData: SettingsData){
context.protoDataStore.updateData {
settingsData
}
}

fun getSettings() = context.protoDataStore.data
}

SettingsData.kt

import com.neco_desarrollo.datastorelesson.ui.theme.Red
import kotlinx.serialization.Serializable

@Serializable
data class SettingsData(
val textSize: Int = 40,
val bgColor: ULong = Red.value
)

SettingsSerializer.kt

import androidx.datastore.core.Serializer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.SerializationException
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import java.io.InputStream
import java.io.OutputStream

object SettingsSerializer : Serializer<SettingsData> {
override val defaultValue: SettingsData
get() = SettingsData()

override suspend fun readFrom(input: InputStream): SettingsData {
return try {
Json.decodeFromString(
deserializer = SettingsData.serializer(),
string = input.readBytes().decodeToString()
)
} catch (e: SerializationException){
e.printStackTrace()
SettingsData()
}
}

override suspend fun writeTo(t: SettingsData, output: OutputStream) {
withContext(Dispatchers.IO) {
output.write(
Json.encodeToString(
serializer = SettingsData.serializer(),
value = t
).encodeToByteArray()
)
}
}
}

зависимость в build.gradle:

implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2'
implementation 'androidx.datastore:datastore:1.0.0'

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

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