From 5068fb6b51a0e1958b1b880d006a44ffb102356b Mon Sep 17 00:00:00 2001 From: Kamila Babayeva Date: Sat, 30 Apr 2022 15:25:36 +0200 Subject: [PATCH 01/30] DataGetter: fix the doubleClick of Add button --- app/src/main/java/ch/sdp/vibester/database/DataGetter.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt b/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt index 5de362094..605156319 100644 --- a/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt +++ b/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt @@ -157,7 +157,8 @@ class DataGetter @Inject constructor() { users.add(userProfile) } } - return callback(users) + callback(users) + queryUsers.removeEventListener(this); } override fun onCancelled(error: DatabaseError) { Log.w(ContentValues.TAG, "searchByField:onCancelled", error.toException()) From 5ba6203ad91bc7f38081b63b2baf16a0faabc2cc Mon Sep 17 00:00:00 2001 From: Kamila Babayeva Date: Sat, 30 Apr 2022 16:30:27 +0200 Subject: [PATCH 02/30] User: add friends subfield --- app/src/main/java/ch/sdp/vibester/user/User.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/ch/sdp/vibester/user/User.kt b/app/src/main/java/ch/sdp/vibester/user/User.kt index b59abebc5..79f744822 100644 --- a/app/src/main/java/ch/sdp/vibester/user/User.kt +++ b/app/src/main/java/ch/sdp/vibester/user/User.kt @@ -9,5 +9,6 @@ data class User( var bestScore: Int = 0, var correctSongs: Int = 0, var ranking: Int = 0, - var uid: String = "" + var uid: String = "", + var friends: Map = mapOf() ) : Serializable {} \ No newline at end of file From fc07400a0b88caf57377a26a49d5fa861c7dbefd Mon Sep 17 00:00:00 2001 From: Kamila Babayeva Date: Sat, 30 Apr 2022 16:56:42 +0200 Subject: [PATCH 03/30] DataGetter: change function to get userData from firebase --- .../activity/CreateProfileActivityTest.kt | 2 +- .../vibester/activity/ProfileActivityTest.kt | 3 +-- .../sdp/vibester/activity/ProfileActivity.kt | 2 +- .../ch/sdp/vibester/database/DataGetter.kt | 24 ++++++------------- 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/app/src/androidTest/java/ch/sdp/vibester/activity/CreateProfileActivityTest.kt b/app/src/androidTest/java/ch/sdp/vibester/activity/CreateProfileActivityTest.kt index eadeffe9b..5702ef981 100644 --- a/app/src/androidTest/java/ch/sdp/vibester/activity/CreateProfileActivityTest.kt +++ b/app/src/androidTest/java/ch/sdp/vibester/activity/CreateProfileActivityTest.kt @@ -46,7 +46,7 @@ class CreateProfileActivityTest { lastArg<(String) -> Unit>().invoke(email) } - every { mockUsersRepo.getUserData(any()) } answers { + every { mockUsersRepo.getUserData(any(),any()) } answers { secondArg<(User) -> Unit>().invoke(User()) } diff --git a/app/src/androidTest/java/ch/sdp/vibester/activity/ProfileActivityTest.kt b/app/src/androidTest/java/ch/sdp/vibester/activity/ProfileActivityTest.kt index e7a6d8c7c..0007d0f19 100644 --- a/app/src/androidTest/java/ch/sdp/vibester/activity/ProfileActivityTest.kt +++ b/app/src/androidTest/java/ch/sdp/vibester/activity/ProfileActivityTest.kt @@ -46,12 +46,11 @@ class ProfileActivityTest { val mockUsersRepo = mockk() private fun createMockInvocation(mockProfile: User) { - every { mockUsersRepo.getUserData(any()) } answers { + every { mockUsersRepo.getUserData(any(),any()) } answers { secondArg<(User) -> Unit>().invoke(mockProfile) } every { mockUsersRepo.updateFieldString(any(), any(), any()) } answers {} - every { mockUsersRepo.getUserData(any())} answers {} } @After diff --git a/app/src/main/java/ch/sdp/vibester/activity/ProfileActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/ProfileActivity.kt index 303b81256..343bb2df8 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/ProfileActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/ProfileActivity.kt @@ -126,7 +126,7 @@ class ProfileActivity : AppCompatActivity() { private fun queryDatabase() { - dataGetter.getUserData(this::setupProfile) + dataGetter.getUserData(FireBaseAuthenticator().getCurrUser()!!.uid,this::setupProfile) } diff --git a/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt b/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt index 605156319..120051c1a 100644 --- a/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt +++ b/app/src/main/java/ch/sdp/vibester/database/DataGetter.kt @@ -168,25 +168,15 @@ class DataGetter @Inject constructor() { /** * This functions fetches the data of the given user from the database + * @param userId * @param callback the function to be called when the data of the appropriate user is available */ - fun getUserData(callback: (User) -> Unit) { - dbUserRef.addValueEventListener(object : ValueEventListener { - override fun onDataChange(dataSnapshot: DataSnapshot) { - for (dataSnapShot in dataSnapshot.children) { - val dbContents = dataSnapShot.getValue() - if (dbContents != null) { - if(FireBaseAuthenticator.isLoggedIn() && dbContents.email == FireBaseAuthenticator.getCurrentUserMail()) { - callback(dbContents) - break - } - } - } - } - override fun onCancelled(databaseError: DatabaseError) { - Log.w(ContentValues.TAG, "loadUsers:onCancelled", databaseError.toException()) - } - }) + fun getUserData(userId: String, callback: (User) -> Unit) { + dbUserRef.child(userId).get().addOnSuccessListener { + it.getValue()?.let { it1 -> callback(it1) } + }.addOnFailureListener{ + Log.w("DataGetter", "getUserData:onCancelled", it) + } } /** From b4c2525eb3fa23cdbc5c9ad183785dd18d8f8724 Mon Sep 17 00:00:00 2001 From: Kamila Babayeva Date: Sat, 30 Apr 2022 16:57:30 +0200 Subject: [PATCH 04/30] UserProfileAdapter: do not display add button if user is a friend --- .../sdp/vibester/user/UserProfileAdapter.kt | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/ch/sdp/vibester/user/UserProfileAdapter.kt b/app/src/main/java/ch/sdp/vibester/user/UserProfileAdapter.kt index dc7087e40..4293611c2 100644 --- a/app/src/main/java/ch/sdp/vibester/user/UserProfileAdapter.kt +++ b/app/src/main/java/ch/sdp/vibester/user/UserProfileAdapter.kt @@ -18,8 +18,15 @@ import ch.sdp.vibester.helper.loadImg class UserProfileAdapter constructor(val users: MutableList): RecyclerView.Adapter() { - val authenticator: FireBaseAuthenticator = FireBaseAuthenticator() - val usersRepo: DataGetter = DataGetter() + private val authenticator: FireBaseAuthenticator = FireBaseAuthenticator() + private val usersRepo: DataGetter = DataGetter() + private val currentUser = authenticator.getCurrUser() + private var userFriends: Array = arrayOf() + private val initFriends = usersRepo.getUserData(currentUser!!.uid,this::setFriends) + + private fun setFriends(user:User){ + userFriends = user.friends.keys.toTypedArray() + } /** * Create a RecycleView layout with the userProfile view as an item */ @@ -50,14 +57,19 @@ class UserProfileAdapter constructor(val users: MutableList): fun bind(user: User) { itemView.findViewById(R.id.search_user_username).text = user.username itemView.findViewById(R.id.profile_image).loadImg(user.image) - val addFriendBtn = itemView.findViewById