Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: profile fragment migrated to compose #1547

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.mifos.mobilewallet.mifospay.designsystem.component

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

@Composable
fun MifosTextUserImage(modifier: Modifier = Modifier, text: String, size: Dp = 100.dp) {
Box(
modifier = modifier
.size(size)
.clip(CircleShape)
.background(color = MaterialTheme.colorScheme.primary),
contentAlignment = Alignment.Center
) {
Text(
text = text,
color = MaterialTheme.colorScheme.onPrimary,
fontSize = with(LocalDensity.current) { (size / 2).toSp() }
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.mifos.mobilewallet.mifospay.home.presenter

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import okhttp3.ResponseBody
import org.mifos.mobilewallet.core.base.UseCase
import org.mifos.mobilewallet.core.base.UseCaseHandler
import org.mifos.mobilewallet.core.domain.usecase.client.FetchClientImage
import org.mifos.mobilewallet.datastore.PreferencesHelper
import org.mifos.mobilewallet.mifospay.common.DebugUtil
import org.mifos.mobilewallet.mifospay.data.local.LocalRepository
import javax.inject.Inject

@HiltViewModel
class ProfileViewModel @Inject constructor(
private val mUsecaseHandler: UseCaseHandler,
private val fetchClientImageUseCase: FetchClientImage,
private val localRepository: LocalRepository,
private val mPreferencesHelper: PreferencesHelper
) : ViewModel() {

private val _profileState = MutableStateFlow<ProfileUiState>(ProfileUiState.Loading)
val profileState: StateFlow<ProfileUiState> get() = _profileState

init {
fetchClientImage()
fetchProfileDetails()
}

private fun fetchClientImage() {
viewModelScope.launch {
mUsecaseHandler.execute(fetchClientImageUseCase,
FetchClientImage.RequestValues(localRepository.clientDetails.clientId),
object : UseCase.UseCaseCallback<FetchClientImage.ResponseValue?> {
override fun onSuccess(response: FetchClientImage.ResponseValue?) {
val bitmap = convertResponseToBitmap(response?.responseBody)
val currentState = _profileState.value as ProfileUiState.Success
_profileState.value = currentState.copy(bitmapImage = bitmap)
}

override fun onError(message: String) {
DebugUtil.log("image", message)
}
})
}
}

private fun fetchProfileDetails() {
val name = mPreferencesHelper.fullName ?: "-"
val email = mPreferencesHelper.email ?: "-"
val vpa = mPreferencesHelper.clientVpa ?: "-"
val mobile = mPreferencesHelper.mobile ?: "-"

_profileState.value = ProfileUiState.Success(
name = name,
email = email,
vpa = vpa,
mobile = mobile
)
}

private fun convertResponseToBitmap(responseBody: ResponseBody?): Bitmap? {
return try {
responseBody?.byteStream()?.use { inputStream ->
BitmapFactory.decodeStream(inputStream)
}
} catch (e: Exception) {
null
}
}
}

sealed class ProfileUiState {
data object Loading : ProfileUiState()
data class Success(
val bitmapImage: Bitmap? = null,
val name: String?,
val email: String?,
val vpa: String?,
val mobile: String?
) : ProfileUiState()
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.mifos.mobilewallet.mifospay.designsystem.component

import android.graphics.Bitmap
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale

/**
* @author pratyush
* @since 20/12/2023
*/

@Composable
fun MifosUserImage(
bitmap: Bitmap?,
modifier: Modifier = Modifier,
username: String? = null
) {
if (bitmap == null) {
MifosTextUserImage(
modifier = modifier,
text = username?.firstOrNull()?.toString() ?: "J"
)
} else {
Image(
modifier = modifier
.clip(CircleShape)
.background(MaterialTheme.colorScheme.primary),
bitmap = bitmap.asImageBitmap(),
contentDescription = "Profile Image",
contentScale = ContentScale.Crop,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.mifos.mobilewallet.mifospay.home.ui

import android.graphics.Bitmap
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import org.mifos.mobilewallet.mifospay.designsystem.component.MifosUserImage

@Composable
fun ProfileImage(bitmap: Bitmap?) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(top = 64.dp, bottom = 12.dp),
horizontalArrangement = Arrangement.Center
) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.size(200.dp)
.border(
width = 2.dp,
color = Color.Gray,
shape = CircleShape
)
) {
MifosUserImage(
bitmap = bitmap,
modifier = Modifier
.size(200.dp)
.padding(10.dp)
)
}
}
}
Loading
Loading