diff --git a/app/src/main/java/be/ugent/sel/studeez/data/remote/FirebaseFriendship.kt b/app/src/main/java/be/ugent/sel/studeez/data/remote/FirebaseFriendship.kt new file mode 100644 index 0000000..fb2af4b --- /dev/null +++ b/app/src/main/java/be/ugent/sel/studeez/data/remote/FirebaseFriendship.kt @@ -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" +} \ No newline at end of file diff --git a/app/src/main/java/be/ugent/sel/studeez/domain/FriendshipDAO.kt b/app/src/main/java/be/ugent/sel/studeez/domain/FriendshipDAO.kt index eda933d..0beb01a 100644 --- a/app/src/main/java/be/ugent/sel/studeez/domain/FriendshipDAO.kt +++ b/app/src/main/java/be/ugent/sel/studeez/domain/FriendshipDAO.kt @@ -9,15 +9,19 @@ import kotlinx.coroutines.flow.Flow interface FriendshipDAO { /** - * @return all friendships of the user that is currently logged in. + * @return all friendships of a chosen user. */ - fun getAllFriendships(): Flow> + fun getAllFriendships( + userId: String + ): Flow> /** - * @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() */ - fun getFriendshipCount(): Flow + fun getFriendshipCount( + userId: String + ): Flow /** * @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. - * @param id of the friendship that you want to update + * @param friendship the one you want to remove * @return: Success/faillure of transaction */ - fun removeFriendship(id: String): Boolean + fun removeFriendship( + friendship: Friendship + ): Boolean } \ No newline at end of file diff --git a/app/src/main/java/be/ugent/sel/studeez/domain/implementation/FirebaseFriendshipDAO.kt b/app/src/main/java/be/ugent/sel/studeez/domain/implementation/FirebaseFriendshipDAO.kt index 4665e0f..bd429e1 100644 --- a/app/src/main/java/be/ugent/sel/studeez/domain/implementation/FirebaseFriendshipDAO.kt +++ b/app/src/main/java/be/ugent/sel/studeez/domain/implementation/FirebaseFriendshipDAO.kt @@ -1,9 +1,16 @@ 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.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.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.FirebaseFirestore import com.google.firebase.firestore.ktx.snapshots @@ -23,21 +30,29 @@ class FirebaseFriendshipDAO @Inject constructor( ): FriendshipDAO { private fun currentUserDocument(): DocumentReference = firestore - .collection(FirebaseCollections.USER_COLLECTION) + .collection(USER_COLLECTION) .document(auth.currentUserId) - override fun getAllFriendships(): Flow> { - return currentUserDocument() - .collection(FirebaseCollections.FRIENDS_COLLECTION) + override fun getAllFriendships( + userId: String + ): Flow> { + return firestore + .collection(USER_COLLECTION) + .document(userId) + .collection(FRIENDS_COLLECTION) .snapshots() .map { it.toObjects(Friendship::class.java) } } - override fun getFriendshipCount(): Flow { + override fun getFriendshipCount( + userId: String + ): Flow { return flow { val friendshipCount = suspendCoroutine { continuation -> - currentUserDocument() - .collection(FirebaseCollections.FRIENDS_COLLECTION) + firestore + .collection(USER_COLLECTION) + .document(userId) + .collection(FRIENDS_COLLECTION) .get() .addOnSuccessListener { querySnapshot -> continuation.resume(querySnapshot.size()) @@ -57,15 +72,63 @@ class FirebaseFriendshipDAO @Inject constructor( } 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 { TODO("Not yet implemented") } - override fun removeFriendship(id: String): Boolean { - TODO("Not yet implemented") + override fun removeFriendship( + 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 } } \ No newline at end of file diff --git a/app/src/main/java/be/ugent/sel/studeez/screens/profile/ProfileViewModel.kt b/app/src/main/java/be/ugent/sel/studeez/screens/profile/ProfileViewModel.kt index e485de6..93fa086 100644 --- a/app/src/main/java/be/ugent/sel/studeez/screens/profile/ProfileViewModel.kt +++ b/app/src/main/java/be/ugent/sel/studeez/screens/profile/ProfileViewModel.kt @@ -5,6 +5,7 @@ import be.ugent.sel.studeez.domain.LogService import be.ugent.sel.studeez.domain.UserDAO import be.ugent.sel.studeez.navigation.StudeezDestinations import be.ugent.sel.studeez.screens.StudeezViewModel +import com.google.firebase.auth.FirebaseAuth import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.Flow import javax.inject.Inject @@ -25,7 +26,7 @@ class ProfileViewModel @Inject constructor( } fun getAmountOfFriends(): Flow { - return friendshipDAO.getFriendshipCount() + return friendshipDAO.getFriendshipCount(userDAO.getCurrentUserId()) } fun onEditProfileClick(open: (String) -> Unit) {