mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-22 13:48:52 +01:00
Add draft support
This commit is contained in:
@@ -27,6 +27,7 @@ open class Conversation : RealmObject() {
|
|||||||
@PrimaryKey var id: Long = 0
|
@PrimaryKey var id: Long = 0
|
||||||
var recipients: RealmList<Recipient> = RealmList()
|
var recipients: RealmList<Recipient> = RealmList()
|
||||||
var archived: Boolean = false
|
var archived: Boolean = false
|
||||||
|
var draft: String = ""
|
||||||
|
|
||||||
fun getTitle(): String {
|
fun getTitle(): String {
|
||||||
var title = ""
|
var title = ""
|
||||||
|
|||||||
@@ -139,6 +139,20 @@ class MessageRepository @Inject constructor(
|
|||||||
.observeOn(AndroidSchedulers.mainThread())
|
.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> {
|
fun getMessages(threadId: Long): RealmResults<Message> {
|
||||||
return Realm.getDefaultInstance()
|
return Realm.getDefaultInstance()
|
||||||
.where(Message::class.java)
|
.where(Message::class.java)
|
||||||
@@ -395,29 +409,23 @@ class MessageRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun markSent(id: Long): Flowable<*> {
|
fun markSent(id: Long) {
|
||||||
val realm = Realm.getDefaultInstance()
|
Realm.getDefaultInstance().use { realm ->
|
||||||
return realm.where(Message::class.java).equalTo("id", id)
|
realm.refresh()
|
||||||
.findAllAsync()
|
|
||||||
.asFlowable()
|
val message = realm.where(Message::class.java).equalTo("id", id).findFirst()
|
||||||
.filter { it.isLoaded }
|
message?.let {
|
||||||
.filter { it.isValid }
|
// Update the message in realm
|
||||||
.mapNotNull { it[0] }
|
realm.executeTransaction {
|
||||||
.take(1)
|
message.boxId = Sms.MESSAGE_TYPE_SENT
|
||||||
.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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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<*> {
|
fun markFailed(id: Long, resultCode: Int): Flowable<*> {
|
||||||
|
|||||||
@@ -20,15 +20,13 @@ package interactor
|
|||||||
|
|
||||||
import data.repository.MessageRepository
|
import data.repository.MessageRepository
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
|
||||||
import javax.inject.Inject
|
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> {
|
override fun buildObservable(params: Long): Flowable<Long> {
|
||||||
return Flowable.just(Unit)
|
return Flowable.just(params)
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.doOnNext { messageRepo.markSent(params) }
|
||||||
.flatMap { messageRepo.markSent(params) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -69,6 +69,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
@Inject lateinit var markRead: MarkRead
|
@Inject lateinit var markRead: MarkRead
|
||||||
@Inject lateinit var deleteMessage: DeleteMessage
|
@Inject lateinit var deleteMessage: DeleteMessage
|
||||||
|
|
||||||
|
private var sharedText: String = ""
|
||||||
private var draft: String = ""
|
private var draft: String = ""
|
||||||
private val attachments: Subject<List<Uri>> = BehaviorSubject.createDefault(ArrayList())
|
private val attachments: Subject<List<Uri>> = BehaviorSubject.createDefault(ArrayList())
|
||||||
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
|
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
|
||||||
@@ -80,7 +81,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
init {
|
init {
|
||||||
appComponent.inject(this)
|
appComponent.inject(this)
|
||||||
|
|
||||||
draft = intent.extras?.getString(Intent.EXTRA_TEXT) ?: ""
|
sharedText = intent.extras?.getString(Intent.EXTRA_TEXT) ?: ""
|
||||||
val threadId = intent.extras?.getLong("threadId") ?: 0L
|
val threadId = intent.extras?.getLong("threadId") ?: 0L
|
||||||
var address = ""
|
var address = ""
|
||||||
|
|
||||||
@@ -168,11 +169,6 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
override fun bindView(view: ComposeView) {
|
override fun bindView(view: ComposeView) {
|
||||||
super.bindView(view)
|
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
|
// 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
|
||||||
@@ -296,6 +292,23 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { attachments.onNext(it) }
|
.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
|
// 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
|
||||||
@@ -305,6 +318,12 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { canSend -> newState { it.copy(canSend = canSend) } }
|
.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
|
// 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 })
|
||||||
@@ -343,4 +362,14 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
.subscribe { message -> deleteMessage.execute(message.id) }
|
.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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user