CreateChannelPresenter.kt 2.62 KB
Newer Older
1 2 3 4
package chat.rocket.android.createchannel.presentation

import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.main.presentation.MainNavigator
5
import chat.rocket.android.members.uimodel.MemberUiModelMapper
6
import chat.rocket.android.server.domain.GetCurrentServerInteractor
7
import chat.rocket.android.server.infrastructure.RocketChatClientFactory
8
import chat.rocket.android.util.extension.launchUI
9 10 11 12 13
import chat.rocket.common.RocketChatException
import chat.rocket.common.model.RoomType
import chat.rocket.common.util.ifNull
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.createChannel
14
import chat.rocket.core.internal.rest.spotlight
15 16 17 18 19
import javax.inject.Inject

class CreateChannelPresenter @Inject constructor(
    private val view: CreateChannelView,
    private val strategy: CancelStrategy,
20
    private val mapper: MemberUiModelMapper,
21 22 23 24
    private val navigator: MainNavigator,
    val serverInteractor: GetCurrentServerInteractor,
    val factory: RocketChatClientFactory
) {
25
    private val client: RocketChatClient = factory.get(serverInteractor.get()!!)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

    fun createChannel(
        roomType: RoomType,
        channelName: String,
        usersList: List<String>,
        readOnly: Boolean
    ) {
        launchUI(strategy) {
            view.showLoading()
            view.disableUserInput()
            try {
                client.createChannel(roomType, channelName, usersList, readOnly)
                view.showChannelCreatedSuccessfullyMessage()
                toChatList()
            } catch (exception: RocketChatException) {
                exception.message?.let {
                    view.showMessage(it)
                }.ifNull {
                    view.showGenericErrorMessage()
                }
            } finally {
                view.hideLoading()
                view.enableUserInput()
            }
        }
    }

    fun searchUser(query: String) {
        launchUI(strategy) {
55
            view.showSuggestionViewInProgress()
56
            try {
57
                val users = client.spotlight(query).users
58 59 60 61 62
                if (users.isEmpty()) {
                    view.showNoUserSuggestion()
                } else {
                    view.showUserSuggestion(mapper.mapToUiModelList(users))
                }
63 64 65 66 67 68
            } catch (ex: RocketChatException) {
                ex.message?.let {
                    view.showMessage(it)
                }.ifNull {
                    view.showGenericErrorMessage()
                }
69 70
            } finally {
                view.hideSuggestionViewInProgress()
71 72 73 74 75 76
            }
        }
    }

    fun toChatList() = navigator.toChatList()
}