MentionsPresenter.kt 1.72 KB
Newer Older
1 2 3 4 5
package chat.rocket.android.mentions.presentention

import chat.rocket.android.chatroom.uimodel.UiModelMapper
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
6
import chat.rocket.android.util.extension.launchUI
7 8 9 10 11
import chat.rocket.common.RocketChatException
import chat.rocket.common.util.ifNull
import chat.rocket.core.internal.rest.getMentions
import timber.log.Timber
import javax.inject.Inject
12
import javax.inject.Named
13 14 15

class MentionsPresenter @Inject constructor(
    private val view: MentionsView,
16
    @Named("currentServer") private val currentServer: String,
17 18 19 20
    private val strategy: CancelStrategy,
    private val mapper: UiModelMapper,
    val factory: RocketChatClientFactory
) {
21
    private val client = factory.get(currentServer)
22 23 24
    private var offset: Long = 0

    /**
25
     * Loads all the authenticated user mentions for the given room id.
26
     *
27
     * @param roomId The id of the room to get the mentions for the authenticated user from.
28 29 30 31 32
     */
    fun loadMentions(roomId: String) {
        launchUI(strategy) {
            try {
                view.showLoading()
33 34 35 36
                val mentions = client.getMentions(roomId, offset, 30)
                val mentionsList = mapper.map(mentions.result, asNotReversed = true)
                view.showMentions(mentionsList)
                offset += 1 * 30
37 38
            } catch (exception: RocketChatException) {
                Timber.e(exception)
39 40 41 42 43
                exception.message?.let {
                    view.showMessage(it)
                }.ifNull {
                    view.showGenericErrorMessage()
                }
44 45 46 47 48 49
            } finally {
                view.hideLoading()
            }
        }
    }
}