Forbid adding friends twice
This commit is contained in:
parent
ef08a773f8
commit
af23a29f6c
4 changed files with 49 additions and 27 deletions
|
@ -1,11 +1,10 @@
|
|||
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.data.remote.FirebaseFriendship.FRIENDSSINCE
|
||||
import be.ugent.sel.studeez.domain.AccountDAO
|
||||
import be.ugent.sel.studeez.domain.FriendshipDAO
|
||||
import be.ugent.sel.studeez.domain.implementation.FirebaseCollections.FRIENDS_COLLECTION
|
||||
|
@ -18,6 +17,7 @@ import kotlinx.coroutines.flow.Flow
|
|||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.tasks.await
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
|
@ -75,24 +75,40 @@ class FirebaseFriendshipDAO @Inject constructor(
|
|||
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
|
||||
// Check if the friendship already exists for the logged in user
|
||||
var allowed = false
|
||||
firestore.collection(USER_COLLECTION)
|
||||
.document(otherUserId)
|
||||
.document(currentUserId)
|
||||
.collection(FRIENDS_COLLECTION)
|
||||
.add(mapOf(
|
||||
FRIENDID to currentUserId,
|
||||
ACCEPTED to true, // TODO Make it not automatically accepted.
|
||||
FRIENDSSINCE to Timestamp.now()
|
||||
))
|
||||
.whereEqualTo(FRIENDID, otherUserId)
|
||||
.get()
|
||||
.addOnSuccessListener {
|
||||
allowed = it.documents.isEmpty()
|
||||
|
||||
if (allowed) {
|
||||
// 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()
|
||||
))
|
||||
}
|
||||
}.addOnSuccessListener {
|
||||
val message = if (allowed) AppText.success else AppText.already_friend
|
||||
SnackbarManager.showMessage(message)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import be.ugent.sel.studeez.R
|
|||
import be.ugent.sel.studeez.common.composable.Headline
|
||||
import be.ugent.sel.studeez.common.composable.SecondaryScreenTemplate
|
||||
import be.ugent.sel.studeez.common.composable.drawer.DrawerEntry
|
||||
import be.ugent.sel.studeez.common.snackbar.SnackbarManager
|
||||
import be.ugent.sel.studeez.data.local.models.User
|
||||
import be.ugent.sel.studeez.resources
|
||||
import be.ugent.sel.studeez.screens.profile.AmountOfFriendsButton
|
||||
|
@ -30,7 +31,7 @@ data class PublicProfileActions(
|
|||
val getUserDetails: () -> Flow<User>,
|
||||
val getAmountOfFriends: () -> Flow<Int>,
|
||||
val onViewFriendsClick: () -> Unit,
|
||||
val sendFriendRequest: () -> Boolean
|
||||
val sendFriendRequest: () -> Unit
|
||||
)
|
||||
|
||||
fun getPublicProfileActions(
|
||||
|
@ -43,9 +44,11 @@ fun getPublicProfileActions(
|
|||
userId = viewModel.uiState.value.userId
|
||||
) },
|
||||
onViewFriendsClick = { viewModel.onViewFriendsClick(open) },
|
||||
sendFriendRequest = { viewModel.sendFriendRequest(
|
||||
userId = viewModel.uiState.value.userId
|
||||
) }
|
||||
sendFriendRequest = {
|
||||
viewModel.sendFriendRequest(
|
||||
userId = viewModel.uiState.value.userId
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -129,7 +132,7 @@ fun PublicProfilePreview() {
|
|||
},
|
||||
getAmountOfFriends = { flowOf(113) },
|
||||
onViewFriendsClick = {},
|
||||
sendFriendRequest = { true }
|
||||
sendFriendRequest = {}
|
||||
),
|
||||
popUp = {}
|
||||
)
|
||||
|
@ -138,7 +141,7 @@ fun PublicProfilePreview() {
|
|||
|
||||
@Composable
|
||||
fun PublicProfileEllipsis(
|
||||
sendFriendRequest: () -> Boolean
|
||||
sendFriendRequest: () -> Unit
|
||||
) {
|
||||
var expanded by remember { mutableStateOf(false) }
|
||||
|
||||
|
@ -172,7 +175,7 @@ fun PublicProfileEllipsis(
|
|||
fun PublicProfileEllipsisPreview() {
|
||||
StudeezTheme {
|
||||
PublicProfileEllipsis(
|
||||
sendFriendRequest = { true }
|
||||
sendFriendRequest = {}
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package be.ugent.sel.studeez.screens.profile.public_profile
|
||||
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import be.ugent.sel.studeez.common.snackbar.SnackbarManager
|
||||
import be.ugent.sel.studeez.data.SelectedUserId
|
||||
import be.ugent.sel.studeez.data.local.models.User
|
||||
import be.ugent.sel.studeez.domain.FriendshipDAO
|
||||
|
@ -11,6 +12,7 @@ import be.ugent.sel.studeez.screens.StudeezViewModel
|
|||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import javax.inject.Inject
|
||||
import be.ugent.sel.studeez.R.string as AppText
|
||||
|
||||
@HiltViewModel
|
||||
class PublicProfileViewModel @Inject constructor(
|
||||
|
@ -53,8 +55,8 @@ class PublicProfileViewModel @Inject constructor(
|
|||
|
||||
fun sendFriendRequest(
|
||||
userId: String
|
||||
): Boolean {
|
||||
return friendshipDAO.sendFriendshipRequest(userId)
|
||||
) {
|
||||
friendshipDAO.sendFriendshipRequest(userId)
|
||||
}
|
||||
|
||||
}
|
|
@ -136,6 +136,7 @@
|
|||
<string name="show_profile">Show profile</string>
|
||||
<string name="click_search_friends">Click to search friends</string>
|
||||
<string name="searching_friends">Searching friends</string>
|
||||
<string name="already_friend">You are already befriended with that person.</string>
|
||||
|
||||
<!-- ========== Create & edit screens ========== -->
|
||||
|
||||
|
|
Reference in a new issue