-
Notifications
You must be signed in to change notification settings - Fork 20
/
UserDataStore.kt
86 lines (71 loc) 路 3.48 KB
/
UserDataStore.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.datastore
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.wire.kalium.logic.data.user.UserAvailabilityStatus
import com.wire.kalium.logic.data.user.UserId
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
class UserDataStore(private val context: Context, userId: UserId) {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "${PREFERENCES_NAME}_$userId")
suspend fun dontShowStatusRationaleAgain(status: UserAvailabilityStatus) {
context.dataStore.edit { preferences ->
preferences[getStatusKey(status)] = false
}
}
fun shouldShowStatusRationaleFlow(status: UserAvailabilityStatus): Flow<Boolean> = context.dataStore.data
.map { preferences ->
preferences[getStatusKey(status)] ?: true
}
val avatarAssetId: Flow<String?> = context.dataStore.data
.map { preferences ->
preferences[USER_AVATAR_ASSET_ID]
}
suspend fun updateUserAvatarAssetId(newAssetId: String) {
context.dataStore.edit { preferences ->
preferences[USER_AVATAR_ASSET_ID] = newAssetId
}
}
suspend fun clear() {
context.dataStore.edit { it.clear() }
}
private fun getStatusKey(status: UserAvailabilityStatus) = when (status) {
UserAvailabilityStatus.AVAILABLE -> SHOW_STATUS_RATIONALE_AVAILABLE
UserAvailabilityStatus.BUSY -> SHOW_STATUS_RATIONALE_BUSY
UserAvailabilityStatus.AWAY -> SHOW_STATUS_RATIONALE_AWAY
UserAvailabilityStatus.NONE -> SHOW_STATUS_RATIONALE_NONE
}
val initialSyncCompleted: Flow<Boolean> = context.dataStore.data.map { it[INITIAL_SYNC_COMPLETED] ?: false }
suspend fun setInitialSyncCompleted() { context.dataStore.edit { it[INITIAL_SYNC_COMPLETED] = true } }
companion object {
private const val PREFERENCES_NAME = "user_data"
// keys
private val SHOW_STATUS_RATIONALE_AVAILABLE = booleanPreferencesKey("show_status_rationale_available")
private val SHOW_STATUS_RATIONALE_BUSY = booleanPreferencesKey("show_status_rationale_busy")
private val SHOW_STATUS_RATIONALE_AWAY = booleanPreferencesKey("show_status_rationale_away")
private val SHOW_STATUS_RATIONALE_NONE = booleanPreferencesKey("show_status_rationale_none")
private val USER_AVATAR_ASSET_ID = stringPreferencesKey("user_avatar_asset_id")
private val INITIAL_SYNC_COMPLETED = booleanPreferencesKey("initial_sync_completed")
}
}