Skip to content

Commit dde3959

Browse files
committed
Base64, CustomNavArgUtil and DataStoreUtil added.
1 parent 43fb57f commit dde3959

4 files changed

Lines changed: 100 additions & 0 deletions

File tree

helpers/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ dependencies {
6262
testImplementation("junit:junit:4.13.2")
6363
androidTestImplementation("androidx.test.ext:junit:1.1.5")
6464
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
65+
66+
implementation("androidx.datastore:datastore-preferences-core:1.0.0")
67+
implementation("com.google.code.gson:gson:2.11.0")
6568
}
6669

6770
publishing {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.streamliners.helpers
2+
3+
import android.util.Base64
4+
5+
fun String.encodeToBase64(): String {
6+
return String(
7+
Base64.encode(toByteArray(), 0)
8+
)
9+
}
10+
11+
fun String.decodeFromBase64(): String {
12+
return String(
13+
Base64.decode(this, 0)
14+
)
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.streamliners.helpers
2+
3+
import com.google.gson.Gson
4+
5+
fun encodeArg(arg: Any): String {
6+
return Gson().toJson(arg).encodeToBase64()
7+
}
8+
9+
inline fun <reified T> decodeArg(arg: String): T {
10+
return Gson().fromJson(
11+
arg.decodeFromBase64(),
12+
T::class.java
13+
)
14+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.streamliners.helpers
2+
3+
import android.content.Context
4+
import androidx.datastore.core.DataStore
5+
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
6+
import androidx.datastore.preferences.core.Preferences
7+
import androidx.datastore.preferences.core.edit
8+
import androidx.datastore.preferences.core.stringPreferencesKey
9+
import com.google.gson.Gson
10+
import kotlinx.coroutines.flow.first
11+
import java.io.File
12+
13+
class DataStoreUtil(
14+
private val dataStore: DataStore<Preferences>
15+
) {
16+
17+
companion object {
18+
fun create(context: Context): DataStoreUtil {
19+
val datastore = PreferenceDataStoreFactory.create {
20+
File(context.filesDir, "datastore/App.preferences_pb")
21+
}
22+
return DataStoreUtil(
23+
datastore
24+
)
25+
}
26+
}
27+
28+
29+
/* ----------------- * Normal get/set * ----------------- */
30+
31+
suspend inline fun <reified T> getData(key: String): T? {
32+
val str = getSerializedData(key)
33+
?: return null
34+
return Gson().fromJson(str, T::class.java)
35+
}
36+
37+
suspend inline fun <reified T> setData(key: String, value: T) {
38+
setSerializedData(key, Gson().toJson(value))
39+
}
40+
41+
42+
/* ----------------- * Read-Write from DataStore * ----------------- */
43+
44+
@PublishedApi
45+
internal suspend fun getSerializedData(key: String): String? {
46+
return dataStore.data.first()[stringPreferencesKey(key)]
47+
}
48+
49+
@PublishedApi
50+
internal suspend fun setSerializedData(key: String, value: String) {
51+
dataStore.edit {
52+
it[stringPreferencesKey(key)] = value
53+
}
54+
}
55+
56+
suspend fun clear() {
57+
dataStore.edit {
58+
it.clear()
59+
}
60+
}
61+
62+
suspend fun removeKey(key: String) {
63+
dataStore.edit {
64+
it.remove(stringPreferencesKey(key))
65+
}
66+
}
67+
68+
}

0 commit comments

Comments
 (0)