diff --git a/app/src/main/java/com/android/periodpals/ui/settings/SettingsScreen.kt b/app/src/main/java/com/android/periodpals/ui/settings/SettingsScreen.kt index aeca957a..5ea309d2 100644 --- a/app/src/main/java/com/android/periodpals/ui/settings/SettingsScreen.kt +++ b/app/src/main/java/com/android/periodpals/ui/settings/SettingsScreen.kt @@ -151,22 +151,7 @@ fun SettingsScreen( SettingsContainer(testTag = SettingsScreen.SLIDER_CONTAINER) { SliderMenu(sliderPosition) { sliderPosition = (it / 100).roundToInt() * 100f - - userViewModel.user.value?.let { user -> - val newUser = - User( - name = user.name, - dob = user.dob, - description = user.description, - imageUrl = user.imageUrl, - preferredDistance = sliderPosition.toInt(), - ) - - userViewModel.saveUser( - newUser, - onSuccess = { Log.d(LOG_SETTINGS_TAG, "User updated successfully") }, - onFailure = { Log.d(LOG_SETTINGS_TAG, "Failed to update user") }) - } + sliderLogic(sliderPosition, userViewModel) } Text( @@ -430,3 +415,31 @@ private fun DeleteAccountDialog( } } } + +/** + * Function that updates the user's preferred distance when the slider is moved. + * + * @param sliderPosition The position of the slider. + * @param userViewModel The ViewModel that handles user data. + */ +public fun sliderLogic( + sliderPosition: Float, + userViewModel: UserViewModel, +) { + + userViewModel.user.value?.let { user -> + val newUser = + User( + name = user.name, + dob = user.dob, + description = user.description, + imageUrl = user.imageUrl, + preferredDistance = sliderPosition.toInt(), + ) + + userViewModel.saveUser( + newUser, + onSuccess = { Log.d(LOG_SETTINGS_TAG, "User updated successfully") }, + onFailure = { Log.d(LOG_SETTINGS_TAG, "Failed to update user") }) + } +} diff --git a/app/src/test/java/com/android/periodpals/ui/settings/SettingsScreenTest.kt b/app/src/test/java/com/android/periodpals/ui/settings/SettingsScreenTest.kt index 3e969b8a..8c212952 100644 --- a/app/src/test/java/com/android/periodpals/ui/settings/SettingsScreenTest.kt +++ b/app/src/test/java/com/android/periodpals/ui/settings/SettingsScreenTest.kt @@ -310,4 +310,14 @@ class SettingsScreenTest { verify(navigationActions, never()).navigateTo(any()) verify(navigationActions, never()).navigateTo(any()) } + + @Test + fun sliderLogicTest() { + `when`(userViewModel.saveUser(any(), any(), any())).thenAnswer { + val onSuccess = it.arguments[1] as () -> Unit + onSuccess() + } + + sliderLogic(preferredDistance.toFloat(), userViewModel) + } }