Build new UI for changing blocking manager

This commit is contained in:
moezbhatti
2019-06-24 01:41:31 -04:00
parent c1ab08dbc0
commit 55073d0f35
22 changed files with 531 additions and 248 deletions
@@ -19,46 +19,33 @@
package com.moez.QKSMS.feature.blocking
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.jakewharton.rxbinding2.view.clicks
import com.bluelinelabs.conductor.Conductor
import com.bluelinelabs.conductor.Router
import com.bluelinelabs.conductor.RouterTransaction
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkThemedActivity
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.blocking_activity.*
import kotlinx.android.synthetic.main.settings_switch_widget.view.*
import javax.inject.Inject
import kotlinx.android.synthetic.main.container_activity.*
class BlockingActivity : QkThemedActivity(), BlockingView {
class BlockingActivity : QkThemedActivity() {
@Inject lateinit var blockingAdapter: BlockingAdapter
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
override val ccClickedIntent by lazy { callControl.clicks() }
override val siaClickedIntent by lazy { shouldIAnswer.clicks() }
override val dropClickedIntent by lazy { drop.clicks() }
override val conversationClicks by lazy { blockingAdapter.clicks }
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory)[BlockingViewModel::class.java] }
private lateinit var router: Router
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.blocking_activity)
setTitle(R.string.blocking_title)
showBackButton(true)
viewModel.bindView(this)
setContentView(R.layout.container_activity)
blockingAdapter.emptyView = empty
conversations.adapter = blockingAdapter
router = Conductor.attachRouter(this, container, savedInstanceState)
if (!router.hasRootController()) {
router.setRoot(RouterTransaction.with(BlockingController()))
}
}
override fun render(state: BlockingState) {
callControl.checkbox.isChecked = state.ccEnabled
shouldIAnswer.checkbox.isChecked = state.siaEnabled
drop.checkbox.isChecked = state.dropEnabled
blockingAdapter.updateData(state.data)
override fun onBackPressed() {
if (!router.handleBack()) {
super.onBackPressed()
}
}
}
@@ -1,35 +0,0 @@
/*
* 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 com.moez.QKSMS.feature.blocking
import androidx.lifecycle.ViewModel
import com.moez.QKSMS.injection.ViewModelKey
import dagger.Module
import dagger.Provides
import dagger.multibindings.IntoMap
@Module
class BlockingActivityModule {
@Provides
@IntoMap
@ViewModelKey(BlockingViewModel::class)
fun provideBlockingViewModel(viewModel: BlockingViewModel): ViewModel = viewModel
}
@@ -0,0 +1,58 @@
package com.moez.QKSMS.feature.blocking
import android.view.View
import androidx.core.view.isVisible
import com.bluelinelabs.conductor.RouterTransaction
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.moez.QKSMS.common.QkChangeHandler
import com.moez.QKSMS.common.base.QkController
import com.moez.QKSMS.feature.blocking.manager.BlockingManagerController
import com.moez.QKSMS.injection.appComponent
import kotlinx.android.synthetic.main.blocking_controller.*
import kotlinx.android.synthetic.main.settings_switch_widget.view.*
import javax.inject.Inject
class BlockingController : QkController<BlockingView, BlockingState, BlockingPresenter>(), BlockingView {
override val blockingManagerIntent by lazy { blockingManager.clicks() }
override val dropClickedIntent by lazy { drop.clicks() }
override val conversationClicks by lazy { blockingAdapter.clicks }
@Inject lateinit var blockingAdapter: BlockingAdapter
@Inject override lateinit var presenter: BlockingPresenter
init {
appComponent.inject(this)
retainViewMode = RetainViewMode.RETAIN_DETACH
layoutRes = R.layout.blocking_controller
}
override fun onViewCreated() {
super.onViewCreated()
blockingAdapter.emptyView = empty
conversations.adapter = blockingAdapter
}
override fun onAttach(view: View) {
super.onAttach(view)
presenter.bindIntents(this)
setTitle(R.string.blocking_title)
showBackButton(true)
}
override fun render(state: BlockingState) {
blockingManager.summary = state.blockingManager
drop.checkbox.isChecked = state.dropEnabled
blockedLabel.isVisible = state.data?.isNotEmpty() == true
blockingAdapter.updateData(state.data)
}
override fun openBlockingManager() {
router.pushController(RouterTransaction.with(BlockingManagerController())
.pushChangeHandler(QkChangeHandler())
.popChangeHandler(QkChangeHandler()))
}
}
@@ -0,0 +1,54 @@
package com.moez.QKSMS.feature.blocking
import android.content.Context
import com.moez.QKSMS.R
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.base.QkPresenter
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.util.Preferences
import com.uber.autodispose.kotlin.autoDisposable
import io.reactivex.rxkotlin.plusAssign
import javax.inject.Inject
class BlockingPresenter @Inject constructor(
private val context: Context,
private val conversationRepo: ConversationRepository,
private val navigator: Navigator,
private val prefs: Preferences
) : QkPresenter<BlockingView, BlockingState>(BlockingState()) {
init {
newState { copy(data = conversationRepo.getBlockedConversations()) }
disposables += prefs.blockingManager.asObservable()
.map { client ->
when (client) {
Preferences.BLOCKING_MANAGER_SIA -> R.string.blocking_manager_sia_title
Preferences.BLOCKING_MANAGER_CC -> R.string.blocking_manager_call_control_title
else -> R.string.app_name
}
}
.map(context::getString)
.subscribe { manager -> newState { copy(blockingManager = manager) }}
disposables += prefs.drop.asObservable()
.subscribe { enabled -> newState { copy(dropEnabled = enabled) } }
}
override fun bindIntents(view: BlockingView) {
super.bindIntents(view)
view.blockingManagerIntent
.autoDisposable(view.scope())
.subscribe { view.openBlockingManager() }
view.dropClickedIntent
.autoDisposable(view.scope())
.subscribe { prefs.drop.set(!prefs.drop.get()) }
view.conversationClicks
.autoDisposable(view.scope())
.subscribe { threadId -> navigator.showConversation(threadId) }
}
}
@@ -22,8 +22,7 @@ import com.moez.QKSMS.model.Conversation
import io.realm.RealmResults
data class BlockingState(
val ccEnabled: Boolean = false,
val siaEnabled: Boolean = false,
val blockingManager: String = "",
val dropEnabled: Boolean = false,
val data: RealmResults<Conversation>? = null
)
@@ -18,14 +18,14 @@
*/
package com.moez.QKSMS.feature.blocking
import com.moez.QKSMS.common.base.QkView
import com.moez.QKSMS.common.base.QkViewContract
import io.reactivex.Observable
interface BlockingView : QkView<BlockingState> {
interface BlockingView : QkViewContract<BlockingState> {
val ccClickedIntent: Observable<*>
val siaClickedIntent: Observable<*>
val blockingManagerIntent: Observable<*>
val dropClickedIntent: Observable<*>
val conversationClicks: Observable<Long>
fun openBlockingManager()
}
@@ -1,99 +0,0 @@
/*
* 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 com.moez.QKSMS.feature.blocking
import android.content.Context
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.base.QkViewModel
import com.moez.QKSMS.common.util.extensions.isInstalled
import com.moez.QKSMS.manager.AnalyticsManager
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.util.Preferences
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
import io.reactivex.rxkotlin.plusAssign
import javax.inject.Inject
class BlockingViewModel @Inject constructor(
private val context: Context,
private val analytics: AnalyticsManager,
private val conversationRepo: ConversationRepository,
private val navigator: Navigator,
private val prefs: Preferences
) : QkViewModel<BlockingView, BlockingState>(BlockingState()) {
init {
newState { copy(data = conversationRepo.getBlockedConversations()) }
disposables += prefs.callControl.asObservable()
.subscribe { enabled -> newState { copy(ccEnabled = enabled) } }
disposables += prefs.sia.asObservable()
.subscribe { enabled -> newState { copy(siaEnabled = enabled) } }
disposables += prefs.drop.asObservable()
.subscribe { enabled -> newState { copy(dropEnabled = enabled) } }
}
override fun bindView(view: BlockingView) {
super.bindView(view)
view.ccClickedIntent
.map { context.isInstalled("com.flexaspect.android.everycallcontrol") }
.map { installed ->
if (!installed) {
navigator.showCallControl()
}
val enabled = prefs.callControl.get()
analytics.track("Clicked Call Control", Pair("enable", !enabled), Pair("installed", installed))
installed && !enabled
}
.autoDisposable(view.scope())
.subscribe(prefs.callControl::set)
view.siaClickedIntent
.map {
listOf("org.mistergroup.shouldianswer",
"org.mistergroup.shouldianswerpersonal",
"org.mistergroup.muzutozvednout")
.any(context::isInstalled)
}
.map { installed ->
if (!installed) {
navigator.showSia()
}
val enabled = prefs.sia.get()
analytics.track("Clicked SIA", Pair("enable", !enabled), Pair("installed", installed))
installed && !enabled
}
.autoDisposable(view.scope())
.subscribe(prefs.sia::set)
view.dropClickedIntent
.autoDisposable(view.scope())
.subscribe { prefs.drop.set(!prefs.drop.get()) }
view.conversationClicks
.autoDisposable(view.scope())
.subscribe { threadId -> navigator.showConversation(threadId) }
}
}
@@ -0,0 +1,42 @@
package com.moez.QKSMS.feature.blocking.manager
import android.view.View
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkController
import com.moez.QKSMS.injection.appComponent
import com.moez.QKSMS.util.Preferences
import io.reactivex.Observable
import kotlinx.android.synthetic.main.blocking_manager_controller.*
import kotlinx.android.synthetic.main.radio_preference_view.view.*
import javax.inject.Inject
class BlockingManagerController : QkController<BlockingManagerView, BlockingManagerState, BlockingManagerPresenter>(),
BlockingManagerView {
@Inject override lateinit var presenter: BlockingManagerPresenter
init {
appComponent.inject(this)
retainViewMode = RetainViewMode.RETAIN_DETACH
layoutRes = R.layout.blocking_manager_controller
}
override fun onAttach(view: View) {
super.onAttach(view)
presenter.bindIntents(this)
setTitle(R.string.blocking_manager_title)
showBackButton(true)
}
override fun render(state: BlockingManagerState) {
qksms.radioButton.isChecked = state.blockingManager == Preferences.BLOCKING_MANAGER_QKSMS
callControl.radioButton.isChecked = state.blockingManager == Preferences.BLOCKING_MANAGER_CC
shouldIAnswer.radioButton.isChecked = state.blockingManager == Preferences.BLOCKING_MANAGER_SIA
}
override fun qksmsClicked(): Observable<*> = qksms.clicks()
override fun callControlClicked(): Observable<*> = callControl.clicks()
override fun siaClicked(): Observable<*> = shouldIAnswer.clicks()
}
@@ -0,0 +1,65 @@
package com.moez.QKSMS.feature.blocking.manager
import android.content.Context
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.base.QkPresenter
import com.moez.QKSMS.common.util.extensions.isInstalled
import com.moez.QKSMS.manager.AnalyticsManager
import com.moez.QKSMS.util.Preferences
import com.uber.autodispose.kotlin.autoDisposable
import io.reactivex.rxkotlin.plusAssign
import javax.inject.Inject
class BlockingManagerPresenter @Inject constructor(
private val analytics: AnalyticsManager,
private val context: Context,
private val navigator: Navigator,
private val prefs: Preferences
) : QkPresenter<BlockingManagerView, BlockingManagerState>(BlockingManagerState()) {
init {
disposables += prefs.blockingManager.asObservable()
.subscribe { manager -> newState { copy(blockingManager = manager) } }
}
override fun bindIntents(view: BlockingManagerView) {
super.bindIntents(view)
view.qksmsClicked()
.autoDisposable(view.scope())
.subscribe { prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_QKSMS) }
view.callControlClicked()
.filter {
val installed = context.isInstalled("com.flexaspect.android.everycallcontrol")
if (!installed) {
navigator.showCallControl()
}
val enabled = prefs.blockingManager.get() == Preferences.BLOCKING_MANAGER_CC
analytics.track("Clicked Call Control", Pair("enable", !enabled), Pair("installed", installed))
installed
}
.autoDisposable(view.scope())
.subscribe { prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_CC) }
view.siaClicked()
.filter {
val installed = listOf("org.mistergroup.shouldianswer",
"org.mistergroup.shouldianswerpersonal",
"org.mistergroup.muzutozvednout")
.any(context::isInstalled)
if (!installed) {
navigator.showSia()
}
val enabled = prefs.blockingManager.get() == Preferences.BLOCKING_MANAGER_SIA
analytics.track("Clicked SIA", Pair("enable", !enabled), Pair("installed", installed))
installed && !enabled
}
.autoDisposable(view.scope())
.subscribe { prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_SIA) }
}
}
@@ -0,0 +1,5 @@
package com.moez.QKSMS.feature.blocking.manager
data class BlockingManagerState(
val blockingManager: Int = 0
)
@@ -0,0 +1,12 @@
package com.moez.QKSMS.feature.blocking.manager
import com.moez.QKSMS.common.base.QkViewContract
import io.reactivex.Observable
interface BlockingManagerView : QkViewContract<BlockingManagerState> {
fun qksmsClicked(): Observable<*>
fun callControlClicked(): Observable<*>
fun siaClicked(): Observable<*>
}