Skip to content

Commit

Permalink
Merge pull request #342 from PeriodPals/feat/alert/alertLists-ui-impr…
Browse files Browse the repository at this point in the history
…ovements

Feat/alert/alert lists UI improvements
  • Loading branch information
agonzalez-r authored Dec 20, 2024
2 parents 21140d6 + 0259879 commit 2f4a8d3
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 117 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/android/periodpals/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ fun PeriodPalsApp(
composable(Screen.ALERT_LIST) {
AlertListsScreen(
alertViewModel,
authenticationViewModel,
userViewModel,
authenticationViewModel,
locationViewModel,
gpsService,
chatViewModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,16 @@ private val messageValidators =
*
* @property alertModelSupabase The repository used for loading and saving alerts.
* @property userId the id linked to the current user.
* @property _alerts Mutable state holding the list of all alerts.
* @property alerts Public state exposing the list of all alerts.
* @property _myAlerts Mutable state holding the list of current users alerts.
* @property myAlerts Public state exposing the list of current users alerts.
* @property _alertsWithinRadius Mutable state holding the ordered list of all alerts within a
* specified radius.
* @property alertsWithinRadius Public state exposing the ordered list of all alerts within a
* specified radius.
* @property alertFilter Mutable state holding a filter for `filterAlerts`.
* @property _filterAlerts Mutable state holding the list of alerts filtered by `alertFilter`.
* @property filterAlerts Public state exposing the list of alerts filtered y `alertFilter`.
* @property _palAlerts Mutable state holding the list of other users alerts within selected radius.
* @property palAlerts Public state exposing the list of other users alerts within selected radius.
* @property _selectedAlert Mutable state holding the selected alert.
* @property palAlerts Public state exposing the list of other users alerts within selected radius,
* minus the accepted alerts.
* @property selectedAlert Public state exposing the selected alert.
* @property acceptedAlerts Public state exposing the list of accepted alerts.
*/
class AlertViewModel(private val alertModelSupabase: AlertModelSupabase) : ViewModel() {
companion object {
Expand Down Expand Up @@ -96,12 +91,17 @@ class AlertViewModel(private val alertModelSupabase: AlertModelSupabase) : ViewM
}
val filterAlerts: State<List<Alert>> = _filterAlerts

private var _palAlerts = derivedStateOf { _filterAlerts.value.filter { it.uid != userId.value } }
private var _palAlerts = derivedStateOf {
_filterAlerts.value.filter { it.uid != userId.value && !_acceptedAlerts.value.contains(it) }
}
val palAlerts: State<List<Alert>> = _palAlerts

private var _selectedAlert = mutableStateOf<Alert?>(null)
val selectedAlert: State<Alert?> = _selectedAlert

private var _acceptedAlerts = mutableStateOf<List<Alert>>(listOf())
val acceptedAlerts: State<List<Alert>> = _acceptedAlerts

val formState =
FormState(
fields =
Expand Down Expand Up @@ -311,4 +311,22 @@ class AlertViewModel(private val alertModelSupabase: AlertModelSupabase) : ViewM
fun selectAlert(alert: Alert) {
viewModelScope.launch { _selectedAlert.value = alert }
}

/**
* Accepts an alert and adds it to the list of accepted alerts.
*
* @param alert The alert to be accepted.
*/
fun acceptAlert(alert: Alert) {
viewModelScope.launch { _acceptedAlerts.value += alert }
}

/**
* Un-accepts an alert and removes it from the list of accepted alerts.
*
* @param alert The alert to be unaccepted.
*/
fun unAcceptAlert(alert: Alert) {
viewModelScope.launch { _acceptedAlerts.value -= alert }
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/android/periodpals/model/user/UserModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ interface UserRepository {
onFailure: (Exception) -> Unit
)

/**
* Downloads a file from the storage.
*
* @param filePath The path of the file to be downloaded.
* @param onSuccess Callback function to be called on success.
* @param onFailure Callback function to be called when there is an exception.
*/
suspend fun downloadFilePublic(
filePath: String,
onSuccess: (bytes: ByteArray) -> Unit,
onFailure: (Exception) -> Unit
)

/**
* Downloads a file from the storage.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ class UserRepositorySupabase(private val supabase: SupabaseClient) : UserReposit
) {
try {
val result =
withContext(Dispatchers.Main) {
supabase.postgrest[USERS].select {}.decodeList<UserDto>()
}
withContext(Dispatchers.IO) { supabase.postgrest[USERS].select {}.decodeList<UserDto>() }
Log.d(TAG, "loadUserProfiles: Success")
onSuccess(result)
} catch (e: Exception) {
Expand Down Expand Up @@ -138,7 +136,7 @@ class UserRepositorySupabase(private val supabase: SupabaseClient) : UserReposit
onFailure: (Exception) -> Unit,
) {
try {
withContext(Dispatchers.Main) {
withContext(Dispatchers.IO) {
val file = supabase.storage.from("avatars").downloadPublic("$filePath.jpg")
Log.d(TAG, "downloadFile: Success")
onSuccess(file)
Expand All @@ -148,4 +146,22 @@ class UserRepositorySupabase(private val supabase: SupabaseClient) : UserReposit
onFailure(e)
}
}

override suspend fun downloadFilePublic(
filePath: String,
onSuccess: (bytes: ByteArray) -> Unit,
onFailure: (Exception) -> Unit,
) {
try {
val file =
withContext(Dispatchers.IO) {
supabase.storage.from("avatars").downloadPublic("$filePath.jpg")
}
Log.d(TAG, "downloadFile: Success")
onSuccess(file)
} catch (e: Exception) {
Log.d(TAG, "downloadFile: fail to download file: ${e.message}")
onFailure(e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,33 @@ class UserViewModel(private val userRepository: UserRepositorySupabase) : ViewMo
)
}
}

/**
* Downloads a file from the storage.
*
* @param filePath The path of the file to be downloaded.
* @param onSuccess Callback function to be called on success, passes the bytes from the
* downloaded file.
* @param onFailure Callback function to be called when there is an exception.
*/
fun downloadFilePublic(
filePath: String,
onSuccess: (ByteArray) -> Unit,
onFailure: (Exception) -> Unit
) {
viewModelScope.launch {
userRepository.downloadFilePublic(
filePath,
onSuccess = { bytes ->
Log.d(TAG, "downloadFile: Success")
onSuccess(bytes)
},
onFailure = { e: Exception ->
Log.d(TAG, "downloadFile: fail to download file: ${e.message}")
onFailure(e)
})
}
}
}

/**
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/android/periodpals/resources/C.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ object C {
const val NO_ALERTS_ICON = "noAlertsIcon"
const val NO_ALERTS_TEXT = "noAlertsText"

const val ACCEPTED_ALERTS_TEXT = "acceptedAlertsText"
const val ACCEPTED_ALERTS_DIVIDER = "acceptedAlertsDivider"

const val FILTER_FAB = "filterFab"
const val FILTER_FAB_BUBBLE = "filterFabBubble"
const val FILTER_DIALOG = "filterDialog"
Expand All @@ -66,12 +69,13 @@ object C {

object PalsAlertItem {
const val PAL_ALERT = "palsAlert"
const val PAL_ACCEPTED_ALERT = "palsAcceptedAlert"
const val PAL_NAME = "palsName"
const val PAL_MESSAGE = "palMessage"
const val PAL_DIVIDER = "palDivider"
const val PAL_BUTTONS = "palButtons"
const val PAL_ACCEPT_BUTTON = "palAcceptButton"
const val PAL_DECLINE_BUTTON = "palDeclineButton"
const val PAL_UNACCEPT_BUTTON = "palUnAcceptButton"
}
}

Expand Down
Loading

0 comments on commit 2f4a8d3

Please sign in to comment.