Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmp-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/

import org.mifos.MifosBuildType
import org.mifos.dynamicVersion

Expand Down Expand Up @@ -154,4 +155,4 @@ dependencyGuard {
modules = true
tree = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ private val SCRIM_COLOR: Int = Color.TRANSPARENT
* [here](https://github.com/android/nowinandroid/blob/689ef92e41427ab70f82e2c9fe59755441deae92/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt#L94).
*/
@Suppress("MaxLineLength")
fun ComponentActivity.setupEdgeToEdge(
appThemeFlow: Flow<DarkThemeConfig>,
) {
fun ComponentActivity.setupEdgeToEdge(appThemeFlow: Flow<DarkThemeConfig>) {
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(state = Lifecycle.State.STARTED) {
combine(
Expand All @@ -55,12 +53,13 @@ fun ComponentActivity.setupEdgeToEdge(
// This handles all the settings to go edge-to-edge. We are using a transparent
// scrim for system bars and switching between "light" and "dark" based on the
// system and internal app theme settings.
val style = SystemBarStyle.auto(
darkScrim = SCRIM_COLOR,
lightScrim = SCRIM_COLOR,
// Disabling Dark Mode for this app
detectDarkMode = { false },
)
val style =
SystemBarStyle.auto(
darkScrim = SCRIM_COLOR,
lightScrim = SCRIM_COLOR,
// Disabling Dark Mode for this app
detectDarkMode = { false },
)
enableEdgeToEdge(statusBarStyle = style, navigationBarStyle = style)
}
}
Expand All @@ -75,9 +74,10 @@ fun ComponentActivity.setupEdgeToEdge(
private fun ComponentActivity.isSystemInDarkModeFlow(): Flow<Boolean> =
callbackFlow {
channel.trySend(element = resources.configuration.isSystemInDarkMode)
val listener = Consumer<Configuration> {
channel.trySend(element = it.isSystemInDarkMode)
}
val listener =
Consumer<Configuration> {
channel.trySend(element = it.isSystemInDarkMode)
}
addOnConfigurationChangedListener(listener = listener)
awaitClose { removeOnConfigurationChangedListener(listener = listener) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ fun MifosBreadcrumbNavBar(
isActive = index == routes.lastIndex,
onClick = {
if (index != routes.lastIndex && route != "...") {
navController.previousBackStackEntry
?.savedStateHandle
?.set("shouldRefresh", true)
navController.popBackStack(route, inclusive = false)
}
},
Expand All @@ -93,10 +96,13 @@ fun MifosBreadcrumbNavBar(
}
}

IconButton(
onClick = { navController.popBackStack() },
modifier = Modifier.size(DesignToken.sizes.iconMedium),
) {

IconButton(onClick = {
navController.previousBackStackEntry
?.savedStateHandle
?.set("shouldRefresh", true)
navController.popBackStack()
}) {
Icon(
painter = painterResource(Res.drawable.bread_crumb_back_icon),
contentDescription = "Back",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -88,6 +90,20 @@ internal fun ClientProfileDetailsScreen(
viewModel: ClientProfileDetailsViewModel = koinViewModel(),
) {
val state by viewModel.stateFlow.collectAsStateWithLifecycle()
val currentBackStackEntry = navController.currentBackStackEntry
val savedStateHandle = currentBackStackEntry?.savedStateHandle

val profileUpdated by savedStateHandle
?.getStateFlow("shouldRefresh", false)
?.collectAsStateWithLifecycle(initialValue = false)
?: remember { mutableStateOf(false) }

LaunchedEffect(profileUpdated) {
if (profileUpdated) {
viewModel.trySendAction(ClientProfileDetailsAction.OnRetry)
savedStateHandle?.set("shouldRefresh", false) // reset after refresh
}
}

EventsEffect(viewModel.eventFlow) { event ->
when (event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ internal fun ClientProfileEditScreen(
) {
val state by viewModel.stateFlow.collectAsStateWithLifecycle()

EventsEffect(viewModel.eventFlow) { event ->
/*EventsEffect(viewModel.eventFlow) { event ->
when (event) {
ClientProfileEditEvent.NavigateBack -> onNavigateBack()
ClientProfileEditEvent.OnSaveSuccess -> {
onNavigateBack()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why commented this ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented it because it is not needed for the navigation, and it is excess code
The screen navigation works well with or without it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove it if not needed

}
}
}
}*/

ClientProfileEditScaffold(
modifier = modifier,
Expand Down Expand Up @@ -206,6 +206,16 @@ private fun ClientProfileEditDialogs(
)
}

is ClientProfileEditState.DialogState.Success -> {
MifosBasicDialog(
title = "Success",
content = { Text("Your profile image has been updated successfully!") },
confirmText = "Done",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Success and Failure Dilaogs instead of Basic Dialog.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one

MifosStatusDialog

which accepts success and Failure state and also move strings to string resources

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am done with the changes

onConfirm = { onAction(ClientProfileEditAction.DismissModalBottomSheet) },
onDismissRequest = { onAction(ClientProfileEditAction.DismissModalBottomSheet) },
)
}

ClientProfileEditState.DialogState.ShowDeleteDialog -> {
MifosBasicDialog(
visibilityState = BasicDialogState.Shown(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.mifos.core.ui.util.BaseViewModel
import com.mifos.core.ui.util.imageToByteArray
import com.mifos.core.ui.util.multipartRequestBody
import com.mifos.feature.client.utils.toPlatformFile
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -123,6 +124,7 @@ internal class ClientProfileEditViewModel(
}
}


is DataState.Loading -> {
mutableStateFlow.update {
it.copy(
Expand All @@ -132,13 +134,22 @@ internal class ClientProfileEditViewModel(
}

is DataState.Success -> {
mutableStateFlow.update {
it.copy(
dialogState = ClientProfileEditState.DialogState.Loading,
openImagePicker = false,
)
viewModelScope.launch {
clientDetailsRepo.getImage(route.id).collect { result ->
when (result) {
is DataState.Success -> mutableStateFlow.update {
it.copy(
profileImage = imageToByteArray(result.data),
dialogState = ClientProfileEditState.DialogState.Success,
)
}
is DataState.Loading -> mutableStateFlow.update {
it.copy(dialogState = ClientProfileEditState.DialogState.Loading)
}
else -> Unit
}
}
Comment on lines +138 to +151
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Surface an error when the refresh fetch fails.

Inside the success branch we collect clientDetailsRepo.getImage, but if that refresh comes back as DataState.Error we never update the state—dialogState stays at Loading, so the user is stuck watching a spinner even though the upload succeeded. Please handle the error emission and surface it through DialogState.Error.

                             when (result) {
                                 is DataState.Success -> mutableStateFlow.update {
                                     it.copy(
                                         profileImage = imageToByteArray(result.data),
                                         dialogState = ClientProfileEditState.DialogState.Success,
                                     )
                                 }
                                 is DataState.Loading -> mutableStateFlow.update {
                                     it.copy(dialogState = ClientProfileEditState.DialogState.Loading)
                                 }
+                                is DataState.Error -> mutableStateFlow.update {
+                                    it.copy(
+                                        dialogState = ClientProfileEditState.DialogState.Error(
+                                            result.message ?: "Unknown Error",
+                                        ),
+                                    )
+                                }
                                 else -> Unit
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
clientDetailsRepo.getImage(route.id).collect { result ->
when (result) {
is DataState.Success -> mutableStateFlow.update {
it.copy(
profileImage = imageToByteArray(result.data),
dialogState = ClientProfileEditState.DialogState.Success,
)
}
is DataState.Loading -> mutableStateFlow.update {
it.copy(dialogState = ClientProfileEditState.DialogState.Loading)
}
else -> Unit
}
}
clientDetailsRepo.getImage(route.id).collect { result ->
when (result) {
is DataState.Success -> mutableStateFlow.update {
it.copy(
profileImage = imageToByteArray(result.data),
dialogState = ClientProfileEditState.DialogState.Success,
)
}
is DataState.Loading -> mutableStateFlow.update {
it.copy(dialogState = ClientProfileEditState.DialogState.Loading)
}
is DataState.Error -> mutableStateFlow.update {
it.copy(
dialogState = ClientProfileEditState.DialogState.Error(
result.message ?: "Unknown Error",
),
)
}
else -> Unit
}
}
🤖 Prompt for AI Agents
In
feature/client/src/commonMain/kotlin/com/mifos/feature/client/clientEditProfile/ClientProfileEditViewModel.kt
around lines 138 to 151, the collect on clientDetailsRepo.getImage only handles
Success and Loading so a DataState.Error leaves dialogState stuck at Loading;
update the collector to handle DataState.Error by calling
mutableStateFlow.update to set dialogState =
ClientProfileEditState.DialogState.Error (include the error message/exception
from the DataState.Error payload if available) and clear Loading so the UI shows
an error state instead of a spinner.

}
loadImage(route.id)
}
}
}
Expand Down Expand Up @@ -179,14 +190,18 @@ data class ClientProfileEditState(
sealed interface DialogState {
data class Error(val message: String) : DialogState
data object Loading : DialogState
data object Success : DialogState
data object ShowDeleteDialog : DialogState
data object ShowUploadOptions : DialogState

}
}

sealed interface ClientProfileEditEvent {
data object NavigateBack : ClientProfileEditEvent
data object OnSaveSuccess : ClientProfileEditEvent
data class OnError(val message: String) : ClientProfileEditEvent

}

sealed interface ClientProfileEditAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.collectAsStateWithLifecycle
Expand Down Expand Up @@ -61,6 +63,20 @@ internal fun ClientProfileScreen(
viewModel: ClientProfileViewModel = koinViewModel(),
) {
val state by viewModel.stateFlow.collectAsStateWithLifecycle()
val currentBackStackEntry = navController.currentBackStackEntry
val savedStateHandle = currentBackStackEntry?.savedStateHandle

val profileUpdated by savedStateHandle
?.getStateFlow("shouldRefresh", false)
?.collectAsStateWithLifecycle(initialValue = false)
?: remember { mutableStateOf(false) }

LaunchedEffect(profileUpdated) {
if (profileUpdated) {
viewModel.trySendAction(ClientProfileAction.OnRetry)
savedStateHandle?.set("shouldRefresh", false) // reset after refresh
}
}

EventsEffect(viewModel.eventFlow) { event ->
when (event) {
Expand Down