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

Fix/profile/solve editprofile bug #363

Merged
merged 7 commits into from
Dec 20, 2024
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) {
Harrish92 marked this conversation as resolved.
Show resolved Hide resolved
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)))
Harrish92 marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
Expand Down
Loading