Skip to content

Commit 0138d6f

Browse files
authored
UserImage working propetrly (#2958)
1 parent 4e10873 commit 0138d6f

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

core/network/src/commonMain/kotlin/org/mifos/mobile/core/network/services/ClientService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.mifos.mobile.core.network.services
1111

1212
import de.jensklingenberg.ktorfit.http.GET
13+
import de.jensklingenberg.ktorfit.http.Headers
1314
import de.jensklingenberg.ktorfit.http.Path
1415
import de.jensklingenberg.ktorfit.http.Query
1516
import io.ktor.client.statement.HttpResponse

core/network/src/commonMain/kotlin/org/mifos/mobile/core/network/utils/KtorInterceptor.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class KtorInterceptor(
3030
scope.requestPipeline.intercept(HttpRequestPipeline.State) {
3131
context.header(CONTENT_TYPE, "application/json")
3232
context.header("Accept", "application/json")
33+
context.header("Accept", "*/*")
3334
context.header(HEADER_TENANT, DEFAULT)
3435

3536
plugin.getToken()?.let { token ->

core/ui/src/commonMain/kotlin/org/mifos/mobile/core/ui/component/MifosUserImage.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ import androidx.compose.foundation.Image
1313
import androidx.compose.foundation.layout.fillMaxSize
1414
import androidx.compose.foundation.shape.CircleShape
1515
import androidx.compose.runtime.Composable
16-
import androidx.compose.runtime.getValue
17-
import androidx.compose.runtime.mutableStateOf
18-
import androidx.compose.runtime.remember
1916
import androidx.compose.ui.Modifier
2017
import androidx.compose.ui.draw.clip
2118
import androidx.compose.ui.layout.ContentScale
2219
import coil3.ImageLoader
2320
import coil3.compose.LocalPlatformContext
2421
import coil3.compose.rememberAsyncImagePainter
22+
import coil3.request.ImageRequest
2523
import org.mifos.mobile.core.designsystem.theme.MifosMobileTheme
2624
import org.mifos.mobile.core.ui.utils.DevicePreview
2725

@@ -32,18 +30,19 @@ fun MifosUserImage(
3230
username: String? = null,
3331
) {
3432
val context = LocalPlatformContext.current
35-
val uploadedImage by remember { mutableStateOf<ByteArray?>(null) }
3633

37-
val painter = rememberAsyncImagePainter(
38-
model = uploadedImage,
39-
imageLoader = ImageLoader(context),
40-
)
4134
if (bitmap == null) {
4235
MifosTextUserImage(
4336
text = username?.firstOrNull()?.toString() ?: "M",
4437
modifier = modifier,
4538
)
4639
} else {
40+
val painter = rememberAsyncImagePainter(
41+
model = ImageRequest.Builder(context)
42+
.data(bitmap)
43+
.build(),
44+
imageLoader = ImageLoader(context),
45+
)
4746
Image(
4847
modifier = modifier
4948
.clip(CircleShape)

feature/settings/src/commonMain/kotlin/org/mifos/mobile/feature/settings/settings/SettingsViewModel.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,16 @@ internal class SettingsViewModel(
221221
private fun setUserProfile(image: String?) {
222222
if (image.isNullOrBlank()) return
223223

224-
// Extract the base64 part, removing any data URI prefix
225-
val base64String = image.substringAfter(",", image)
226-
227-
// Basic validation for a base64 string
228-
if (!base64String.matches(Regex("^[A-Za-z0-9+/=]+$"))) return
229-
224+
// Extract the Base64 payload after the standard data URL prefix (more robust than first comma)
225+
val afterPrefix = image.substringAfter("base64,", image)
226+
// Remove any whitespace/newlines that can appear in API responses
227+
val cleaned = afterPrefix.replace(Regex("\\s+"), "")
230228
try {
231-
val decodedBytes = Base64.decode(base64String)
232-
val bitmap = ImageUtil.compressImage(decodedBytes)
233-
updateState { it.copy(profileImage = bitmap) }
229+
val decodedBytes = Base64.decode(cleaned)
230+
val compressedImageBytes = ImageUtil.compressImage(decodedBytes)
231+
if (compressedImageBytes.isNotEmpty()) {
232+
updateState { it.copy(profileImage = compressedImageBytes) }
233+
}
234234
} catch (e: Exception) {
235235
// Log the error but fail silently in the UI
236236
println(e.message)

0 commit comments

Comments
 (0)