1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package chat.rocket.android.createchannel.presentation
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.main.presentation.MainNavigator
import chat.rocket.android.members.uimodel.MemberUiModelMapper
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.infrastructure.RocketChatClientFactory
import chat.rocket.android.util.extension.launchUI
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
import chat.rocket.core.internal.rest.spotlight
import javax.inject.Inject
class CreateChannelPresenter @Inject constructor(
private val view: CreateChannelView,
private val strategy: CancelStrategy,
private val mapper: MemberUiModelMapper,
private val navigator: MainNavigator,
val serverInteractor: GetCurrentServerInteractor,
val factory: RocketChatClientFactory
) {
private val client: RocketChatClient = factory.get(serverInteractor.get()!!)
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) {
view.showSuggestionViewInProgress()
try {
val users = client.spotlight(query).users
if (users.isEmpty()) {
view.showNoUserSuggestion()
} else {
view.showUserSuggestion(mapper.mapToUiModelList(users))
}
} catch (ex: RocketChatException) {
ex.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
} finally {
view.hideSuggestionViewInProgress()
}
}
}
fun toChatList() = navigator.toChatList()
}