Skip to content

Fix/misc/clean up toasts #361

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.NotificationCompat
Expand Down Expand Up @@ -180,14 +179,12 @@ class PushNotificationsServiceImpl(
private fun handlePermissionResult(isGranted: Boolean) {
if (isGranted) {
Log.d(TAG, "Notification permission granted")
Toast.makeText(activity, "Notification permission granted", Toast.LENGTH_SHORT).show()
_pushPermissionsGranted.value = true
createNotificationChannel()
createDeviceToken()
return
}
Log.d(TAG, "Notification permission denied")
Toast.makeText(activity, "Notification permission denied", Toast.LENGTH_SHORT).show()
_pushPermissionsGranted.value = false
}

Expand Down
12 changes: 2 additions & 10 deletions app/src/main/java/com/android/periodpals/ui/alert/AlertLists.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.android.periodpals.ui.alert

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -128,13 +126,7 @@ fun AlertListsScreen(
var urgencyFilter by remember { mutableStateOf<Urgency?>(null) }

authenticationViewModel.loadAuthenticationUserData(
onFailure = {
Handler(Looper.getMainLooper()).post { // used to show the Toast in the main thread
Toast.makeText(context, "Error loading your data! Try again later.", Toast.LENGTH_SHORT)
.show()
}
Log.d(TAG, "Authentication data is null")
},
onFailure = { Log.d(TAG, "Authentication data is null") },
)

val uid by remember { mutableStateOf(authenticationViewModel.authUserData.value!!.uid) }
Expand Down Expand Up @@ -591,7 +583,7 @@ private fun AlertAcceptButtons(
context.startActivity(intent)
}
} else {
Toast.makeText(context, "Error: User data is not available", Toast.LENGTH_SHORT).show()
Log.d(TAG, "Authentication data is null")
}
},
contentDescription = "Accept Alert",
Expand Down
25 changes: 2 additions & 23 deletions app/src/main/java/com/android/periodpals/ui/alert/CreateAlert.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.android.periodpals.ui.alert

import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
Expand Down Expand Up @@ -97,22 +94,10 @@ fun CreateAlertScreen(
gpsService.askPermissionAndStartUpdates() // Permission to access location
}
authenticationViewModel.loadAuthenticationUserData(
onFailure = {
Handler(Looper.getMainLooper()).post { // used to show the Toast in the main thread
Toast.makeText(context, "Error loading your data! Try again later.", Toast.LENGTH_SHORT)
.show()
}
Log.d(TAG, "Authentication data is null")
})
onFailure = { Log.d(TAG, "Authentication data is null") })
userViewModel.loadUser(
authenticationViewModel.authUserData.value!!.uid,
onFailure = {
Handler(Looper.getMainLooper()).post { // used to show the Toast in the main thread
Toast.makeText(context, "Error loading your data! Try again later.", Toast.LENGTH_SHORT)
.show()
}
Log.d(TAG, "User data is null")
})
onFailure = { Log.d(TAG, "User data is null") })

val name by remember { mutableStateOf(userViewModel.user.value?.name ?: "") }
val uid by remember { mutableStateOf(authenticationViewModel.authUserData.value!!.uid) }
Expand Down Expand Up @@ -183,7 +168,6 @@ fun CreateAlertScreen(
else -> null
}
if (errorMessage != null) {
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a Log.d for errorMessage

return@ActionButton
}

Expand All @@ -202,11 +186,6 @@ fun CreateAlertScreen(
onSuccess = { Log.d(TAG, "Alert created") },
onFailure = { e -> Log.e(TAG, "createAlert: fail to create alert: ${e.message}") },
)
Toast.makeText(
context,
context.getString(R.string.create_alert_toast_successful_submission_message),
Toast.LENGTH_SHORT)
.show()
navigationActions.navigateTo(Screen.ALERT_LIST)
},
colors = getFilledPrimaryContainerButtonColors(),
Expand Down
12 changes: 1 addition & 11 deletions app/src/main/java/com/android/periodpals/ui/alert/EditAlert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ fun EditAlertScreen(
alertViewModel.selectedAlert.value
?: run {
Log.e(TAG, "EditAlertScreen: selectedAlert is null")
Toast.makeText(context, "No selected alert", Toast.LENGTH_SHORT).show()
navigationActions.navigateTo(Screen.ALERT_LIST)
return
}
Expand Down Expand Up @@ -147,10 +146,7 @@ fun EditAlertScreen(
onClick = {
alertViewModel.deleteAlert(
alert.id,
onSuccess = {
Toast.makeText(context, "Alert deleted", Toast.LENGTH_SHORT).show()
navigationActions.navigateTo(Screen.ALERT_LIST)
},
onSuccess = { navigationActions.navigateTo(Screen.ALERT_LIST) },
onFailure = { e ->
Log.e(TAG, "deleteAlert: fail to delete alert: ${e.message}")
},
Expand All @@ -177,15 +173,9 @@ fun EditAlertScreen(
else -> null
}
if (errorMessage != null) {
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a Log.d for errorMessage

return@ActionButton
}

Toast.makeText(
context,
context.getString(R.string.edit_alert_toast_successful_update),
Toast.LENGTH_SHORT)
.show()
val newAlert =
Alert(
id = alert.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.android.periodpals.ui.authentication

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -132,7 +130,6 @@ fun SignInScreen(
emailState = emailState,
passwordState = passwordState,
authenticationViewModel = authenticationViewModel,
context = context,
navigationActions = navigationActions,
)
},
Expand Down Expand Up @@ -217,47 +214,27 @@ fun AuthenticationGoogleButton(
* @param emailState The email entered by the user.
* @param passwordState The password entered by the user.
* @param authenticationViewModel The ViewModel that handles authentication logic.
* @param context The context used to show Toast messages.
* @param navigationActions The navigation actions to navigate between screens.
* @return A lambda function to be called on button click.
*/
private fun attemptSignIn(
emailState: TextFieldState,
passwordState: TextFieldState,
authenticationViewModel: AuthenticationViewModel,
context: Context,
navigationActions: NavigationActions,
) {
if (!emailState.validate() || !passwordState.validate()) {
Toast.makeText(
context, context.getString(R.string.sign_in_toast_invalid_attempt), Toast.LENGTH_SHORT)
.show()
return
}

authenticationViewModel.logInWithEmail(
userEmail = emailState.value,
userPassword = passwordState.value,
onSuccess = {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
context,
context.getString(R.string.sign_in_toast_successful_sign_in),
Toast.LENGTH_SHORT)
.show()
}
PushNotificationsServiceImpl().createDeviceToken()
navigationActions.navigateTo(Screen.PROFILE)
},
onFailure = {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
context,
context.getString(R.string.sign_in_toast_failed_sign_in),
Toast.LENGTH_SHORT)
.show()
}
},
onFailure = {},
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.android.periodpals.ui.authentication

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
Expand Down Expand Up @@ -167,7 +164,7 @@ fun SignUpScreen(
* @param passwordState The password entered by the user.
* @param confirmPasswordState The confirmed password entered by the user.
* @param authenticationViewModel The ViewModel that handles authentication logic.
* @param context The context used to show Toast messages.
* @param context The context used to display error messages.
* @param navigationActions The navigation actions to navigate between screens.
*/
private fun attemptSignUp(
Expand All @@ -180,42 +177,21 @@ private fun attemptSignUp(
) {
// strange if statements, but necessary to show the proper error messages
if (!emailState.validate() || !passwordState.validate()) {
Toast.makeText(
context, context.getString(R.string.sign_up_toast_invalid_attempt), Toast.LENGTH_SHORT)
.show()
return
}
if (!confirmPasswordState.validate() || passwordState.value != confirmPasswordState.value) {
confirmPasswordState.errorMessage =
context.getString(R.string.sign_up_not_matching_password_error_message)
Toast.makeText(
context, context.getString(R.string.sign_up_toast_invalid_attempt), Toast.LENGTH_SHORT)
.show()
return
}

authenticationViewModel.signUpWithEmail(
userEmail = emailState.value,
userPassword = passwordState.value,
onSuccess = {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
context,
context.getString(R.string.sign_up_toast_successful_sign_up),
Toast.LENGTH_SHORT)
.show()
}
PushNotificationsServiceImpl().createDeviceToken()
navigationActions.navigateTo(Screen.CREATE_PROFILE)
},
onFailure = { _: Exception ->
Handler(Looper.getMainLooper()).post {
Toast.makeText(
context,
context.getString(R.string.sign_up_toast_failed_sign_up),
Toast.LENGTH_SHORT)
.show()
}
},
onFailure = {},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ fun AuthenticationEmailInput(
else MaterialTheme.typography.labelLarge,
)
},
isError = emailErrorMessage.isNotEmpty(),
colors = getOutlinedTextFieldColors(),
)
if (emailErrorMessage.isNotEmpty()) {
Expand Down Expand Up @@ -221,6 +222,7 @@ fun AuthenticationPasswordInput(
else MaterialTheme.typography.labelLarge,
)
},
isError = passwordErrorMessage.isNotEmpty(),
visualTransformation =
if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(),
trailingIcon = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.android.periodpals.ui.components

import android.content.Context
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
Expand Down Expand Up @@ -57,8 +54,6 @@ const val LOG_TAG = "CreateProfileScreen"
const val LOG_FAILURE = "Failed to save profile"
const val LOG_SAVING_PROFILE = "Saving user profile"
const val LOG_SUCCESS = "Profile saved"
const val TOAST_FAILURE = "Failed to save profile"
const val TOAST_SUCCESS = "Profile saved"

/** A composable that displays a profile picture with [model] and [testTag] for testing purposes. */
@OptIn(ExperimentalGlideComposeApi::class)
Expand Down Expand Up @@ -209,7 +204,6 @@ fun ProfileInputDescription(description: String, onValueChange: (String) -> Unit
* @param dobState The date of birth entered by the user.
* @param descriptionState The description entered by the user.
* @param profileImageState The URI of the profile image selected by the user.
* @param context The context used to show Toast messages.
* @param userViewModel The ViewModel that handles user data.
* @param navigationActions The navigation actions to navigate between screens.
*/
Expand All @@ -221,7 +215,6 @@ fun ProfileSaveButton(
profileImageState: TextFieldState,
byteArray: ByteArray?,
preferredDistance: Int,
context: Context,
userViewModel: UserViewModel,
navigationActions: NavigationActions,
) {
Expand All @@ -238,7 +231,6 @@ fun ProfileSaveButton(
}
if (errorMessage != null) {
Log.d(LOG_TAG, "$LOG_FAILURE: $errorMessage")
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
return@Button
}

Expand All @@ -260,29 +252,14 @@ fun ProfileSaveButton(
it,
onSuccess = {
Log.d(LOG_TAG, LOG_SUCCESS)
Handler(Looper.getMainLooper())
.post { // used to show the Toast on the main thread
Toast.makeText(context, TOAST_SUCCESS, Toast.LENGTH_SHORT).show()
}
Log.d(LOG_TAG, "Profile image uploaded")
navigationActions.navigateTo(Screen.PROFILE)
},
onFailure = {
Handler(Looper.getMainLooper())
.post { // used to show the Toast on the main thread
Toast.makeText(context, TOAST_FAILURE, Toast.LENGTH_SHORT).show()
}
Log.d(LOG_TAG, LOG_FAILURE)
},
onFailure = { Log.d(LOG_TAG, LOG_FAILURE) },
)
}
},
onFailure = {
Handler(Looper.getMainLooper()).post { // used to show the Toast on the main thread
Toast.makeText(context, TOAST_FAILURE, Toast.LENGTH_SHORT).show()
}
Log.d(LOG_TAG, LOG_FAILURE)
},
onFailure = { Log.d(LOG_TAG, LOG_FAILURE) },
)
},
colors = getFilledPrimaryContainerButtonColors(),
Expand Down
Loading
Loading