Add draft support

This commit is contained in:
Moez Bhatti
2018-02-23 22:33:12 -05:00
parent 2252f52217
commit 9ac7fadfdd
4 changed files with 70 additions and 34 deletions
@@ -27,6 +27,7 @@ open class Conversation : RealmObject() {
@PrimaryKey var id: Long = 0
var recipients: RealmList<Recipient> = RealmList()
var archived: Boolean = false
var draft: String = ""
fun getTitle(): String {
var title = ""
@@ -139,6 +139,20 @@ class MessageRepository @Inject constructor(
.observeOn(AndroidSchedulers.mainThread())
}
fun saveDraft(threadId: Long, draft: String) {
Realm.getDefaultInstance().use { realm ->
val conversation = realm.where(Conversation::class.java)
.equalTo("id", threadId)
.findFirst()
conversation?.let {
realm.executeTransaction {
conversation.draft = draft
}
}
}
}
fun getMessages(threadId: Long): RealmResults<Message> {
return Realm.getDefaultInstance()
.where(Message::class.java)
@@ -395,29 +409,23 @@ class MessageRepository @Inject constructor(
}
}
fun markSent(id: Long): Flowable<*> {
val realm = Realm.getDefaultInstance()
return realm.where(Message::class.java).equalTo("id", id)
.findAllAsync()
.asFlowable()
.filter { it.isLoaded }
.filter { it.isValid }
.mapNotNull { it[0] }
.take(1)
.doOnNext { message ->
// Update the message in realm
realm.executeTransaction {
message.boxId = Sms.MESSAGE_TYPE_SENT
}
}
.map { message -> message.getUri() }
.observeOn(Schedulers.computation())
.doOnNext { uri ->
// Update the message in the native ContentProvider
val values = ContentValues()
values.put(Sms.TYPE, Sms.MESSAGE_TYPE_SENT)
context.contentResolver.update(uri, values, null, null)
fun markSent(id: Long) {
Realm.getDefaultInstance().use { realm ->
realm.refresh()
val message = realm.where(Message::class.java).equalTo("id", id).findFirst()
message?.let {
// Update the message in realm
realm.executeTransaction {
message.boxId = Sms.MESSAGE_TYPE_SENT
}
// Update the message in the native ContentProvider
val values = ContentValues()
values.put(Sms.TYPE, Sms.MESSAGE_TYPE_SENT)
context.contentResolver.update(message.getUri(), values, null, null)
}
}
}
fun markFailed(id: Long, resultCode: Int): Flowable<*> {
+4 -6
View File
@@ -20,15 +20,13 @@ package interactor
import data.repository.MessageRepository
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import javax.inject.Inject
class MarkSent @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Long, Any>() {
class MarkSent @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Long, Long>() {
override fun buildObservable(params: Long): Flowable<Any> {
return Flowable.just(Unit)
.observeOn(AndroidSchedulers.mainThread())
.flatMap { messageRepo.markSent(params) }
override fun buildObservable(params: Long): Flowable<Long> {
return Flowable.just(params)
.doOnNext { messageRepo.markSent(params) }
}
}
@@ -69,6 +69,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
@Inject lateinit var markRead: MarkRead
@Inject lateinit var deleteMessage: DeleteMessage
private var sharedText: String = ""
private var draft: String = ""
private val attachments: Subject<List<Uri>> = BehaviorSubject.createDefault(ArrayList())
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
@@ -80,7 +81,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
init {
appComponent.inject(this)
draft = intent.extras?.getString(Intent.EXTRA_TEXT) ?: ""
sharedText = intent.extras?.getString(Intent.EXTRA_TEXT) ?: ""
val threadId = intent.extras?.getLong("threadId") ?: 0L
var address = ""
@@ -168,11 +169,6 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
override fun bindView(view: ComposeView) {
super.bindView(view)
if (draft.isNotEmpty()) {
view.setDraft(draft)
draft = ""
}
// 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
Observables
@@ -296,6 +292,23 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
.autoDisposable(view.scope())
.subscribe { attachments.onNext(it) }
conversation
.map { conversation -> conversation.draft }
.distinctUntilChanged()
.autoDisposable(view.scope())
.subscribe { draft ->
// If text was shared into the conversation, it should take priority over the
// existing draft
//
// TODO: Show dialog warning user about overwriting draft
if (sharedText.isNotBlank()) {
view.setDraft(sharedText)
} else {
view.setDraft(draft)
}
}
// Enable the send button when there is text input into the new message body or there's
// an attachment, disable otherwise
Observables
@@ -305,6 +318,12 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
.autoDisposable(view.scope())
.subscribe { canSend -> newState { it.copy(canSend = canSend) } }
// Update the draft whenever the text is changed
view.textChangedIntent
.map { draft -> draft.toString() }
.autoDisposable(view.scope())
.subscribe { draft -> this.draft = draft }
// Send a message when the send button is clicked, and disable editing mode if it's enabled
view.sendIntent
.withLatestFrom(view.textChangedIntent, { _, body -> body })
@@ -343,4 +362,14 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
.subscribe { message -> deleteMessage.execute(message.id) }
}
override fun onCleared() {
super.onCleared()
// Save the draft
val conversation = conversation.blockingLatest().firstOrNull { it.id != 0L }
conversation?.let {
messageRepo.saveDraft(conversation.id, draft)
}
}
}