Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 253c204

Browse files
committedFeb 22, 2024·
refactor #1532: profile fragment migrated to compose
1 parent b215617 commit 253c204

File tree

9 files changed

+459
-126
lines changed

9 files changed

+459
-126
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.mifos.mobilewallet.mifospay.designsystem.component
2+
3+
import androidx.compose.foundation.background
4+
import androidx.compose.foundation.layout.Box
5+
import androidx.compose.foundation.shape.CircleShape
6+
import androidx.compose.material3.MaterialTheme
7+
import androidx.compose.material3.Text
8+
import androidx.compose.runtime.Composable
9+
import androidx.compose.runtime.getValue
10+
import androidx.compose.runtime.mutableStateOf
11+
import androidx.compose.runtime.remember
12+
import androidx.compose.runtime.setValue
13+
import androidx.compose.ui.Alignment
14+
import androidx.compose.ui.Modifier
15+
import androidx.compose.ui.draw.clip
16+
import androidx.compose.ui.geometry.Size
17+
import androidx.compose.ui.layout.onGloballyPositioned
18+
import androidx.compose.ui.platform.LocalDensity
19+
import kotlin.math.min
20+
21+
@Composable
22+
fun MifosTextUserImage(modifier: Modifier = Modifier, text: String) {
23+
var boxSize by remember { mutableStateOf(Size.Zero) }
24+
Box(
25+
modifier = modifier
26+
.clip(CircleShape)
27+
.background(color = MaterialTheme.colorScheme.primary)
28+
.onGloballyPositioned { coordinates ->
29+
boxSize = Size(
30+
coordinates.size.width.toFloat(),
31+
coordinates.size.height.toFloat()
32+
)
33+
},
34+
contentAlignment = Alignment.Center
35+
) {
36+
Text(
37+
text = text,
38+
color = MaterialTheme.colorScheme.onPrimary,
39+
fontSize = with(LocalDensity.current) {
40+
(min(boxSize.width, boxSize.height) / 2).toSp()
41+
}
42+
)
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.mifos.mobilewallet.mifospay.designsystem.component
2+
3+
import android.graphics.Bitmap
4+
import androidx.compose.foundation.Image
5+
import androidx.compose.foundation.background
6+
import androidx.compose.foundation.shape.CircleShape
7+
import androidx.compose.material3.MaterialTheme
8+
import androidx.compose.runtime.Composable
9+
import androidx.compose.ui.Modifier
10+
import androidx.compose.ui.draw.clip
11+
import androidx.compose.ui.graphics.asImageBitmap
12+
import androidx.compose.ui.layout.ContentScale
13+
14+
/**
15+
* @author pratyush
16+
* @since 20/12/2023
17+
*/
18+
19+
@Composable
20+
fun MifosUserImage(
21+
bitmap: Bitmap?,
22+
modifier: Modifier = Modifier,
23+
username: String? = null
24+
) {
25+
if (bitmap == null) {
26+
MifosTextUserImage(
27+
modifier = modifier,
28+
text = username?.firstOrNull()?.toString() ?: "J"
29+
)
30+
} else {
31+
Image(
32+
modifier = modifier
33+
.clip(CircleShape)
34+
.background(MaterialTheme.colorScheme.primary),
35+
bitmap = bitmap.asImageBitmap(),
36+
contentDescription = "Profile Image",
37+
contentScale = ContentScale.Crop,
38+
)
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.mifos.mobilewallet.mifospay.designsystem.component
2+
3+
import android.graphics.Bitmap
4+
import androidx.compose.foundation.border
5+
import androidx.compose.foundation.layout.Arrangement
6+
import androidx.compose.foundation.layout.Box
7+
import androidx.compose.foundation.layout.Row
8+
import androidx.compose.foundation.layout.fillMaxWidth
9+
import androidx.compose.foundation.layout.padding
10+
import androidx.compose.foundation.layout.size
11+
import androidx.compose.foundation.shape.CircleShape
12+
import androidx.compose.runtime.Composable
13+
import androidx.compose.ui.Alignment
14+
import androidx.compose.ui.Modifier
15+
import androidx.compose.ui.graphics.Color
16+
import androidx.compose.ui.unit.dp
17+
18+
@Composable
19+
fun ProfileImage(bitmap: Bitmap?) {
20+
Row(
21+
modifier = Modifier
22+
.fillMaxWidth()
23+
.padding(top = 64.dp, bottom = 12.dp),
24+
horizontalArrangement = Arrangement.Center
25+
) {
26+
Box(
27+
contentAlignment = Alignment.Center,
28+
modifier = Modifier
29+
.size(200.dp)
30+
.border(
31+
width = 2.dp,
32+
color = Color.Gray,
33+
shape = CircleShape
34+
)
35+
) {
36+
MifosUserImage(
37+
bitmap = bitmap,
38+
modifier = Modifier
39+
.size(200.dp)
40+
.padding(10.dp)
41+
)
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.mifos.mobilewallet.mifospay.ui.utility
2+
3+
enum class Orientation {
4+
VERTICAL, HORIZONTAL
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.mifos.mobilewallet.mifospay.home.presenter
2+
3+
import android.graphics.Bitmap
4+
import android.graphics.BitmapFactory
5+
import androidx.lifecycle.ViewModel
6+
import androidx.lifecycle.viewModelScope
7+
import dagger.hilt.android.lifecycle.HiltViewModel
8+
import kotlinx.coroutines.flow.MutableStateFlow
9+
import kotlinx.coroutines.flow.StateFlow
10+
import kotlinx.coroutines.launch
11+
import okhttp3.ResponseBody
12+
import org.mifos.mobilewallet.core.base.UseCase
13+
import org.mifos.mobilewallet.core.base.UseCaseHandler
14+
import org.mifos.mobilewallet.core.domain.usecase.client.FetchClientImage
15+
import org.mifos.mobilewallet.datastore.PreferencesHelper
16+
import org.mifos.mobilewallet.mifospay.data.local.LocalRepository
17+
import org.mifos.mobilewallet.mifospay.utils.DebugUtil
18+
import javax.inject.Inject
19+
20+
@HiltViewModel
21+
class ProfileViewModel @Inject constructor(
22+
private val mUsecaseHandler: UseCaseHandler,
23+
private val fetchClientImageUseCase: FetchClientImage,
24+
private val localRepository: LocalRepository,
25+
private val mPreferencesHelper: PreferencesHelper
26+
) : ViewModel() {
27+
28+
private val _bitmapImage = MutableStateFlow<Bitmap?>(null)
29+
val bitmapImage: StateFlow<Bitmap?> = _bitmapImage
30+
31+
private val _name = MutableStateFlow<String?>(null)
32+
val name: StateFlow<String?> = _name
33+
34+
private val _email = MutableStateFlow<String?>(null)
35+
val email: StateFlow<String?> = _email
36+
37+
private val _vpa = MutableStateFlow<String?>(null)
38+
val vpa: StateFlow<String?> = _vpa
39+
40+
private val _mobile = MutableStateFlow<String?>(null)
41+
val mobile: StateFlow<String?> = _mobile
42+
43+
init {
44+
fetchClientImage()
45+
fetchProfileDetails()
46+
}
47+
48+
private fun fetchClientImage() {
49+
viewModelScope.launch {
50+
mUsecaseHandler.execute(fetchClientImageUseCase,
51+
FetchClientImage.RequestValues(localRepository.clientDetails.clientId),
52+
object : UseCase.UseCaseCallback<FetchClientImage.ResponseValue?> {
53+
override fun onSuccess(response: FetchClientImage.ResponseValue?) {
54+
val bitmap = convertResponseToBitmap(response?.responseBody)
55+
_bitmapImage.value = bitmap
56+
}
57+
58+
override fun onError(message: String) {
59+
DebugUtil.log("image", message)
60+
}
61+
})
62+
}
63+
}
64+
65+
private fun fetchProfileDetails() {
66+
_name.value = mPreferencesHelper.fullName ?: "-"
67+
_email.value = mPreferencesHelper.email ?: "-"
68+
_vpa.value = mPreferencesHelper.clientVpa ?: "-"
69+
_mobile.value = mPreferencesHelper.mobile ?: "-"
70+
}
71+
72+
private fun convertResponseToBitmap(responseBody: ResponseBody?): Bitmap? {
73+
return try {
74+
responseBody?.byteStream()?.use { inputStream ->
75+
BitmapFactory.decodeStream(inputStream)
76+
}
77+
} catch (e: Exception) {
78+
null
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)
Please sign in to comment.