Migrasi DataStore

This commit is contained in:
202310715297 RAIHAN ARIQ MUZAKKI 2025-12-13 13:50:30 +07:00
parent 9fb59df8b9
commit efabb0a172
3 changed files with 28 additions and 7 deletions

View File

@ -4,7 +4,6 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
@ -38,7 +37,6 @@ import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.UUID
import androidx.compose.material.icons.outlined.Star
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
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.TextOverflow
import androidx.compose.ui.zIndex
import com.example.notesai.data.local.DataStoreManager
import kotlinx.coroutines.delay
// Data Classes

View File

@ -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 androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.example.notesai.Category
import com.example.notesai.Note
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.Serializable
@ -53,7 +57,7 @@ class DataStoreManager(private val context: Context) {
val categoriesFlow: Flow<List<Category>> = context.dataStore.data
.catch { exception ->
if (exception is IOException) {
emit(androidx.datastore.preferences.core.emptyPreferences())
emit(emptyPreferences())
} else {
throw exception
}
@ -72,7 +76,7 @@ class DataStoreManager(private val context: Context) {
val notesFlow: Flow<List<Note>> = context.dataStore.data
.catch { exception ->
if (exception is IOException) {
emit(androidx.datastore.preferences.core.emptyPreferences())
emit(emptyPreferences())
} else {
throw exception
}
@ -81,7 +85,16 @@ class DataStoreManager(private val context: Context) {
val jsonString = preferences[NOTES_KEY] ?: "[]"
try {
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) {
emptyList()

View File

@ -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")
}