diff --git a/.gitignore b/.gitignore index d0f1af2e1..28c7537e2 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ local.properties *.log node_modules/ -firebase/tests/coverage/ \ No newline at end of file +firebase/tests/coverage/ +/misc/.env \ No newline at end of file diff --git a/app/src/main/java/com/android/unio/mocks/firestore/MockReferenceList.kt b/app/src/main/java/com/android/unio/mocks/firestore/MockReferenceList.kt index 914c53374..8cb55c9a8 100644 --- a/app/src/main/java/com/android/unio/mocks/firestore/MockReferenceList.kt +++ b/app/src/main/java/com/android/unio/mocks/firestore/MockReferenceList.kt @@ -13,6 +13,8 @@ class MockReferenceList(elements: List = emptyList( override fun add(uid: String) {} + override fun add(element: T) {} + override fun addAll(uids: List) {} override fun remove(uid: String) {} diff --git a/app/src/main/java/com/android/unio/model/association/AssociationViewModel.kt b/app/src/main/java/com/android/unio/model/association/AssociationViewModel.kt index f28cd066d..7c2d5cc86 100644 --- a/app/src/main/java/com/android/unio/model/association/AssociationViewModel.kt +++ b/app/src/main/java/com/android/unio/model/association/AssociationViewModel.kt @@ -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 @@ -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, diff --git a/app/src/main/java/com/android/unio/model/firestore/FirestoreReferenceList.kt b/app/src/main/java/com/android/unio/model/firestore/FirestoreReferenceList.kt index c9220514c..ff2ac1e77 100644 --- a/app/src/main/java/com/android/unio/model/firestore/FirestoreReferenceList.kt +++ b/app/src/main/java/com/android/unio/model/firestore/FirestoreReferenceList.kt @@ -60,6 +60,16 @@ class FirestoreReferenceList( _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. * @@ -75,7 +85,9 @@ class FirestoreReferenceList( * @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 } + } } /** diff --git a/app/src/main/java/com/android/unio/model/firestore/ReferenceList.kt b/app/src/main/java/com/android/unio/model/firestore/ReferenceList.kt index cc2c75dd8..dd0f4ef22 100644 --- a/app/src/main/java/com/android/unio/model/firestore/ReferenceList.kt +++ b/app/src/main/java/com/android/unio/model/firestore/ReferenceList.kt @@ -8,6 +8,8 @@ interface ReferenceList { fun add(uid: String) + fun add(element: T) + fun addAll(uids: List) fun remove(uid: String) diff --git a/app/src/main/java/com/android/unio/ui/event/EventCreation.kt b/app/src/main/java/com/android/unio/ui/event/EventCreation.kt index 1ba070879..a6f1c2ba7 100644 --- a/app/src/main/java/com/android/unio/ui/event/EventCreation.kt +++ b/app/src/main/java/com/android/unio/ui/event/EventCreation.kt @@ -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, @@ -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, diff --git a/app/src/main/java/com/android/unio/ui/event/EventEdit.kt b/app/src/main/java/com/android/unio/ui/event/EventEdit.kt index 26d11a529..7f26a2afb 100644 --- a/app/src/main/java/com/android/unio/ui/event/EventEdit.kt +++ b/app/src/main/java/com/android/unio/ui/event/EventEdit.kt @@ -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,