forked from upstream-mirrors/quik
Fix hang when leaving compose screen, save draft in background
This commit is contained in:
@@ -54,6 +54,7 @@ import io.realm.RealmList
|
|||||||
import presentation.common.Navigator
|
import presentation.common.Navigator
|
||||||
import presentation.common.base.QkViewModel
|
import presentation.common.base.QkViewModel
|
||||||
import java.net.URLDecoder
|
import java.net.URLDecoder
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(ComposeState()) {
|
class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(ComposeState()) {
|
||||||
@@ -70,7 +71,6 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
@Inject lateinit var deleteMessage: DeleteMessage
|
@Inject lateinit var deleteMessage: DeleteMessage
|
||||||
|
|
||||||
private var sharedText: String = ""
|
private var sharedText: 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() }
|
||||||
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
|
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
|
||||||
@@ -148,13 +148,10 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
.filter { conversation -> conversation.isValid }
|
.filter { conversation -> conversation.isValid }
|
||||||
.filter { conversation -> conversation.id != 0L }
|
.filter { conversation -> conversation.id != 0L }
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
.doOnNext { conversation ->
|
|
||||||
newState { it.copy(title = conversation.getTitle(), archived = conversation.archived) }
|
|
||||||
}
|
|
||||||
|
|
||||||
// When the conversation changes, update the messages for the adapter
|
// When the conversation changes, update the messages for the adapter
|
||||||
messages = conversation
|
messages = conversation
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged { conversation -> conversation.id }
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.map { conversation ->
|
.map { conversation ->
|
||||||
val messages = messageRepo.getMessages(conversation.id)
|
val messages = messageRepo.getMessages(conversation.id)
|
||||||
@@ -167,7 +164,23 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
disposables += markRead
|
disposables += markRead
|
||||||
disposables += conversation.subscribe()
|
disposables += conversation.subscribe()
|
||||||
disposables += messages.subscribe()
|
disposables += messages.subscribe()
|
||||||
disposables += attachments.subscribe { attachments -> newState { it.copy(attachments = attachments) } }
|
|
||||||
|
disposables += conversation
|
||||||
|
.distinctUntilChanged { conversation -> conversation.getTitle() }
|
||||||
|
.subscribe { conversation ->
|
||||||
|
newState { it.copy(title = conversation.getTitle()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
disposables += conversation
|
||||||
|
.distinctUntilChanged { conversation -> conversation.archived }
|
||||||
|
.subscribe { conversation ->
|
||||||
|
newState { it.copy(archived = conversation.archived) }
|
||||||
|
}
|
||||||
|
|
||||||
|
disposables += attachments
|
||||||
|
.subscribe { attachments ->
|
||||||
|
newState { it.copy(attachments = attachments) }
|
||||||
|
}
|
||||||
|
|
||||||
if (threadId == 0L) {
|
if (threadId == 0L) {
|
||||||
syncContacts.execute(Unit)
|
syncContacts.execute(Unit)
|
||||||
@@ -328,9 +341,14 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
|
|
||||||
// Update the draft whenever the text is changed
|
// Update the draft whenever the text is changed
|
||||||
view.textChangedIntent
|
view.textChangedIntent
|
||||||
|
.debounce(100, TimeUnit.MILLISECONDS)
|
||||||
.map { draft -> draft.toString() }
|
.map { draft -> draft.toString() }
|
||||||
|
.observeOn(Schedulers.io())
|
||||||
|
.withLatestFrom(conversation.map { it.id }, { draft, threadId ->
|
||||||
|
messageRepo.saveDraft(threadId, draft)
|
||||||
|
})
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { draft -> this.draft = draft }
|
.subscribe()
|
||||||
|
|
||||||
// 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
|
||||||
@@ -370,14 +388,4 @@ 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