-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from SwEnt-Group13/chore/firestore
Chore/firestore
- Loading branch information
Showing
15 changed files
with
644 additions
and
72 deletions.
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5
app/src/main/java/com/android/unio/model/association/Association.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package com.android.unio.model.association | ||
|
||
import com.android.unio.model.firestore.FirestoreReferenceList | ||
import com.android.unio.model.user.User | ||
|
||
data class Association( | ||
val uid: String, | ||
val url: String = "", | ||
val acronym: String = "", | ||
val fullName: String = "", | ||
val description: String = "", | ||
val members: List<String> = emptyList() | ||
val members: FirestoreReferenceList<User> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/android/unio/model/firestore/FirestorePaths.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.android.unio.model.firestore | ||
|
||
object FirestorePaths { | ||
const val ASSOCIATION_PATH = "associations" | ||
const val USER_PATH = "users" | ||
} |
91 changes: 91 additions & 0 deletions
91
app/src/main/java/com/android/unio/model/firestore/FirestoreReferenceList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.android.unio.model.firestore | ||
|
||
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 kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
/** | ||
* A class that represents a list of Firestore objects that are identified by their UIDs. The list | ||
* is stored as a [StateFlow] and can be updated by calling [requestAll]. | ||
* | ||
* In a @Composable function, the list should be accessed using: | ||
* ```kotlin | ||
* val list by FirestoreReferenceList.list.collectAsState() | ||
* ``` | ||
* | ||
* @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 hydrate A function that converts a [DocumentSnapshot] to a [T]. | ||
*/ | ||
class FirestoreReferenceList<T>( | ||
private val db: FirebaseFirestore, | ||
private val collectionPath: String, | ||
private val hydrate: (DocumentSnapshot) -> T | ||
) : ReferenceList { | ||
// The internal list of UIDs. | ||
private var _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 | ||
|
||
/** | ||
* Adds a UID to the list. | ||
* | ||
* @param uid The UID to add. | ||
*/ | ||
override fun add(uid: String) { | ||
_uids.add(uid) | ||
} | ||
|
||
/** | ||
* Adds a list of UIDs to the list. | ||
* | ||
* @param uids The UIDs to add. | ||
*/ | ||
override fun addAll(uids: List<String>) { | ||
_uids.addAll(uids) | ||
} | ||
|
||
/** 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") | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
/** Creates a [FirestoreReferenceList] from a list of UIDs. */ | ||
fun <T> fromList( | ||
list: List<String>, | ||
db: FirebaseFirestore = Firebase.firestore, | ||
collectionPath: String, | ||
hydrate: (DocumentSnapshot) -> T | ||
): FirestoreReferenceList<T> { | ||
val result = FirestoreReferenceList(db, collectionPath, hydrate) | ||
result.addAll(list) | ||
return result | ||
} | ||
|
||
/** Creates an empty [FirestoreReferenceList]. */ | ||
fun <T> empty( | ||
db: FirebaseFirestore = Firebase.firestore, | ||
collectionPath: String, | ||
hydrate: (DocumentSnapshot) -> T | ||
): FirestoreReferenceList<T> { | ||
return FirestoreReferenceList(db, collectionPath, hydrate) | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/android/unio/model/firestore/ReferenceList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.android.unio.model.firestore | ||
|
||
interface ReferenceList { | ||
fun add(uid: String) | ||
|
||
fun addAll(uids: List<String>) | ||
|
||
fun requestAll() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package com.android.unio.model.user | ||
|
||
import com.android.unio.model.association.Association | ||
import com.android.unio.model.firestore.FirestoreReferenceList | ||
|
||
data class User( | ||
val id: String, | ||
val uid: String, | ||
val name: String, | ||
val email: String, | ||
val followingAssociations: List<Association> | ||
val followingAssociations: FirestoreReferenceList<Association> | ||
) |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/android/unio/model/user/UserRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.android.unio.model.user | ||
|
||
interface UserRepository { | ||
fun init(onSuccess: () -> Unit) | ||
|
||
fun getUsers(onSuccess: (List<User>) -> Unit, onFailure: (Exception) -> Unit) | ||
|
||
fun getUserWithId(id: String, onSuccess: (User) -> Unit, onFailure: (Exception) -> Unit) | ||
} |
67 changes: 67 additions & 0 deletions
67
app/src/main/java/com/android/unio/model/user/UserRepositoryFirestore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.android.unio.model.user | ||
|
||
import com.android.unio.model.association.AssociationRepositoryFirestore | ||
import com.android.unio.model.firestore.FirestorePaths.ASSOCIATION_PATH | ||
import com.android.unio.model.firestore.FirestorePaths.USER_PATH | ||
import com.android.unio.model.firestore.FirestoreReferenceList | ||
import com.google.firebase.Firebase | ||
import com.google.firebase.auth.auth | ||
import com.google.firebase.firestore.DocumentSnapshot | ||
import com.google.firebase.firestore.FirebaseFirestore | ||
|
||
class UserRepositoryFirestore(private val db: FirebaseFirestore) : UserRepository { | ||
|
||
override fun init(onSuccess: () -> Unit) { | ||
Firebase.auth.addAuthStateListener { | ||
if (it.currentUser != null) { | ||
onSuccess() | ||
} | ||
} | ||
} | ||
|
||
override fun getUsers(onSuccess: (List<User>) -> Unit, onFailure: (Exception) -> Unit) { | ||
db.collection(USER_PATH) | ||
.get() | ||
.addOnSuccessListener { result -> | ||
val associations = result.map { hydrate(it) } | ||
onSuccess(associations) | ||
} | ||
.addOnFailureListener { exception -> onFailure(exception) } | ||
} | ||
|
||
override fun getUserWithId( | ||
id: String, | ||
onSuccess: (User) -> Unit, | ||
onFailure: (Exception) -> Unit | ||
) { | ||
db.collection(USER_PATH) | ||
.document(id) | ||
.get() | ||
.addOnSuccessListener { document -> | ||
val association = hydrate(document) | ||
onSuccess(association) | ||
} | ||
.addOnFailureListener { exception -> onFailure(exception) } | ||
} | ||
|
||
companion object { | ||
fun hydrate(doc: DocumentSnapshot): User { | ||
val db = FirebaseFirestore.getInstance() | ||
|
||
val followingAssociationsUids = | ||
doc.get("followingAssociations") as? List<String> ?: emptyList() | ||
val followingAssociations = | ||
FirestoreReferenceList.fromList( | ||
followingAssociationsUids, | ||
db, | ||
ASSOCIATION_PATH, | ||
AssociationRepositoryFirestore::hydrate) | ||
|
||
return User( | ||
uid = doc.id, | ||
name = doc.getString("name") ?: "", | ||
email = doc.getString("email") ?: "", | ||
followingAssociations = followingAssociations) | ||
} | ||
} | ||
} |
Oops, something went wrong.