Skip to content

Commit

Permalink
Merge pull request #42 from SwEnt-Group13/chore/firestore
Browse files Browse the repository at this point in the history
Chore/firestore #2
  • Loading branch information
oskar-codes authored Oct 10, 2024
2 parents c45f92e + 919166e commit 43b5e4d
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test

class ExploreScreen {
class ExploreScreenTest {
@get:Rule val composeTestRule = createComposeRule()

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.android.unio.model.association

import com.android.unio.model.firestore.FirestoreReferenceList
import com.android.unio.model.firestore.ReferenceList
import com.android.unio.model.user.User

data class Association(
Expand All @@ -9,5 +9,5 @@ data class Association(
val acronym: String = "",
val fullName: String = "",
val description: String = "",
val members: FirestoreReferenceList<User>
val members: ReferenceList<User>
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.google.firebase.Firebase
import com.google.firebase.auth.auth
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.firestore

class AssociationRepositoryFirestore(private val db: FirebaseFirestore) : AssociationRepository {

Expand Down Expand Up @@ -109,7 +110,7 @@ class AssociationRepositoryFirestore(private val db: FirebaseFirestore) : Associ
val members =
FirestoreReferenceList.fromList(
list = memberUids,
collectionPath = USER_PATH,
collection = Firebase.firestore.collection(USER_PATH),
hydrate = UserRepositoryFirestore::hydrate)

return Association(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.android.unio.model.association

import com.android.unio.model.firestore.FirestorePaths.USER_PATH
import com.android.unio.model.firestore.FirestoreReferenceList
import com.android.unio.model.user.UserRepositoryFirestore
import com.android.unio.model.firestore.MockReferenceList
import com.android.unio.model.user.User

enum class AssociationType {
MUSIC,
Expand All @@ -16,69 +15,67 @@ enum class AssociationType {

data class MockAssociation(val association: Association, val type: AssociationType)

val emptyMembers = {
FirestoreReferenceList.empty(
collectionPath = USER_PATH, hydrate = UserRepositoryFirestore::hydrate)
}
val emptyMembers = { MockReferenceList<User>() }

val mockAssociations =
listOf(
MockAssociation(
Association(
uid = "1",
acronym = "Musical",
fullName = "Musical Association",
description =
"AGEPoly Commission – stimulation of the practice of music on the campus",
members = emptyMembers()),
AssociationType.MUSIC),
MockAssociation(
Association(
uid = "2",
acronym = "Nuit De la Magistrale",
fullName = "Nuit De la Magistrale Association",
description =
"AGEPoly Commission – party following the formal Magistrale Graduation Ceremony",
members = emptyMembers()),
AssociationType.FESTIVALS),
MockAssociation(
Association(
uid = "3",
acronym = "Balélec",
fullName = "Festival Balélec",
description = "Open-air unique en Suisse, organisée par des bénévoles étudiants.",
members = emptyMembers()),
AssociationType.FESTIVALS),
MockAssociation(
Association(
uid = "4",
acronym = "Artiphys",
fullName = "Festival Artiphys",
description = "Festival à l'EPFL",
members = emptyMembers()),
AssociationType.FESTIVALS),
MockAssociation(
Association(
uid = "5",
acronym = "Sysmic",
fullName = "Festival Sysmic",
description = "Festival à l'EPFL",
members = emptyMembers()),
AssociationType.FESTIVALS),
MockAssociation(
Association(
uid = "6",
acronym = "IFL",
fullName = "Innovation Forum Lausanne",
description = "Innovation Forum Lausanne",
members = emptyMembers()),
AssociationType.INNOVATION),
MockAssociation(
Association(
uid = "7",
acronym = "Clic",
fullName = "Clic Association",
description = "Association of EPFL Students of IC Faculty",
members = emptyMembers()),
AssociationType.FACULTIES),
)
fun mockAssociations(): List<MockAssociation> {
return listOf(
MockAssociation(
Association(
uid = "1",
acronym = "Musical",
fullName = "Musical Association",
description =
"AGEPoly Commission – stimulation of the practice of music on the campus",
members = emptyMembers()),
AssociationType.MUSIC),
MockAssociation(
Association(
uid = "2",
acronym = "Nuit De la Magistrale",
fullName = "Nuit De la Magistrale Association",
description =
"AGEPoly Commission – party following the formal Magistrale Graduation Ceremony",
members = emptyMembers()),
AssociationType.FESTIVALS),
MockAssociation(
Association(
uid = "3",
acronym = "Balélec",
fullName = "Festival Balélec",
description = "Open-air unique en Suisse, organisée par des bénévoles étudiants.",
members = emptyMembers()),
AssociationType.FESTIVALS),
MockAssociation(
Association(
uid = "4",
acronym = "Artiphys",
fullName = "Festival Artiphys",
description = "Festival à l'EPFL",
members = emptyMembers()),
AssociationType.FESTIVALS),
MockAssociation(
Association(
uid = "5",
acronym = "Sysmic",
fullName = "Festival Sysmic",
description = "Festival à l'EPFL",
members = emptyMembers()),
AssociationType.FESTIVALS),
MockAssociation(
Association(
uid = "6",
acronym = "IFL",
fullName = "Innovation Forum Lausanne",
description = "Innovation Forum Lausanne",
members = emptyMembers()),
AssociationType.INNOVATION),
MockAssociation(
Association(
uid = "7",
acronym = "Clic",
fullName = "Clic Association",
description = "Association of EPFL Students of IC Faculty",
members = emptyMembers()),
AssociationType.FACULTIES),
)
}
8 changes: 5 additions & 3 deletions app/src/main/java/com/android/unio/model/event/Event.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.android.unio.model.event

import com.android.unio.model.association.Association
import com.android.unio.model.firestore.ReferenceList
import com.android.unio.model.map.Location
import com.google.firebase.Timestamp
import java.util.Date

data class Event(
val uid: String = "",
val title: String = "",
val organisers: List<String> = mutableListOf<String>(),
val taggedAssociations: List<String> = mutableListOf<String>(),
val organisers: ReferenceList<Association>,
val taggedAssociations: ReferenceList<Association>,
val image: String = "",
val description: String = "",
val catchyDescription: String = "",
val price: Double = 0.0,
val date: Timestamp = Timestamp(Date()),
val location: Location = Location(),
val types: List<String> = mutableListOf<String>()
val types: List<String> = mutableListOf()
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.android.unio.model.event

import android.util.Log
import com.android.unio.model.firestore.FirestorePaths.EVENT_PATH
import com.google.firebase.Timestamp
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
Expand Down Expand Up @@ -77,16 +78,13 @@ class EventRepositoryFirestore(private val db: FirebaseFirestore) : EventReposit
}
}

private fun hydrate(doc: DocumentSnapshot): Event? {

val event = doc.toObject(Event::class.java)
if (event == null) {
Log.e("EventRepositoryFirestore", "Error while converting db document to Event object")
}
return event
}

companion object {
private const val EVENT_PATH = "events"
fun hydrate(doc: DocumentSnapshot): Event? {
val event = doc.toObject(Event::class.java)
if (event == null) {
Log.e("EventRepositoryFirestore", "Error while converting db document to Event object")
}
return event
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package com.android.unio.model.firestore
object FirestorePaths {
const val ASSOCIATION_PATH = "associations"
const val USER_PATH = "users"
const val EVENT_PATH = "events"
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.android.unio.model.firestore

import android.util.Log
import com.google.firebase.firestore.CollectionReference
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import com.google.firebase.firestore.FieldPath
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

Expand All @@ -17,23 +17,21 @@ import kotlinx.coroutines.flow.StateFlow
* ```
*
* @param T The type of the objects in the list.
* @property db The [FirebaseFirestore] instance to use.
* @property collectionPath The path to the Firestore collection that contains the objects.
* @property collection The reference to the Firestore collection.
* @property hydrate A function that converts a [DocumentSnapshot] to a [T].
*/
class FirestoreReferenceList<T>(
private val db: FirebaseFirestore,
private val collectionPath: String,
private val collection: CollectionReference,
private val hydrate: (DocumentSnapshot) -> T
) : ReferenceList {
) : ReferenceList<T> {
// The internal list of UIDs.
private var _uids = mutableListOf<String>()
private val _uids = mutableListOf<String>()

// The internal list of objects.
private val _list = MutableStateFlow<List<T>>(emptyList())

// The public list of objects.
val list: StateFlow<List<T>> = _list
override val list: StateFlow<List<T>> = _list

/**
* Adds a UID to the list.
Expand All @@ -55,37 +53,37 @@ class FirestoreReferenceList<T>(

/** Requests all documents from Firestore and updates the list. */
override fun requestAll() {
println("Requesting all")
_list.value = emptyList()
_uids.forEach { uid ->
db.collection(collectionPath).document(uid).get().addOnSuccessListener { result ->
val item = hydrate(result)
_list.value += item
println("Added $item")
}
}
collection
.whereIn(FieldPath.documentId(), _uids)
.get()
.addOnSuccessListener { result ->
val items = result.documents.map { hydrate(it) }
_list.value = items
}
.addOnFailureListener { exception ->
Log.e("FirestoreReferenceList", "Failed to get documents", exception)
}
}

companion object {
/** Creates a [FirestoreReferenceList] from a list of UIDs. */
fun <T> fromList(
list: List<String>,
db: FirebaseFirestore = Firebase.firestore,
collectionPath: String,
collection: CollectionReference,
hydrate: (DocumentSnapshot) -> T
): FirestoreReferenceList<T> {
val result = FirestoreReferenceList(db, collectionPath, hydrate)
val result = FirestoreReferenceList(collection, hydrate)
result.addAll(list)
return result
}

/** Creates an empty [FirestoreReferenceList]. */
fun <T> empty(
db: FirebaseFirestore = Firebase.firestore,
collectionPath: String,
collection: CollectionReference,
hydrate: (DocumentSnapshot) -> T
): FirestoreReferenceList<T> {
return FirestoreReferenceList(db, collectionPath, hydrate)
return FirestoreReferenceList(collection, hydrate)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
package com.android.unio.model.firestore

interface ReferenceList {
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

interface ReferenceList<T> {
val list: StateFlow<List<T>>

fun add(uid: String)

fun addAll(uids: List<String>)

fun requestAll()
}

class MockReferenceList<T> : ReferenceList<T> {
private val _uids = mutableListOf<String>()
private val _list = MutableStateFlow<List<T>>(emptyList())
override val list: StateFlow<List<T>> = _list

override fun add(uid: String) {
_uids.add(uid)
}

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

override fun requestAll() {
_list.value = emptyList()
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/android/unio/model/user/User.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.android.unio.model.user

import com.android.unio.model.association.Association
import com.android.unio.model.firestore.FirestoreReferenceList
import com.android.unio.model.firestore.ReferenceList

data class User(
val uid: String,
val name: String,
val email: String,
val followingAssociations: FirestoreReferenceList<Association>
val followingAssociations: ReferenceList<Association>
)
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ class UserRepositoryFirestore(private val db: FirebaseFirestore) : UserRepositor
val followingAssociations =
FirestoreReferenceList.fromList(
followingAssociationsUids,
db,
ASSOCIATION_PATH,
db.collection(ASSOCIATION_PATH),
AssociationRepositoryFirestore::hydrate)

return User(
Expand Down
Loading

0 comments on commit 43b5e4d

Please sign in to comment.