Move lambdas outside of parentheses

This commit is contained in:
Moez Bhatti
2018-06-21 08:57:08 -04:00
parent 89b43adfab
commit 9b624d33d5
18 changed files with 96 additions and 96 deletions
@@ -35,10 +35,10 @@ fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {
} }
fun <T> Cursor.map(map: (Cursor) -> T): List<T> { fun <T> Cursor.map(map: (Cursor) -> T): List<T> {
return List(count, { position -> return List(count) { position ->
moveToPosition(position) moveToPosition(position)
map(this) map(this)
}) }
} }
fun <T> Cursor.mapWhile(map: (Cursor) -> T, predicate: (T) -> Boolean): ArrayList<T> { fun <T> Cursor.mapWhile(map: (Cursor) -> T, predicate: (T) -> Boolean): ArrayList<T> {
@@ -40,10 +40,10 @@ class RatingManagerImpl @Inject constructor(
override val shouldShowRating = Observables.combineLatest( override val shouldShowRating = Observables.combineLatest(
sessions.asObservable(), sessions.asObservable(),
rated.asObservable(), rated.asObservable(),
dismissed.asObservable(), { sessions, rated, dismissed -> dismissed.asObservable()) { sessions, rated, dismissed ->
sessions > SESSION_THRESHOLD && !rated && !dismissed sessions > SESSION_THRESHOLD && !rated && !dismissed
}) }
override fun addSession() { override fun addSession() {
sessions.set(sessions.get() + 1) sessions.set(sessions.get() + 1)
@@ -40,7 +40,7 @@ class DefaultSmsChangedReceiver : BroadcastReceiver() {
if (intent.getBooleanExtra(Telephony.Sms.Intents.EXTRA_IS_DEFAULT_SMS_APP, false)) { if (intent.getBooleanExtra(Telephony.Sms.Intents.EXTRA_IS_DEFAULT_SMS_APP, false)) {
val pendingResult = goAsync() val pendingResult = goAsync()
syncMessages.execute(Unit, { pendingResult.finish() }) syncMessages.execute(Unit) { pendingResult.finish() }
} }
} }
@@ -37,7 +37,7 @@ class SmsReceiver : BroadcastReceiver() {
val subId = intent.extras?.getInt("subscription", -1) ?: -1 val subId = intent.extras?.getInt("subscription", -1) ?: -1
val pendingResult = goAsync() val pendingResult = goAsync()
receiveMessage.execute(ReceiveSms.Params(subId, messages), { pendingResult.finish() }) receiveMessage.execute(ReceiveSms.Params(subId, messages)) { pendingResult.finish() }
} }
} }
@@ -108,7 +108,7 @@ abstract class QkThemedActivity : QkActivity() {
super.onPostCreate(savedInstanceState) super.onPostCreate(savedInstanceState)
// Update the colours of the menu items // Update the colours of the menu items
Observables.combineLatest(menu, theme, { menu, theme -> Observables.combineLatest(menu, theme) { menu, theme ->
val text = resolveThemeColor(android.R.attr.textColorSecondary) val text = resolveThemeColor(android.R.attr.textColorSecondary)
(0 until menu.size()) (0 until menu.size())
.map { position -> menu.getItem(position) } .map { position -> menu.getItem(position) }
@@ -122,7 +122,7 @@ abstract class QkThemedActivity : QkActivity() {
menuItem.icon = this menuItem.icon = this
} }
} }
}).autoDisposable(scope(Lifecycle.Event.ON_DESTROY)).subscribe() }.autoDisposable(scope(Lifecycle.Event.ON_DESTROY)).subscribe()
} }
open fun getColoredMenuItems(): List<Int> { open fun getColoredMenuItems(): List<Int> {
@@ -65,7 +65,7 @@ class BlockedActivity : QkThemedActivity(), BlockedView {
AlertDialog.Builder(this) AlertDialog.Builder(this)
.setTitle(R.string.blocked_unblock_dialog_title) .setTitle(R.string.blocked_unblock_dialog_title)
.setMessage(R.string.blocked_unblock_dialog_message) .setMessage(R.string.blocked_unblock_dialog_message)
.setPositiveButton(R.string.button_unblock, { _, _ -> confirmUnblockIntent.onNext(Unit) }) .setPositiveButton(R.string.button_unblock) { _, _ -> confirmUnblockIntent.onNext(Unit) }
.setNegativeButton(R.string.button_cancel, null) .setNegativeButton(R.string.button_cancel, null)
.show() .show()
} }
@@ -56,7 +56,7 @@ class BlockedViewModel @Inject constructor(
?: false ?: false
} }
.doOnNext { installed -> if (!installed) navigator.showSia() } .doOnNext { installed -> if (!installed) navigator.showSia() }
.withLatestFrom(prefs.sia.asObservable(), { installed, enabled -> installed && !enabled }) .withLatestFrom(prefs.sia.asObservable()) { installed, enabled -> installed && !enabled }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { shouldEnable -> prefs.sia.set(shouldEnable) } .subscribe { shouldEnable -> prefs.sia.set(shouldEnable) }
@@ -67,7 +67,7 @@ class BlockedViewModel @Inject constructor(
// Unblock conversation // Unblock conversation
view.confirmUnblockIntent view.confirmUnblockIntent
.withLatestFrom(view.unblockIntent, { _, threadId -> threadId }) .withLatestFrom(view.unblockIntent) { _, threadId -> threadId }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { threadId -> markUnblocked.execute(listOf(threadId)) } .subscribe { threadId -> markUnblocked.execute(listOf(threadId)) }
} }
@@ -143,7 +143,7 @@ class ComposeViewModel @Inject constructor(
} }
disposables += contactsReducer disposables += contactsReducer
.scan(listOf<Contact>(), { previousState, reducer -> reducer(previousState) }) .scan(listOf<Contact>()) { previousState, reducer -> reducer(previousState) }
.doOnNext { contacts -> newState { copy(selectedContacts = contacts) } } .doOnNext { contacts -> newState { copy(selectedContacts = contacts) } }
.skipUntil(state.filter { state -> state.editingMode }) .skipUntil(state.filter { state -> state.editingMode })
.takeUntil(state.filter { state -> !state.editingMode }) .takeUntil(state.filter { state -> !state.editingMode })
@@ -209,9 +209,9 @@ class ComposeViewModel @Inject constructor(
// Set the contact suggestions list to visible at all times when in editing mode and there are no contacts // 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 // selected yet, and also visible while in editing mode and there is text entered in the query field
Observables Observables
.combineLatest(view.queryChangedIntent, selectedContacts, { query, selectedContacts -> .combineLatest(view.queryChangedIntent, selectedContacts) { query, selectedContacts ->
selectedContacts.isEmpty() || query.isNotEmpty() selectedContacts.isEmpty() || query.isNotEmpty()
}) }
.skipUntil(state.filter { state -> state.editingMode }) .skipUntil(state.filter { state -> state.editingMode })
.takeUntil(state.filter { state -> !state.editingMode }) .takeUntil(state.filter { state -> !state.editingMode })
.distinctUntilChanged() .distinctUntilChanged()
@@ -221,7 +221,7 @@ class ComposeViewModel @Inject constructor(
// 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
Observables Observables
.combineLatest(view.queryChangedIntent, contacts, selectedContacts, { query, contacts, selectedContacts -> .combineLatest(view.queryChangedIntent, contacts, selectedContacts) { query, contacts, selectedContacts ->
// Strip the accents from the query. This can be an expensive operation, so // Strip the accents from the query. This can be an expensive operation, so
// cache the result instead of doing it for each contact // cache the result instead of doing it for each contact
@@ -240,7 +240,7 @@ class ComposeViewModel @Inject constructor(
} }
filteredContacts filteredContacts
}) }
.skipUntil(state.filter { state -> state.editingMode }) .skipUntil(state.filter { state -> state.editingMode })
.takeUntil(state.filter { state -> !state.editingMode }) .takeUntil(state.filter { state -> !state.editingMode })
.subscribeOn(Schedulers.computation()) .subscribeOn(Schedulers.computation())
@@ -250,18 +250,18 @@ class ComposeViewModel @Inject constructor(
// 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
view.queryBackspaceIntent view.queryBackspaceIntent
.withLatestFrom(selectedContacts, view.queryChangedIntent, { event, contacts, query -> .withLatestFrom(selectedContacts, view.queryChangedIntent) { event, contacts, query ->
if (contacts.isNotEmpty() && query.isEmpty()) { if (contacts.isNotEmpty() && query.isEmpty()) {
contactsReducer.onNext { it.dropLast(1) } contactsReducer.onNext { it.dropLast(1) }
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
// Enter the first contact suggestion if the enter button is pressed // Enter the first contact suggestion if the enter button is pressed
view.queryEditorActionIntent view.queryEditorActionIntent
.filter { actionId -> actionId == EditorInfo.IME_ACTION_DONE } .filter { actionId -> actionId == EditorInfo.IME_ACTION_DONE }
.withLatestFrom(state, { _, state -> state }) .withLatestFrom(state) { _, state -> state }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { state -> .subscribe { state ->
state.contacts.firstOrNull()?.let { contact -> state.contacts.firstOrNull()?.let { contact ->
@@ -290,7 +290,7 @@ class ComposeViewModel @Inject constructor(
// Open the phone dialer if the call button is clicked // Open the phone dialer if the call button is clicked
view.optionsItemIntent view.optionsItemIntent
.filter { it == R.id.call } .filter { it == R.id.call }
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.map { conversation -> conversation.recipients.first() } .map { conversation -> conversation.recipients.first() }
.map { recipient -> recipient.address } .map { recipient -> recipient.address }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -299,19 +299,19 @@ class ComposeViewModel @Inject constructor(
// Open the conversation settings if info button is clicked // Open the conversation settings if info button is clicked
view.optionsItemIntent view.optionsItemIntent
.filter { it == R.id.info } .filter { it == R.id.info }
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { conversation -> navigator.showConversationInfo(conversation.id) } .subscribe { conversation -> navigator.showConversationInfo(conversation.id) }
// Copy the message contents // Copy the message contents
view.optionsItemIntent view.optionsItemIntent
.filter { it == R.id.copy } .filter { it == R.id.copy }
.withLatestFrom(view.messagesSelectedIntent, { _, messages -> .withLatestFrom(view.messagesSelectedIntent) { _, messages ->
messages?.firstOrNull()?.let { messageRepo.getMessage(it) }?.let { message -> messages?.firstOrNull()?.let { messageRepo.getMessage(it) }?.let { message ->
ClipboardUtils.copy(context, message.getText()) ClipboardUtils.copy(context, message.getText())
context.makeToast(R.string.toast_copied) context.makeToast(R.string.toast_copied)
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { view.clearSelection() } .subscribe { view.clearSelection() }
@@ -328,21 +328,21 @@ class ComposeViewModel @Inject constructor(
// Delete the messages // Delete the messages
view.optionsItemIntent view.optionsItemIntent
.filter { it == R.id.delete } .filter { it == R.id.delete }
.withLatestFrom(view.messagesSelectedIntent, conversation, { _, messages, conversation -> .withLatestFrom(view.messagesSelectedIntent, conversation) { _, messages, conversation ->
deleteMessages.execute(DeleteMessages.Params(messages, conversation.id)) deleteMessages.execute(DeleteMessages.Params(messages, conversation.id))
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { view.clearSelection() } .subscribe { view.clearSelection() }
// Forward the message // Forward the message
view.optionsItemIntent view.optionsItemIntent
.filter { it == R.id.forward } .filter { it == R.id.forward }
.withLatestFrom(view.messagesSelectedIntent, { _, messages -> .withLatestFrom(view.messagesSelectedIntent) { _, messages ->
messages?.firstOrNull()?.let { messageRepo.getMessage(it) }?.let { message -> messages?.firstOrNull()?.let { messageRepo.getMessage(it) }?.let { message ->
val images = message.parts.filter { it.isImage() }.mapNotNull { it.getUri() } val images = message.parts.filter { it.isImage() }.mapNotNull { it.getUri() }
navigator.showCompose(message.body, images) navigator.showCompose(message.body, images)
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { view.clearSelection() } .subscribe { view.clearSelection() }
@@ -416,18 +416,18 @@ class ComposeViewModel @Inject constructor(
// Save draft when the activity goes into the background // Save draft when the activity goes into the background
view.activityVisibleIntent view.activityVisibleIntent
.filter { visible -> !visible } .filter { visible -> !visible }
.withLatestFrom(conversation, { _, conversation -> conversation.id }) .withLatestFrom(conversation) { _, conversation -> conversation.id }
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.withLatestFrom(view.textChangedIntent, { threadId, draft -> .withLatestFrom(view.textChangedIntent) { threadId, draft ->
conversationRepo.saveDraft(threadId, draft.toString()) conversationRepo.saveDraft(threadId, draft.toString())
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
// Mark the conversation read, if in foreground // Mark the conversation read, if in foreground
Observables.combineLatest(messages, view.activityVisibleIntent, { _, visible -> visible }) Observables.combineLatest(messages, view.activityVisibleIntent) { _, visible -> visible }
.filter { visible -> visible } .filter { visible -> visible }
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.mapNotNull { conversation -> conversation.takeIf { it.isValid }?.id } .mapNotNull { conversation -> conversation.takeIf { it.isValid }?.id }
.debounce(200, TimeUnit.MILLISECONDS) .debounce(200, TimeUnit.MILLISECONDS)
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -457,14 +457,14 @@ class ComposeViewModel @Inject constructor(
Observable.merge( Observable.merge(
view.attachmentSelectedIntent.map { uri -> Attachment(uri) }, view.attachmentSelectedIntent.map { uri -> Attachment(uri) },
view.inputContentIntent.map { inputContent -> Attachment(inputContent = inputContent) }) view.inputContentIntent.map { inputContent -> Attachment(inputContent = inputContent) })
.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 { copy(attaching = false) } } .subscribe { newState { copy(attaching = false) } }
// Detach a photo // Detach a photo
view.attachmentDeletedIntent view.attachmentDeletedIntent
.withLatestFrom(attachments, { bitmap, attachments -> attachments.filter { it !== bitmap } }) .withLatestFrom(attachments) { bitmap, attachments -> attachments.filter { it !== bitmap } }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { attachments.onNext(it) } .subscribe { attachments.onNext(it) }
@@ -488,9 +488,9 @@ class ComposeViewModel @Inject constructor(
// 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
Observables Observables
.combineLatest(view.textChangedIntent, attachments, { text, attachments -> .combineLatest(view.textChangedIntent, attachments) { text, attachments ->
text.isNotBlank() || attachments.isNotEmpty() text.isNotBlank() || attachments.isNotEmpty()
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { canSend -> newState { copy(canSend = canSend) } } .subscribe { canSend -> newState { copy(canSend = canSend) } }
@@ -531,7 +531,7 @@ class ComposeViewModel @Inject constructor(
view.sendIntent view.sendIntent
.withLatestFrom(view.textChangedIntent) { _, body -> body } .withLatestFrom(view.textChangedIntent) { _, body -> body }
.map { body -> body.toString() } .map { body -> body.toString() }
.withLatestFrom(state, attachments, conversation, selectedContacts, { body, state, attachments, conversation, contacts -> .withLatestFrom(state, attachments, conversation, selectedContacts) { body, state, attachments, conversation, contacts ->
val subId = state.subscription?.subscriptionId ?: -1 val subId = state.subscription?.subscriptionId ?: -1
if (state.sendAsGroup) { if (state.sendAsGroup) {
@@ -561,7 +561,7 @@ class ComposeViewModel @Inject constructor(
if (state.editingMode) { if (state.editingMode) {
newState { copy(editingMode = false, sendAsGroup = true, hasError = !state.sendAsGroup) } newState { copy(editingMode = false, sendAsGroup = true, hasError = !state.sendAsGroup) }
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -570,12 +570,12 @@ class ComposeViewModel @Inject constructor(
.filter { it == android.R.id.home } .filter { it == android.R.id.home }
.map { Unit } .map { Unit }
.mergeWith(view.backPressedIntent) .mergeWith(view.backPressedIntent)
.withLatestFrom(state, { _, state -> .withLatestFrom(state) { _, state ->
when { when {
state.selectedMessages > 0 -> view.clearSelection() state.selectedMessages > 0 -> view.clearSelection()
else -> newState { copy(hasError = true) } else -> newState { copy(hasError = true) }
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -129,7 +129,7 @@ class ConversationInfoActivity : QkThemedActivity(), ConversationInfoView {
AlertDialog.Builder(this) AlertDialog.Builder(this)
.setTitle(R.string.info_name) .setTitle(R.string.info_name)
.setView(editText) .setView(editText)
.setPositiveButton(R.string.button_save, { _, _ -> nameChangedIntent.onNext(editText.text.toString()) }) .setPositiveButton(R.string.button_save) { _, _ -> nameChangedIntent.onNext(editText.text.toString()) }
.setNegativeButton(R.string.button_cancel, null) .setNegativeButton(R.string.button_cancel, null)
.show() .show()
} }
@@ -138,7 +138,7 @@ class ConversationInfoActivity : QkThemedActivity(), ConversationInfoView {
AlertDialog.Builder(this) AlertDialog.Builder(this)
.setTitle(R.string.dialog_delete_title) .setTitle(R.string.dialog_delete_title)
.setMessage(R.string.dialog_delete_message) .setMessage(R.string.dialog_delete_message)
.setPositiveButton(R.string.button_delete, { _, _ -> confirmDeleteIntent.onNext(Unit) }) .setPositiveButton(R.string.button_delete) { _, _ -> confirmDeleteIntent.onNext(Unit) }
.setNegativeButton(R.string.button_cancel, null) .setNegativeButton(R.string.button_cancel, null)
.show() .show()
} }
@@ -118,19 +118,19 @@ class ConversationInfoViewModel @Inject constructor(
// Show the notifications settings for the conversation // Show the notifications settings for the conversation
view.notificationsIntent view.notificationsIntent
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { conversation -> navigator.showNotificationSettings(conversation.id) } .subscribe { conversation -> navigator.showNotificationSettings(conversation.id) }
// Show the theme settings for the conversation // Show the theme settings for the conversation
view.themeIntent view.themeIntent
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { conversation -> navigator.showThemePicker(conversation.id) } .subscribe { conversation -> navigator.showThemePicker(conversation.id) }
// Toggle the archived state of the conversation // Toggle the archived state of the conversation
view.archiveIntent view.archiveIntent
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { conversation -> .subscribe { conversation ->
when (conversation.archived) { when (conversation.archived) {
@@ -141,7 +141,7 @@ class ConversationInfoViewModel @Inject constructor(
// Toggle the blocked state of the conversation // Toggle the blocked state of the conversation
view.blockIntent view.blockIntent
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { conversation -> .subscribe { conversation ->
when (conversation.blocked) { when (conversation.blocked) {
@@ -157,7 +157,7 @@ class ConversationInfoViewModel @Inject constructor(
// Delete the conversation // Delete the conversation
view.confirmDeleteIntent view.confirmDeleteIntent
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { conversation -> deleteConversations.execute(listOf(conversation.id)) } .subscribe { conversation -> deleteConversations.execute(listOf(conversation.id)) }
} }
@@ -62,7 +62,7 @@ class GalleryViewModel @Inject constructor(
// When the screen is touched, toggle the visibility of the navigation UI // When the screen is touched, toggle the visibility of the navigation UI
view.screenTouchedIntent view.screenTouchedIntent
.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 { copy(navigationVisible = navigationVisible) } } .subscribe { navigationVisible -> newState { copy(navigationVisible = navigationVisible) } }
@@ -70,7 +70,7 @@ class GalleryViewModel @Inject constructor(
// Save image to device // Save image to device
view.optionsItemSelectedIntent view.optionsItemSelectedIntent
.filter { itemId -> itemId == R.id.save } .filter { itemId -> itemId == R.id.save }
.withLatestFrom(partIdFlowable.toObservable(), { _, partId -> partId }) .withLatestFrom(partIdFlowable.toObservable()) { _, partId -> partId }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { partId -> saveImage.execute(partId) { context.makeToast(R.string.gallery_toast_saved) } } .subscribe { partId -> saveImage.execute(partId) { context.makeToast(R.string.gallery_toast_saved) } }
} }
@@ -100,7 +100,7 @@ class MainActivity : QkThemedActivity(), MainView {
private val itemTouchHelper by lazy { ItemTouchHelper(itemTouchCallback) } private val itemTouchHelper by lazy { ItemTouchHelper(itemTouchCallback) }
private val archiveSnackbar by lazy { private val archiveSnackbar by lazy {
Snackbar.make(drawerLayout, R.string.toast_archived, Snackbar.LENGTH_INDEFINITE).apply { Snackbar.make(drawerLayout, R.string.toast_archived, Snackbar.LENGTH_INDEFINITE).apply {
setAction(R.string.button_undo, { undoSwipeConversationIntent.onNext(Unit) }) setAction(R.string.button_undo) { undoSwipeConversationIntent.onNext(Unit) }
} }
} }
@@ -296,7 +296,7 @@ class MainActivity : QkThemedActivity(), MainView {
AlertDialog.Builder(this) AlertDialog.Builder(this)
.setTitle(R.string.dialog_delete_title) .setTitle(R.string.dialog_delete_title)
.setMessage(R.string.dialog_delete_message) .setMessage(R.string.dialog_delete_message)
.setPositiveButton(R.string.button_delete, { _, _ -> confirmDeleteIntent.onNext(Unit) }) .setPositiveButton(R.string.button_delete) { _, _ -> confirmDeleteIntent.onNext(Unit) }
.setNegativeButton(R.string.button_cancel, null) .setNegativeButton(R.string.button_cancel, null)
.show() .show()
} }
@@ -112,10 +112,10 @@ class MainViewModel @Inject constructor(
Observables.combineLatest( Observables.combineLatest(
view.activityResumedIntent.map { permissionManager.isDefaultSms() }.distinctUntilChanged(), view.activityResumedIntent.map { permissionManager.isDefaultSms() }.distinctUntilChanged(),
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 { copy(defaultSms = defaultSms, smsPermission = smsPermission, contactPermission = contactPermission) } newState { copy(defaultSms = defaultSms, smsPermission = smsPermission, contactPermission = contactPermission) }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -138,12 +138,12 @@ class MainViewModel @Inject constructor(
.debounce(200, TimeUnit.MILLISECONDS) .debounce(200, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.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 { copy(page = Inbox(data = conversationRepo.getConversations())) } newState { copy(page = Inbox(data = conversationRepo.getConversations())) }
} }
query query
}) }
.filter { query -> query.length >= 2 } .filter { query -> query.length >= 2 }
.doOnNext { .doOnNext {
newState { newState {
@@ -161,7 +161,7 @@ class MainViewModel @Inject constructor(
.subscribe { navigator.showCompose() } .subscribe { navigator.showCompose() }
view.homeIntent view.homeIntent
.withLatestFrom(state, { _, state -> .withLatestFrom(state) { _, state ->
when { when {
state.page is Searching -> view.clearSearch() state.page is Searching -> view.clearSearch()
@@ -172,7 +172,7 @@ class MainViewModel @Inject constructor(
else -> newState { copy(drawerOpen = true) } else -> newState { copy(drawerOpen = true) }
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -199,7 +199,7 @@ class MainViewModel @Inject constructor(
.subscribe() .subscribe()
view.optionsItemIntent view.optionsItemIntent
.withLatestFrom(view.conversationsSelectedIntent, { itemId, conversations -> .withLatestFrom(view.conversationsSelectedIntent) { itemId, conversations ->
when (itemId) { when (itemId) {
R.id.read -> { R.id.read -> {
markRead.execute(conversations) markRead.execute(conversations)
@@ -228,7 +228,7 @@ class MainViewModel @Inject constructor(
R.id.delete -> view.showDeleteDialog() R.id.delete -> view.showDeleteDialog()
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -244,7 +244,7 @@ class MainViewModel @Inject constructor(
.subscribe { ratingManager.dismiss() } .subscribe { ratingManager.dismiss() }
view.conversationsSelectedIntent view.conversationsSelectedIntent
.withLatestFrom(state, { selection, state -> .withLatestFrom(state) { selection, state ->
val read = selection val read = selection
.mapNotNull(conversationRepo::getConversation) .mapNotNull(conversationRepo::getConversation)
.sumBy { if (it.read) -1 else 1 } >= 0 .sumBy { if (it.read) -1 else 1 } >= 0
@@ -261,13 +261,13 @@ class MainViewModel @Inject constructor(
newState { copy(page = page) } newState { copy(page = page) }
} }
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
// Delete the conversation // Delete the conversation
view.confirmDeleteIntent view.confirmDeleteIntent
.withLatestFrom(view.conversationsSelectedIntent, { _, conversations -> conversations }) .withLatestFrom(view.conversationsSelectedIntent) { _, conversations -> conversations }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { conversations -> .subscribe { conversations ->
deleteConversations.execute(conversations) deleteConversations.execute(conversations)
@@ -275,44 +275,44 @@ class MainViewModel @Inject constructor(
} }
view.swipeConversationIntent view.swipeConversationIntent
.withLatestFrom(state, { threadId, state -> .withLatestFrom(state) { threadId, state ->
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 { copy(page = page) } newState { copy(page = page) }
} }
} }
}) }
.switchMap { Observable.timer(2750, TimeUnit.MILLISECONDS) } .switchMap { Observable.timer(2750, TimeUnit.MILLISECONDS) }
.withLatestFrom(state, { threadId, state -> .withLatestFrom(state) { threadId, state ->
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 { copy(page = page) } newState { copy(page = page) }
} }
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
view.undoSwipeConversationIntent view.undoSwipeConversationIntent
.withLatestFrom(view.swipeConversationIntent, { _, threadId -> threadId }) .withLatestFrom(view.swipeConversationIntent) { _, threadId -> threadId }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { threadId -> markUnarchived.execute(listOf(threadId)) } .subscribe { threadId -> markUnarchived.execute(listOf(threadId)) }
view.snackbarButtonIntent view.snackbarButtonIntent
.withLatestFrom(state, { _, state -> .withLatestFrom(state) { _, state ->
when { when {
!state.smsPermission -> view.requestPermissions() !state.smsPermission -> view.requestPermissions()
!state.defaultSms -> navigator.showDefaultSmsDialog() !state.defaultSms -> navigator.showDefaultSmsDialog()
!state.contactPermission -> view.requestPermissions() !state.contactPermission -> view.requestPermissions()
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
view.backPressedIntent view.backPressedIntent
.withLatestFrom(state, { _, state -> .withLatestFrom(state) { _, state ->
when { when {
state.drawerOpen -> newState { copy(drawerOpen = false) } state.drawerOpen -> newState { copy(drawerOpen = false) }
@@ -327,7 +327,7 @@ class MainViewModel @Inject constructor(
else -> newState { copy(hasError = true) } else -> newState { copy(hasError = true) }
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
} }
@@ -111,7 +111,7 @@ class QkReplyViewModel @Inject constructor(
// Mark read // Mark read
view.menuItemIntent view.menuItemIntent
.filter { id -> id == R.id.read } .filter { id -> id == R.id.read }
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.map { conversation -> conversation.id } .map { conversation -> conversation.id }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { threadId -> .subscribe { threadId ->
@@ -121,7 +121,7 @@ class QkReplyViewModel @Inject constructor(
// Call // Call
view.menuItemIntent view.menuItemIntent
.filter { id -> id == R.id.call } .filter { id -> id == R.id.call }
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.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())
@@ -130,7 +130,7 @@ class QkReplyViewModel @Inject constructor(
// Show all messages // Show all messages
view.menuItemIntent view.menuItemIntent
.filter { id -> id == R.id.expand } .filter { id -> id == R.id.expand }
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.map { conversation -> messageRepo.getMessages(conversation.id) } .map { conversation -> messageRepo.getMessages(conversation.id) }
.doOnNext(messages::onNext) .doOnNext(messages::onNext)
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -139,7 +139,7 @@ class QkReplyViewModel @Inject constructor(
// Show unread messages only // Show unread messages only
view.menuItemIntent view.menuItemIntent
.filter { id -> id == R.id.collapse } .filter { id -> id == R.id.collapse }
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.map { conversation -> messageRepo.getUnreadMessages(conversation.id) } .map { conversation -> messageRepo.getUnreadMessages(conversation.id) }
.doOnNext(messages::onNext) .doOnNext(messages::onNext)
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -148,7 +148,7 @@ class QkReplyViewModel @Inject constructor(
// View conversation // View conversation
view.menuItemIntent view.menuItemIntent
.filter { id -> id == R.id.view } .filter { id -> id == R.id.view }
.withLatestFrom(conversation, { _, conversation -> conversation }) .withLatestFrom(conversation) { _, conversation -> conversation }
.map { conversation -> conversation.id } .map { conversation -> conversation.id }
.doOnNext { threadId -> navigator.showConversation(threadId) } .doOnNext { threadId -> navigator.showConversation(threadId) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -184,9 +184,9 @@ class QkReplyViewModel @Inject constructor(
.debounce(100, TimeUnit.MILLISECONDS) .debounce(100, TimeUnit.MILLISECONDS)
.map { draft -> draft.toString() } .map { draft -> draft.toString() }
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.withLatestFrom(conversation.map { it.id }, { draft, threadId -> .withLatestFrom(conversation.map { it.id }) { draft, threadId ->
conversationRepo.saveDraft(threadId, draft) conversationRepo.saveDraft(threadId, draft)
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -207,16 +207,16 @@ class QkReplyViewModel @Inject constructor(
// Send a message when the send button is clicked, and disable editing mode if it's enabled // Send a message when the send button is clicked, and disable editing mode if it's enabled
view.sendIntent view.sendIntent
.withLatestFrom(view.textChangedIntent, { _, body -> body }) .withLatestFrom(view.textChangedIntent) { _, body -> body }
.map { body -> body.toString() } .map { body -> body.toString() }
.withLatestFrom(state, conversation, { body, state, conversation -> .withLatestFrom(state, conversation) { body, state, conversation ->
val subId = state.subscription?.subscriptionId ?: -1 val subId = state.subscription?.subscriptionId ?: -1
val threadId = conversation.id val threadId = conversation.id
val addresses = conversation.recipients.map { it.address } val addresses = conversation.recipients.map { it.address }
sendMessage.execute(SendMessage.Params(subId, threadId, addresses, body)) sendMessage.execute(SendMessage.Params(subId, threadId, addresses, body))
view.setDraft("") view.setDraft("")
threadId threadId
}) }
.doOnNext { threadId -> .doOnNext { threadId ->
markRead.execute(listOf(threadId)) { markRead.execute(listOf(threadId)) {
newState { copy(hasError = true) } newState { copy(hasError = true) }
@@ -140,7 +140,7 @@ class SettingsActivity : QkThemedActivity(), SettingsView {
override fun showQksmsPlusSnackbar() { override fun showQksmsPlusSnackbar() {
Snackbar.make(contentView, R.string.toast_qksms_plus, Snackbar.LENGTH_LONG).run { Snackbar.make(contentView, R.string.toast_qksms_plus, Snackbar.LENGTH_LONG).run {
setAction(R.string.button_more, { viewQksmsPlusIntent.onNext(Unit) }) setAction(R.string.button_more) { viewQksmsPlusIntent.onNext(Unit) }
show() show()
} }
} }
@@ -153,9 +153,9 @@ class SettingsViewModel @Inject constructor(
R.id.sync -> { R.id.sync -> {
newState { copy(syncing = true) } newState { copy(syncing = true) }
syncMessages.execute(Unit, { syncMessages.execute(Unit) {
newState { copy(syncing = false) } newState { copy(syncing = false) }
}) }
} }
R.id.about -> navigator.showAbout() R.id.about -> navigator.showAbout()
@@ -163,13 +163,13 @@ class SettingsViewModel @Inject constructor(
} }
view.nightModeSelectedIntent view.nightModeSelectedIntent
.withLatestFrom(billingManager.upgradeStatus, { mode, upgraded -> .withLatestFrom(billingManager.upgradeStatus) { mode, upgraded ->
if (!upgraded && mode == Preferences.NIGHT_MODE_AUTO) { if (!upgraded && mode == Preferences.NIGHT_MODE_AUTO) {
view.showQksmsPlusSnackbar() view.showQksmsPlusSnackbar()
} else { } else {
nightModeManager.updateNightMode(mode) nightModeManager.updateNightMode(mode)
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -190,13 +190,13 @@ class SettingsViewModel @Inject constructor(
.subscribe { prefs.textSize.set(it) } .subscribe { prefs.textSize.set(it) }
view.sendDelayChangedIntent view.sendDelayChangedIntent
.withLatestFrom(billingManager.upgradeStatus, { duration, upgraded -> .withLatestFrom(billingManager.upgradeStatus) { duration, upgraded ->
if (!upgraded && duration != 0) { if (!upgraded && duration != 0) {
view.showQksmsPlusSnackbar() view.showQksmsPlusSnackbar()
} else { } else {
prefs.sendDelay.set(duration) prefs.sendDelay.set(duration)
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -69,7 +69,7 @@ class ThemePickerActivity : QkThemedActivity(), ThemePickerView {
override fun showQksmsPlusSnackbar() { override fun showQksmsPlusSnackbar() {
Snackbar.make(contentView, R.string.toast_qksms_plus, Snackbar.LENGTH_LONG).run { Snackbar.make(contentView, R.string.toast_qksms_plus, Snackbar.LENGTH_LONG).run {
setAction(R.string.button_more, { viewQksmsPlusIntent.onNext(Unit) }) setAction(R.string.button_more) { viewQksmsPlusIntent.onNext(Unit) }
show() show()
} }
} }
@@ -62,20 +62,20 @@ class ThemePickerViewModel @Inject constructor(
.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 { 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
.withLatestFrom(view.hsvThemeSelectedIntent, { _, color -> color }) .withLatestFrom(view.hsvThemeSelectedIntent) { _, color -> color }
.withLatestFrom(billingManager.upgradeStatus, { color, upgraded -> .withLatestFrom(billingManager.upgradeStatus) { color, upgraded ->
if (!upgraded) { if (!upgraded) {
view.showQksmsPlusSnackbar() view.showQksmsPlusSnackbar()
} else { } else {
theme.set(color) theme.set(color)
} }
}) }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe() .subscribe()
@@ -86,7 +86,7 @@ class ThemePickerViewModel @Inject constructor(
// Reset the theme // Reset the theme
view.hsvThemeClearedIntent view.hsvThemeClearedIntent
.withLatestFrom(theme.asObservable(), { _, color -> color }) .withLatestFrom(theme.asObservable()) { _, color -> color }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { color -> view.setCurrentTheme(color) } .subscribe { color -> view.setCurrentTheme(color) }
} }