This repository was archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathSettingsPresenter.kt
More file actions
168 lines (154 loc) · 6.73 KB
/
SettingsPresenter.kt
File metadata and controls
168 lines (154 loc) · 6.73 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package chat.rocket.android.settings.presentation
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.db.DatabaseManagerFactory
import chat.rocket.android.helper.UserHelper
import chat.rocket.android.main.presentation.MainNavigator
import chat.rocket.android.server.domain.AnalyticsTrackingInteractor
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.PermissionsInteractor
import chat.rocket.android.server.domain.RemoveAccountInteractor
import chat.rocket.android.server.domain.SaveCurrentLanguageInteractor
import chat.rocket.android.server.domain.TokenRepository
import chat.rocket.android.server.infrastructure.ConnectionManagerFactory
import chat.rocket.android.server.infrastructure.RocketChatClientFactory
import chat.rocket.android.server.presentation.CheckServerPresenter
import chat.rocket.android.util.extension.gethash
import chat.rocket.android.util.extension.launchUI
import chat.rocket.android.util.extension.toHex
import chat.rocket.android.util.extensions.adminPanelUrl
import chat.rocket.android.util.extensions.avatarUrl
import chat.rocket.android.util.extensions.isNotNullNorBlank
import chat.rocket.android.util.retryIO
import chat.rocket.common.util.ifNull
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.deleteOwnAccount
import chat.rocket.core.internal.rest.me
import chat.rocket.core.internal.rest.requestDataDownload
import chat.rocket.core.internal.rest.serverInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Named
class SettingsPresenter @Inject constructor(
private val view: SettingsView,
private val strategy: CancelStrategy,
private val navigator: MainNavigator,
@Named("currentServer") private val currentServer: String,
private val userHelper: UserHelper,
private val analyticsTrackingInteractor: AnalyticsTrackingInteractor,
private val tokenRepository: TokenRepository,
private val permissions: PermissionsInteractor,
private val rocketChatClientFactory: RocketChatClientFactory,
serverInteractor: GetCurrentServerInteractor,
removeAccountInteractor: RemoveAccountInteractor,
databaseManagerFactory: DatabaseManagerFactory,
connectionManagerFactory: ConnectionManagerFactory,
private val saveLanguageInteractor: SaveCurrentLanguageInteractor
) : CheckServerPresenter(
strategy = strategy,
factory = rocketChatClientFactory,
serverInteractor = serverInteractor,
removeAccountInteractor = removeAccountInteractor,
tokenRepository = tokenRepository,
dbManagerFactory = databaseManagerFactory,
managerFactory = connectionManagerFactory,
tokenView = view,
navigator = navigator
) {
private val serverUrl = serverInteractor.get()!!
private val client: RocketChatClient = rocketChatClientFactory.get(serverUrl)
private val token = tokenRepository.get(currentServer)
fun setupView() {
launchUI(strategy) {
try {
val serverInfo = retryIO(description = "serverInfo", times = 5) {
rocketChatClientFactory.get(currentServer).serverInfo()
}
val me = retryIO(description = "serverInfo", times = 5) {
rocketChatClientFactory.get(currentServer).me()
}
userHelper.user()?.let { user ->
view.setupSettingsView(
currentServer.avatarUrl(me.username!!, token?.userId, token?.authToken),
userHelper.displayName(user) ?: me.username ?: "",
me.status.toString(),
permissions.isAdministrationEnabled(),
analyticsTrackingInteractor.get(),
true,
serverInfo.version
)
}
} catch (exception: Exception) {
Timber.d(exception, "Error getting server info")
exception.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
}
}
}
fun requestDataDownload() {
launchUI(strategy) {
try {
userHelper.user()?.let { user ->
val response = retryIO { client.requestDataDownload(user.id) }
if (response.requested) {
view.showMessage("Data download request placed successfully")
} else if (response.exportOperation.status.isNotNullNorBlank()) {
view.showMessage("Data download request already placed and has status: ${ response.exportOperation.status }")
}
}
} catch (exception: Exception) {
Timber.d(exception, "Error placing request for data download")
exception.message?.let {
view.showMessage("Error placing request for data download: $it")
}.ifNull {
view.showGenericErrorMessage()
}
}
}
}
fun enableAnalyticsTracking(isEnabled: Boolean) {
analyticsTrackingInteractor.save(isEnabled)
}
fun logout() {
setupConnectionInfo(currentServer)
super.logout(null) // TODO null?
}
fun deleteAccount(password: String) {
launchUI(strategy) {
view.showLoading()
try {
withContext(Dispatchers.Default) {
// REMARK: Backend API is only working with a lowercase hash.
// https://github.com/RocketChat/Rocket.Chat/issues/12573
retryIO {
rocketChatClientFactory.get(currentServer)
.deleteOwnAccount(password.gethash().toHex().toLowerCase())
}
setupConnectionInfo(currentServer)
logout(null)
}
} catch (exception: Exception) {
exception.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
} finally {
view.hideLoading()
}
}
}
fun saveLocale(language: String, country: String? = null) {
saveLanguageInteractor.save(language, country)
}
fun toProfile() = navigator.toProfile()
fun toAdmin() = tokenRepository.get(currentServer)?.let {
navigator.toAdminPanel(currentServer.adminPanelUrl(), it.authToken)
}
fun toLicense(licenseUrl: String, licenseTitle: String) =
navigator.toLicense(licenseUrl, licenseTitle)
}