Commit 16694cbc authored by Niklas Fix's avatar Niklas Fix 🎓

quick fix user recommendations

parent 7655553f
......@@ -117,15 +117,15 @@ class FriendsListFragment : Fragment(),
}
override fun bindRecommendedUsersAdapter(
allUsers: ArrayList<User>,
recommendedUsers: ArrayList<User>,
friends: ArrayList<User>,
duels: ArrayList<Duel>
) {
nestedScollView_friends.visibility = View.VISIBLE
textView_friends_recommended_users.visibility = View.VISIBLE
textView_your_friends.visibility = View.VISIBLE
textView_friends_recommended_users.visibility = View.VISIBLE
recyclerView_recommended_user_list.visibility = View.VISIBLE
recyclerView_recommended_user_list.adapter = RecommendedUsersAdapter(allUsers, duels, presenter, this, requireActivity())
recyclerView_recommended_user_list.adapter = RecommendedUsersAdapter(recommendedUsers, duels, presenter, this, requireActivity())
recyclerView_recommended_user_list.layoutManager = LinearLayoutManager(requireActivity(), RecyclerView.HORIZONTAL, false)
}
......
......@@ -35,7 +35,7 @@ class RecommendedUsersAdapter(
}
override fun getItemCount(): Int {
return Constants.MAX_RECOMMENDATIONS
return recommendedUsers.size
}
override fun onBindViewHolder(holder: RecommendedUsersAdapter.RecommendedUserViewHolder, position: Int) {
......
......@@ -14,7 +14,7 @@ interface FriendsListContract {
*/
fun bindFriendListAdapter(friends: ArrayList<User>, duels: ArrayList<Duel>)
/** Bind RecommendedUsersAdapter **/
fun bindRecommendedUsersAdapter(allUsers: ArrayList<User>, friends: ArrayList<User>, duels: ArrayList<Duel>)
fun bindRecommendedUsersAdapter(recommendedUsers: ArrayList<User>, friends: ArrayList<User>, duels: ArrayList<Duel>)
/** Let the user know that fetching his/her friends has failed due to [reason] **/
fun showFetchFriendsFailed(reason: String)
/** Let the user know that sending a challenge request has failed due to [reason] **/
......
......@@ -20,6 +20,7 @@ class FriendsListPresenter(
lateinit var allUsers: ArrayList<User>
lateinit var mFriends: ArrayList<User>
lateinit var mDuels: ArrayList<Duel>
lateinit var mRecommendedUsers: ArrayList<User>
private var userFetched: Boolean = false
private var friendsFetched: Boolean = false
......@@ -33,17 +34,17 @@ class FriendsListPresenter(
override fun fetchAllUsers() {
friendsInteractor.getAllUsers(object : FriendsDataSource.GetAllUsersCallback {
override fun onGetAllUsersSuccess(all: ArrayList<User>) {
allUsers = all
userFetched = true
handleFriendsAndDuelCallback()
}
override fun onGetAllUsersSuccess(all: ArrayList<User>) {
allUsers = all
userFetched = true
handleFriendsAndDuelCallback()
}
override fun onGetAllUsersFailed(reason: String) {
// silent?
}
override fun onGetAllUsersFailed(reason: String) {
// silent?
}
})
})
}
override fun fetchFriends() {
......@@ -75,8 +76,9 @@ class FriendsListPresenter(
}
private fun handleFriendsAndDuelCallback() {
if(friendsFetched && duelsFetched && userFetched) {
view?.bindRecommendedUsersAdapter(getRecommendedUsers(allUsers), mFriends, mDuels)
if (friendsFetched && duelsFetched && userFetched) {
mRecommendedUsers = getRecommendedUsers(allUsers)
view?.bindRecommendedUsersAdapter(mRecommendedUsers, mFriends, mDuels)
view?.bindFriendListAdapter(mFriends, mDuels)
}
}
......@@ -84,66 +86,74 @@ class FriendsListPresenter(
private fun getRecommendedUsers(allUsers: ArrayList<User>): ArrayList<User> {
val generatedIndices: ArrayList<Int> = arrayListOf()
val recommendedUsers: ArrayList<User> = arrayListOf()
val allUsersCopy: ArrayList<User> = arrayListOf()
var allUsersCopy: ArrayList<User> = arrayListOf()
allUsersCopy.addAll(allUsers)
val allUsersIterator = allUsersCopy.listIterator()
// Make sure we don't recommend someone who is already a friend
while (allUsersIterator.hasNext()) {
val user = allUsersIterator.next()
// Remove yourself fromm the list
if (user.id == ID_ME) {
allUsersIterator.remove()
}
for (friend in mFriends) {
if (user.username == friend.username) {
allUsersIterator.remove()
}
}
}
allUsersCopy = ArrayList(allUsersCopy.distinct())
// Make sure we don't recommend someone who is already a friend or ourselves
val possibleUsers = ArrayList(allUsersCopy.filter{user ->
user.id != ID_ME && !isAlreadyFriend(user)
})
if (possibleUsers.isEmpty()) return possibleUsers
val maxShownRecommendations =
if (possibleUsers.size < Constants.MAX_RECOMMENDATIONS) possibleUsers.size
else Constants.MAX_RECOMMENDATIONS
for (i in 0 until Constants.MAX_RECOMMENDATIONS) {
var index = (0 until allUsersCopy.size).random()
for (i in 0 until maxShownRecommendations) {
var index = (0 until possibleUsers.size).random()
// Make sure we do not recommend the same user twice or more
while (generatedIndices.contains(index)) {
index = (0 until allUsersCopy.size).random()
index = (0 until possibleUsers.size).random()
}
generatedIndices.add(index)
recommendedUsers.add(allUsersCopy[index])
recommendedUsers.add(possibleUsers[index])
}
return recommendedUsers
}
override fun sendChallengeRequest(friend: User, callback: ChallengeSentCallback) {
friendsInteractor.sendChallengeRequest(friend, object : FriendsDataSource.ChallengeRequestCallback {
override fun onChallengeRequestSuccess(friendName: String) {
view?.showSendChallengeRequestSuccess(friendName)
callback.indicateChallengeSentSuccessfully()
private fun isAlreadyFriend(user: User) : Boolean {
for (friend in mFriends) {
if (user.username == friend.username) {
return true
}
}
return false
}
override fun onChallengeRequestFailedNoPoolsSelected() {
view?.showSendChallengeRequestFailedNoPoolsSelected()
callback.indicateChallengeSentFailed()
}
override fun sendChallengeRequest(friend: User, callback: ChallengeSentCallback) {
friendsInteractor.sendChallengeRequest(
friend,
object : FriendsDataSource.ChallengeRequestCallback {
override fun onChallengeRequestSuccess(friendName: String) {
view?.showSendChallengeRequestSuccess(friendName)
callback.indicateChallengeSentSuccessfully()
}
override fun onChallengeRequestFailed(reason: String) {
view?.showSendChallengeRequestFailed(reason)
callback.indicateChallengeSentFailed()
}
override fun onChallengeRequestFailedNoPoolsSelected() {
view?.showSendChallengeRequestFailedNoPoolsSelected()
callback.indicateChallengeSentFailed()
}
override fun onChallengeRequestFailedNoPoolsInCommon() {
view?.showSendChallengeRequestFailedNoPoolsInCommon()
callback.indicateChallengeSentFailed()
}
override fun onChallengeRequestFailed(reason: String) {
view?.showSendChallengeRequestFailed(reason)
callback.indicateChallengeSentFailed()
}
})
override fun onChallengeRequestFailedNoPoolsInCommon() {
view?.showSendChallengeRequestFailedNoPoolsInCommon()
callback.indicateChallengeSentFailed()
}
})
}
override fun findUsersThatStartWith(startsWith: String) {
val usersThatStartsWith = ArrayList<User>()
allUsers.forEach {
if (it.username.startsWith(startsWith, true)
&& it.username != me.username) {
&& it.username != me.username
) {
usersThatStartsWith.add(it)
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment