Skip to content

Commit

Permalink
Merge pull request #9 from The-Streamliners/dev
Browse files Browse the repository at this point in the history
new : Base(handleException public), ComposeAndroid(TitleBar color customization), Pickers(Media - bugFix CameraCrop), Helpers(Base64, CustomNavArgUtil, DataStoreUtil))
  • Loading branch information
LavishSwarnkar authored Nov 21, 2024
2 parents 7f5a121 + 1d5d178 commit 491aced
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 12 deletions.
2 changes: 1 addition & 1 deletion base/src/main/java/com/streamliners/base/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class BaseActivity: FragmentActivity() {
}
}

private fun handleException(e: Throwable) {
fun handleException(e: Throwable) {
if (debugMode) e.printStackTrace()

handleUiEvent(
Expand Down
2 changes: 1 addition & 1 deletion base/src/main/java/com/streamliners/base/BaseViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open class BaseViewModel : ViewModel() {

open fun init() { }

private fun handleException(throwable: Throwable) {
fun handleException(throwable: Throwable) {
viewModelScope.launch {
throwable.printStackTrace()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,31 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarColors
import androidx.compose.material3.TopAppBarDefaults.topAppBarColors
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TitleBarScaffold(
title: String,
navigationIcon: (@Composable () -> Unit)? = null,
navigateUp: (() -> Unit)? = null,
containerColor: Color = MaterialTheme.colorScheme.background,
contentColor: Color = contentColorFor(containerColor),
actions: @Composable RowScope.() -> Unit = {},
content: @Composable (PaddingValues) -> Unit
) {
Scaffold(
modifier = Modifier.fillMaxSize(),
containerColor = containerColor,
contentColor = contentColor,
topBar = {
TitleBar(
title, navigationIcon, navigateUp, actions
title, navigationIcon, navigateUp, actions = actions
)
}
) { paddingValues ->
Expand All @@ -46,6 +53,12 @@ fun TitleBar(
title: String,
navigationIcon: (@Composable () -> Unit)? = null,
navigateUp: (() -> Unit)? = null,
colors: TopAppBarColors = topAppBarColors(
containerColor = MaterialTheme.colorScheme.primary,
navigationIconContentColor = Color.White,
titleContentColor = Color.White,
actionIconContentColor = Color.White
),
actions: @Composable RowScope.() -> Unit = {}
) {
TopAppBar(
Expand All @@ -64,12 +77,7 @@ fun TitleBar(
}
}
},
colors = topAppBarColors(
containerColor = MaterialTheme.colorScheme.primary,
navigationIconContentColor = Color.White,
titleContentColor = Color.White,
actionIconContentColor = Color.White
),
colors = colors,
actions = actions
)
}
3 changes: 3 additions & 0 deletions helpers/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

implementation("androidx.datastore:datastore-preferences-core:1.0.0")
implementation("com.google.code.gson:gson:2.11.0")
}

publishing {
Expand Down
15 changes: 15 additions & 0 deletions helpers/src/main/java/com/streamliners/helpers/Base64Util.kt
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 helpers/src/main/java/com/streamliners/helpers/CustomNavArgUtil.kt
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 helpers/src/main/java/com/streamliners/helpers/DataStoreUtil.kt
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))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fun MediaPickerDialog(
) {
FromCameraButton(
modifier = Modifier.weight(1f),
state, data, authority, cameraPermissionIsGranted, imageCropper
state, data, authority, cameraPermissionIsGranted, imageCropper, scope
)

FromGalleryButton(
Expand Down Expand Up @@ -149,13 +149,13 @@ fun FromCameraButton(
data: MediaPickerDialogState.ShowMediaPicker,
authority: String,
cameraPermissionIsGranted: () -> Boolean,
imageCropper: ImageCropper
imageCropper: ImageCropper,
scope: CoroutineScope
) {
val context = LocalContext.current

val filePath = remember { mutableStateOf<String?>(null) }
val fileUri = remember { mutableStateOf<String?>(null) }
val scope = rememberCoroutineScope()

val cameraLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult(),
Expand Down

0 comments on commit 491aced

Please sign in to comment.