11package com.whereismymotivation.data.local.prefs
22
3- import android.content.SharedPreferences
3+ import androidx.datastore.core.DataStore
4+ import androidx.datastore.preferences.core.Preferences
5+ import androidx.datastore.preferences.core.booleanPreferencesKey
6+ import androidx.datastore.preferences.core.edit
7+ import androidx.datastore.preferences.core.stringPreferencesKey
8+ import kotlinx.coroutines.flow.first
9+ import kotlinx.coroutines.flow.map
410import javax.inject.Inject
511import javax.inject.Singleton
612
713@Singleton
8- class UserPreferences @Inject constructor(private val prefs : SharedPreferences ) {
14+ class UserPreferences @Inject constructor(private val dataStore : DataStore < Preferences > ) {
915
1016 companion object {
11- private const val ON_BOARDING_COMPLETED = " PREF_KEY_USER_ON_BOARDING_COMPLETED "
12- private const val USER_ID = " PREF_KEY_USER_ID "
13- private const val USER_NAME = " PREF_KEY_USER_NAME "
14- private const val USER_EMAIL = " PREF_KEY_USER_EMAIL "
15- private const val USER_PROFILE_PIC_URL = " USER_PROFILE_PIC_URL"
16- private const val ACCESS_TOKEN = " PREF_KEY_ACCESS_TOKEN "
17- private const val REFRESH_TOKEN = " PREF_KEY_REFRESH_TOKEN "
18- private const val DEVICE_ID = " PREF_KEY_DEVICE_ID "
19- private const val USER_ROLES = " PREF_KEY_USER_ROLES "
20- private const val FIREBASE_TOKEN = " FIREBASE_TOKEN"
21- private const val FIREBASE_TOKEN_SENT = " FIREBASE_TOKEN_SENT"
17+ private val ON_BOARDING_COMPLETED = booleanPreferencesKey( " ON_BOARDING_COMPLETED " )
18+ private val USER_ID = stringPreferencesKey( " USER_ID " )
19+ private val USER_NAME = stringPreferencesKey( " USER_NAME " )
20+ private val USER_EMAIL = stringPreferencesKey( " USER_EMAIL " )
21+ private val USER_PROFILE_PIC_URL = stringPreferencesKey( " USER_PROFILE_PIC_URL" )
22+ private val ACCESS_TOKEN = stringPreferencesKey( " ACCESS_TOKEN " )
23+ private val REFRESH_TOKEN = stringPreferencesKey( " REFRESH_TOKEN " )
24+ private val DEVICE_ID = stringPreferencesKey( " DEVICE_ID " )
25+ private val USER_ROLES = stringPreferencesKey( " USER_ROLES " )
26+ private val FIREBASE_TOKEN = stringPreferencesKey( " FIREBASE_TOKEN" )
27+ private val FIREBASE_TOKEN_SENT = booleanPreferencesKey( " FIREBASE_TOKEN_SENT" )
2228 }
2329
24- fun getUserId (): String? =
25- prefs.getString(USER_ID , null )
30+ suspend fun getUserId () = dataStore.data.map { it[USER_ID ] }.first()
2631
27- fun setUserId (userId : String ) =
28- prefs.edit().putString(USER_ID , userId).apply ()
32+ suspend fun setUserId (userId : String ) {
33+ dataStore.edit { it[USER_ID ] = userId }
34+ }
2935
30- fun removeUserId () =
31- prefs.edit().remove(USER_ID ).apply ()
36+ suspend fun removeUserId () {
37+ dataStore.edit { it.remove(USER_ID ) }
38+ }
3239
33- fun getUserName (): String? =
34- prefs.getString(USER_NAME , null )
40+ suspend fun getUserName () = dataStore.data.map { it[USER_NAME ] }.first()
3541
36- fun setUserName (userName : String ) =
37- prefs.edit().putString(USER_NAME , userName).apply ()
42+ suspend fun setUserName (userName : String ) {
43+ dataStore.edit { it[USER_NAME ] = userName }
44+ }
3845
39- fun removeUserName () =
40- prefs.edit().remove(USER_NAME ).apply ()
46+ suspend fun removeUserName () {
47+ dataStore.edit { it.remove(USER_NAME ) }
48+ }
4149
42- fun getUserEmail (): String? =
43- prefs.getString(USER_EMAIL , null )
50+ suspend fun getUserEmail () = dataStore.data.map { it[USER_EMAIL ] }.first()
4451
45- fun setUserEmail (email : String ) =
46- prefs.edit().putString(USER_EMAIL , email).apply ()
52+ suspend fun setUserEmail (email : String ) {
53+ dataStore.edit { it[USER_EMAIL ] = email }
54+ }
4755
48- fun removeUserEmail () =
49- prefs.edit().remove(USER_EMAIL ).apply ()
56+ suspend fun removeUserEmail () {
57+ dataStore.edit { it.remove(USER_EMAIL ) }
58+ }
5059
51- fun getUserProfilePicUrlUrl (): String? =
52- prefs.getString(USER_PROFILE_PIC_URL , null )
60+ suspend fun getUserProfilePicUrlUrl () = dataStore.data.map { it[USER_PROFILE_PIC_URL ] }.first()
5361
54- fun setUserProfileProfilePicUrl (url : String? ) =
55- prefs.edit().putString(USER_PROFILE_PIC_URL , url).apply ()
62+ suspend fun setUserProfileProfilePicUrl (url : String? ) {
63+ url?.let {
64+ dataStore.edit { it[USER_PROFILE_PIC_URL ] = url }
65+ } ? : removeUserProfilePicUrl()
66+ }
5667
57- fun removeUserProfilePicUrl () =
58- prefs.edit().remove(USER_PROFILE_PIC_URL ).apply ()
68+ suspend fun removeUserProfilePicUrl () {
69+ dataStore.edit { it.remove(USER_PROFILE_PIC_URL ) }
70+ }
5971
60- fun getAccessToken (): String? =
61- prefs.getString(ACCESS_TOKEN , null )
72+ suspend fun getAccessToken () = dataStore.data.map { it[ACCESS_TOKEN ] }.first()
6273
63- fun setAccessToken (token : String ) =
64- prefs.edit().putString(ACCESS_TOKEN , token).apply ()
74+ suspend fun setAccessToken (token : String ) {
75+ dataStore.edit { it[ACCESS_TOKEN ] = token }
76+ }
6577
66- fun removeAccessToken () =
67- prefs.edit().remove(ACCESS_TOKEN ).apply ()
78+ suspend fun removeAccessToken () {
79+ dataStore.edit { it.remove(ACCESS_TOKEN ) }
80+ }
6881
69- fun getRefreshToken (): String? =
70- prefs.getString(REFRESH_TOKEN , null )
82+ suspend fun getRefreshToken () = dataStore.data.map { it[REFRESH_TOKEN ] }.first()
7183
72- fun setRefreshToken (token : String ) =
73- prefs.edit().putString(REFRESH_TOKEN , token).apply ()
84+ suspend fun setRefreshToken (token : String ) {
85+ dataStore.edit { it[REFRESH_TOKEN ] = token }
86+ }
7487
75- fun removeRefreshToken () =
76- prefs.edit().remove(REFRESH_TOKEN ).apply ()
88+ suspend fun removeRefreshToken () {
89+ dataStore.edit { it.remove(REFRESH_TOKEN ) }
90+ }
7791
78- fun getOnBoardingComplete (): Boolean =
79- prefs.getBoolean( ON_BOARDING_COMPLETED , false )
92+ suspend fun getOnBoardingComplete () =
93+ dataStore.data.map { it[ ON_BOARDING_COMPLETED ] }.first() ? : false
8094
81- fun setOnBoardingComplete (complete : Boolean ) =
82- prefs.edit().putBoolean(ON_BOARDING_COMPLETED , complete).apply ()
95+ suspend fun setOnBoardingComplete (complete : Boolean ) {
96+ dataStore.edit { it[ON_BOARDING_COMPLETED ] = complete }
97+ }
8398
84- fun removeOnBoardingComplete () =
85- prefs.edit().remove(ON_BOARDING_COMPLETED ).apply ()
99+ suspend fun removeOnBoardingComplete () {
100+ dataStore.edit { it.remove(ON_BOARDING_COMPLETED ) }
101+ }
86102
87- fun getDeviceId (): String? =
88- prefs.getString(DEVICE_ID , null )
103+ suspend fun getDeviceId () = dataStore.data.map { it[DEVICE_ID ] }.first()
89104
90- fun setDeviceId (deviceId : String ) =
91- prefs.edit().putString(DEVICE_ID , deviceId).apply ()
105+ suspend fun setDeviceId (deviceId : String ) {
106+ dataStore.edit { it[DEVICE_ID ] = deviceId }
107+ }
92108
93- fun setFirebaseToken (token : String ) =
94- prefs.edit().putString(FIREBASE_TOKEN , token).apply ()
109+ suspend fun setFirebaseToken (token : String ) {
110+ dataStore.edit { it[FIREBASE_TOKEN ] = token }
111+ }
95112
96- fun getFirebaseToken (): String? =
97- prefs.getString(FIREBASE_TOKEN , null )
113+ suspend fun getFirebaseToken () = dataStore.data.map { it[FIREBASE_TOKEN ] }.first()
98114
99- fun getFirebaseTokenSent (): Boolean =
100- prefs.getBoolean( FIREBASE_TOKEN_SENT , false )
115+ suspend fun getFirebaseTokenSent () =
116+ dataStore.data.map { it[ FIREBASE_TOKEN_SENT ] }.first() ? : false
101117
102- fun setFirebaseTokenSent () =
103- prefs.edit().putBoolean(FIREBASE_TOKEN_SENT , true ).apply ()
118+ suspend fun setFirebaseTokenSent () {
119+ dataStore.edit { it[FIREBASE_TOKEN_SENT ] = true }
120+ }
104121
105- fun removeFirebaseTokenSent () =
106- prefs.edit().remove(FIREBASE_TOKEN_SENT ).apply ()
122+ suspend fun removeFirebaseTokenSent () {
123+ dataStore.edit { it.remove(FIREBASE_TOKEN_SENT ) }
124+ }
107125
108- fun getUserRoles (): String? = prefs.getString( USER_ROLES , null )
126+ suspend fun getUserRoles () = dataStore.data.map { it[ USER_ROLES ] }.first( )
109127
110- fun setUserRoles (roles : String ) = prefs.edit().putString(USER_ROLES , roles).apply ()
128+ suspend fun setUserRoles (roles : String ) {
129+ dataStore.edit { it[USER_ROLES ] = roles }
130+ }
111131
112- fun removeUserRoles () = prefs.edit().remove(USER_ROLES ).apply ()
132+ suspend fun removeUserRoles () {
133+ dataStore.edit { it.remove(USER_ROLES ) }
134+ }
113135
114136}
0 commit comments