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
@Parcelize | |
data class User(val userId: String, //Document ID is actually the user id | |
val name: String, | |
val bio: String, | |
val imageUrl: String) : Parcelable { | |
companion object { | |
fun DocumentSnapshot.toUser(): User? { | |
try { | |
val name = getString("name")!! |
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
object FirebaseProfileService { | |
private const val TAG = "FirebaseProfileService" | |
suspend fun getProfileData(userId: String): User? { | |
val db = FirebaseFirestore.getInstance() | |
return try { | |
db.collection("users") | |
.document(userId).get().await().toUser() | |
} catch (e: Exception) { | |
Log.e(TAG, "Error getting user details", e) | |
FirebaseCrashlytics.getInstance().log("Error getting user details") |
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
suspend fun getFriends(userId: String): List<User> { | |
val db = FirebaseFirestore.getInstance() | |
return try { | |
db.collection("users") | |
.document(userId) | |
.collection("friends").get().await() | |
.documents.mapNotNull { it.toUser() } | |
} catch (e: Exception) { | |
Log.e(TAG, "Error getting user friends", e) | |
FirebaseCrashlytics.getInstance().log("Error getting user friends") |
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
fun getPosts(userId: String): Flow<List<Post>> { | |
val db = FirebaseFirestore.getInstance() | |
return callbackFlow { | |
val listenerRegistration = db.collection("users") | |
.document(userId) | |
.collection("posts") | |
.addSnapshotListener { querySnapshot: QuerySnapshot?, firebaseFirestoreException: FirebaseFirestoreException? -> | |
if (firebaseFirestoreException != null) { | |
cancel(message = "Error fetching posts", | |
cause = firebaseFirestoreException) |