SettingsPresenter.kt 8.83 KB
Newer Older
Moez Bhatti's avatar
Moez Bhatti committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
 *
 * This file is part of QKSMS.
 *
 * QKSMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QKSMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QKSMS.  If not, see <http://www.gnu.org/licenses/>.
 */
19
package com.moez.QKSMS.feature.settings
20

21
import android.content.Context
22
import com.moez.QKSMS.R
23
import com.moez.QKSMS.common.Navigator
24
import com.moez.QKSMS.common.base.QkPresenter
25 26
import com.moez.QKSMS.common.util.Colors
import com.moez.QKSMS.common.util.DateFormatter
27
import com.moez.QKSMS.common.util.extensions.makeToast
28
import com.moez.QKSMS.interactor.SyncMessages
29
import com.moez.QKSMS.repository.SyncRepository
30 31
import com.moez.QKSMS.util.NightModeManager
import com.moez.QKSMS.util.Preferences
32
import com.uber.autodispose.kotlin.autoDisposable
33
import io.reactivex.rxkotlin.plusAssign
34
import timber.log.Timber
35
import java.util.*
36
import java.util.concurrent.TimeUnit
37
import javax.inject.Inject
38

39
class SettingsPresenter @Inject constructor(
40 41 42 43 44 45 46 47
        private val context: Context,
        private val colors: Colors,
        private val dateFormatter: DateFormatter,
        private val navigator: Navigator,
        private val nightModeManager: NightModeManager,
        private val prefs: Preferences,
        private val syncMessages: SyncMessages,
        private val syncRepo: SyncRepository
48
) : QkPresenter<SettingsView, SettingsState>(SettingsState(theme = colors.theme().theme)) {
49

50 51
    init {
        newState { copy(theme = colors.theme().theme) }
52

53 54 55
        disposables += prefs.theme().asObservable()
                .subscribe { color -> newState { copy(theme = color) } }

56
        val nightModeLabels = context.resources.getStringArray(R.array.night_modes)
57
        disposables += prefs.nightMode.asObservable()
58
                .subscribe { nightMode ->
59 60 61
                    newState {
                        copy(nightModeSummary = nightModeLabels[nightMode], nightModeId = nightMode)
                    }
62
                }
63

64
        disposables += prefs.nightStart.asObservable()
65 66 67
                .map { time -> nightModeManager.parseTime(time) }
                .map { calendar -> calendar.timeInMillis }
                .map { millis -> dateFormatter.getTimestamp(millis) }
68
                .subscribe { nightStart -> newState { copy(nightStart = nightStart) } }
69

70
        disposables += prefs.nightEnd.asObservable()
71 72 73
                .map { time -> nightModeManager.parseTime(time) }
                .map { calendar -> calendar.timeInMillis }
                .map { millis -> dateFormatter.getTimestamp(millis) }
74
                .subscribe { nightEnd -> newState { copy(nightEnd = nightEnd) } }
75

76
        disposables += prefs.black.asObservable()
77
                .subscribe { black -> newState { copy(black = black) } }
78

79
        disposables += prefs.notifications().asObservable()
80
                .subscribe { enabled -> newState { copy(notificationsEnabled = enabled) } }
Moez Bhatti's avatar
Moez Bhatti committed
81

82
        disposables += prefs.autoEmoji.asObservable()
83
                .subscribe { enabled -> newState { copy(autoEmojiEnabled = enabled) } }
84

Moez Bhatti's avatar
Moez Bhatti committed
85
        val delayedSendingLabels = context.resources.getStringArray(R.array.delayed_sending_labels)
86
        disposables += prefs.sendDelay.asObservable()
87 88 89 90 91
                .subscribe { id ->
                    newState {
                        copy(sendDelaySummary = delayedSendingLabels[id], sendDelayId = id)
                    }
                }
92

93
        disposables += prefs.delivery.asObservable()
94
                .subscribe { enabled -> newState { copy(deliveryEnabled = enabled) } }
95

96
        val textSizeLabels = context.resources.getStringArray(R.array.text_sizes)
97
        disposables += prefs.textSize.asObservable()
98
                .subscribe { textSize ->
99 100 101
                    newState {
                        copy(textSizeSummary = textSizeLabels[textSize], textSizeId = textSize)
                    }
102
                }
103

104
        disposables += prefs.systemFont.asObservable()
105
                .subscribe { enabled -> newState { copy(systemFontEnabled = enabled) } }
106

107
        disposables += prefs.unicode.asObservable()
108
                .subscribe { enabled -> newState { copy(stripUnicodeEnabled = enabled) } }
109

110 111 112
        disposables += prefs.mobileOnly.asObservable()
                .subscribe { enabled -> newState { copy(mobileOnly = enabled) } }

113 114
        val mmsSizeLabels = context.resources.getStringArray(R.array.mms_sizes)
        val mmsSizeIds = context.resources.getIntArray(R.array.mms_sizes_ids)
115
        disposables += prefs.mmsSize.asObservable()
116 117
                .subscribe { maxMmsSize ->
                    val index = mmsSizeIds.indexOf(maxMmsSize)
118 119 120
                    newState {
                        copy(maxMmsSizeSummary = mmsSizeLabels[index], maxMmsSizeId = maxMmsSize)
                    }
121
                }
122

123 124 125 126 127
        disposables += syncRepo.syncProgress
                .sample(16, TimeUnit.MILLISECONDS)
                .distinctUntilChanged()
                .subscribe { syncProgress -> newState { copy(syncProgress = syncProgress) } }

128 129 130 131 132 133 134
        disposables += syncMessages
    }

    override fun bindIntents(view: SettingsView) {
        super.bindIntents(view)

        view.preferenceClicks()
135 136 137
                .autoDisposable(view.scope())
                .subscribe {
                    Timber.v("Preference click: ${context.resources.getResourceName(it.id)}")
138

139
                    when (it.id) {
140
                        R.id.theme -> view.showThemePicker()
141

142 143 144
                        R.id.night -> view.showNightModeDialog()

                        R.id.nightStart -> {
145
                            val date = nightModeManager.parseTime(prefs.nightStart.get())
146 147
                            view.showStartTimePicker(date.get(Calendar.HOUR_OF_DAY),
                                    date.get(Calendar.MINUTE))
148 149 150
                        }

                        R.id.nightEnd -> {
151
                            val date = nightModeManager.parseTime(prefs.nightEnd.get())
152 153
                            view.showEndTimePicker(date.get(Calendar.HOUR_OF_DAY),
                                    date.get(Calendar.MINUTE))
154
                        }
155

156 157
                        R.id.black -> prefs.black.set(!prefs.black.get())

158
                        R.id.autoEmoji -> prefs.autoEmoji.set(!prefs.autoEmoji.get())
159

160
                        R.id.notifications -> navigator.showNotificationSettings()
161

162 163
                        R.id.swipeActions -> view.showSwipeActions()

Moez Bhatti's avatar
Moez Bhatti committed
164 165
                        R.id.delayed -> view.showDelayDurationDialog()

166
                        R.id.delivery -> prefs.delivery.set(!prefs.delivery.get())
167

168 169
                        R.id.textSize -> view.showTextSizePicker()

170 171
                        R.id.systemFont -> prefs.systemFont.set(!prefs.systemFont.get())

172
                        R.id.unicode -> prefs.unicode.set(!prefs.unicode.get())
173

174 175
                        R.id.mobileOnly -> prefs.mobileOnly.set(!prefs.mobileOnly.get())

176
                        R.id.mmsSize -> view.showMmsSizePicker()
177

178
                        R.id.sync -> syncMessages.execute(Unit)
179

180
                        R.id.about -> view.showAbout()
181
                    }
182
                }
183

184 185 186 187 188
        view.aboutLongClicks()
                .map { !prefs.logging.get() }
                .doOnNext { enabled -> prefs.logging.set(enabled) }
                .autoDisposable(view.scope())
                .subscribe { enabled ->
189
                    context.makeToast(when (enabled) {
190 191 192 193 194
                        true -> R.string.settings_logging_enabled
                        false -> R.string.settings_logging_disabled
                    })
                }

195
        view.nightModeSelected()
196
                .autoDisposable(view.scope())
197
                .subscribe { mode -> nightModeManager.updateNightMode(mode) }
198

199
        view.nightStartSelected()
200
                .autoDisposable(view.scope())
201
                .subscribe { nightModeManager.setNightStart(it.first, it.second) }
202

203
        view.nightEndSelected()
204
                .autoDisposable(view.scope())
205
                .subscribe { nightModeManager.setNightEnd(it.first, it.second) }
206

207
        view.textSizeSelected()
208 209 210
                .autoDisposable(view.scope())
                .subscribe { prefs.textSize.set(it) }

211
        view.sendDelaySelected()
Moez Bhatti's avatar
Moez Bhatti committed
212
                .autoDisposable(view.scope())
213
                .subscribe { duration -> prefs.sendDelay.set(duration) }
Moez Bhatti's avatar
Moez Bhatti committed
214

215
        view.mmsSizeSelected()
216 217
                .autoDisposable(view.scope())
                .subscribe { prefs.mmsSize.set(it) }
218
    }
219

220
}