forked from upstream-mirrors/quik
Add ability to archive/unarchive from compose activity
This commit is contained in:
@@ -127,7 +127,17 @@ class MessageRepository @Inject constructor(
|
||||
.findFirst()
|
||||
|
||||
realm.executeTransaction { conversation?.archived = true }
|
||||
realm.close()
|
||||
}
|
||||
|
||||
fun markUnarchived(threadId: Long) {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
val conversation = realm.where(Conversation::class.java)
|
||||
.equalTo("id", threadId)
|
||||
.equalTo("archived", true)
|
||||
.findFirst()
|
||||
|
||||
realm.executeTransaction { conversation?.archived = false }
|
||||
realm.close()
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.moez.QKSMS.domain.interactor
|
||||
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import javax.inject.Inject
|
||||
|
||||
class MarkUnarchived @Inject constructor(val messageRepo: MessageRepository) : Interactor<Unit, Long>() {
|
||||
|
||||
override fun buildObservable(params: Long): Flowable<Unit> {
|
||||
return Flowable.just(Unit)
|
||||
.doOnNext { messageRepo.markUnarchived(params) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -34,6 +34,7 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
override val chipSelectedIntent: Subject<Contact> by lazy { contactsAdapter.contactSelected }
|
||||
override val chipDeletedIntent: Subject<Contact> by lazy { chipsAdapter.chipDeleted }
|
||||
override val callIntent: Subject<Unit> = PublishSubject.create()
|
||||
override val archiveIntent: Subject<Unit> = PublishSubject.create()
|
||||
override val copyTextIntent: Subject<Message> = PublishSubject.create()
|
||||
override val forwardMessageIntent: Subject<Message> = PublishSubject.create()
|
||||
override val deleteMessageIntent: Subject<Message> = PublishSubject.create()
|
||||
@@ -46,6 +47,8 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
private val messageAdapter by lazy { MessagesAdapter() }
|
||||
|
||||
private var pendingCallVisibility = false
|
||||
private var pendingArchiveVisibility = false
|
||||
private var pendingArchiveTitle = ""
|
||||
|
||||
init {
|
||||
appComponent.inject(this)
|
||||
@@ -91,9 +94,18 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
|
||||
menu?.findItem(R.id.call).let { menuItem ->
|
||||
if (menuItem == null) {
|
||||
pendingCallVisibility = state.canCall
|
||||
pendingCallVisibility = !state.editingMode
|
||||
} else {
|
||||
menuItem.setVisible(state.canCall)
|
||||
menuItem.isVisible = !state.editingMode
|
||||
}
|
||||
}
|
||||
|
||||
menu?.findItem(R.id.archive).let { menuItem ->
|
||||
if (menuItem == null) {
|
||||
pendingArchiveVisibility = !state.editingMode
|
||||
} else {
|
||||
menuItem.isVisible = !state.editingMode
|
||||
menuItem.setTitle(if (state.archived) R.string.menu_unarchive else R.string.menu_archive)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,6 +164,10 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.compose, menu)
|
||||
menu?.findItem(R.id.call)?.isVisible = pendingCallVisibility
|
||||
menu?.findItem(R.id.archive)?.run {
|
||||
isVisible = pendingArchiveVisibility
|
||||
title = pendingArchiveTitle
|
||||
}
|
||||
return super.onCreateOptionsMenu(menu)
|
||||
}
|
||||
|
||||
@@ -162,6 +178,11 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
true
|
||||
}
|
||||
|
||||
R.id.archive -> {
|
||||
archiveIntent.onNext(Unit)
|
||||
true
|
||||
}
|
||||
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ data class ComposeState(
|
||||
val contactsVisible: Boolean = false,
|
||||
val selectedContacts: List<Contact> = ArrayList(),
|
||||
val title: String = "",
|
||||
val canCall: Boolean = false,
|
||||
val archived: Boolean = false,
|
||||
val messages: RealmResults<Message>? = null,
|
||||
val draft: String = "",
|
||||
val canSend: Boolean = false
|
||||
|
||||
@@ -12,6 +12,7 @@ interface ComposeView : QkView<ComposeState> {
|
||||
val chipSelectedIntent: Subject<Contact>
|
||||
val chipDeletedIntent: Subject<Contact>
|
||||
val callIntent: Subject<Unit>
|
||||
val archiveIntent: Subject<Unit>
|
||||
val copyTextIntent: Subject<Message>
|
||||
val forwardMessageIntent: Subject<Message>
|
||||
val deleteMessageIntent: Subject<Message>
|
||||
|
||||
@@ -11,9 +11,7 @@ import com.moez.QKSMS.data.model.Contact
|
||||
import com.moez.QKSMS.data.model.Conversation
|
||||
import com.moez.QKSMS.data.repository.ContactRepository
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.domain.interactor.DeleteMessage
|
||||
import com.moez.QKSMS.domain.interactor.MarkRead
|
||||
import com.moez.QKSMS.domain.interactor.SendMessage
|
||||
import com.moez.QKSMS.domain.interactor.*
|
||||
import com.moez.QKSMS.presentation.Navigator
|
||||
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||
import io.reactivex.Observable
|
||||
@@ -32,6 +30,8 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
||||
@Inject lateinit var contactsRepo: ContactRepository
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
@Inject lateinit var navigator: Navigator
|
||||
@Inject lateinit var markArchived: MarkArchived
|
||||
@Inject lateinit var markUnarchived: MarkUnarchived
|
||||
@Inject lateinit var sendMessage: SendMessage
|
||||
@Inject lateinit var markRead: MarkRead
|
||||
@Inject lateinit var deleteMessage: DeleteMessage
|
||||
@@ -60,18 +60,21 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
||||
.filter { conversation -> conversation.isLoaded }
|
||||
.filter { conversation -> conversation.isValid }
|
||||
.mergeWith(selectedConversation)
|
||||
.distinctUntilChanged { conversation -> conversation.id }
|
||||
.distinctUntilChanged()
|
||||
.doOnNext { conversation ->
|
||||
newState { it.copy(title = conversation.getTitle(), canCall = conversation.contacts.isNotEmpty()) }
|
||||
newState { it.copy(title = conversation.getTitle(), archived = conversation.archived) }
|
||||
}
|
||||
|
||||
disposables += sendMessage
|
||||
disposables += markRead
|
||||
disposables += markArchived
|
||||
disposables += markUnarchived
|
||||
disposables += conversation.subscribe()
|
||||
|
||||
// When the conversation changes, update the messages for the adapter
|
||||
// When the message list changes, make sure to mark them read
|
||||
disposables += conversation
|
||||
.distinctUntilChanged()
|
||||
.map { conversation -> messageRepo.getMessages(conversation.id) }
|
||||
.doOnNext { messages -> newState { it.copy(messages = messages) } }
|
||||
.flatMap { messages -> messages.asObservable() }
|
||||
@@ -125,6 +128,16 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
||||
.map { contact -> contact.address }
|
||||
.subscribe { address -> navigator.makePhoneCall(address) }
|
||||
|
||||
// Toggle the archived state of the conversation
|
||||
intents += view.archiveIntent
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
.subscribe { conversation ->
|
||||
when (conversation.archived) {
|
||||
true -> markUnarchived.execute(conversation.id, { context.makeToast(R.string.toast_unarchived) })
|
||||
false -> markArchived.execute(conversation.id, { context.makeToast(R.string.toast_archived) })
|
||||
}
|
||||
}
|
||||
|
||||
// Enable the send button when there is text input into the new message body, disable otherwise
|
||||
intents += view.textChangedIntent.subscribe { text ->
|
||||
newState { it.copy(draft = text.toString(), canSend = text.isNotEmpty()) }
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item
|
||||
android:id="@+id/call"
|
||||
android:icon="@drawable/ic_call_black_24dp"
|
||||
android:title="@string/menu_call"
|
||||
android:visible="false"
|
||||
app:showAsAction="always" />
|
||||
|
||||
<item
|
||||
android:id="@+id/archive"
|
||||
android:title="@string/menu_archive"
|
||||
android:visible="false"
|
||||
app:showAsAction="never" />
|
||||
|
||||
</menu>
|
||||
@@ -6,6 +6,8 @@
|
||||
<string name="title_compose">Type a name or number</string>
|
||||
|
||||
<string name="menu_call">Call</string>
|
||||
<string name="menu_archive">Archive</string>
|
||||
<string name="menu_unarchive">Unarchive</string>
|
||||
|
||||
<string name="compose_hint">Write a message</string>
|
||||
|
||||
@@ -42,6 +44,8 @@
|
||||
<string name="notification_read">Mark read</string>
|
||||
|
||||
<string name="toast_copied">Copied</string>
|
||||
<string name="toast_archived">Archived</string>
|
||||
<string name="toast_unarchived">Moved to inbox</string>
|
||||
|
||||
<string-array name="message_options">
|
||||
<item>Copy text</item>
|
||||
|
||||
Reference in New Issue
Block a user