Skip to content

Commit 491aced

Browse files
Merge pull request #9 from The-Streamliners/dev
new : Base(handleException public), ComposeAndroid(TitleBar color customization), Pickers(Media - bugFix CameraCrop), Helpers(Base64, CustomNavArgUtil, DataStoreUtil))
2 parents 7f5a121 + 1d5d178 commit 491aced

8 files changed

Lines changed: 120 additions & 12 deletions

File tree

base/src/main/java/com/streamliners/base/BaseActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ abstract class BaseActivity: FragmentActivity() {
4444
}
4545
}
4646

47-
private fun handleException(e: Throwable) {
47+
fun handleException(e: Throwable) {
4848
if (debugMode) e.printStackTrace()
4949

5050
handleUiEvent(

base/src/main/java/com/streamliners/base/BaseViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ open class BaseViewModel : ViewModel() {
2626

2727
open fun init() { }
2828

29-
private fun handleException(throwable: Throwable) {
29+
fun handleException(throwable: Throwable) {
3030
viewModelScope.launch {
3131
throwable.printStackTrace()
3232

compose-android/src/main/java/com/streamliners/compose/android/comp/appBar/TitleBar.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,31 @@ import androidx.compose.material3.MaterialTheme
1414
import androidx.compose.material3.Scaffold
1515
import androidx.compose.material3.Text
1616
import androidx.compose.material3.TopAppBar
17+
import androidx.compose.material3.TopAppBarColors
1718
import androidx.compose.material3.TopAppBarDefaults.topAppBarColors
19+
import androidx.compose.material3.contentColorFor
1820
import androidx.compose.runtime.Composable
1921
import androidx.compose.ui.Modifier
2022
import androidx.compose.ui.graphics.Color
2123

24+
@OptIn(ExperimentalMaterial3Api::class)
2225
@Composable
2326
fun TitleBarScaffold(
2427
title: String,
2528
navigationIcon: (@Composable () -> Unit)? = null,
2629
navigateUp: (() -> Unit)? = null,
30+
containerColor: Color = MaterialTheme.colorScheme.background,
31+
contentColor: Color = contentColorFor(containerColor),
2732
actions: @Composable RowScope.() -> Unit = {},
2833
content: @Composable (PaddingValues) -> Unit
2934
) {
3035
Scaffold(
3136
modifier = Modifier.fillMaxSize(),
37+
containerColor = containerColor,
38+
contentColor = contentColor,
3239
topBar = {
3340
TitleBar(
34-
title, navigationIcon, navigateUp, actions
41+
title, navigationIcon, navigateUp, actions = actions
3542
)
3643
}
3744
) { paddingValues ->
@@ -46,6 +53,12 @@ fun TitleBar(
4653
title: String,
4754
navigationIcon: (@Composable () -> Unit)? = null,
4855
navigateUp: (() -> Unit)? = null,
56+
colors: TopAppBarColors = topAppBarColors(
57+
containerColor = MaterialTheme.colorScheme.primary,
58+
navigationIconContentColor = Color.White,
59+
titleContentColor = Color.White,
60+
actionIconContentColor = Color.White
61+
),
4962
actions: @Composable RowScope.() -> Unit = {}
5063
) {
5164
TopAppBar(
@@ -64,12 +77,7 @@ fun TitleBar(
6477
}
6578
}
6679
},
67-
colors = topAppBarColors(
68-
containerColor = MaterialTheme.colorScheme.primary,
69-
navigationIconContentColor = Color.White,
70-
titleContentColor = Color.White,
71-
actionIconContentColor = Color.White
72-
),
80+
colors = colors,
7381
actions = actions
7482
)
7583
}

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+
}

pickers/src/main/java/com/streamliners/pickers/media/MediaPickerDialog.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fun MediaPickerDialog(
120120
) {
121121
FromCameraButton(
122122
modifier = Modifier.weight(1f),
123-
state, data, authority, cameraPermissionIsGranted, imageCropper
123+
state, data, authority, cameraPermissionIsGranted, imageCropper, scope
124124
)
125125

126126
FromGalleryButton(
@@ -149,13 +149,13 @@ fun FromCameraButton(
149149
data: MediaPickerDialogState.ShowMediaPicker,
150150
authority: String,
151151
cameraPermissionIsGranted: () -> Boolean,
152-
imageCropper: ImageCropper
152+
imageCropper: ImageCropper,
153+
scope: CoroutineScope
153154
) {
154155
val context = LocalContext.current
155156

156157
val filePath = remember { mutableStateOf<String?>(null) }
157158
val fileUri = remember { mutableStateOf<String?>(null) }
158-
val scope = rememberCoroutineScope()
159159

160160
val cameraLauncher = rememberLauncherForActivityResult(
161161
contract = ActivityResultContracts.StartActivityForResult(),

0 commit comments

Comments
 (0)