Update newState signature to not require calling it.copy()

This commit is contained in:
Moez Bhatti
2018-06-05 19:56:34 -04:00
parent fc99ae83ad
commit 1aeddec7bd
11 changed files with 96 additions and 96 deletions
@@ -40,7 +40,7 @@ abstract class QkViewModel<in View : QkView<State>, State>(initialState: State)
.subscribe { view.render(it) } .subscribe { view.render(it) }
} }
protected fun newState(reducer: (State) -> State) { protected fun newState(reducer: State.() -> State) {
state.value?.let { state.onNext(reducer(it)) } state.value?.let { state.onNext(reducer(it)) }
} }
@@ -40,10 +40,10 @@ class BlockedViewModel @Inject constructor(
) : QkViewModel<BlockedView, BlockedState>(BlockedState()) { ) : QkViewModel<BlockedView, BlockedState>(BlockedState()) {
init { init {
newState { it.copy(data = messageRepo.getBlockedConversations()) } newState { copy(data = messageRepo.getBlockedConversations()) }
disposables += prefs.sia.asObservable() disposables += prefs.sia.asObservable()
.subscribe { enabled -> newState { it.copy(siaEnabled = enabled) } } .subscribe { enabled -> newState { copy(siaEnabled = enabled) } }
} }
override fun bindView(view: BlockedView) { override fun bindView(view: BlockedView) {
@@ -121,12 +121,12 @@ class ComposeViewModel @Inject constructor(
val initialConversation: Observable<Conversation> = when { val initialConversation: Observable<Conversation> = when {
threadId != 0L -> { threadId != 0L -> {
newState { it.copy(selectedConversation = threadId, editingMode = false) } newState { copy(selectedConversation = threadId, editingMode = false) }
messageRepo.getConversationAsync(threadId).asObservable() messageRepo.getConversationAsync(threadId).asObservable()
} }
address.isNotBlank() -> { address.isNotBlank() -> {
newState { it.copy(editingMode = false) } newState { copy(editingMode = false) }
Observable.just(address) Observable.just(address)
.mapNotNull { messageRepo.getOrCreateConversation(it) } .mapNotNull { messageRepo.getOrCreateConversation(it) }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
@@ -134,14 +134,14 @@ class ComposeViewModel @Inject constructor(
} }
else -> { else -> {
newState { it.copy(editingMode = true) } newState { copy(editingMode = true) }
Observable.empty() Observable.empty()
} }
} }
selectedContacts = contactsReducer selectedContacts = contactsReducer
.scan(listOf<Contact>(), { previousState, reducer -> reducer(previousState) }) .scan(listOf<Contact>(), { previousState, reducer -> reducer(previousState) })
.doOnNext { contacts -> newState { it.copy(selectedContacts = contacts) } } .doOnNext { contacts -> newState { copy(selectedContacts = contacts) } }
// Merges two potential conversation sources (threadId from constructor and contact selection) into a single // Merges two potential conversation sources (threadId from constructor and contact selection) into a single
// stream of conversations. If the conversation was deleted, notify the activity to shut down // stream of conversations. If the conversation was deleted, notify the activity to shut down
@@ -154,7 +154,7 @@ class ComposeViewModel @Inject constructor(
.filter { conversation -> conversation.isLoaded } .filter { conversation -> conversation.isLoaded }
.doOnNext { conversation -> .doOnNext { conversation ->
if (!conversation.isValid) { if (!conversation.isValid) {
newState { it.copy(hasError = true) } newState { copy(hasError = true) }
} }
} }
.filter { conversation -> conversation.isValid } .filter { conversation -> conversation.isValid }
@@ -168,7 +168,7 @@ class ComposeViewModel @Inject constructor(
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.map { conversation -> .map { conversation ->
val messages = messageRepo.getMessages(conversation.id) val messages = messageRepo.getMessages(conversation.id)
newState { it.copy(selectedConversation = conversation.id, messages = Pair(conversation, messages)) } newState { copy(selectedConversation = conversation.id, messages = Pair(conversation, messages)) }
messages messages
} }
.switchMap { messages -> messages.asObservable() } .switchMap { messages -> messages.asObservable() }
@@ -177,10 +177,10 @@ class ComposeViewModel @Inject constructor(
disposables += conversation disposables += conversation
.map { conversation -> conversation.getTitle() } .map { conversation -> conversation.getTitle() }
.distinctUntilChanged() .distinctUntilChanged()
.subscribe { title -> newState { it.copy(conversationtitle = title) } } .subscribe { title -> newState { copy(conversationtitle = title) } }
disposables += attachments disposables += attachments
.subscribe { attachments -> newState { it.copy(attachments = attachments) } } .subscribe { attachments -> newState { copy(attachments = attachments) } }
disposables += conversation disposables += conversation
.map { conversation -> conversation.id } .map { conversation -> conversation.id }
@@ -197,7 +197,7 @@ class ComposeViewModel @Inject constructor(
messages.lastOrNull()?.let { message -> searchSelection.onNext(message.id) } messages.lastOrNull()?.let { message -> searchSelection.onNext(message.id) }
} else { } else {
val position = messages.indexOfFirst { it.id == selected } + 1 val position = messages.indexOfFirst { it.id == selected } + 1
newState { it.copy(searchSelectionPosition = position, searchResults = messages.size) } newState { copy(searchSelectionPosition = position, searchResults = messages.size) }
} }
}.subscribe() }.subscribe()
@@ -207,7 +207,7 @@ class ComposeViewModel @Inject constructor(
disposables += Observables.combineLatest(latestSubId, subUtils.subscriptionsObservable) { subId, subs -> disposables += Observables.combineLatest(latestSubId, subUtils.subscriptionsObservable) { subId, subs ->
val sub = if (subs.size > 1) subs.firstOrNull { it.subscriptionId == subId } ?: subs[0] else null val sub = if (subs.size > 1) subs.firstOrNull { it.subscriptionId == subId } ?: subs[0] else null
newState { it.copy(subscription = sub) } newState { copy(subscription = sub) }
}.subscribe() }.subscribe()
if (threadId == 0L) { if (threadId == 0L) {
@@ -228,7 +228,7 @@ class ComposeViewModel @Inject constructor(
.takeUntil(state.filter { state -> !state.editingMode }) .takeUntil(state.filter { state -> !state.editingMode })
.distinctUntilChanged() .distinctUntilChanged()
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { contactsVisible -> newState { it.copy(contactsVisible = contactsVisible && it.editingMode) } } .subscribe { contactsVisible -> newState { copy(contactsVisible = contactsVisible && editingMode) } }
// Update the list of contact suggestions based on the query input, while also filtering out any contacts // Update the list of contact suggestions based on the query input, while also filtering out any contacts
// that have already been selected // that have already been selected
@@ -257,7 +257,7 @@ class ComposeViewModel @Inject constructor(
.takeUntil(state.filter { state -> !state.editingMode }) .takeUntil(state.filter { state -> !state.editingMode })
.subscribeOn(Schedulers.computation()) .subscribeOn(Schedulers.computation())
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { contacts -> newState { it.copy(contacts = contacts) } } .subscribe { contacts -> newState { copy(contacts = contacts) } }
// Backspaces should delete the most recent contact if there's no text input // Backspaces should delete the most recent contact if there's no text input
// Close the activity if user presses back // Close the activity if user presses back
@@ -297,7 +297,7 @@ class ComposeViewModel @Inject constructor(
// When the menu is loaded, trigger a new state so that the menu options can be rendered correctly // When the menu is loaded, trigger a new state so that the menu options can be rendered correctly
view.menuReadyIntent view.menuReadyIntent
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { newState { it.copy() } } .subscribe { newState { copy() } }
// Open the phone dialer if the call button is clicked // Open the phone dialer if the call button is clicked
view.optionsItemIntent view.optionsItemIntent
@@ -379,19 +379,19 @@ class ComposeViewModel @Inject constructor(
view.optionsItemIntent view.optionsItemIntent
.filter { it == R.id.clear } .filter { it == R.id.clear }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { newState { it.copy(query = "", searchSelectionId = -1) } } .subscribe { newState { copy(query = "", searchSelectionId = -1) } }
// Toggle the group sending mode // Toggle the group sending mode
view.sendAsGroupIntent view.sendAsGroupIntent
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { newState { it.copy(sendAsGroup = !it.sendAsGroup) } } .subscribe { newState { copy(sendAsGroup = !sendAsGroup) } }
// Scroll to search position // Scroll to search position
searchSelection searchSelection
.filter { id -> id != -1L } .filter { id -> id != -1L }
.doOnNext { id -> newState { it.copy(searchSelectionId = id) } } .doOnNext { id -> newState { copy(searchSelectionId = id) } }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe(view::scrollToMessage) .subscribe(view::scrollToMessage)
@@ -407,7 +407,7 @@ class ComposeViewModel @Inject constructor(
view.messagesSelectedIntent view.messagesSelectedIntent
.map { selection -> selection.size } .map { selection -> selection.size }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { messages -> newState { it.copy(selectedMessages = messages, editingMode = false) } } .subscribe { messages -> newState { copy(selectedMessages = messages, editingMode = false) } }
// Cancel sending a message // Cancel sending a message
view.cancelSendingIntent view.cancelSendingIntent
@@ -438,7 +438,7 @@ class ComposeViewModel @Inject constructor(
// Open the attachment options // Open the attachment options
view.attachIntent view.attachIntent
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { newState { it.copy(attaching = !it.attaching) } } .subscribe { newState { copy(attaching = !attaching) } }
// Attach a photo from camera // Attach a photo from camera
view.cameraIntent view.cameraIntent
@@ -462,7 +462,7 @@ class ComposeViewModel @Inject constructor(
.withLatestFrom(attachments, { attachment, attachments -> attachments + attachment }) .withLatestFrom(attachments, { attachment, attachments -> attachments + attachment })
.doOnNext { attachments.onNext(it) } .doOnNext { attachments.onNext(it) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { newState { it.copy(attaching = false) } } .subscribe { newState { copy(attaching = false) } }
// Detach a photo // Detach a photo
view.attachmentDeletedIntent view.attachmentDeletedIntent
@@ -494,7 +494,7 @@ class ComposeViewModel @Inject constructor(
text.isNotBlank() || attachments.isNotEmpty() text.isNotBlank() || attachments.isNotEmpty()
}) })
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { canSend -> newState { it.copy(canSend = canSend) } } .subscribe { canSend -> newState { copy(canSend = canSend) } }
// Show the remaining character counter when necessary // Show the remaining character counter when necessary
view.textChangedIntent view.textChangedIntent
@@ -512,7 +512,7 @@ class ComposeViewModel @Inject constructor(
} }
.distinctUntilChanged() .distinctUntilChanged()
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { remaining -> newState { it.copy(remaining = remaining) } } .subscribe { remaining -> newState { copy(remaining = remaining) } }
// Toggle to the next sim slot // Toggle to the next sim slot
view.changeSimIntent view.changeSimIntent
@@ -524,7 +524,7 @@ class ComposeViewModel @Inject constructor(
subIndex < subs.size - 1 -> subs[subIndex + 1] subIndex < subs.size - 1 -> subs[subIndex + 1]
else -> subs[0] else -> subs[0]
} }
newState { it.copy(subscription = subscription) } newState { copy(subscription = subscription) }
} }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -557,7 +557,7 @@ class ComposeViewModel @Inject constructor(
this.attachments.onNext(ArrayList()) this.attachments.onNext(ArrayList())
if (state.editingMode) { if (state.editingMode) {
newState { it.copy(editingMode = false, sendAsGroup = true, hasError = !state.sendAsGroup) } newState { copy(editingMode = false, sendAsGroup = true, hasError = !state.sendAsGroup) }
} }
}) })
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -571,7 +571,7 @@ class ComposeViewModel @Inject constructor(
.withLatestFrom(state, { _, state -> .withLatestFrom(state, { _, state ->
when { when {
state.selectedMessages > 0 -> view.clearSelection() state.selectedMessages > 0 -> view.clearSelection()
else -> newState { it.copy(hasError = true) } else -> newState { copy(hasError = true) }
} }
}) })
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -57,7 +57,7 @@ class ConversationInfoViewModel @Inject constructor(
.filter { conversation -> conversation.isLoaded } .filter { conversation -> conversation.isLoaded }
.doOnNext { conversation -> .doOnNext { conversation ->
if (!conversation.isValid) { if (!conversation.isValid) {
newState { it.copy(hasError = true) } newState { copy(hasError = true) }
} }
} }
.filter { conversation -> conversation.isValid } .filter { conversation -> conversation.isValid }
@@ -73,19 +73,19 @@ class ConversationInfoViewModel @Inject constructor(
disposables += conversation disposables += conversation
.map { conversation -> conversation.recipients } .map { conversation -> conversation.recipients }
.distinctUntilChanged() .distinctUntilChanged()
.subscribe { recipients -> newState { it.copy(recipients = recipients) } } .subscribe { recipients -> newState { copy(recipients = recipients) } }
// Update the view's archived state whenever it changes // Update the view's archived state whenever it changes
disposables += conversation disposables += conversation
.map { conversation -> conversation.archived } .map { conversation -> conversation.archived }
.distinctUntilChanged() .distinctUntilChanged()
.subscribe { archived -> newState { it.copy(archived = archived) } } .subscribe { archived -> newState { copy(archived = archived) } }
// Update the view's blocked state whenever it changes // Update the view's blocked state whenever it changes
disposables += conversation disposables += conversation
.map { conversation -> conversation.blocked } .map { conversation -> conversation.blocked }
.distinctUntilChanged() .distinctUntilChanged()
.subscribe { blocked -> newState { it.copy(blocked = blocked) } } .subscribe { blocked -> newState { copy(blocked = blocked) } }
} }
override fun bindView(view: ConversationInfoView) { override fun bindView(view: ConversationInfoView) {
@@ -48,12 +48,12 @@ class GalleryViewModel @Inject constructor(
disposables += partIdFlowable disposables += partIdFlowable
.mapNotNull { partId -> messageRepo.getPart(partId) } .mapNotNull { partId -> messageRepo.getPart(partId) }
.mapNotNull { part -> part.getUri() } .mapNotNull { part -> part.getUri() }
.subscribe { uri -> newState { it.copy(imageUri = uri) } } .subscribe { uri -> newState { copy(imageUri = uri) } }
disposables += partIdFlowable disposables += partIdFlowable
.mapNotNull { partId -> messageRepo.getMessageForPart(partId) } .mapNotNull { partId -> messageRepo.getMessageForPart(partId) }
.mapNotNull { message -> messageRepo.getConversation(message.threadId) } .mapNotNull { message -> messageRepo.getConversation(message.threadId) }
.subscribe { conversation -> newState { it.copy(title = conversation.getTitle()) } } .subscribe { conversation -> newState { copy(title = conversation.getTitle()) } }
} }
override fun bindView(view: GalleryView) { override fun bindView(view: GalleryView) {
@@ -64,7 +64,7 @@ class GalleryViewModel @Inject constructor(
.withLatestFrom(state, { _, state -> state.navigationVisible }) .withLatestFrom(state, { _, state -> state.navigationVisible })
.map { navigationVisible -> !navigationVisible } .map { navigationVisible -> !navigationVisible }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { navigationVisible -> newState { it.copy(navigationVisible = navigationVisible) } } .subscribe { navigationVisible -> newState { copy(navigationVisible = navigationVisible) } }
// Save image to device // Save image to device
view.optionsItemSelectedIntent view.optionsItemSelectedIntent
@@ -72,12 +72,12 @@ class MainViewModel @Inject constructor(
// Show the syncing UI // Show the syncing UI
disposables += syncRepository.syncProgress disposables += syncRepository.syncProgress
.distinctUntilChanged() .distinctUntilChanged()
.subscribe { syncing -> newState { it.copy(syncing = syncing) } } .subscribe { syncing -> newState { copy(syncing = syncing) } }
// Show the rating UI // Show the rating UI
disposables += ratingManager.shouldShowRating disposables += ratingManager.shouldShowRating
.subscribe { show -> newState { it.copy(showRating = show) } } .subscribe { show -> newState { copy(showRating = show) } }
// Migrate the preferences from 2.7.3 // Migrate the preferences from 2.7.3
@@ -108,7 +108,7 @@ class MainViewModel @Inject constructor(
view.activityResumedIntent.map { permissionManager.hasSms() }.distinctUntilChanged(), view.activityResumedIntent.map { permissionManager.hasSms() }.distinctUntilChanged(),
view.activityResumedIntent.map { permissionManager.hasContacts() }.distinctUntilChanged(), view.activityResumedIntent.map { permissionManager.hasContacts() }.distinctUntilChanged(),
{ defaultSms, smsPermission, contactPermission -> { defaultSms, smsPermission, contactPermission ->
newState { it.copy(defaultSms = defaultSms, smsPermission = smsPermission, contactPermission = contactPermission) } newState { copy(defaultSms = defaultSms, smsPermission = smsPermission, contactPermission = contactPermission) }
}) })
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -134,21 +134,21 @@ class MainViewModel @Inject constructor(
.map { query -> query.removeAccents() } .map { query -> query.removeAccents() }
.withLatestFrom(state, { query, state -> .withLatestFrom(state, { query, state ->
if (query.isEmpty() && state.page is Searching) { if (query.isEmpty() && state.page is Searching) {
newState { it.copy(page = Inbox(data = messageRepo.getConversations())) } newState { copy(page = Inbox(data = messageRepo.getConversations())) }
} }
query query
}) })
.filter { query -> query.length >= 2 } .filter { query -> query.length >= 2 }
.doOnNext { .doOnNext {
newState { state -> newState {
val page = (state.page as? Searching) ?: Searching() val page = (page as? Searching) ?: Searching()
state.copy(page = page.copy(loading = true)) copy(page = page.copy(loading = true))
} }
} }
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.switchMap { query -> Observable.just(query).map { messageRepo.searchConversations(it) } } .switchMap { query -> Observable.just(query).map { messageRepo.searchConversations(it) } }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { data -> newState { it.copy(page = Searching(loading = false, data = data)) } } .subscribe { data -> newState { copy(page = Searching(loading = false, data = data)) } }
view.composeIntent view.composeIntent
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -164,7 +164,7 @@ class MainViewModel @Inject constructor(
state.page is Archived && state.page.selected > 0 -> view.clearSelection() state.page is Archived && state.page.selected > 0 -> view.clearSelection()
else -> newState { it.copy(drawerOpen = true) } else -> newState { copy(drawerOpen = true) }
} }
}) })
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -172,19 +172,19 @@ class MainViewModel @Inject constructor(
view.drawerOpenIntent view.drawerOpenIntent
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { open -> newState { it.copy(drawerOpen = open) } } .subscribe { open -> newState { copy(drawerOpen = open) } }
view.drawerItemIntent view.drawerItemIntent
.doOnNext { newState { it.copy(drawerOpen = false) } } .doOnNext { newState { copy(drawerOpen = false) } }
.doOnNext { if (it == DrawerItem.SETTINGS) navigator.showSettings() } .doOnNext { if (it == DrawerItem.SETTINGS) navigator.showSettings() }
.doOnNext { if (it == DrawerItem.PLUS) navigator.showQksmsPlusActivity() } .doOnNext { if (it == DrawerItem.PLUS) navigator.showQksmsPlusActivity() }
.doOnNext { if (it == DrawerItem.HELP) navigator.showSupport() } .doOnNext { if (it == DrawerItem.HELP) navigator.showSupport() }
.distinctUntilChanged() .distinctUntilChanged()
.doOnNext { .doOnNext {
when (it) { when (it) {
DrawerItem.INBOX -> newState { it.copy(page = Inbox(data = messageRepo.getConversations())) } DrawerItem.INBOX -> newState { copy(page = Inbox(data = messageRepo.getConversations())) }
DrawerItem.ARCHIVED -> newState { it.copy(page = Archived(data = messageRepo.getConversations(true))) } DrawerItem.ARCHIVED -> newState { copy(page = Archived(data = messageRepo.getConversations(true))) }
DrawerItem.SCHEDULED -> newState { it.copy(page = Scheduled()) } DrawerItem.SCHEDULED -> newState { copy(page = Scheduled()) }
else -> { else -> {
} // Do nothing } // Do nothing
} }
@@ -233,12 +233,12 @@ class MainViewModel @Inject constructor(
when (state.page) { when (state.page) {
is Inbox -> { is Inbox -> {
val page = state.page.copy(selected = selected, showClearButton = selected > 0) val page = state.page.copy(selected = selected, showClearButton = selected > 0)
newState { it.copy(page = page) } newState { copy(page = page) }
} }
is Archived -> { is Archived -> {
val page = state.page.copy(selected = selected, showClearButton = selected > 0) val page = state.page.copy(selected = selected, showClearButton = selected > 0)
newState { it.copy(page = page) } newState { copy(page = page) }
} }
} }
}) })
@@ -259,7 +259,7 @@ class MainViewModel @Inject constructor(
markArchived.execute(listOf(threadId)) { markArchived.execute(listOf(threadId)) {
if (state.page is Inbox) { if (state.page is Inbox) {
val page = state.page.copy(showArchivedSnackbar = true) val page = state.page.copy(showArchivedSnackbar = true)
newState { it.copy(page = page) } newState { copy(page = page) }
} }
} }
}) })
@@ -268,7 +268,7 @@ class MainViewModel @Inject constructor(
markArchived.execute(listOf(threadId)) { markArchived.execute(listOf(threadId)) {
if (state.page is Inbox) { if (state.page is Inbox) {
val page = state.page.copy(showArchivedSnackbar = false) val page = state.page.copy(showArchivedSnackbar = false)
newState { it.copy(page = page) } newState { copy(page = page) }
} }
} }
}) })
@@ -294,7 +294,7 @@ class MainViewModel @Inject constructor(
view.backPressedIntent view.backPressedIntent
.withLatestFrom(state, { _, state -> .withLatestFrom(state, { _, state ->
when { when {
state.drawerOpen -> newState { it.copy(drawerOpen = false) } state.drawerOpen -> newState { copy(drawerOpen = false) }
state.page is Searching -> view.clearSearch() state.page is Searching -> view.clearSearch()
@@ -303,9 +303,9 @@ class MainViewModel @Inject constructor(
state.page is Archived && state.page.selected > 0 -> view.clearSelection() state.page is Archived && state.page.selected > 0 -> view.clearSelection()
state.page !is Inbox -> newState { it.copy(page = Inbox(data = messageRepo.getConversations())) } state.page !is Inbox -> newState { copy(page = Inbox(data = messageRepo.getConversations())) }
else -> newState { it.copy(hasError = true) } else -> newState { copy(hasError = true) }
} }
}) })
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -61,19 +61,19 @@ class NotificationPrefsViewModel @Inject constructor(
.mapNotNull { threadId -> messageRepo.getConversation(threadId) } .mapNotNull { threadId -> messageRepo.getConversation(threadId) }
.map { conversation -> conversation.getTitle() } .map { conversation -> conversation.getTitle() }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.subscribe { title -> newState { it.copy(conversationTitle = title) } } .subscribe { title -> newState { copy(conversationTitle = title) } }
disposables += notifications.asObservable() disposables += notifications.asObservable()
.subscribe { enabled -> newState { it.copy(notificationsEnabled = enabled) } } .subscribe { enabled -> newState { copy(notificationsEnabled = enabled) } }
val previewLabels = context.resources.getStringArray(R.array.notification_preview_options) val previewLabels = context.resources.getStringArray(R.array.notification_preview_options)
disposables += previews.asObservable() disposables += previews.asObservable()
.subscribe { previewId -> .subscribe { previewId ->
newState { it.copy(previewSummary = previewLabels[previewId], previewId = previewId) } newState { copy(previewSummary = previewLabels[previewId], previewId = previewId) }
} }
disposables += vibration.asObservable() disposables += vibration.asObservable()
.subscribe { enabled -> newState { it.copy(vibrationEnabled = enabled) } } .subscribe { enabled -> newState { copy(vibrationEnabled = enabled) } }
disposables += ringtone.asObservable() disposables += ringtone.asObservable()
.map { uriString -> .map { uriString ->
@@ -82,13 +82,13 @@ class NotificationPrefsViewModel @Inject constructor(
?.let { uri -> RingtoneManager.getRingtone(context, uri) }?.getTitle(context) ?.let { uri -> RingtoneManager.getRingtone(context, uri) }?.getTitle(context)
?: context.getString(R.string.settings_ringtone_none) ?: context.getString(R.string.settings_ringtone_none)
} }
.subscribe { title -> newState { it.copy(ringtoneName = title) } } .subscribe { title -> newState { copy(ringtoneName = title) } }
disposables += prefs.qkreply.asObservable() disposables += prefs.qkreply.asObservable()
.subscribe { enabled -> newState { it.copy(qkReplyEnabled = enabled) } } .subscribe { enabled -> newState { copy(qkReplyEnabled = enabled) } }
disposables += prefs.qkreplyTapDismiss.asObservable() disposables += prefs.qkreplyTapDismiss.asObservable()
.subscribe { enabled -> newState { it.copy(qkReplyTapDismiss = enabled) } } .subscribe { enabled -> newState { copy(qkReplyTapDismiss = enabled) } }
} }
override fun bindView(view: NotificationPrefsView) { override fun bindView(view: NotificationPrefsView) {
@@ -38,14 +38,14 @@ class PlusViewModel @Inject constructor(
analyticsManager.track("Viewed QKSMS+") analyticsManager.track("Viewed QKSMS+")
disposables += billingManager.upgradeStatus disposables += billingManager.upgradeStatus
.subscribe { upgraded -> newState { it.copy(upgraded = upgraded) } } .subscribe { upgraded -> newState { copy(upgraded = upgraded) } }
disposables += billingManager.products disposables += billingManager.products
.subscribe { products -> .subscribe { products ->
newState { newState {
val upgrade = products.firstOrNull { it.sku == BillingManager.SKU_PLUS } val upgrade = products.firstOrNull { it.sku == BillingManager.SKU_PLUS }
val upgradeDonate = products.firstOrNull { it.sku == BillingManager.SKU_PLUS_DONATE } val upgradeDonate = products.firstOrNull { it.sku == BillingManager.SKU_PLUS_DONATE }
it.copy(upgradePrice = upgrade?.price ?: "", upgradeDonatePrice = upgradeDonate?.price ?: "", copy(upgradePrice = upgrade?.price ?: "", upgradeDonatePrice = upgradeDonate?.price ?: "",
currency = upgrade?.priceCurrencyCode ?: upgradeDonate?.priceCurrencyCode ?: "") currency = upgrade?.priceCurrencyCode ?: upgradeDonate?.priceCurrencyCode ?: "")
} }
} }
@@ -71,19 +71,19 @@ class QkReplyViewModel @Inject constructor(
// If we're ever showing an empty set of messages, then it's time to shut down to activity // If we're ever showing an empty set of messages, then it's time to shut down to activity
disposables += Observables disposables += Observables
.combineLatest(messages, conversation) { messages, conversation -> .combineLatest(messages, conversation) { messages, conversation ->
newState { it.copy(data = Pair(conversation, messages)) } newState { copy(data = Pair(conversation, messages)) }
messages messages
} }
.switchMap { messages -> messages.asObservable() } .switchMap { messages -> messages.asObservable() }
.filter { it.isLoaded } .filter { it.isLoaded }
.filter { it.isValid } .filter { it.isValid }
.filter { it.isEmpty() } .filter { it.isEmpty() }
.subscribe { newState { it.copy(hasError = true) } } .subscribe { newState { copy(hasError = true) } }
disposables += conversation disposables += conversation
.map { conversation -> conversation.getTitle() } .map { conversation -> conversation.getTitle() }
.distinctUntilChanged() .distinctUntilChanged()
.subscribe { title -> newState { it.copy(title = title) } } .subscribe { title -> newState { copy(title = title) } }
val latestSubId = messages val latestSubId = messages
.map { messages -> messages.lastOrNull()?.subId ?: -1 } .map { messages -> messages.lastOrNull()?.subId ?: -1 }
@@ -91,7 +91,7 @@ class QkReplyViewModel @Inject constructor(
disposables += Observables.combineLatest(latestSubId, subUtils.subscriptionsObservable) { subId, subs -> disposables += Observables.combineLatest(latestSubId, subUtils.subscriptionsObservable) { subId, subs ->
val sub = if (subs.size > 1) subs.firstOrNull { it.subscriptionId == subId } ?: subs[0] else null val sub = if (subs.size > 1) subs.firstOrNull { it.subscriptionId == subId } ?: subs[0] else null
newState { it.copy(subscription = sub) } newState { copy(subscription = sub) }
}.subscribe() }.subscribe()
} }
@@ -111,7 +111,7 @@ class QkReplyViewModel @Inject constructor(
.map { conversation -> conversation.id } .map { conversation -> conversation.id }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { threadId -> .subscribe { threadId ->
markRead.execute(threadId) { newState { it.copy(hasError = true) } } markRead.execute(threadId) { newState { copy(hasError = true) } }
} }
// Call // Call
@@ -121,7 +121,7 @@ class QkReplyViewModel @Inject constructor(
.mapNotNull { conversation -> conversation.recipients.first()?.address } .mapNotNull { conversation -> conversation.recipients.first()?.address }
.doOnNext { address -> navigator.makePhoneCall(address) } .doOnNext { address -> navigator.makePhoneCall(address) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { newState { it.copy(hasError = true) } } .subscribe { newState { copy(hasError = true) } }
// Show all messages // Show all messages
view.menuItemIntent view.menuItemIntent
@@ -130,7 +130,7 @@ class QkReplyViewModel @Inject constructor(
.map { conversation -> messageRepo.getMessages(conversation.id) } .map { conversation -> messageRepo.getMessages(conversation.id) }
.doOnNext(messages::onNext) .doOnNext(messages::onNext)
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { newState { it.copy(expanded = true) } } .subscribe { newState { copy(expanded = true) } }
// Show unread messages only // Show unread messages only
view.menuItemIntent view.menuItemIntent
@@ -139,7 +139,7 @@ class QkReplyViewModel @Inject constructor(
.map { conversation -> messageRepo.getUnreadMessages(conversation.id) } .map { conversation -> messageRepo.getUnreadMessages(conversation.id) }
.doOnNext(messages::onNext) .doOnNext(messages::onNext)
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { newState { it.copy(expanded = false) } } .subscribe { newState { copy(expanded = false) } }
// View conversation // View conversation
view.menuItemIntent view.menuItemIntent
@@ -148,14 +148,14 @@ class QkReplyViewModel @Inject constructor(
.map { conversation -> conversation.id } .map { conversation -> conversation.id }
.doOnNext { threadId -> navigator.showConversation(threadId) } .doOnNext { threadId -> navigator.showConversation(threadId) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { newState { it.copy(hasError = true) } } .subscribe { newState { copy(hasError = true) } }
// Enable the send button when there is text input into the new message body or there's // Enable the send button when there is text input into the new message body or there's
// an attachment, disable otherwise // an attachment, disable otherwise
view.textChangedIntent view.textChangedIntent
.map { text -> text.isNotBlank() } .map { text -> text.isNotBlank() }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { canSend -> newState { it.copy(canSend = canSend) } } .subscribe { canSend -> newState { copy(canSend = canSend) } }
// Show the remaining character counter when necessary // Show the remaining character counter when necessary
view.textChangedIntent view.textChangedIntent
@@ -173,7 +173,7 @@ class QkReplyViewModel @Inject constructor(
} }
.distinctUntilChanged() .distinctUntilChanged()
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { remaining -> newState { it.copy(remaining = remaining) } } .subscribe { remaining -> newState { copy(remaining = remaining) } }
// Update the draft whenever the text is changed // Update the draft whenever the text is changed
view.textChangedIntent view.textChangedIntent
@@ -196,7 +196,7 @@ class QkReplyViewModel @Inject constructor(
subIndex < subs.size - 1 -> subs[subIndex + 1] subIndex < subs.size - 1 -> subs[subIndex + 1]
else -> subs[0] else -> subs[0]
} }
newState { it.copy(subscription = subscription) } newState { copy(subscription = subscription) }
} }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -215,7 +215,7 @@ class QkReplyViewModel @Inject constructor(
}) })
.doOnNext { threadId -> .doOnNext { threadId ->
markRead.execute(threadId) { markRead.execute(threadId) {
newState { it.copy(hasError = true) } newState { copy(hasError = true) }
} }
} }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -27,11 +27,11 @@ import common.base.QkViewModel
import common.util.BillingManager import common.util.BillingManager
import common.util.Colors import common.util.Colors
import common.util.DateFormatter import common.util.DateFormatter
import util.NightModeManager
import interactor.SyncMessages import interactor.SyncMessages
import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom import io.reactivex.rxkotlin.withLatestFrom
import timber.log.Timber import timber.log.Timber
import util.NightModeManager
import util.Preferences import util.Preferences
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
@@ -49,60 +49,60 @@ class SettingsViewModel @Inject constructor(
init { init {
newState { it.copy(theme = colors.theme().theme) } newState { copy(theme = colors.theme().theme) }
val nightModeLabels = context.resources.getStringArray(R.array.night_modes) val nightModeLabels = context.resources.getStringArray(R.array.night_modes)
disposables += prefs.nightMode.asObservable() disposables += prefs.nightMode.asObservable()
.subscribe { nightMode -> .subscribe { nightMode ->
newState { it.copy(nightModeSummary = nightModeLabels[nightMode], nightModeId = nightMode) } newState { copy(nightModeSummary = nightModeLabels[nightMode], nightModeId = nightMode) }
} }
disposables += prefs.nightStart.asObservable() disposables += prefs.nightStart.asObservable()
.map { time -> nightModeManager.parseTime(time) } .map { time -> nightModeManager.parseTime(time) }
.map { calendar -> calendar.timeInMillis } .map { calendar -> calendar.timeInMillis }
.map { millis -> dateFormatter.getTimestamp(millis) } .map { millis -> dateFormatter.getTimestamp(millis) }
.subscribe { nightStart -> newState { it.copy(nightStart = nightStart) } } .subscribe { nightStart -> newState { copy(nightStart = nightStart) } }
disposables += prefs.nightEnd.asObservable() disposables += prefs.nightEnd.asObservable()
.map { time -> nightModeManager.parseTime(time) } .map { time -> nightModeManager.parseTime(time) }
.map { calendar -> calendar.timeInMillis } .map { calendar -> calendar.timeInMillis }
.map { millis -> dateFormatter.getTimestamp(millis) } .map { millis -> dateFormatter.getTimestamp(millis) }
.subscribe { nightEnd -> newState { it.copy(nightEnd = nightEnd) } } .subscribe { nightEnd -> newState { copy(nightEnd = nightEnd) } }
disposables += prefs.black.asObservable() disposables += prefs.black.asObservable()
.subscribe { black -> newState { it.copy(black = black) } } .subscribe { black -> newState { copy(black = black) } }
disposables += prefs.notifications().asObservable() disposables += prefs.notifications().asObservable()
.subscribe { enabled -> newState { it.copy(notificationsEnabled = enabled) } } .subscribe { enabled -> newState { copy(notificationsEnabled = enabled) } }
disposables += prefs.autoEmoji.asObservable() disposables += prefs.autoEmoji.asObservable()
.subscribe { enabled -> newState { it.copy(autoEmojiEnabled = enabled) } } .subscribe { enabled -> newState { copy(autoEmojiEnabled = enabled) } }
val delayedSendingLabels = context.resources.getStringArray(R.array.delayed_sending_labels) val delayedSendingLabels = context.resources.getStringArray(R.array.delayed_sending_labels)
disposables += prefs.sendDelay.asObservable() disposables += prefs.sendDelay.asObservable()
.subscribe { id -> newState { it.copy(sendDelaySummary = delayedSendingLabels[id], sendDelayId = id) } } .subscribe { id -> newState { copy(sendDelaySummary = delayedSendingLabels[id], sendDelayId = id) } }
disposables += prefs.delivery.asObservable() disposables += prefs.delivery.asObservable()
.subscribe { enabled -> newState { it.copy(deliveryEnabled = enabled) } } .subscribe { enabled -> newState { copy(deliveryEnabled = enabled) } }
val textSizeLabels = context.resources.getStringArray(R.array.text_sizes) val textSizeLabels = context.resources.getStringArray(R.array.text_sizes)
disposables += prefs.textSize.asObservable() disposables += prefs.textSize.asObservable()
.subscribe { textSize -> .subscribe { textSize ->
newState { it.copy(textSizeSummary = textSizeLabels[textSize], textSizeId = textSize) } newState { copy(textSizeSummary = textSizeLabels[textSize], textSizeId = textSize) }
} }
disposables += prefs.systemFont.asObservable() disposables += prefs.systemFont.asObservable()
.subscribe { enabled -> newState { it.copy(systemFontEnabled = enabled) } } .subscribe { enabled -> newState { copy(systemFontEnabled = enabled) } }
disposables += prefs.unicode.asObservable() disposables += prefs.unicode.asObservable()
.subscribe { enabled -> newState { it.copy(stripUnicodeEnabled = enabled) } } .subscribe { enabled -> newState { copy(stripUnicodeEnabled = enabled) } }
val mmsSizeLabels = context.resources.getStringArray(R.array.mms_sizes) val mmsSizeLabels = context.resources.getStringArray(R.array.mms_sizes)
val mmsSizeIds = context.resources.getIntArray(R.array.mms_sizes_ids) val mmsSizeIds = context.resources.getIntArray(R.array.mms_sizes_ids)
disposables += prefs.mmsSize.asObservable() disposables += prefs.mmsSize.asObservable()
.subscribe { maxMmsSize -> .subscribe { maxMmsSize ->
val index = mmsSizeIds.indexOf(maxMmsSize) val index = mmsSizeIds.indexOf(maxMmsSize)
newState { it.copy(maxMmsSizeSummary = mmsSizeLabels[index], maxMmsSizeId = maxMmsSize) } newState { copy(maxMmsSizeSummary = mmsSizeLabels[index], maxMmsSizeId = maxMmsSize) }
} }
disposables += syncMessages disposables += syncMessages
@@ -152,9 +152,9 @@ class SettingsViewModel @Inject constructor(
R.id.mmsSize -> view.showMmsSizePicker() R.id.mmsSize -> view.showMmsSizePicker()
R.id.sync -> { R.id.sync -> {
newState { it.copy(syncing = true) } newState { copy(syncing = true) }
syncMessages.execute(Unit, { syncMessages.execute(Unit, {
newState { it.copy(syncing = false) } newState { copy(syncing = false) }
}) })
} }
@@ -55,16 +55,16 @@ class ThemePickerViewModel @Inject constructor(
// Update the color of the apply button // Update the color of the apply button
view.hsvThemeSelectedIntent view.hsvThemeSelectedIntent
.doOnNext { color -> newState { it.copy(newColor = color) } } .doOnNext { color -> newState { copy(newColor = color) } }
.map { color -> colors.textPrimaryOnThemeForColor(color) } .map { color -> colors.textPrimaryOnThemeForColor(color) }
.doOnNext { color -> newState { it.copy(newTextColor = color) } } .doOnNext { color -> newState { copy(newTextColor = color) } }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
// Toggle the visibility of the apply group // Toggle the visibility of the apply group
Observables.combineLatest(theme.asObservable(), view.hsvThemeSelectedIntent, { old, new -> old != new }) Observables.combineLatest(theme.asObservable(), view.hsvThemeSelectedIntent, { old, new -> old != new })
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { themeChanged -> newState { it.copy(applyThemeVisible = themeChanged) } } .subscribe { themeChanged -> newState { copy(applyThemeVisible = themeChanged) } }
// Update the theme, when apply is clicked // Update the theme, when apply is clicked
view.hsvThemeAppliedIntent view.hsvThemeAppliedIntent