Flatten hierarchy in presentation module

This commit is contained in:
Moez Bhatti
2018-03-22 01:43:09 -04:00
parent 2e4d96059a
commit 7212983ffe
95 changed files with 51 additions and 65 deletions
@@ -0,0 +1,176 @@
/*
* 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/>.
*/
package presentation.feature.settings
import android.app.AlertDialog
import android.app.ProgressDialog
import android.app.TimePickerDialog
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import injection.appComponent
import util.Preferences
import common.util.extensions.dpToPx
import common.util.extensions.setVisible
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.settings_activity.*
import kotlinx.android.synthetic.main.settings_switch_widget.view.*
import presentation.common.MenuItemAdapter
import presentation.common.base.QkThemedActivity
import presentation.common.widget.PreferenceView
import javax.inject.Inject
class SettingsActivity : QkThemedActivity<SettingsViewModel>(), SettingsView {
@Inject lateinit var nightModeAdapter: MenuItemAdapter
@Inject lateinit var mmsSizeAdapter: MenuItemAdapter
override val viewModelClass = SettingsViewModel::class
override val preferenceClickIntent: Subject<PreferenceView> = PublishSubject.create()
override val nightModeSelectedIntent by lazy { nightModeAdapter.menuItemClicks }
override val viewQksmsPlusIntent: Subject<Unit> = PublishSubject.create()
override val startTimeSelectedIntent: Subject<Pair<Int, Int>> = PublishSubject.create()
override val endTimeSelectedIntent: Subject<Pair<Int, Int>> = PublishSubject.create()
override val mmsSizeSelectedIntent: Subject<Int> by lazy { mmsSizeAdapter.menuItemClicks }
// TODO remove this
private val progressDialog by lazy {
ProgressDialog(this).apply {
isIndeterminate = true
setCancelable(false)
setCanceledOnTouchOutside(false)
}
}
private var nightModeDialog: AlertDialog? = null
private var mmsSizeDialog: AlertDialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
appComponent.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.settings_activity)
setTitle(R.string.title_settings)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
viewModel.bindView(this)
nightModeAdapter.setData(R.array.night_modes)
mmsSizeAdapter.setData(R.array.mms_sizes, R.array.mms_sizes_ids)
colors.background
.autoDisposable(scope())
.subscribe { color -> window.decorView.setBackgroundColor(color) }
// Listen to clicks for all of the preferences
(0 until preferences.childCount)
.map { index -> preferences.getChildAt(index) }
.filter { view -> view is PreferenceView }
.forEach { preference ->
preference.clicks().subscribe {
preferenceClickIntent.onNext(preference as PreferenceView)
}
}
}
override fun render(state: SettingsState) {
if (progressDialog.isShowing && !state.syncing) progressDialog.dismiss()
else if (!progressDialog.isShowing && state.syncing) progressDialog.show()
defaultSms.summary = getString(when (state.isDefaultSmsApp) {
true -> R.string.settings_default_sms_summary_true
else -> R.string.settings_default_sms_summary_false
})
night.summary = state.nightModeSummary
nightStart.setVisible(state.nightModeId == Preferences.NIGHT_MODE_AUTO)
nightStart.summary = state.nightStart
nightEnd.setVisible(state.nightModeId == Preferences.NIGHT_MODE_AUTO)
nightEnd.summary = state.nightEnd
black.setVisible(state.nightModeId != Preferences.NIGHT_MODE_OFF)
black.checkbox.isChecked = state.black
autoEmoji.checkbox.isChecked = state.autoEmojiEnabled
delivery.checkbox.isChecked = state.deliveryEnabled
unicode.checkbox.isChecked = state.stripUnicodeEnabled
mms.checkbox.isChecked = state.mmsEnabled
mmsSize.summary = state.maxMmsSizeSummary
mmsSizeAdapter.selectedItem = state.maxMmsSizeId
}
// TODO change this to a PopupWindow
override fun showNightModeDialog() {
val recyclerView = RecyclerView(this)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = nightModeAdapter
recyclerView.setPadding(0, 8.dpToPx(this), 0, 8.dpToPx(this))
nightModeDialog = AlertDialog.Builder(this)
.setTitle(R.string.settings_night_title)
.setView(recyclerView)
.create().apply { show() }
}
override fun dismissNightModeDialog() {
nightModeDialog?.dismiss()
nightModeDialog = null
}
override fun showQksmsPlusSnackbar() {
Snackbar.make(contentView, R.string.toast_qksms_plus, Snackbar.LENGTH_LONG).run {
setAction(R.string.button_more, { viewQksmsPlusIntent.onNext(Unit) })
show()
}
}
override fun showStartTimePicker(hour: Int, minute: Int) {
TimePickerDialog(this, TimePickerDialog.OnTimeSetListener { _, newHour, newMinute ->
startTimeSelectedIntent.onNext(Pair(newHour, newMinute))
}, hour, minute, false).show()
}
override fun showEndTimePicker(hour: Int, minute: Int) {
TimePickerDialog(this, TimePickerDialog.OnTimeSetListener { _, newHour, newMinute ->
endTimeSelectedIntent.onNext(Pair(newHour, newMinute))
}, hour, minute, false).show()
}
override fun showMmsSizePicker() {
val recyclerView = RecyclerView(this)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = mmsSizeAdapter
recyclerView.setPadding(0, 8.dpToPx(this), 0, 8.dpToPx(this))
mmsSizeDialog = AlertDialog.Builder(this)
.setTitle(R.string.settings_mms_size_title)
.setNegativeButton(R.string.button_cancel, null)
.setView(recyclerView)
.create().apply { show() }
}
override fun dismissMmsSizePicker() {
mmsSizeDialog?.dismiss()
mmsSizeDialog = null
}
}
@@ -0,0 +1,40 @@
/*
* 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/>.
*/
package presentation.feature.settings
import util.Preferences
data class SettingsState(
val syncing: Boolean = false,
val isDefaultSmsApp: Boolean = false,
val nightModeSummary: String = "",
val nightModeId: Int = Preferences.NIGHT_MODE_OFF,
val nightStart: String = "",
val nightEnd: String = "",
val black: Boolean = false,
val autoEmojiEnabled: Boolean = true,
val notificationsEnabled: Boolean = true,
val deliveryEnabled: Boolean = false,
val splitSmsEnabled: Boolean = false,
val stripUnicodeEnabled: Boolean = false,
val mmsEnabled: Boolean = true,
val maxMmsSizeSummary: String = "100KB",
val maxMmsSizeId: Int = 100
)
@@ -0,0 +1,42 @@
/*
* 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/>.
*/
package presentation.feature.settings
import io.reactivex.Observable
import io.reactivex.subjects.Subject
import presentation.common.base.QkView
import presentation.common.widget.PreferenceView
interface SettingsView : QkView<SettingsState> {
val preferenceClickIntent: Subject<PreferenceView>
val nightModeSelectedIntent: Observable<Int>
val viewQksmsPlusIntent: Subject<Unit>
val startTimeSelectedIntent: Subject<Pair<Int, Int>>
val endTimeSelectedIntent: Subject<Pair<Int, Int>>
val mmsSizeSelectedIntent: Observable<Int>
fun showNightModeDialog()
fun dismissNightModeDialog()
fun showQksmsPlusSnackbar()
fun showStartTimePicker(hour: Int, minute: Int)
fun showEndTimePicker(hour: Int, minute: Int)
fun showMmsSizePicker()
fun dismissMmsSizePicker()
}
@@ -0,0 +1,182 @@
/*
* 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/>.
*/
package presentation.feature.settings
import android.content.Context
import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import injection.appComponent
import common.util.BillingManager
import common.util.DateFormatter
import common.util.NightModeManager
import util.Preferences
import interactor.FullSync
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
import presentation.common.Navigator
import presentation.common.base.QkViewModel
import timber.log.Timber
import java.util.*
import javax.inject.Inject
class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(SettingsState()) {
@Inject lateinit var context: Context
@Inject lateinit var billingManager: BillingManager
@Inject lateinit var dateFormatter: DateFormatter
@Inject lateinit var navigator: Navigator
@Inject lateinit var nightModeManager: NightModeManager
@Inject lateinit var prefs: Preferences
@Inject lateinit var fullSync: FullSync
init {
appComponent.inject(this)
disposables += prefs.defaultSms
.asObservable()
.subscribe { isDefaultSmsApp ->
newState { it.copy(isDefaultSmsApp = isDefaultSmsApp) }
}
val nightModeLabels = context.resources.getStringArray(R.array.night_modes)
disposables += prefs.nightMode.asObservable()
.subscribe { nightMode ->
newState { it.copy(nightModeSummary = nightModeLabels[nightMode], nightModeId = nightMode) }
}
disposables += prefs.nightStart.asObservable()
.subscribe { nightStart -> newState { it.copy(nightStart = nightStart) } }
disposables += prefs.nightEnd.asObservable()
.subscribe { nightEnd -> newState { it.copy(nightEnd = nightEnd) } }
disposables += prefs.black.asObservable()
.subscribe { black -> newState { it.copy(black = black) } }
disposables += prefs.autoEmoji.asObservable()
.subscribe { enabled -> newState { it.copy(autoEmojiEnabled = enabled) } }
disposables += prefs.notifications().asObservable()
.subscribe { enabled -> newState { it.copy(notificationsEnabled = enabled) } }
disposables += prefs.delivery.asObservable()
.subscribe { enabled -> newState { it.copy(deliveryEnabled = enabled) } }
disposables += prefs.unicode.asObservable()
.subscribe { enabled -> newState { it.copy(stripUnicodeEnabled = enabled) } }
disposables += prefs.mms.asObservable()
.subscribe { enabled -> newState { it.copy(mmsEnabled = enabled) } }
val mmsSizeLabels = context.resources.getStringArray(R.array.mms_sizes)
val mmsSizeIds = context.resources.getIntArray(R.array.mms_sizes_ids)
disposables += prefs.mmsSize.asObservable()
.subscribe { maxMmsSize ->
val index = mmsSizeIds.indexOf(maxMmsSize)
newState { it.copy(maxMmsSizeSummary = mmsSizeLabels[index], maxMmsSizeId = maxMmsSize) }
}
disposables += fullSync
}
override fun bindView(view: SettingsView) {
super.bindView(view)
view.preferenceClickIntent
.autoDisposable(view.scope())
.subscribe {
Timber.v("Preference click: ${context.resources.getResourceName(it.id)}")
when (it.id) {
R.id.defaultSms -> navigator.showDefaultSmsDialog()
R.id.theme -> navigator.showThemePicker()
R.id.night -> view.showNightModeDialog()
R.id.nightStart -> {
val date = dateFormatter.parseTime(prefs.nightStart.get())
view.showStartTimePicker(date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE))
}
R.id.nightEnd -> {
val date = dateFormatter.parseTime(prefs.nightEnd.get())
view.showEndTimePicker(date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE))
}
R.id.black -> prefs.black.set(!prefs.black.get())
R.id.autoEmoji -> prefs.autoEmoji.set(!prefs.autoEmoji.get())
R.id.notifications -> navigator.showNotificationSettings()
R.id.blocked -> navigator.showBlockedConversations()
R.id.delivery -> prefs.delivery.set(!prefs.delivery.get())
R.id.unicode -> prefs.unicode.set(!prefs.unicode.get())
R.id.mms -> prefs.mms.set(!prefs.mms.get())
R.id.mmsSize -> view.showMmsSizePicker()
R.id.sync -> {
newState { it.copy(syncing = true) }
fullSync.execute(Unit, {
newState { it.copy(syncing = false) }
})
}
}
}
view.nightModeSelectedIntent
.doOnNext { view.dismissNightModeDialog() }
.withLatestFrom(billingManager.plusStatus, { mode, plusStatus ->
if (plusStatus == BillingManager.UpgradeStatus.REGULAR && mode == Preferences.NIGHT_MODE_AUTO) {
view.showQksmsPlusSnackbar()
} else {
nightModeManager.updateNightMode(mode)
}
})
.autoDisposable(view.scope())
.subscribe()
view.viewQksmsPlusIntent
.autoDisposable(view.scope())
.subscribe { navigator.showQksmsPlusActivity() }
view.startTimeSelectedIntent
.doOnNext { nightModeManager.updateAlarms() }
.map { dateFormatter.formatTime(it.first, it.second) }
.autoDisposable(view.scope())
.subscribe { prefs.nightStart.set(it) }
view.endTimeSelectedIntent
.doOnNext { nightModeManager.updateAlarms() }
.map { dateFormatter.formatTime(it.first, it.second) }
.autoDisposable(view.scope())
.subscribe { prefs.nightEnd.set(it) }
view.mmsSizeSelectedIntent
.doOnNext { view.dismissMmsSizePicker() }
.autoDisposable(view.scope())
.subscribe { prefs.mmsSize.set(it) }
}
}