Refactor blocked to blocking

This commit is contained in:
moezbhatti
2019-06-23 22:34:43 -04:00
parent f2e736b638
commit c1ab08dbc0
51 changed files with 399 additions and 400 deletions
@@ -0,0 +1,64 @@
/*
* 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.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.jakewharton.rxbinding2.view.clicks
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
class BlockingActivity : QkThemedActivity(), BlockingView {
@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] }
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)
blockingAdapter.emptyView = empty
conversations.adapter = blockingAdapter
}
override fun render(state: BlockingState) {
callControl.checkbox.isChecked = state.ccEnabled
shouldIAnswer.checkbox.isChecked = state.siaEnabled
drop.checkbox.isChecked = state.dropEnabled
blockingAdapter.updateData(state.data)
}
}
@@ -0,0 +1,35 @@
/*
* 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,54 @@
/*
* 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.view.LayoutInflater
import android.view.ViewGroup
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkRealmAdapter
import com.moez.QKSMS.common.base.QkViewHolder
import com.moez.QKSMS.model.Conversation
import io.reactivex.subjects.PublishSubject
import kotlinx.android.synthetic.main.blocked_list_item.view.*
import javax.inject.Inject
class BlockingAdapter @Inject constructor() : QkRealmAdapter<Conversation>() {
val clicks: PublishSubject<Long> = PublishSubject.create()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.blocked_list_item, parent, false)
return QkViewHolder(view).apply {
view.setOnClickListener {
val conversation = getItem(adapterPosition) ?: return@setOnClickListener
clicks.onNext(conversation.id)
}
}
}
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val conversation = getItem(position) ?: return
val view = holder.containerView
view.avatars.contacts = conversation.recipients
view.title.collapseEnabled = conversation.recipients.size > 1
view.title.text = conversation.getTitle()
}
}
@@ -0,0 +1,75 @@
package com.moez.QKSMS.feature.blocking
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import com.moez.QKSMS.R
import com.moez.QKSMS.blocking.BlockingClient
import com.moez.QKSMS.interactor.MarkBlocked
import com.moez.QKSMS.interactor.MarkUnblocked
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.util.Preferences
import javax.inject.Inject
// TODO: Once we have a custom dialog based on conductor, turn this into a controller
class BlockingDialog @Inject constructor(
private val blockingManager: BlockingClient,
private val context: Context,
private val conversationRepo: ConversationRepository,
private val prefs: Preferences,
private val markBlocked: MarkBlocked,
private val markUnblocked: MarkUnblocked
) {
fun show(activity: Activity, conversationIds: List<Long>, block: Boolean) {
val addresses = conversationIds.toLongArray()
.let(conversationRepo::getConversations)
.flatMap { conversation -> conversation.recipients }
.map { it.address }
.distinct()
// If we can block/unblock in the external manager, then just fire that off and exit
if (block) {
markBlocked.execute(conversationIds)
if (blockingManager.canBlock()) {
blockingManager.block(addresses).subscribe()
return
}
} else {
markUnblocked.execute(conversationIds)
if (blockingManager.canUnblock()) {
blockingManager.unblock(addresses).subscribe()
return
}
}
val res = when (block) {
true -> R.plurals.blocking_block_external
false -> R.plurals.blocking_unblock_external
}
val manager = context.getString(when (prefs.blockingManager.get()) {
Preferences.BLOCKING_MANAGER_SIA -> R.string.blocking_manager_sia_title
Preferences.BLOCKING_MANAGER_CC -> R.string.blocking_manager_call_control_title
else -> R.string.blocking_manager_android
})
val message = context.resources.getQuantityString(res, addresses.size, manager)
// Otherwise, show a dialog asking the user if they want to be directed to the external
// blocking manager
AlertDialog.Builder(activity)
.setTitle(R.string.main_menu_block)
.setMessage(message)
.setPositiveButton(R.string.button_yes) { _, _ ->
when (block) {
true -> blockingManager.block(addresses)
false -> blockingManager.unblock(addresses)
}.subscribe()
}
.setNegativeButton(R.string.button_cancel) { _, _ -> }
.create()
.show()
}
}
@@ -0,0 +1,29 @@
/*
* 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 com.moez.QKSMS.model.Conversation
import io.realm.RealmResults
data class BlockingState(
val ccEnabled: Boolean = false,
val siaEnabled: Boolean = false,
val dropEnabled: Boolean = false,
val data: RealmResults<Conversation>? = null
)
@@ -0,0 +1,31 @@
/*
* 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 com.moez.QKSMS.common.base.QkView
import io.reactivex.Observable
interface BlockingView : QkView<BlockingState> {
val ccClickedIntent: Observable<*>
val siaClickedIntent: Observable<*>
val dropClickedIntent: Observable<*>
val conversationClicks: Observable<Long>
}
@@ -0,0 +1,99 @@
/*
* 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) }
}
}