This repository was archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathSettingPresenter.kt
More file actions
203 lines (183 loc) · 7.95 KB
/
Copy pathSettingPresenter.kt
File metadata and controls
203 lines (183 loc) · 7.95 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
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package mozilla.lockbox.presenter
import android.os.Build
import androidx.annotation.RequiresApi
import io.reactivex.Observable
import io.reactivex.functions.Consumer
import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.addTo
import kotlinx.coroutines.ExperimentalCoroutinesApi
import mozilla.lockbox.BuildConfig
import mozilla.lockbox.R
import mozilla.lockbox.action.AppWebPageAction
import mozilla.lockbox.action.DialogAction
import mozilla.lockbox.action.FingerprintAuthAction
import mozilla.lockbox.action.RouteAction
import mozilla.lockbox.action.SettingAction
import mozilla.lockbox.action.SettingIntent
import mozilla.lockbox.adapter.AppVersionSettingConfiguration
import mozilla.lockbox.adapter.SectionedAdapter
import mozilla.lockbox.adapter.SettingCellConfiguration
import mozilla.lockbox.adapter.TextSettingConfiguration
import mozilla.lockbox.adapter.ToggleSettingConfiguration
import mozilla.lockbox.flux.Dispatcher
import mozilla.lockbox.flux.Presenter
import mozilla.lockbox.store.AccountStore
import mozilla.lockbox.store.FingerprintStore
import mozilla.lockbox.store.SettingStore
interface SettingView {
fun updateSettingList(
settings: List<SettingCellConfiguration>,
sections: List<SectionedAdapter.Section>
)
}
@ExperimentalCoroutinesApi
class SettingPresenter(
val view: SettingView,
val chinaBuild: Boolean,
private val dispatcher: Dispatcher = Dispatcher.shared,
private val accountStore: AccountStore = AccountStore.shared,
private val settingStore: SettingStore = SettingStore.shared,
private val fingerprintStore: FingerprintStore = FingerprintStore.shared
) : Presenter() {
private val autoLockTimeClickListener: Consumer<Unit>
get() = Consumer {
if (fingerprintStore.isDeviceSecure) {
dispatcher.dispatch(RouteAction.AutoLockSetting)
} else {
dispatcher.dispatch(DialogAction.SecurityDisclaimer)
}
}
private val enableFingerprintObserver: Consumer<Boolean>
get() = Consumer { isToggleOn ->
if (isToggleOn && fingerprintStore.isFingerprintAuthAvailable) {
dispatcher.dispatch(SettingAction.UnlockWithFingerprintPendingAuth(true))
dispatcher.dispatch(
RouteAction.DialogFragment.FingerprintDialog(
R.string.enable_fingerprint_dialog_title,
R.string.enable_fingerprint_dialog_subtitle
)
)
} else {
dispatcher.dispatch(SettingAction.UnlockWithFingerprint(false))
}
}
private val autoFillObserver: Consumer<Boolean>
@RequiresApi(Build.VERSION_CODES.O)
get() = Consumer { newValue ->
dispatcher.dispatch(SettingAction.Autofill(newValue))
if (newValue) {
dispatcher.dispatch(RouteAction.SystemSetting(SettingIntent.Autofill))
}
}
private val sendUsageDataObserver: Consumer<Boolean>
get() = Consumer { newValue ->
dispatcher.dispatch(SettingAction.SendUsageData(newValue))
}
private val useLocalServiceObserver: Consumer<Boolean>
get() = Consumer { newValue ->
dispatcher.dispatch((SettingAction.UseLocalService(newValue)))
}
private val learnMoreSendUsageDataObserver: Consumer<Unit>
get() = Consumer {
dispatcher.dispatch(AppWebPageAction.Privacy)
}
override fun onViewReady() {
settingStore.onEnablingFingerprint
.subscribe {
when (it) {
is FingerprintAuthAction.OnSuccess ->
dispatcher.dispatch(SettingAction.UnlockWithFingerprint(true))
is FingerprintAuthAction.OnError ->
dispatcher.dispatch(SettingAction.UnlockWithFingerprint(false))
is FingerprintAuthAction.OnCancel ->
dispatcher.dispatch(SettingAction.UnlockWithFingerprint(false))
}
dispatcher.dispatch(SettingAction.UnlockWithFingerprintPendingAuth(false))
}
.addTo(compositeDisposable)
}
override fun onResume() {
super.onResume()
updateSettings()
}
private fun updateSettings() {
var configurationSettings: List<SettingCellConfiguration> = listOf(
TextSettingConfiguration(
title = R.string.auto_lock,
contentDescription = R.string.auto_lock_description,
detailTextDriver = settingStore.autoLockTime.map { it.stringValue },
clickListener = autoLockTimeClickListener
)
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && settingStore.autofillAvailable) {
configurationSettings += ToggleSettingConfiguration(
title = R.string.autofill,
subtitle = R.string.autofill_summary,
contentDescription = R.string.autofill_description,
toggleDriver = Observable.just(settingStore.isCurrentAutofillProvider),
toggleObserver = autoFillObserver
)
}
if (fingerprintStore.isFingerprintAuthAvailable) {
configurationSettings = listOf(
ToggleSettingConfiguration(
title = R.string.unlock,
contentDescription = R.string.fingerprint_description,
toggleDriver = Observables.combineLatest(
settingStore.unlockWithFingerprintPendingAuth,
settingStore.unlockWithFingerprint
)
.map { unlock -> unlock.first.takeIf { it } ?: unlock.second },
toggleObserver = enableFingerprintObserver
)
) + configurationSettings
}
val supportSettings = listOf(
ToggleSettingConfiguration(
title = R.string.send_usage_data,
subtitle = R.string.send_usage_data_summary,
contentDescription = R.string.send_usage_data,
buttonTitle = R.string.learn_more,
buttonObserver = learnMoreSendUsageDataObserver,
toggleDriver = settingStore.sendUsageData,
toggleObserver = sendUsageDataObserver
),
AppVersionSettingConfiguration(
title = R.string.app_version_title,
appVersion = BuildConfig.VERSION_NAME,
buildNumber = BuildConfig.BITRISE_BUILD_NUMBER,
contentDescription = R.string.app_version_description
)
)
val serviceSettings = listOf(
ToggleSettingConfiguration(
title = R.string.use_local_service,
contentDescription = R.string.use_local_service_description,
toggleDriver = settingStore.useLocalService,
toggleObserver = useLocalServiceObserver
)
)
val sections = listOf(
SectionedAdapter.Section(0, R.string.configuration_title),
SectionedAdapter.Section(configurationSettings.size, R.string.support_title)
)
val sectionsSync = listOf(
SectionedAdapter.Section(0, R.string.service_title),
SectionedAdapter.Section(serviceSettings.size, R.string.support_title)
)
if (chinaBuild) {
if (accountStore.checkAccountExisting()) {
view.updateSettingList(configurationSettings + supportSettings, sections)
} else {
view.updateSettingList(serviceSettings + supportSettings, sectionsSync)
}
} else {
view.updateSettingList(configurationSettings + supportSettings, sections)
}
}
}