Skip to content
Open
Show file tree
Hide file tree
Changes from 14 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
4 changes: 4 additions & 0 deletions cmp-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@
<string name="no_more_clients_available">No more Clients Available</string>
<string name="feature_groups_no_more_groups_available">No more Groups Available</string>
<string name="no_more_centers_available">No more Centers Available</string>
<string name="feature_client_profile_update_success_message">Your profile image has been updated successfully!</string>
<string name="feature_client_profile_update_error_message">We couldn’t update your profile image. Please try again.</string>
<string name="should_refresh">should_refresh</string>
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be added to feature:client's commonMain strings.xml (android-client\feature\client\src\commonMain\composeResources\values\strings.xml)

Copy link
Author

Choose a reason for hiding this comment

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

Oh okay, I will do that and push it



<string name="numberofrepayments">Number of repayments</string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -32,6 +35,10 @@ import com.mifos.core.designsystem.theme.MifosTypography
import org.jetbrains.compose.ui.tooling.preview.Preview
import org.jetbrains.compose.ui.tooling.preview.PreviewParameter
import org.jetbrains.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.Error
import androidx.compose.ui.graphics.Color

@Composable
fun MifosBasicDialog(
Expand Down Expand Up @@ -197,6 +204,73 @@ fun MifosBasicDialog(
)
}



enum class MifosDialogStatus { SUCCESS, FAILURE }

@Composable
fun MifosStatusDialog(
status: MifosDialogStatus,
message: String,
onDismissRequest: () -> Unit
) {
data class DialogUI(
val title: String,
val icon: androidx.compose.ui.graphics.vector.ImageVector,
val color: Color
)

val dialogUI: DialogUI = when (status) {
MifosDialogStatus.SUCCESS -> DialogUI(
title = "Success",
icon = Icons.Filled.CheckCircle,
color = Color(0xFF4CAF50)
)
MifosDialogStatus.FAILURE -> DialogUI(
title = "Error",
icon = Icons.Filled.Error,
color = Color(0xFFF44336)
)
}

AlertDialog(
onDismissRequest = onDismissRequest,
icon = {
Icon(
imageVector = dialogUI.icon,
contentDescription = dialogUI.title,
tint = dialogUI.color,
modifier = Modifier
.size(84.dp)
.padding(bottom = 4.dp)
)
},
title = {
Text(
text = dialogUI.title,
style = MaterialTheme.typography.titleLarge
)
},
text = {
Text(
text = message,
style = MaterialTheme.typography.bodyMedium
)
},
confirmButton = {
Button(
onClick = onDismissRequest,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp)
) {
Text("Continue")
}
},
modifier = Modifier.testTag("MifosStatusDialog")
)
}

@Preview
@Composable
private fun MifosBasicDialog_preview() {
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 @@ -17,7 +17,10 @@ import androidclient.feature.client.generated.resources.choose_from_option
import androidclient.feature.client.generated.resources.delete_dialog_message
import androidclient.feature.client.generated.resources.delete_dialog_title
import androidclient.feature.client.generated.resources.delete_photo
import androidclient.feature.client.generated.resources.dialog_continue
import androidclient.feature.client.generated.resources.edit_profile_title
import androidclient.feature.client.generated.resources.feature_client_Image_Upload_Failed
import androidclient.feature.client.generated.resources.feature_client_Image_Upload_Successful
import androidclient.feature.client.generated.resources.from_camera
import androidclient.feature.client.generated.resources.from_gallery
import androidclient.feature.client.generated.resources.remove
Expand Down Expand Up @@ -48,8 +51,10 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import com.mifos.core.designsystem.component.BasicDialogState
import com.mifos.core.designsystem.component.MifosBasicDialog
import com.mifos.core.designsystem.component.MifosDialogStatus
import com.mifos.core.designsystem.component.MifosOutlinedButton
import com.mifos.core.designsystem.component.MifosScaffold
import com.mifos.core.designsystem.component.MifosStatusDialog
import com.mifos.core.designsystem.component.MifosTextButton
import com.mifos.core.designsystem.icon.MifosIcons
import com.mifos.core.designsystem.theme.DesignToken
Expand All @@ -76,14 +81,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 @@ -198,11 +203,18 @@ private fun ClientProfileEditDialogs(
is ClientProfileEditState.DialogState.Loading -> MifosProgressIndicator()

is ClientProfileEditState.DialogState.Error -> {
MifosErrorComponent(
isNetworkConnected = state.networkConnection,
message = state.dialogState.message,
isRetryEnabled = true,
onRetry = onRetry,
MifosStatusDialog(
status = MifosDialogStatus.FAILURE,
message = stringResource(Res.string.feature_client_Image_Upload_Failed),
onDismissRequest = { onAction(ClientProfileEditAction.DismissModalBottomSheet) }
)
}

is ClientProfileEditState.DialogState.Success -> {
MifosStatusDialog(
status = MifosDialogStatus.SUCCESS,
message = stringResource(Res.string.feature_client_profile_update_success_message),
onDismissRequest = { onAction(ClientProfileEditAction.DismissModalBottomSheet) }
)
}

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