Migrasi DataStore
This commit is contained in:
parent
9fb59df8b9
commit
efabb0a172
@ -4,7 +4,6 @@ import android.os.Bundle
|
|||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.compose.animation.*
|
import androidx.compose.animation.*
|
||||||
import androidx.compose.animation.core.*
|
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
@ -38,7 +37,6 @@ import java.text.SimpleDateFormat
|
|||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import androidx.compose.material.icons.outlined.Star
|
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
import androidx.compose.material3.HorizontalDivider as Divider
|
import androidx.compose.material3.HorizontalDivider as Divider
|
||||||
@ -56,6 +54,7 @@ import androidx.compose.ui.platform.LocalContext
|
|||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
import androidx.compose.ui.text.style.TextOverflow
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
import androidx.compose.ui.zIndex
|
import androidx.compose.ui.zIndex
|
||||||
|
import com.example.notesai.data.local.DataStoreManager
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
|
||||||
// Data Classes
|
// Data Classes
|
||||||
|
|||||||
@ -1,16 +1,20 @@
|
|||||||
@file:OptIn(kotlinx.serialization.InternalSerializationApi::class)
|
@file:OptIn(InternalSerializationApi::class)
|
||||||
|
|
||||||
package com.example.notesai
|
package com.example.notesai.data.local
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.datastore.core.DataStore
|
import androidx.datastore.core.DataStore
|
||||||
import androidx.datastore.preferences.core.Preferences
|
import androidx.datastore.preferences.core.Preferences
|
||||||
import androidx.datastore.preferences.core.edit
|
import androidx.datastore.preferences.core.edit
|
||||||
|
import androidx.datastore.preferences.core.emptyPreferences
|
||||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
import androidx.datastore.preferences.preferencesDataStore
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
|
import com.example.notesai.Category
|
||||||
|
import com.example.notesai.Note
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.catch
|
import kotlinx.coroutines.flow.catch
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.serialization.InternalSerializationApi
|
||||||
import kotlinx.serialization.encodeToString
|
import kotlinx.serialization.encodeToString
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
@ -53,7 +57,7 @@ class DataStoreManager(private val context: Context) {
|
|||||||
val categoriesFlow: Flow<List<Category>> = context.dataStore.data
|
val categoriesFlow: Flow<List<Category>> = context.dataStore.data
|
||||||
.catch { exception ->
|
.catch { exception ->
|
||||||
if (exception is IOException) {
|
if (exception is IOException) {
|
||||||
emit(androidx.datastore.preferences.core.emptyPreferences())
|
emit(emptyPreferences())
|
||||||
} else {
|
} else {
|
||||||
throw exception
|
throw exception
|
||||||
}
|
}
|
||||||
@ -72,7 +76,7 @@ class DataStoreManager(private val context: Context) {
|
|||||||
val notesFlow: Flow<List<Note>> = context.dataStore.data
|
val notesFlow: Flow<List<Note>> = context.dataStore.data
|
||||||
.catch { exception ->
|
.catch { exception ->
|
||||||
if (exception is IOException) {
|
if (exception is IOException) {
|
||||||
emit(androidx.datastore.preferences.core.emptyPreferences())
|
emit(emptyPreferences())
|
||||||
} else {
|
} else {
|
||||||
throw exception
|
throw exception
|
||||||
}
|
}
|
||||||
@ -81,7 +85,16 @@ class DataStoreManager(private val context: Context) {
|
|||||||
val jsonString = preferences[NOTES_KEY] ?: "[]"
|
val jsonString = preferences[NOTES_KEY] ?: "[]"
|
||||||
try {
|
try {
|
||||||
json.decodeFromString<List<SerializableNote>>(jsonString).map {
|
json.decodeFromString<List<SerializableNote>>(jsonString).map {
|
||||||
Note(it.id, it.categoryId, it.title, it.content, it.timestamp, it.isArchived, it.isDeleted, it.isPinned)
|
Note(
|
||||||
|
it.id,
|
||||||
|
it.categoryId,
|
||||||
|
it.title,
|
||||||
|
it.content,
|
||||||
|
it.timestamp,
|
||||||
|
it.isArchived,
|
||||||
|
it.isDeleted,
|
||||||
|
it.isPinned
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
emptyList()
|
emptyList()
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
// File: data/local/PreferencesKeys.kt
|
||||||
|
package com.example.notesai.data.local
|
||||||
|
|
||||||
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
|
|
||||||
|
object PreferencesKeys {
|
||||||
|
val CATEGORIES_KEY = stringPreferencesKey("categories")
|
||||||
|
val NOTES_KEY = stringPreferencesKey("notes")
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user