-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from The-Streamliners/dev
new : Base(handleException public), ComposeAndroid(TitleBar color customization), Pickers(Media - bugFix CameraCrop), Helpers(Base64, CustomNavArgUtil, DataStoreUtil))
- Loading branch information
Showing
8 changed files
with
120 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
helpers/src/main/java/com/streamliners/helpers/Base64Util.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.streamliners.helpers | ||
|
||
import android.util.Base64 | ||
|
||
fun String.encodeToBase64(): String { | ||
return String( | ||
Base64.encode(toByteArray(), 0) | ||
) | ||
} | ||
|
||
fun String.decodeFromBase64(): String { | ||
return String( | ||
Base64.decode(this, 0) | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
helpers/src/main/java/com/streamliners/helpers/CustomNavArgUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.streamliners.helpers | ||
|
||
import com.google.gson.Gson | ||
|
||
fun encodeArg(arg: Any): String { | ||
return Gson().toJson(arg).encodeToBase64() | ||
} | ||
|
||
inline fun <reified T> decodeArg(arg: String): T { | ||
return Gson().fromJson( | ||
arg.decodeFromBase64(), | ||
T::class.java | ||
) | ||
} |
68 changes: 68 additions & 0 deletions
68
helpers/src/main/java/com/streamliners/helpers/DataStoreUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.streamliners.helpers | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import com.google.gson.Gson | ||
import kotlinx.coroutines.flow.first | ||
import java.io.File | ||
|
||
class DataStoreUtil( | ||
private val dataStore: DataStore<Preferences> | ||
) { | ||
|
||
companion object { | ||
fun create(context: Context): DataStoreUtil { | ||
val datastore = PreferenceDataStoreFactory.create { | ||
File(context.filesDir, "datastore/App.preferences_pb") | ||
} | ||
return DataStoreUtil( | ||
datastore | ||
) | ||
} | ||
} | ||
|
||
|
||
/* ----------------- * Normal get/set * ----------------- */ | ||
|
||
suspend inline fun <reified T> getData(key: String): T? { | ||
val str = getSerializedData(key) | ||
?: return null | ||
return Gson().fromJson(str, T::class.java) | ||
} | ||
|
||
suspend inline fun <reified T> setData(key: String, value: T) { | ||
setSerializedData(key, Gson().toJson(value)) | ||
} | ||
|
||
|
||
/* ----------------- * Read-Write from DataStore * ----------------- */ | ||
|
||
@PublishedApi | ||
internal suspend fun getSerializedData(key: String): String? { | ||
return dataStore.data.first()[stringPreferencesKey(key)] | ||
} | ||
|
||
@PublishedApi | ||
internal suspend fun setSerializedData(key: String, value: String) { | ||
dataStore.edit { | ||
it[stringPreferencesKey(key)] = value | ||
} | ||
} | ||
|
||
suspend fun clear() { | ||
dataStore.edit { | ||
it.clear() | ||
} | ||
} | ||
|
||
suspend fun removeKey(key: String) { | ||
dataStore.edit { | ||
it.remove(stringPreferencesKey(key)) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters