From c5913000bb25a14ed98568bc2db79106f914e1f1 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Sun, 1 Jul 2018 02:23:45 -0400 Subject: [PATCH] Fix crash when saving draft in QkReply --- .../com/moez/QKSMS/feature/qkreply/QkReplyViewModel.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/qkreply/QkReplyViewModel.kt b/presentation/src/main/java/com/moez/QKSMS/feature/qkreply/QkReplyViewModel.kt index 6d6648d0..679f2340 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/qkreply/QkReplyViewModel.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/qkreply/QkReplyViewModel.kt @@ -179,16 +179,18 @@ class QkReplyViewModel @Inject constructor( .autoDisposable(view.scope()) .subscribe { remaining -> newState { copy(remaining = remaining) } } + // This allows the conversation to be mapped to a threadId in the correct thread + val safeThreadId = conversation.map { it.takeIf { it.isValid }?.id ?: -1 } + // Update the draft whenever the text is changed view.textChangedIntent .debounce(100, TimeUnit.MILLISECONDS) .map { draft -> draft.toString() } .observeOn(Schedulers.io()) - .withLatestFrom(conversation.map { it.id }) { draft, threadId -> - conversationRepo.saveDraft(threadId, draft) - } + .withLatestFrom(safeThreadId) { draft, threadId -> Pair(draft, threadId) } + .filter { (_, threadId) -> threadId != -1L } .autoDisposable(view.scope()) - .subscribe() + .subscribe { (draft, threadId) -> conversationRepo.saveDraft(threadId, draft) } // Toggle to the next sim slot view.changeSimIntent