/
HomePresenterImpl.kt
215 lines (180 loc) · 7.71 KB
/
HomePresenterImpl.kt
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package io.homeassistant.companion.android.home
import android.util.Log
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.data.authentication.AuthenticationRepository
import io.homeassistant.companion.android.common.data.authentication.SessionState
import io.homeassistant.companion.android.common.data.integration.DeviceRegistration
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import io.homeassistant.companion.android.common.data.websocket.WebSocketRepository
import io.homeassistant.companion.android.common.data.websocket.WebSocketState
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryResponse
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryUpdatedEvent
import io.homeassistant.companion.android.common.data.websocket.impl.entities.DeviceRegistryResponse
import io.homeassistant.companion.android.common.data.websocket.impl.entities.DeviceRegistryUpdatedEvent
import io.homeassistant.companion.android.common.data.websocket.impl.entities.EntityRegistryResponse
import io.homeassistant.companion.android.common.data.websocket.impl.entities.EntityRegistryUpdatedEvent
import io.homeassistant.companion.android.data.SimplifiedEntity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import javax.inject.Inject
import io.homeassistant.companion.android.common.R as commonR
class HomePresenterImpl @Inject constructor(
private val authenticationUseCase: AuthenticationRepository,
private val integrationUseCase: IntegrationRepository,
private val webSocketUseCase: WebSocketRepository
) : HomePresenter {
companion object {
val toggleDomains = listOf(
"cover", "fan", "humidifier", "input_boolean", "light", "lock",
"media_player", "remote", "siren", "switch"
)
val domainsWithNames = mapOf(
"button" to commonR.string.buttons,
"cover" to commonR.string.covers,
"fan" to commonR.string.fans,
"input_boolean" to commonR.string.input_booleans,
"input_button" to commonR.string.input_buttons,
"light" to commonR.string.lights,
"lock" to commonR.string.locks,
"switch" to commonR.string.switches,
"script" to commonR.string.scripts,
"scene" to commonR.string.scenes
)
val supportedDomains = domainsWithNames.keys.toList()
const val TAG = "HomePresenter"
}
private val mainScope: CoroutineScope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var view: HomeView
override fun init(homeView: HomeView) {
view = homeView
}
override fun onViewReady() {
mainScope.launch {
val sessionValid = authenticationUseCase.getSessionState() == SessionState.CONNECTED
if (sessionValid && integrationUseCase.isRegistered()) {
resyncRegistration()
} else if (sessionValid) {
view.displayMobileAppIntegration()
} else {
view.displayOnBoarding()
}
}
}
override suspend fun getEntities(): List<Entity<*>>? {
return integrationUseCase.getEntities()
}
override suspend fun getEntityUpdates(): Flow<Entity<*>>? {
return integrationUseCase.getEntityUpdates()
}
override suspend fun onEntityClicked(entityId: String, state: String) {
val domain = entityId.split(".")[0]
val serviceName = when (domain) {
"button", "input_button" -> "press"
"lock" -> {
// Defaults to locking, to be save
if (state == "locked")
"unlock"
else
"lock"
}
in toggleDomains -> "toggle"
else -> "turn_on"
}
integrationUseCase.callService(
domain,
serviceName,
hashMapOf("entity_id" to entityId)
)
}
override fun onInvalidAuthorization() = finishSession()
override fun onLogoutClicked() = finishSession()
private fun finishSession() {
mainScope.launch {
authenticationUseCase.revokeSession()
view.displayOnBoarding()
}
}
override fun onFinish() {
mainScope.cancel()
}
private fun resyncRegistration() {
mainScope.launch {
try {
integrationUseCase.updateRegistration(
DeviceRegistration(
"${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})",
null,
null
)
)
} catch (e: Exception) {
Log.e(TAG, "Issue updating Registration", e)
}
}
}
override suspend fun isConnected(): Boolean {
return integrationUseCase.isRegistered()
}
override fun getWebSocketState(): WebSocketState? {
return webSocketUseCase.getConnectionState()
}
override suspend fun getAreaRegistry(): List<AreaRegistryResponse>? {
return webSocketUseCase.getAreaRegistry()
}
override suspend fun getDeviceRegistry(): List<DeviceRegistryResponse>? {
return webSocketUseCase.getDeviceRegistry()
}
override suspend fun getEntityRegistry(): List<EntityRegistryResponse>? {
return webSocketUseCase.getEntityRegistry()
}
override suspend fun getAreaRegistryUpdates(): Flow<AreaRegistryUpdatedEvent>? {
return webSocketUseCase.getAreaRegistryUpdates()
}
override suspend fun getDeviceRegistryUpdates(): Flow<DeviceRegistryUpdatedEvent>? {
return webSocketUseCase.getDeviceRegistryUpdates()
}
override suspend fun getEntityRegistryUpdates(): Flow<EntityRegistryUpdatedEvent>? {
return webSocketUseCase.getEntityRegistryUpdates()
}
override suspend fun getTileShortcuts(): List<SimplifiedEntity> {
return integrationUseCase.getTileShortcuts().map { SimplifiedEntity(it) }
}
override suspend fun setTileShortcuts(entities: List<SimplifiedEntity>) {
integrationUseCase.setTileShortcuts(entities.map { it.entityString })
}
override suspend fun getWearHapticFeedback(): Boolean {
return integrationUseCase.getWearHapticFeedback()
}
override suspend fun setWearHapticFeedback(enabled: Boolean) {
integrationUseCase.setWearHapticFeedback(enabled)
}
override suspend fun getWearToastConfirmation(): Boolean {
return integrationUseCase.getWearToastConfirmation()
}
override suspend fun setWearToastConfirmation(enabled: Boolean) {
integrationUseCase.setWearToastConfirmation(enabled)
}
override suspend fun getShowShortcutText(): Boolean {
return integrationUseCase.getShowShortcutText()
}
override suspend fun setShowShortcutTextEnabled(enabled: Boolean) {
integrationUseCase.setShowShortcutTextEnabled(enabled)
}
override suspend fun getTemplateTileContent(): String {
return integrationUseCase.getTemplateTileContent()
}
override suspend fun setTemplateTileContent(content: String) {
integrationUseCase.setTemplateTileContent(content)
}
override suspend fun getTemplateTileRefreshInterval(): Int {
return integrationUseCase.getTemplateTileRefreshInterval()
}
override suspend fun setTemplateTileRefreshInterval(interval: Int) {
integrationUseCase.setTemplateTileRefreshInterval(interval)
}
}