Skip to content

Commit

Permalink
Merge pull request #298 from SwEnt-Group13/fix/add-delete-event-local
Browse files Browse the repository at this point in the history
Fix/add delete event local
  • Loading branch information
AlouchLaBouche authored Dec 13, 2024
2 parents ecd2db5 + 792e518 commit d1b7aa4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
local.properties
*.log
node_modules/
firebase/tests/coverage/
firebase/tests/coverage/
/misc/.env
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class MockReferenceList<T : UniquelyIdentifiable>(elements: List<T> = emptyList(

override fun add(uid: String) {}

override fun add(element: T) {}

override fun addAll(uids: List<String>) {}

override fun remove(uid: String) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.android.unio.model.association

import android.util.Log
import androidx.lifecycle.ViewModel
import com.android.unio.model.event.Event
import com.android.unio.model.event.EventRepository
import com.android.unio.model.follow.ConcurrentAssociationUserRepository
import com.android.unio.model.image.ImageRepository
Expand Down Expand Up @@ -151,6 +152,48 @@ constructor(
{ exception -> Log.e("AssociationViewModel", "Failed to update follow", exception) })
}

/**
* Deletes an event from the events list of the selected association locally.
*
* @param eventId The ID of the event to be deleted.
*/
fun deleteEventLocally(eventId: String) {
val selectedAssociation = _selectedAssociation.value
if (selectedAssociation != null) {
val eventToDelete = selectedAssociation.events.uids.find { it == eventId }
// if event exists -> remove it from the events list
if (eventToDelete != null) {
selectedAssociation.events.remove(
eventId) // check the definition of remove to see that it does not fetch the database ;
// )
} else {
Log.e("AssociationViewModel", "Event with ID $eventId not found")
}
} else {
Log.e("AssociationViewModel", "No association selected to delete event from")
}
}

/**
* Adds an event to the events list of the selected association locally.
*
* @param event The event to be added.
*/
fun addEventLocally(event: Event) {
val selectedAssociation = _selectedAssociation.value
if (selectedAssociation != null) {
val eventAlreadyExists = selectedAssociation.events.uids.contains(event.uid)
// Check if the event already exists in the events list
if (!eventAlreadyExists) {
selectedAssociation.events.add(event) // Ensure `add` does not fetch the database
} else {
Log.w("AssociationViewModel", "Event with ID ${event.uid} already exists")
}
} else {
Log.e("AssociationViewModel", "No association selected to add event to")
}
}

/**
* Saves an association to the repository. If an image stream is provided, the image is uploaded
* to Firebase Storage and the image URL is saved to the association. If the image stream is null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ class FirestoreReferenceList<T : UniquelyIdentifiable>(
_uids.add(uid)
}

/**
* Adds an element to the list.
*
* @param element The element to add.
*/
override fun add(element: T) {
add(element.uid)
_list.value += element
}

/**
* Adds a list of UIDs to the list.
*
Expand All @@ -75,7 +85,9 @@ class FirestoreReferenceList<T : UniquelyIdentifiable>(
* @param uid The UID to remove.
*/
override fun remove(uid: String) {
_uids.remove(uid)
if (_uids.remove(uid)) {
_list.value = _list.value.filter { it.uid != uid }
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface ReferenceList<T> {

fun add(uid: String)

fun add(element: T)

fun addAll(uids: List<String>)

fun remove(uid: String)
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/com/android/unio/ui/event/EventCreation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ fun EventCreationScreen(
selectedLocation != null,
onClick = {
val inputStream = context.contentResolver.openInputStream(eventBannerUri.value)!!
eventViewModel.addEvent(
inputStream,
val newEvent =
Event(
uid = "", // This gets overwritten by eventViewModel.addEvent
title = name,
Expand All @@ -306,8 +305,14 @@ fun EventCreationScreen(
endDate = endTimestamp!!,
location = selectedLocation!!,
eventPictures = MockReferenceList(),
),
onSuccess = { navigationAction.goBack() },
)
eventViewModel.addEvent(
inputStream,
newEvent,
onSuccess = {
associationViewModel.addEventLocally(newEvent)
navigationAction.goBack()
},
onFailure = {
Toast.makeText(
context,
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/android/unio/ui/event/EventEdit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ fun EventEditScreen(
// A dialog should be added to prevent accidental deletion
eventViewModel.deleteEvent(
eventToEdit,
onSuccess = { navigationAction.goBack() },
onSuccess = {
associationViewModel.deleteEventLocally(eventToEdit.uid)
navigationAction.goBack()
},
onFailure = {
Toast.makeText(
context,
Expand Down

0 comments on commit d1b7aa4

Please sign in to comment.