forked from upstream-mirrors/quik
Replace CompositeDisposable with AutoDispose for ViewModel intents
This commit is contained in:
@@ -18,7 +18,9 @@
|
||||
*/
|
||||
package presentation.common.base
|
||||
|
||||
interface QkView<in State> {
|
||||
import android.arch.lifecycle.LifecycleOwner
|
||||
|
||||
interface QkView<in State> : LifecycleOwner {
|
||||
|
||||
fun render(state: State)
|
||||
|
||||
|
||||
@@ -20,9 +20,10 @@ package presentation.common.base
|
||||
|
||||
import android.arch.lifecycle.ViewModel
|
||||
import android.support.annotation.CallSuper
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
|
||||
abstract class QkViewModel<in View : QkView<State>, State>(initialState: State) : ViewModel() {
|
||||
@@ -30,14 +31,12 @@ abstract class QkViewModel<in View : QkView<State>, State>(initialState: State)
|
||||
protected val state: BehaviorSubject<State> = BehaviorSubject.createDefault(initialState)
|
||||
|
||||
protected val disposables = CompositeDisposable()
|
||||
protected val intents = CompositeDisposable()
|
||||
|
||||
@CallSuper
|
||||
open fun bindView(view: View) {
|
||||
intents.clear()
|
||||
|
||||
intents += state
|
||||
state
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.render(it) }
|
||||
}
|
||||
|
||||
@@ -48,7 +47,6 @@ abstract class QkViewModel<in View : QkView<State>, State>(initialState: State)
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
disposables.dispose()
|
||||
intents.dispose()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,6 +25,8 @@ import com.mlsdev.rximagepicker.RxImageConverters
|
||||
import com.mlsdev.rximagepicker.RxImagePicker
|
||||
import com.mlsdev.rximagepicker.Sources
|
||||
import com.moez.QKSMS.R
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import common.di.appComponent
|
||||
import common.util.ClipboardUtils
|
||||
import common.util.extensions.asObservable
|
||||
@@ -172,18 +174,19 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
|
||||
// Set the contact suggestions list to visible at all times when in editing mode and there are no contacts
|
||||
// selected yet, and also visible while in editing mode and there is text entered in the query field
|
||||
intents += Observables
|
||||
Observables
|
||||
.combineLatest(view.queryChangedIntent, selectedContacts, { query, selectedContacts ->
|
||||
selectedContacts.isEmpty() || query.isNotEmpty()
|
||||
})
|
||||
.skipUntil(state.filter { state -> state.editingMode == true })
|
||||
.takeUntil(state.filter { state -> state.editingMode == false })
|
||||
.distinctUntilChanged()
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { contactsVisible -> newState { it.copy(contactsVisible = contactsVisible && it.editingMode) } }
|
||||
|
||||
// Update the list of contact suggestions based on the query input, while also filtering out any contacts
|
||||
// that have already been selected
|
||||
intents += Observables
|
||||
Observables
|
||||
.combineLatest(view.queryChangedIntent, contacts, selectedContacts, { query, contacts, selectedContacts ->
|
||||
contacts
|
||||
.filterNot { contact -> selectedContacts.contains(contact) }
|
||||
@@ -192,10 +195,11 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
.skipUntil(state.filter { state -> state.editingMode == true })
|
||||
.takeUntil(state.filter { state -> state.editingMode == false })
|
||||
.subscribeOn(Schedulers.computation())
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { contacts -> newState { it.copy(contacts = contacts) } }
|
||||
|
||||
// Update the list of selected contacts when a new contact is selected or an existing one is deselected
|
||||
intents += Observable.merge(
|
||||
Observable.merge(
|
||||
view.chipDeletedIntent.doOnNext { contact ->
|
||||
contactsReducer.onNext { contacts -> contacts.filterNot { it == contact } }
|
||||
},
|
||||
@@ -204,23 +208,26 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
})
|
||||
.skipUntil(state.filter { state -> state.editingMode == true })
|
||||
.takeUntil(state.filter { state -> state.editingMode == false })
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
// When the menu is loaded, trigger a new state so that the menu options can be rendered correctly
|
||||
intents += view.menuReadyIntent.subscribe {
|
||||
newState { it.copy() }
|
||||
}
|
||||
view.menuReadyIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { newState { it.copy() } }
|
||||
|
||||
// Open the phone dialer if the call button is clicked
|
||||
intents += view.callIntent
|
||||
view.callIntent
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
.map { conversation -> conversation.recipients.first() }
|
||||
.map { recipient -> recipient.address }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { address -> navigator.makePhoneCall(address) }
|
||||
|
||||
// Toggle the archived state of the conversation
|
||||
intents += view.archiveIntent
|
||||
view.archiveIntent
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { conversation ->
|
||||
when (conversation.archived) {
|
||||
true -> markUnarchived.execute(conversation.id, { context.makeToast(R.string.toast_unarchived) })
|
||||
@@ -229,39 +236,44 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
}
|
||||
|
||||
// Delete the conversation
|
||||
intents += view.deleteIntent
|
||||
view.deleteIntent
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { conversation -> deleteConversation.execute(conversation.id) }
|
||||
|
||||
// Mark the conversation read, if in foreground
|
||||
intents += Observables.combineLatest(messages, view.activityVisibleIntent, { _, b -> b })
|
||||
Observables.combineLatest(messages, view.activityVisibleIntent, { _, b -> b })
|
||||
.withLatestFrom(conversation, { visible, conversation ->
|
||||
if (visible) markRead.execute(conversation.id)
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
// Attach a photo
|
||||
intents += view.attachIntent
|
||||
view.attachIntent
|
||||
.flatMap { RxImagePicker.with(context).requestImage(Sources.GALLERY) }
|
||||
.flatMap { uri -> RxImageConverters.uriToBitmap(context, uri) }
|
||||
.withLatestFrom(attachments, { attachment, attachments -> attachments + attachment })
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { attachments.onNext(it) }
|
||||
|
||||
// Detach a photo
|
||||
intents += view.attachmentDeletedIntent
|
||||
view.attachmentDeletedIntent
|
||||
.withLatestFrom(attachments, { bitmap, attachments -> attachments.filter { it !== bitmap } })
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { attachments.onNext(it) }
|
||||
|
||||
// Enable the send button when there is text input into the new message body or there's
|
||||
// an attachment, disable otherwise
|
||||
intents += Observables
|
||||
Observables
|
||||
.combineLatest(view.textChangedIntent, attachments, { text, attachments ->
|
||||
text.isNotBlank() || attachments.isNotEmpty()
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { canSend -> newState { it.copy(canSend = canSend) } }
|
||||
|
||||
// Send a message when the send button is clicked, and disable editing mode if it's enabled
|
||||
intents += view.sendIntent
|
||||
view.sendIntent
|
||||
.withLatestFrom(view.textChangedIntent, { _, body -> body })
|
||||
.map { body -> body.toString() }
|
||||
.withLatestFrom(attachments, conversation, { body, attachments, conversation ->
|
||||
@@ -276,23 +288,26 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
newState { it.copy(editingMode = false) }
|
||||
}
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
// Copy the body of the selected message into the clipboard
|
||||
intents += view.copyTextIntent.subscribe { message ->
|
||||
ClipboardUtils.copy(context, message.body)
|
||||
context.makeToast(R.string.toast_copied)
|
||||
}
|
||||
view.copyTextIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { message ->
|
||||
ClipboardUtils.copy(context, message.body)
|
||||
context.makeToast(R.string.toast_copied)
|
||||
}
|
||||
|
||||
// Forward the message
|
||||
intents += view.forwardMessageIntent.subscribe { message ->
|
||||
navigator.showCompose(message.body)
|
||||
}
|
||||
view.forwardMessageIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { message -> navigator.showCompose(message.body) }
|
||||
|
||||
// Delete the selected message
|
||||
intents += view.deleteMessageIntent.subscribe { message ->
|
||||
deleteMessage.execute(message.id)
|
||||
}
|
||||
view.deleteMessageIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { message -> deleteMessage.execute(message.id) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,6 +24,8 @@ import android.content.pm.PackageManager
|
||||
import android.provider.Telephony
|
||||
import android.support.v4.content.ContextCompat
|
||||
import com.moez.QKSMS.R
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import common.di.appComponent
|
||||
import common.util.filter.ConversationFilter
|
||||
import data.model.MenuItem
|
||||
@@ -97,7 +99,7 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
override fun bindView(view: MainView) {
|
||||
super.bindView(view)
|
||||
|
||||
intents += view.queryChangedIntent
|
||||
view.queryChangedIntent
|
||||
.skip(1)
|
||||
.debounce(200, TimeUnit.MILLISECONDS)
|
||||
.withLatestFrom(state, { query, state ->
|
||||
@@ -111,18 +113,22 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
newState { it.copy(page = page) }
|
||||
}
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
intents += view.queryCancelledIntent
|
||||
view.queryCancelledIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.clearSearch() }
|
||||
|
||||
intents += view.composeIntent
|
||||
view.composeIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { navigator.showCompose() }
|
||||
|
||||
intents += view.drawerOpenIntent
|
||||
view.drawerOpenIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { open -> newState { it.copy(drawerOpen = open) } }
|
||||
|
||||
intents += view.drawerItemIntent
|
||||
view.drawerItemIntent
|
||||
.doOnNext { newState { it.copy(drawerOpen = false) } }
|
||||
.doOnNext { if (it == DrawerItem.SETTINGS) navigator.showSettings() }
|
||||
.doOnNext { if (it == DrawerItem.HELP) navigator.showSupport() }
|
||||
@@ -135,14 +141,16 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
DrawerItem.BLOCKED -> newState { it.copy(page = Blocked()) }
|
||||
}
|
||||
}
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
intents += view.conversationClickIntent
|
||||
view.conversationClickIntent
|
||||
.doOnNext { view.clearSearch() }
|
||||
.doOnNext { threadId -> navigator.showConversation(threadId) }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
intents += view.conversationLongClickIntent
|
||||
view.conversationLongClickIntent
|
||||
.withLatestFrom(state, { l, mainState ->
|
||||
when (mainState.page) {
|
||||
is Inbox -> {
|
||||
@@ -155,9 +163,10 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
}
|
||||
}
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
intents += view.conversationMenuItemIntent
|
||||
view.conversationMenuItemIntent
|
||||
.withLatestFrom(state, { actionId, mainState ->
|
||||
when (mainState.page) {
|
||||
is Inbox -> {
|
||||
@@ -178,6 +187,7 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
menuDelete.actionId -> deleteConversation.execute(threadId)
|
||||
}
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
val swipedConversation = view.swipeConversationIntent
|
||||
@@ -185,7 +195,7 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
.map { pair -> pair.conversation }
|
||||
.map { conversation -> conversation.id }
|
||||
|
||||
intents += swipedConversation
|
||||
swipedConversation
|
||||
.withLatestFrom(state, { threadId, state ->
|
||||
markArchived.execute(threadId) {
|
||||
if (state.page is Inbox) {
|
||||
@@ -203,10 +213,12 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
}
|
||||
}
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
intents += view.undoSwipeConversationIntent
|
||||
view.undoSwipeConversationIntent
|
||||
.withLatestFrom(swipedConversation, { _, threadId -> threadId })
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { threadId -> markUnarchived.execute(threadId) }
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,8 @@ package presentation.feature.settings
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import com.moez.QKSMS.R
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import common.di.appComponent
|
||||
import common.util.Preferences
|
||||
import interactor.FullSync
|
||||
@@ -84,51 +86,55 @@ class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(SettingsState
|
||||
override fun bindView(view: SettingsView) {
|
||||
super.bindView(view)
|
||||
|
||||
intents += view.preferenceClickIntent.subscribe {
|
||||
Timber.v("Preference click: ${context.resources.getResourceName(it.id)}")
|
||||
view.preferenceClickIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe {
|
||||
Timber.v("Preference click: ${context.resources.getResourceName(it.id)}")
|
||||
|
||||
when (it.id) {
|
||||
R.id.defaultSms -> navigator.showDefaultSmsDialog()
|
||||
when (it.id) {
|
||||
R.id.defaultSms -> navigator.showDefaultSmsDialog()
|
||||
|
||||
R.id.theme -> newState { it.copy(selectingTheme = true) }
|
||||
R.id.theme -> newState { it.copy(selectingTheme = true) }
|
||||
|
||||
R.id.dark -> prefs.dark.set(!prefs.dark.get())
|
||||
R.id.dark -> prefs.dark.set(!prefs.dark.get())
|
||||
|
||||
R.id.autoEmoji -> prefs.autoEmoji.set(!prefs.autoEmoji.get())
|
||||
R.id.autoEmoji -> prefs.autoEmoji.set(!prefs.autoEmoji.get())
|
||||
|
||||
R.id.notificationsO -> navigator.showNotificationSettings()
|
||||
R.id.notificationsO -> navigator.showNotificationSettings()
|
||||
|
||||
R.id.notifications -> prefs.notifications.set(!prefs.notifications.get())
|
||||
R.id.notifications -> prefs.notifications.set(!prefs.notifications.get())
|
||||
|
||||
R.id.vibration -> prefs.vibration.set(!prefs.vibration.get())
|
||||
R.id.vibration -> prefs.vibration.set(!prefs.vibration.get())
|
||||
|
||||
R.id.ringtone -> view.showRingtonePicker(Uri.parse(prefs.ringtone.get()))
|
||||
R.id.ringtone -> view.showRingtonePicker(Uri.parse(prefs.ringtone.get()))
|
||||
|
||||
R.id.delivery -> prefs.delivery.set(!prefs.delivery.get())
|
||||
R.id.delivery -> prefs.delivery.set(!prefs.delivery.get())
|
||||
|
||||
R.id.unicode -> prefs.unicode.set(!prefs.unicode.get())
|
||||
R.id.unicode -> prefs.unicode.set(!prefs.unicode.get())
|
||||
|
||||
R.id.mms -> prefs.mms.set(!prefs.mms.get())
|
||||
R.id.mms -> prefs.mms.set(!prefs.mms.get())
|
||||
|
||||
R.id.mmsSize -> {
|
||||
R.id.mmsSize -> {
|
||||
}
|
||||
|
||||
R.id.sync -> {
|
||||
newState { it.copy(syncing = true) }
|
||||
fullSync.execute(Unit, {
|
||||
newState { it.copy(syncing = false) }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
R.id.sync -> {
|
||||
newState { it.copy(syncing = true) }
|
||||
fullSync.execute(Unit, {
|
||||
newState { it.copy(syncing = false) }
|
||||
})
|
||||
view.themeSelectedIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { color ->
|
||||
prefs.theme.set(color)
|
||||
newState { it.copy(selectingTheme = false) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
intents += view.themeSelectedIntent.subscribe { color ->
|
||||
prefs.theme.set(color)
|
||||
newState { it.copy(selectingTheme = false) }
|
||||
}
|
||||
|
||||
intents += view.ringtoneSelectedIntent.subscribe { ringtone ->
|
||||
prefs.ringtone.set(ringtone)
|
||||
}
|
||||
view.ringtoneSelectedIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { ringtone -> prefs.ringtone.set(ringtone) }
|
||||
}
|
||||
}
|
||||
@@ -20,10 +20,11 @@ package presentation.feature.setup
|
||||
|
||||
import android.content.Context
|
||||
import android.provider.Telephony
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import common.di.appComponent
|
||||
import common.util.Permissions
|
||||
import presentation.common.base.QkViewModel
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import javax.inject.Inject
|
||||
|
||||
class SetupViewModel : QkViewModel<SetupView, SetupState>(SetupState()) {
|
||||
@@ -38,7 +39,8 @@ class SetupViewModel : QkViewModel<SetupView, SetupState>(SetupState()) {
|
||||
override fun bindView(view: SetupView) {
|
||||
super.bindView(view)
|
||||
|
||||
intents += view.activityResumedIntent
|
||||
view.activityResumedIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe {
|
||||
val isDefault = Telephony.Sms.getDefaultSmsPackage(context) == context.packageName
|
||||
|
||||
@@ -46,9 +48,13 @@ class SetupViewModel : QkViewModel<SetupView, SetupState>(SetupState()) {
|
||||
else if (isDefault) view.finish()
|
||||
}
|
||||
|
||||
intents += view.skipIntent.subscribe { view.finish() }
|
||||
view.skipIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.finish() }
|
||||
|
||||
intents += view.nextIntent.subscribe { view.requestDefaultSms() }
|
||||
view.nextIntent
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.requestDefaultSms() }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user