FriendsListPresenter.kt 6.75 KB
Newer Older
Niklas Fix's avatar
Niklas Fix committed
1 2
package de.akamu.tudarmstadt.features.friends

3
import de.akamu.tudarmstadt.data.duel.DuelDataSource
Niklas Fix's avatar
Niklas Fix committed
4
import de.akamu.tudarmstadt.data.friends.FriendsDataSource
5
import de.akamu.tudarmstadt.interactors.DuelInteractor
6
import de.akamu.tudarmstadt.interactors.UserInteractor
7
import de.akamu.tudarmstadt.model.Duel
Niklas Fix's avatar
Niklas Fix committed
8
import de.akamu.tudarmstadt.model.User
Niklas Fix's avatar
Niklas Fix committed
9 10
import de.akamu.tudarmstadt.util.AppUserUtil
import de.akamu.tudarmstadt.util.Constants
Niklas Fix's avatar
Niklas Fix committed
11

12
class FriendsListPresenter(
13 14
    private val friendsInteractor: FriendsListInteractor,
    private val duelInteractor: DuelInteractor,
15
    private val userInteractor: UserInteractor,
16
    private val me: User,
Niklas Fix's avatar
Niklas Fix committed
17
    private var view: FriendsListContract.View?
18
) : FriendsListContract.Presenter {
19

Niklas Fix's avatar
Niklas Fix committed
20
    lateinit var allUsers: ArrayList<User>
21 22
    lateinit var mFriends: ArrayList<User>
    lateinit var mDuels: ArrayList<Duel>
Niklas Fix's avatar
Niklas Fix committed
23
    lateinit var mRecommendedUsers: ArrayList<User>
24

Niklas Fix's avatar
Niklas Fix committed
25
    private var userFetched: Boolean = false
26 27 28
    private var friendsFetched: Boolean = false
    private var duelsFetched: Boolean = false

Niklas Fix's avatar
Niklas Fix committed
29 30
    override var ID_ME: Long = 0

31
    init {
Niklas Fix's avatar
Niklas Fix committed
32
        view?.presenter = this
33
    }
Niklas Fix's avatar
Niklas Fix committed
34

Niklas Fix's avatar
Niklas Fix committed
35 36
    override fun fetchAllUsers() {
        friendsInteractor.getAllUsers(object : FriendsDataSource.GetAllUsersCallback {
Niklas Fix's avatar
Niklas Fix committed
37 38 39 40 41
            override fun onGetAllUsersSuccess(all: ArrayList<User>) {
                allUsers = all
                userFetched = true
                handleFriendsAndDuelCallback()
            }
Niklas Fix's avatar
Niklas Fix committed
42

Niklas Fix's avatar
Niklas Fix committed
43 44 45
            override fun onGetAllUsersFailed(reason: String) {
                // silent?
            }
Niklas Fix's avatar
Niklas Fix committed
46

Niklas Fix's avatar
Niklas Fix committed
47
        })
Niklas Fix's avatar
Niklas Fix committed
48 49
    }

Niklas Fix's avatar
Niklas Fix committed
50
    override fun fetchFriends() {
51
        friendsInteractor.fetchFriends(object : FriendsDataSource.FetchFriendsCallback {
Niklas Fix's avatar
Niklas Fix committed
52
            override fun onFetchFriendsSuccess(friends: ArrayList<User>) {
53 54 55
                mFriends = friends
                friendsFetched = true
                handleFriendsAndDuelCallback()
Niklas Fix's avatar
Niklas Fix committed
56 57 58
            }

            override fun onFetchFriendsFailed(reason: String) {
Niklas Fix's avatar
Niklas Fix committed
59
                view?.showFetchFriendsFailed(reason)
Niklas Fix's avatar
Niklas Fix committed
60 61 62 63
            }
        })
    }

64 65 66 67 68 69 70 71 72
    override fun getRunningDuels() {
        duelInteractor.getDuels(object : DuelDataSource.GetDuelsCallback {
            override fun onGetDuelsSuccess(duels: ArrayList<Duel>) {
                mDuels = duels
                duelsFetched = true
                handleFriendsAndDuelCallback()
            }

            override fun onGetDuelFailed(reason: String) {
Niklas Fix's avatar
Niklas Fix committed
73
                view?.showFetchDuelsFailed(reason)
74 75 76 77 78
            }
        })
    }

    private fun handleFriendsAndDuelCallback() {
Niklas Fix's avatar
Niklas Fix committed
79 80 81
        if (friendsFetched && duelsFetched && userFetched) {
            mRecommendedUsers = getRecommendedUsers(allUsers)
            view?.bindRecommendedUsersAdapter(mRecommendedUsers, mFriends, mDuels)
Niklas Fix's avatar
Niklas Fix committed
82
            view?.bindFriendListAdapter(mFriends, mDuels)
83 84 85
        }
    }

Niklas Fix's avatar
Niklas Fix committed
86 87 88
    private fun getRecommendedUsers(allUsers: ArrayList<User>): ArrayList<User> {
        val generatedIndices: ArrayList<Int> = arrayListOf()
        val recommendedUsers: ArrayList<User> = arrayListOf()
Niklas Fix's avatar
Niklas Fix committed
89
        var allUsersCopy: ArrayList<User> = arrayListOf()
Niklas Fix's avatar
Niklas Fix committed
90
        allUsersCopy.addAll(allUsers)
Niklas Fix's avatar
Niklas Fix committed
91 92 93 94 95 96 97 98 99 100 101
        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
Niklas Fix's avatar
Niklas Fix committed
102

Niklas Fix's avatar
Niklas Fix committed
103 104
        for (i in 0 until maxShownRecommendations) {
            var index = (0 until possibleUsers.size).random()
Niklas Fix's avatar
Niklas Fix committed
105 106
            // Make sure we do not recommend the same user twice or more
            while (generatedIndices.contains(index)) {
Niklas Fix's avatar
Niklas Fix committed
107
                index = (0 until possibleUsers.size).random()
Niklas Fix's avatar
Niklas Fix committed
108 109
            }
            generatedIndices.add(index)
Niklas Fix's avatar
Niklas Fix committed
110
            recommendedUsers.add(possibleUsers[index])
Niklas Fix's avatar
Niklas Fix committed
111 112 113 114
        }
        return recommendedUsers
    }

Niklas Fix's avatar
Niklas Fix committed
115 116 117 118
    private fun isAlreadyFriend(user: User) : Boolean {
        for (friend in mFriends) {
            if (user.username == friend.username) {
                return true
Niklas Fix's avatar
Niklas Fix committed
119
            }
Niklas Fix's avatar
Niklas Fix committed
120 121 122
        }
        return false
    }
Niklas Fix's avatar
Niklas Fix committed
123

Niklas Fix's avatar
Niklas Fix committed
124 125 126 127 128 129 130 131
    override fun sendChallengeRequest(friend: User, callback: ChallengeSentCallback) {
        friendsInteractor.sendChallengeRequest(
            friend,
            object : FriendsDataSource.ChallengeRequestCallback {
                override fun onChallengeRequestSuccess(friendName: String) {
                    view?.showSendChallengeRequestSuccess(friendName)
                    callback.indicateChallengeSentSuccessfully()
                }
Niklas Fix's avatar
fix #22  
Niklas Fix committed
132

Niklas Fix's avatar
Niklas Fix committed
133 134 135 136
                override fun onChallengeRequestFailedNoPoolsSelected() {
                    view?.showSendChallengeRequestFailedNoPoolsSelected()
                    callback.indicateChallengeSentFailed()
                }
Niklas Fix's avatar
Niklas Fix committed
137

Niklas Fix's avatar
Niklas Fix committed
138 139 140 141
                override fun onChallengeRequestFailed(reason: String) {
                    view?.showSendChallengeRequestFailed(reason)
                    callback.indicateChallengeSentFailed()
                }
Niklas Fix's avatar
Niklas Fix committed
142

Niklas Fix's avatar
Niklas Fix committed
143 144 145 146 147 148
                override fun onChallengeRequestFailedNoPoolsInCommon() {
                    view?.showSendChallengeRequestFailedNoPoolsInCommon()
                    callback.indicateChallengeSentFailed()
                }

            })
Niklas Fix's avatar
Niklas Fix committed
149 150
    }

151
    override fun findUsersThatStartWith(startsWith: String) {
Niklas Fix's avatar
Niklas Fix committed
152 153 154
        val usersThatStartsWith = ArrayList<User>()
        allUsers.forEach {
            if (it.username.startsWith(startsWith, true)
Niklas Fix's avatar
Niklas Fix committed
155 156
                && it.username != me.username
            ) {
Niklas Fix's avatar
Niklas Fix committed
157
                usersThatStartsWith.add(it)
158
            }
Niklas Fix's avatar
Niklas Fix committed
159 160
        }
        view?.bindSearchListAdapter(usersThatStartsWith, mDuels)
161 162 163
    }

    override fun addFriend(user: User) {
164
        friendsInteractor.addFriend(user, object : FriendsDataSource.AddFriendCallback {
165
            override fun onAddFriendSuccess(user: User) {
Niklas Fix's avatar
Niklas Fix committed
166
                view?.showUserAddedAsFriend(user)
167 168 169
            }

            override fun onAddFriendFailed(reason: String) {
Niklas Fix's avatar
Niklas Fix committed
170
                view?.showFetchFriendsFailed(reason)
171 172 173 174 175
            }

        })
    }

Niklas Fix's avatar
Niklas Fix committed
176
    override fun deleteFriend(friend: User, callback: FriendsListAdapter.RemoveFriendCallback) {
177
        friendsInteractor.deleteFriend(friend, object : FriendsDataSource.DeleteFriendCallback {
178
            override fun onDeleteFriendSuccess(user: User) {
Niklas Fix's avatar
Niklas Fix committed
179
                view?.showDeleteFriendSuccess(user)
Niklas Fix's avatar
Niklas Fix committed
180
                callback.indicateRemoveFriendSuccessfully()
181 182 183
            }

            override fun onDeleteFriendFailed(reason: String) {
Niklas Fix's avatar
Niklas Fix committed
184
                view?.showDeleteFriendFailed(reason)
Niklas Fix's avatar
Niklas Fix committed
185
                callback.indicateRemoveFriendFailed()
186 187 188 189 190
            }

        })
    }

Niklas Fix's avatar
Niklas Fix committed
191 192 193 194
    override fun stop() {
        view = null
    }

Niklas Fix's avatar
Niklas Fix committed
195
}