Skip to content

Commit 4426a94

Browse files
committed
Migrated Shared Pref to Pref Datastore
1 parent 5549516 commit 4426a94

File tree

4 files changed

+83
-66
lines changed

4 files changed

+83
-66
lines changed

app/build.gradle

+7-10
Original file line numberDiff line numberDiff line change
@@ -94,27 +94,24 @@ kapt {
9494
correctErrorTypes true
9595
}
9696

97-
hilt {
98-
enableTransformForLocalTests = true
99-
}
100-
10197
dependencies {
10298
implementation fileTree(dir: 'libs', include: ['*.jar'], exclude: [])
10399
implementation fileTree(dir: 'system_libs', include: ['*.aar', '*.jar'], exclude: [])
104100
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
105-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
101+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3'
106102
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2'
107-
implementation 'androidx.core:core-ktx:1.3.2'
108-
implementation 'androidx.appcompat:appcompat:1.2.0'
103+
implementation 'androidx.core:core-ktx:1.5.0'
104+
implementation 'androidx.appcompat:appcompat:1.3.0'
109105
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
106+
implementation "androidx.datastore:datastore-preferences:1.0.0-beta01"
110107
implementation 'com.google.android.material:material:1.3.0'
111108

112109
// Widgets library
113-
implementation 'com.github.LiteKite:Android-Widgets:0.0.6'
110+
implementation 'com.github.litekite:android-widgets:0.0.7'
114111

115112
// Activity & Fragments KTX
116-
implementation "androidx.activity:activity-ktx:1.2.2"
117-
implementation "androidx.fragment:fragment-ktx:1.3.3"
113+
implementation "androidx.activity:activity-ktx:1.2.3"
114+
implementation "androidx.fragment:fragment-ktx:1.3.4"
118115

119116
// Lifecycle KTX
120117
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"

app/src/main/kotlin/com/litekite/systemui/preference/PreferenceController.kt

+72-52
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,28 @@
1616
package com.litekite.systemui.preference
1717

1818
import android.content.Context
19-
import android.content.SharedPreferences
19+
import androidx.datastore.core.DataStore
20+
import androidx.datastore.preferences.core.Preferences
21+
import androidx.datastore.preferences.core.booleanPreferencesKey
22+
import androidx.datastore.preferences.core.edit
23+
import androidx.datastore.preferences.core.floatPreferencesKey
24+
import androidx.datastore.preferences.core.intPreferencesKey
25+
import androidx.datastore.preferences.core.longPreferencesKey
26+
import androidx.datastore.preferences.core.stringPreferencesKey
27+
import androidx.datastore.preferences.preferencesDataStore
28+
import kotlinx.coroutines.CoroutineScope
29+
import kotlinx.coroutines.Dispatchers
30+
import kotlinx.coroutines.flow.Flow
31+
import kotlinx.coroutines.flow.catch
32+
import kotlinx.coroutines.flow.flowOn
33+
import kotlinx.coroutines.flow.map
34+
import kotlinx.coroutines.launch
2035
import javax.inject.Inject
2136
import javax.inject.Singleton
2237

2338
/**
24-
* A Controller for Device Protected Shared Preferences that can be accessed even if the device
25-
* was in locked state.
39+
* A Preference DataStore Controller for Device Protected Storage
40+
* that can be accessed even if the device was in locked state.
2641
*
2742
* These preferences are stored in /data/user[release-version] or in /data/user-de[debug-version]
2843
*
@@ -32,79 +47,84 @@ import javax.inject.Singleton
3247
*/
3348
@Suppress("UNUSED")
3449
@Singleton
35-
class PreferenceController @Inject constructor(private val context: Context) {
50+
class PreferenceController @Inject constructor(context: Context) {
3651

3752
companion object {
3853
const val PREFERENCES_SYSTEM_UI = "preferences_system_ui"
3954
}
4055

41-
private val preferences = getPreferences()
42-
private val editor = getEditor()
56+
private val protectedContext = context.createDeviceProtectedStorageContext()
4357

44-
fun getBoolean(key: String): Boolean {
45-
return preferences.getBoolean(key, false)
46-
}
58+
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(
59+
PREFERENCES_SYSTEM_UI
60+
)
4761

48-
fun getInt(key: String): Int {
49-
return preferences.getInt(key, 0)
50-
}
62+
private val scope = CoroutineScope(Dispatchers.IO)
5163

52-
fun getLong(key: String): Long {
53-
return preferences.getLong(key, 0)
64+
fun getBoolean(key: String): Flow<Boolean> = protectedContext.dataStore.data.map { pref ->
65+
pref[booleanPreferencesKey(key)] ?: false
66+
}.flowOn(
67+
Dispatchers.IO
68+
).catch {
69+
it.printStackTrace()
5470
}
5571

56-
fun getFloat(key: String): Float {
57-
return preferences.getFloat(key, 0F)
72+
fun getInt(key: String): Flow<Int> = protectedContext.dataStore.data.map { pref ->
73+
pref[intPreferencesKey(key)] ?: 0
74+
}.flowOn(
75+
Dispatchers.IO
76+
).catch {
77+
it.printStackTrace()
5878
}
5979

60-
fun getDouble(key: String): Double {
61-
return java.lang.Double.longBitsToDouble(
62-
preferences.getLong(key, 0)
63-
)
80+
fun getLong(key: String): Flow<Long> = protectedContext.dataStore.data.map { pref ->
81+
pref[longPreferencesKey(key)] ?: 0L
82+
}.flowOn(
83+
Dispatchers.IO
84+
).catch {
85+
it.printStackTrace()
6486
}
6587

66-
fun getString(key: String): String {
67-
return preferences.getString(key, "") ?: ""
88+
fun getFloat(key: String): Flow<Float> = protectedContext.dataStore.data.map { pref ->
89+
pref[floatPreferencesKey(key)] ?: 0F
90+
}.flowOn(
91+
Dispatchers.IO
92+
).catch {
93+
it.printStackTrace()
6894
}
6995

70-
fun store(key: String, value: Boolean) {
71-
editor.putBoolean(key, value).apply()
96+
fun getDouble(key: String): Flow<Double> = protectedContext.dataStore.data.map { pref ->
97+
java.lang.Double.longBitsToDouble(pref[longPreferencesKey(key)] ?: 0)
98+
}.flowOn(
99+
Dispatchers.IO
100+
).catch {
101+
it.printStackTrace()
72102
}
73103

74-
fun store(key: String, value: Int) {
75-
editor.putInt(key, value).apply()
104+
fun getString(key: String): Flow<String> = protectedContext.dataStore.data.map { pref ->
105+
pref[stringPreferencesKey(key)] ?: ""
106+
}.flowOn(
107+
Dispatchers.IO
108+
).catch {
109+
it.printStackTrace()
76110
}
77111

78-
fun store(key: String, value: Long) {
79-
editor.putLong(
80-
key,
81-
value
82-
).apply()
83-
}
112+
fun store(key: String, value: Boolean) = set(booleanPreferencesKey(key), value)
84113

85-
fun store(key: String, value: Float) {
86-
editor.putFloat(key, value).apply()
87-
}
114+
fun store(key: String, value: Int) = set(intPreferencesKey(key), value)
88115

89-
fun store(key: String, value: Double) {
90-
editor.putLong(
91-
key,
92-
java.lang.Double.doubleToRawLongBits((value))
93-
).apply()
94-
}
116+
fun store(key: String, value: Long) = set(longPreferencesKey(key), value)
95117

96-
fun store(key: String, value: String) {
97-
editor.putString(key, value).apply()
98-
}
118+
fun store(key: String, value: Float) = set(floatPreferencesKey(key), value)
99119

100-
private fun getEditor(): SharedPreferences.Editor {
101-
return preferences.edit()
102-
}
120+
fun store(key: String, value: Double) = set(
121+
longPreferencesKey(key),
122+
java.lang.Double.doubleToRawLongBits((value))
123+
)
124+
125+
fun store(key: String, value: String) = set(stringPreferencesKey(key), value)
103126

104-
private fun getPreferences(): SharedPreferences {
105-
return context.createDeviceProtectedStorageContext().getSharedPreferences(
106-
PREFERENCES_SYSTEM_UI,
107-
Context.MODE_PRIVATE
108-
)
127+
private fun <T> set(prefKey: Preferences.Key<T>, value: T) = scope.launch {
128+
protectedContext.dataStore.edit { pref -> pref[prefKey] = value }
109129
}
110130
}

build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
// Top-level build file where you can add configuration options common to all sub-projects/modules.
1818

1919
buildscript {
20-
ext.kotlin_version = "1.5.0"
21-
ext.hilt_version = "2.35.1"
20+
ext.kotlin_version = "1.5.10"
21+
ext.hilt_version = "2.36"
2222
ext.room_version = "2.3.0"
2323
ext.lifecycle_version = "2.3.1"
2424
repositories {
@@ -27,7 +27,7 @@ buildscript {
2727
maven { url 'https://jitpack.io' }
2828
}
2929
dependencies {
30-
classpath 'com.android.tools.build:gradle:4.2.0'
30+
classpath 'com.android.tools.build:gradle:4.2.1'
3131
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
3232
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
3333
// NOTE: Do not place your application dependencies here; they belong

gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip

0 commit comments

Comments
 (0)