Skip to content

Commit

Permalink
Merge pull request #363 from PeriodPals/fix/profile/solve-editprofile…
Browse files Browse the repository at this point in the history
…-bug

Fix/profile/solve editprofile bug
  • Loading branch information
Harrish92 authored Dec 20, 2024
2 parents 2f4a8d3 + 3fad4f4 commit f77cee6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fun ProfileSaveButton(
descriptionState: TextFieldState,
profileImageState: TextFieldState,
byteArray: ByteArray?,
preferredDistance: Int,
preferredDistance: Int?,
context: Context,
userViewModel: UserViewModel,
navigationActions: NavigationActions,
Expand All @@ -241,6 +241,11 @@ fun ProfileSaveButton(
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
return@Button
}
if (preferredDistance == null) {
Log.d(LOG_TAG, LOG_FAILURE)
Toast.makeText(context, TOAST_FAILURE, Toast.LENGTH_SHORT).show()
return@Button
}

Log.d(LOG_TAG, LOG_SAVING_PROFILE)
val newUser =
Expand Down
22 changes: 13 additions & 9 deletions app/src/main/java/com/android/periodpals/ui/profile/EditProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,27 @@ fun EditProfileScreen(userViewModel: UserViewModel, navigationActions: Navigatio
formState.reset()

val nameState = formState.getState<TextFieldState>(UserViewModel.NAME_STATE_NAME)
nameState.change(userState.value?.name ?: "")
val dobState = formState.getState<TextFieldState>(UserViewModel.DOB_STATE_NAME)
dobState.change(userState.value?.dob ?: "")
val descriptionState = formState.getState<TextFieldState>(UserViewModel.DESCRIPTION_STATE_NAME)
descriptionState.change(userState.value?.description ?: "")
val profileImageState = formState.getState<TextFieldState>(UserViewModel.PROFILE_IMAGE_STATE_NAME)
profileImageState.change(userState.value?.imageUrl ?: DEFAULT_PROFILE_PICTURE)

userState.value?.let {
nameState.change(it.name)
dobState.change(it.dob)
descriptionState.change(it.description)
profileImageState.change(it.imageUrl)
}

var userAvatarState by remember {
mutableStateOf(userAvatar?.value ?: Uri.parse(DEFAULT_PROFILE_PICTURE).uriToByteArray(context))
mutableStateOf(userAvatar.value ?: Uri.parse(DEFAULT_PROFILE_PICTURE).uriToByteArray(context))
}

val launcher =
rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
profileImageState.change(result.data?.data.toString())
userAvatarState = result.data?.data?.uriToByteArray(context)
if ((result.resultCode == Activity.RESULT_OK) && (result.data != null)) {
profileImageState.change(result.data!!.data.toString())
userAvatarState = result.data!!.data?.uriToByteArray(context)
}
}

Expand Down Expand Up @@ -165,7 +169,7 @@ fun EditProfileScreen(userViewModel: UserViewModel, navigationActions: Navigatio
descriptionState,
profileImageState,
userAvatarState,
userViewModel.user.value!!.preferredDistance,
userViewModel.user.value?.preferredDistance,
context,
userViewModel,
navigationActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,17 @@ fun init(
onSuccess = {
Log.d(TAG, "Authentication data loaded successfully")
chatViewModel.connectUser(userState, authenticationViewModel = authenticationViewModel)
userViewModel.loadUser(
authenticationViewModel.authUserData.value!!.uid,
onSuccess = {
userViewModel.user.value?.let {
userViewModel.downloadFile(
it.imageUrl,
onSuccess = { onSuccess() },
onFailure = { e: Exception -> onFailure(Exception(e)) })
}
},
onFailure = { e: Exception -> onFailure(Exception(e)) })
},
onFailure = { Log.d(TAG, "Authentication data is null") })
userViewModel.loadUser(
authenticationViewModel.authUserData.value!!.uid,
onSuccess = {
userViewModel.user.value?.let {
userViewModel.downloadFile(
it.imageUrl,
onSuccess = { onSuccess() },
onFailure = { e: Exception -> onFailure(Exception(e)) })
}
},
onFailure = { e: Exception -> onFailure(Exception(e)) })
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class EditProfileTest {

`when`(navigationActions.currentRoute()).thenReturn(Route.PROFILE)
`when`(userViewModel.formState).thenReturn(formState)
`when`(userViewModel.avatar).thenReturn(mutableStateOf(ByteArray(0)))
}

@Test
Expand Down

0 comments on commit f77cee6

Please sign in to comment.