mirror of
https://github.com/anyproto/anytype-kotlin.git
synced 2025-06-08 05:47:05 +09:00
DROID-2731 Vault | Tech | Persist vault settings (#1609)
This commit is contained in:
parent
56dc92f28c
commit
c0fdc36db4
7 changed files with 121 additions and 2 deletions
|
@ -2,6 +2,7 @@ package com.anytypeio.anytype.persistence.preferences
|
|||
|
||||
import androidx.datastore.core.Serializer
|
||||
import com.anytypeio.anytype.persistence.SpacePreferences
|
||||
import com.anytypeio.anytype.persistence.VaultPreferences
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
|
@ -20,4 +21,22 @@ object SpacePrefSerializer : Serializer<SpacePreferences> {
|
|||
}
|
||||
}
|
||||
|
||||
const val SPACE_PREFERENCE_FILENAME = "space-preferences.pb"
|
||||
object VaultPrefsSerializer : Serializer<VaultPreferences> {
|
||||
override val defaultValue: VaultPreferences = VaultPreferences(
|
||||
preferences = emptyMap()
|
||||
)
|
||||
|
||||
override suspend fun readFrom(input: InputStream): VaultPreferences {
|
||||
return VaultPreferences.ADAPTER.decode(input)
|
||||
}
|
||||
|
||||
override suspend fun writeTo(t: VaultPreferences, output: OutputStream) {
|
||||
VaultPreferences.ADAPTER.encode(
|
||||
stream = output,
|
||||
value = t
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const val SPACE_PREFERENCE_FILENAME = "space-preferences.pb"
|
||||
const val VAULT_PREFERENCE_FILENAME = "vault-preferences.pb"
|
|
@ -4,6 +4,7 @@ import android.content.Context
|
|||
import android.content.SharedPreferences
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.dataStore
|
||||
import com.anytypeio.anytype.core_models.Account
|
||||
import com.anytypeio.anytype.core_models.GlobalSearchHistory
|
||||
import com.anytypeio.anytype.core_models.Id
|
||||
import com.anytypeio.anytype.core_models.NO_VALUE
|
||||
|
@ -12,10 +13,13 @@ import com.anytypeio.anytype.core_models.Wallpaper
|
|||
import com.anytypeio.anytype.core_models.WidgetSession
|
||||
import com.anytypeio.anytype.core_models.primitives.SpaceId
|
||||
import com.anytypeio.anytype.core_models.primitives.TypeId
|
||||
import com.anytypeio.anytype.core_models.settings.VaultSettings
|
||||
import com.anytypeio.anytype.data.auth.repo.UserSettingsCache
|
||||
import com.anytypeio.anytype.persistence.GlobalSearchHistoryProto
|
||||
import com.anytypeio.anytype.persistence.SpacePreference
|
||||
import com.anytypeio.anytype.persistence.SpacePreferences
|
||||
import com.anytypeio.anytype.persistence.VaultPreference
|
||||
import com.anytypeio.anytype.persistence.VaultPreferences
|
||||
import com.anytypeio.anytype.persistence.common.JsonString
|
||||
import com.anytypeio.anytype.persistence.common.deserializeWallpaperSettings
|
||||
import com.anytypeio.anytype.persistence.common.serializeWallpaperSettings
|
||||
|
@ -25,6 +29,8 @@ import com.anytypeio.anytype.persistence.model.asSettings
|
|||
import com.anytypeio.anytype.persistence.model.asWallpaper
|
||||
import com.anytypeio.anytype.persistence.preferences.SPACE_PREFERENCE_FILENAME
|
||||
import com.anytypeio.anytype.persistence.preferences.SpacePrefSerializer
|
||||
import com.anytypeio.anytype.persistence.preferences.VAULT_PREFERENCE_FILENAME
|
||||
import com.anytypeio.anytype.persistence.preferences.VaultPrefsSerializer
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
@ -39,6 +45,11 @@ class DefaultUserSettingsCache(
|
|||
serializer = SpacePrefSerializer
|
||||
)
|
||||
|
||||
private val Context.vaultPrefsStore: DataStore<VaultPreferences> by dataStore(
|
||||
fileName = VAULT_PREFERENCE_FILENAME,
|
||||
serializer = VaultPrefsSerializer
|
||||
)
|
||||
|
||||
override suspend fun setCurrentSpace(space: SpaceId) {
|
||||
prefs.edit()
|
||||
.putString(CURRENT_SPACE_KEY, space.id)
|
||||
|
@ -380,6 +391,49 @@ class DefaultUserSettingsCache(
|
|||
}
|
||||
}
|
||||
|
||||
override suspend fun getVaultSettings(account: Account): VaultSettings {
|
||||
return context.vaultPrefsStore
|
||||
.data
|
||||
.map { prefs ->
|
||||
VaultSettings(
|
||||
orderOfSpaces = prefs.preferences.getOrDefault(
|
||||
key = account.id,
|
||||
defaultValue = VaultPreference()
|
||||
).orderOfSpaces
|
||||
)
|
||||
}
|
||||
.first()
|
||||
}
|
||||
|
||||
override suspend fun observeVaultSettings(account: Account): Flow<VaultSettings> {
|
||||
return context.vaultPrefsStore
|
||||
.data
|
||||
.map { prefs ->
|
||||
VaultSettings(
|
||||
orderOfSpaces = prefs.preferences.getOrDefault(
|
||||
key = account.id,
|
||||
defaultValue = VaultPreference()
|
||||
).orderOfSpaces
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun setVaultSpaceOrder(account: Account, order: List<Id>) {
|
||||
context.vaultPrefsStore.updateData { existingPreferences ->
|
||||
val curr = existingPreferences.preferences.getOrDefault(
|
||||
key = account.id,
|
||||
defaultValue = VaultPreference()
|
||||
)
|
||||
existingPreferences.copy(
|
||||
preferences = existingPreferences.preferences + mapOf(
|
||||
account.id to curr.copy(
|
||||
orderOfSpaces = order
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val CURRENT_SPACE_KEY = "prefs.user_settings.current_space"
|
||||
const val DEFAULT_OBJECT_TYPE_ID_KEY = "prefs.user_settings.default_object_type.id"
|
||||
|
|
|
@ -8,6 +8,15 @@ message SpacePreferences {
|
|||
map <string, SpacePreference> preferences = 1;
|
||||
}
|
||||
|
||||
message VaultPreferences {
|
||||
// maps account id to vault preference
|
||||
map <string, VaultPreference> preferences = 1;
|
||||
}
|
||||
|
||||
message VaultPreference {
|
||||
repeated string orderOfSpaces = 1;
|
||||
}
|
||||
|
||||
message SpacePreference {
|
||||
optional string defaultObjectTypeKey = 1;
|
||||
repeated string pinnedObjectTypeIds = 2;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue