More with friendship
This commit is contained in:
		
							parent
							
								
									a6a5fb5e95
								
							
						
					
					
						commit
						566102d5d4
					
				
					 4 changed files with 94 additions and 17 deletions
				
			
		|  | @ -0,0 +1,7 @@ | ||||||
|  | package be.ugent.sel.studeez.data.remote | ||||||
|  | 
 | ||||||
|  | object FirebaseFriendship { | ||||||
|  |     const val FRIENDID: String = "friendId" | ||||||
|  |     const val ACCEPTED: String = "accepted" | ||||||
|  |     const val FRIENDSSINCE: String = "friendsSince" | ||||||
|  | } | ||||||
|  | @ -9,15 +9,19 @@ import kotlinx.coroutines.flow.Flow | ||||||
| interface FriendshipDAO { | interface FriendshipDAO { | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @return all friendships of the user that is currently logged in. |      * @return all friendships of a chosen user. | ||||||
|      */ |      */ | ||||||
|     fun getAllFriendships(): Flow<List<Friendship>> |     fun getAllFriendships( | ||||||
|  |         userId: String | ||||||
|  |     ): Flow<List<Friendship>> | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @return the amount of friends of the currently logged in user. |      * @return the amount of friends of a chosen user. | ||||||
|      * This method should be faster than just counting the length of getAllFriends() |      * This method should be faster than just counting the length of getAllFriends() | ||||||
|      */ |      */ | ||||||
|     fun getFriendshipCount(): Flow<Int> |     fun getFriendshipCount( | ||||||
|  |         userId: String | ||||||
|  |     ): Flow<Int> | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @param id the id of the friendship that you want details of |      * @param id the id of the friendship that you want details of | ||||||
|  | @ -41,8 +45,10 @@ interface FriendshipDAO { | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Remove a friend or decline a friendrequest. |      * Remove a friend or decline a friendrequest. | ||||||
|      * @param id of the friendship that you want to update |      * @param friendship the one you want to remove | ||||||
|      * @return: Success/faillure of transaction |      * @return: Success/faillure of transaction | ||||||
|      */ |      */ | ||||||
|     fun removeFriendship(id: String): Boolean |     fun removeFriendship( | ||||||
|  |         friendship: Friendship | ||||||
|  |     ): Boolean | ||||||
| } | } | ||||||
|  | @ -1,9 +1,16 @@ | ||||||
| package be.ugent.sel.studeez.domain.implementation | package be.ugent.sel.studeez.domain.implementation | ||||||
| 
 | 
 | ||||||
|  | import androidx.compose.runtime.collectAsState | ||||||
| import be.ugent.sel.studeez.common.snackbar.SnackbarManager | import be.ugent.sel.studeez.common.snackbar.SnackbarManager | ||||||
| import be.ugent.sel.studeez.data.local.models.Friendship | import be.ugent.sel.studeez.data.local.models.Friendship | ||||||
|  | import be.ugent.sel.studeez.data.remote.FirebaseFriendship.ACCEPTED | ||||||
|  | import be.ugent.sel.studeez.data.remote.FirebaseFriendship.FRIENDSSINCE | ||||||
|  | import be.ugent.sel.studeez.data.remote.FirebaseFriendship.FRIENDID | ||||||
| import be.ugent.sel.studeez.domain.AccountDAO | import be.ugent.sel.studeez.domain.AccountDAO | ||||||
| import be.ugent.sel.studeez.domain.FriendshipDAO | import be.ugent.sel.studeez.domain.FriendshipDAO | ||||||
|  | import be.ugent.sel.studeez.domain.implementation.FirebaseCollections.FRIENDS_COLLECTION | ||||||
|  | import be.ugent.sel.studeez.domain.implementation.FirebaseCollections.USER_COLLECTION | ||||||
|  | import com.google.firebase.Timestamp | ||||||
| import com.google.firebase.firestore.DocumentReference | import com.google.firebase.firestore.DocumentReference | ||||||
| import com.google.firebase.firestore.FirebaseFirestore | import com.google.firebase.firestore.FirebaseFirestore | ||||||
| import com.google.firebase.firestore.ktx.snapshots | import com.google.firebase.firestore.ktx.snapshots | ||||||
|  | @ -23,21 +30,29 @@ class FirebaseFriendshipDAO @Inject constructor( | ||||||
| ): FriendshipDAO { | ): FriendshipDAO { | ||||||
| 
 | 
 | ||||||
|     private fun currentUserDocument(): DocumentReference = firestore |     private fun currentUserDocument(): DocumentReference = firestore | ||||||
|         .collection(FirebaseCollections.USER_COLLECTION) |         .collection(USER_COLLECTION) | ||||||
|         .document(auth.currentUserId) |         .document(auth.currentUserId) | ||||||
| 
 | 
 | ||||||
|     override fun getAllFriendships(): Flow<List<Friendship>> { |     override fun getAllFriendships( | ||||||
|         return currentUserDocument() |         userId: String | ||||||
|             .collection(FirebaseCollections.FRIENDS_COLLECTION) |     ): Flow<List<Friendship>> { | ||||||
|  |         return firestore | ||||||
|  |             .collection(USER_COLLECTION) | ||||||
|  |             .document(userId) | ||||||
|  |             .collection(FRIENDS_COLLECTION) | ||||||
|             .snapshots() |             .snapshots() | ||||||
|             .map { it.toObjects(Friendship::class.java) } |             .map { it.toObjects(Friendship::class.java) } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun getFriendshipCount(): Flow<Int> { |     override fun getFriendshipCount( | ||||||
|  |         userId: String | ||||||
|  |     ): Flow<Int> { | ||||||
|         return flow { |         return flow { | ||||||
|             val friendshipCount = suspendCoroutine { continuation -> |             val friendshipCount = suspendCoroutine { continuation -> | ||||||
|                 currentUserDocument() |                 firestore | ||||||
|                     .collection(FirebaseCollections.FRIENDS_COLLECTION) |                     .collection(USER_COLLECTION) | ||||||
|  |                     .document(userId) | ||||||
|  |                     .collection(FRIENDS_COLLECTION) | ||||||
|                     .get() |                     .get() | ||||||
|                     .addOnSuccessListener { querySnapshot -> |                     .addOnSuccessListener { querySnapshot -> | ||||||
|                         continuation.resume(querySnapshot.size()) |                         continuation.resume(querySnapshot.size()) | ||||||
|  | @ -57,15 +72,63 @@ class FirebaseFriendshipDAO @Inject constructor( | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun sendFriendshipRequest(id: String): Boolean { |     override fun sendFriendshipRequest(id: String): Boolean { | ||||||
|         TODO("Not yet implemented") |         val currentUserId: String = auth.currentUserId | ||||||
|  |         val otherUserId: String = id | ||||||
|  | 
 | ||||||
|  |         // Add entry to current user | ||||||
|  |         currentUserDocument() | ||||||
|  |             .collection(FRIENDS_COLLECTION) | ||||||
|  |             .add(mapOf( | ||||||
|  |                 FRIENDID to otherUserId, | ||||||
|  |                 ACCEPTED to true, // TODO Make it not automatically accepted. | ||||||
|  |                 FRIENDSSINCE to Timestamp.now() | ||||||
|  |             )) | ||||||
|  | 
 | ||||||
|  |         // Add entry to other user | ||||||
|  |         firestore.collection(USER_COLLECTION) | ||||||
|  |             .document(otherUserId) | ||||||
|  |             .collection(FRIENDS_COLLECTION) | ||||||
|  |             .add(mapOf( | ||||||
|  |                 FRIENDID to currentUserId, | ||||||
|  |                 ACCEPTED to true, // TODO Make it not automatically accepted. | ||||||
|  |                 FRIENDSSINCE to Timestamp.now() | ||||||
|  |             )) | ||||||
|  | 
 | ||||||
|  |         return true | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun acceptFriendship(id: String): Boolean { |     override fun acceptFriendship(id: String): Boolean { | ||||||
|         TODO("Not yet implemented") |         TODO("Not yet implemented") | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun removeFriendship(id: String): Boolean { |     override fun removeFriendship( | ||||||
|         TODO("Not yet implemented") |         friendship: Friendship | ||||||
|  |     ): Boolean { | ||||||
|  |         val currentUserId: String = auth.currentUserId | ||||||
|  |         val otherUserId: String = friendship.friendId | ||||||
|  | 
 | ||||||
|  |         // Remove at logged in user | ||||||
|  |         firestore.collection(USER_COLLECTION) | ||||||
|  |             .document(currentUserId) | ||||||
|  |             .collection(FRIENDS_COLLECTION) | ||||||
|  |             .document(friendship.id) | ||||||
|  |             .delete() | ||||||
|  | 
 | ||||||
|  |         // Remove at other user | ||||||
|  |         firestore.collection(USER_COLLECTION) | ||||||
|  |             .document(otherUserId) | ||||||
|  |             .collection(FRIENDS_COLLECTION) | ||||||
|  |             .whereEqualTo(FRIENDID, currentUserId) | ||||||
|  |             .get() | ||||||
|  |             .addOnSuccessListener { | ||||||
|  |                 for (document in it) { | ||||||
|  |                     document.reference.delete() | ||||||
|  |                 } | ||||||
|  |             }.addOnFailureListener { | ||||||
|  |                 SnackbarManager.showMessage(AppText.generic_error) | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |         return true | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | @ -5,6 +5,7 @@ import be.ugent.sel.studeez.domain.LogService | ||||||
| import be.ugent.sel.studeez.domain.UserDAO | import be.ugent.sel.studeez.domain.UserDAO | ||||||
| import be.ugent.sel.studeez.navigation.StudeezDestinations | import be.ugent.sel.studeez.navigation.StudeezDestinations | ||||||
| import be.ugent.sel.studeez.screens.StudeezViewModel | import be.ugent.sel.studeez.screens.StudeezViewModel | ||||||
|  | import com.google.firebase.auth.FirebaseAuth | ||||||
| import dagger.hilt.android.lifecycle.HiltViewModel | import dagger.hilt.android.lifecycle.HiltViewModel | ||||||
| import kotlinx.coroutines.flow.Flow | import kotlinx.coroutines.flow.Flow | ||||||
| import javax.inject.Inject | import javax.inject.Inject | ||||||
|  | @ -25,7 +26,7 @@ class ProfileViewModel @Inject constructor( | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fun getAmountOfFriends(): Flow<Int> { |     fun getAmountOfFriends(): Flow<Int> { | ||||||
|         return friendshipDAO.getFriendshipCount() |         return friendshipDAO.getFriendshipCount(userDAO.getCurrentUserId()) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fun onEditProfileClick(open: (String) -> Unit) { |     fun onEditProfileClick(open: (String) -> Unit) { | ||||||
|  |  | ||||||
		Reference in a new issue