mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
Allow deleting conversation from menu
This commit is contained in:
@@ -4,6 +4,7 @@ import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.provider.BaseColumns
|
||||
import android.provider.Telephony
|
||||
import android.provider.Telephony.Sms
|
||||
import android.provider.Telephony.TextBasedSmsColumns
|
||||
import com.moez.QKSMS.common.util.MessageUtils
|
||||
@@ -239,6 +240,23 @@ class MessageRepository @Inject constructor(
|
||||
realm.close()
|
||||
}
|
||||
|
||||
fun deleteConversation(threadId: Long) {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
|
||||
val conversation = realm.where(Conversation::class.java).equalTo("id", threadId).findAll()
|
||||
val messages = realm.where(Message::class.java).equalTo("threadId", threadId).findAll()
|
||||
|
||||
realm.executeTransaction {
|
||||
conversation.deleteAllFromRealm()
|
||||
messages.deleteAllFromRealm()
|
||||
}
|
||||
|
||||
realm.close()
|
||||
|
||||
val uri = Uri.withAppendedPath(Telephony.Threads.CONTENT_URI, threadId.toString())
|
||||
context.contentResolver.delete(uri, null, null)
|
||||
}
|
||||
|
||||
fun getConversationFromCp(threadId: Long): Conversation? {
|
||||
var conversation: Conversation? = null
|
||||
|
||||
|
||||
@@ -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 DeleteConversation @Inject constructor(private val messageRepo: MessageRepository): Interactor<Unit, Long>() {
|
||||
|
||||
override fun buildObservable(params: Long): Flowable<Unit> {
|
||||
return Flowable.just(Unit)
|
||||
.doOnNext { messageRepo.deleteConversation(params) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,8 +33,10 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
override val queryChangedIntent: Observable<CharSequence> by lazy { chipsAdapter.textChanges }
|
||||
override val chipSelectedIntent: Subject<Contact> by lazy { contactsAdapter.contactSelected }
|
||||
override val chipDeletedIntent: Subject<Contact> by lazy { chipsAdapter.chipDeleted }
|
||||
override val menuReadyIntent: Subject<Unit> = PublishSubject.create()
|
||||
override val callIntent: Subject<Unit> = PublishSubject.create()
|
||||
override val archiveIntent: Subject<Unit> = PublishSubject.create()
|
||||
override val deleteIntent: 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,10 +48,6 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
private val contactsAdapter by lazy { ContactAdapter(this) }
|
||||
private val messageAdapter by lazy { MessagesAdapter() }
|
||||
|
||||
private var pendingCallVisibility = false
|
||||
private var pendingArchiveVisibility = false
|
||||
private var pendingArchiveTitle = ""
|
||||
|
||||
init {
|
||||
appComponent.inject(this)
|
||||
}
|
||||
@@ -87,26 +85,27 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
}
|
||||
|
||||
override fun render(state: ComposeState) {
|
||||
if (state.hasError) {
|
||||
finish()
|
||||
return
|
||||
}
|
||||
|
||||
toolbarTitle.setVisible(!state.editingMode)
|
||||
chips.setVisible(state.editingMode)
|
||||
contacts.setVisible(state.contactsVisible)
|
||||
composeBar.setVisible(!state.contactsVisible)
|
||||
|
||||
menu?.findItem(R.id.call).let { menuItem ->
|
||||
if (menuItem == null) {
|
||||
pendingCallVisibility = !state.editingMode
|
||||
} else {
|
||||
menuItem.isVisible = !state.editingMode
|
||||
}
|
||||
menu?.findItem(R.id.call)?.run {
|
||||
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)
|
||||
}
|
||||
menu?.findItem(R.id.archive)?.run {
|
||||
isVisible = !state.editingMode
|
||||
setTitle(if (state.archived) R.string.menu_unarchive else R.string.menu_archive)
|
||||
}
|
||||
|
||||
menu?.findItem(R.id.delete)?.run {
|
||||
isVisible = !state.editingMode
|
||||
}
|
||||
|
||||
if (chipsAdapter.data.isEmpty() && state.selectedContacts.isNotEmpty()) {
|
||||
@@ -163,28 +162,20 @@ 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)
|
||||
val result = super.onCreateOptionsMenu(menu)
|
||||
menuReadyIntent.onNext(Unit)
|
||||
return result
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
return when (item.itemId) {
|
||||
R.id.call -> {
|
||||
callIntent.onNext(Unit)
|
||||
true
|
||||
}
|
||||
|
||||
R.id.archive -> {
|
||||
archiveIntent.onNext(Unit)
|
||||
true
|
||||
}
|
||||
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
when (item.itemId) {
|
||||
R.id.call -> callIntent.onNext(Unit)
|
||||
R.id.archive -> archiveIntent.onNext(Unit)
|
||||
R.id.delete -> deleteIntent.onNext(Unit)
|
||||
else -> return super.onOptionsItemSelected(item)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import com.moez.QKSMS.data.model.Message
|
||||
import io.realm.RealmResults
|
||||
|
||||
data class ComposeState(
|
||||
val hasError: Boolean = false,
|
||||
val editingMode: Boolean = false,
|
||||
val contacts: List<Contact> = ArrayList(),
|
||||
val contactsVisible: Boolean = false,
|
||||
|
||||
@@ -11,8 +11,10 @@ interface ComposeView : QkView<ComposeState> {
|
||||
val queryChangedIntent: Observable<CharSequence>
|
||||
val chipSelectedIntent: Subject<Contact>
|
||||
val chipDeletedIntent: Subject<Contact>
|
||||
val menuReadyIntent: Subject<Unit>
|
||||
val callIntent: Subject<Unit>
|
||||
val archiveIntent: Subject<Unit>
|
||||
val deleteIntent: Subject<Unit>
|
||||
val copyTextIntent: Subject<Message>
|
||||
val forwardMessageIntent: Subject<Message>
|
||||
val deleteMessageIntent: Subject<Message>
|
||||
|
||||
@@ -32,6 +32,7 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
||||
@Inject lateinit var navigator: Navigator
|
||||
@Inject lateinit var markArchived: MarkArchived
|
||||
@Inject lateinit var markUnarchived: MarkUnarchived
|
||||
@Inject lateinit var deleteConversation: DeleteConversation
|
||||
@Inject lateinit var sendMessage: SendMessage
|
||||
@Inject lateinit var markRead: MarkRead
|
||||
@Inject lateinit var deleteMessage: DeleteMessage
|
||||
@@ -54,11 +55,17 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
||||
.flatMapMaybe { addresses -> messageRepo.getOrCreateConversation(addresses) }
|
||||
|
||||
// Merges two potential conversation sources (threadId from constructor and contact selection) into a single
|
||||
// stream of conversations
|
||||
// stream of conversations. If the conversation was deleted, notify the activity to shut down
|
||||
conversation = messageRepo.getConversationAsync(threadId)
|
||||
.asObservable<Conversation>()
|
||||
.filter { conversation -> conversation.isLoaded }
|
||||
.filter { conversation -> conversation.isValid }
|
||||
.filter { conversation ->
|
||||
if (!conversation.isValid) {
|
||||
newState { it.copy(hasError = true) }
|
||||
return@filter false
|
||||
}
|
||||
return@filter true
|
||||
}
|
||||
.mergeWith(selectedConversation)
|
||||
.distinctUntilChanged()
|
||||
.doOnNext { conversation ->
|
||||
@@ -69,6 +76,7 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
||||
disposables += markRead
|
||||
disposables += markArchived
|
||||
disposables += markUnarchived
|
||||
disposables += deleteConversation
|
||||
disposables += conversation.subscribe()
|
||||
|
||||
// When the conversation changes, update the messages for the adapter
|
||||
@@ -121,6 +129,11 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
||||
})
|
||||
.subscribe()
|
||||
|
||||
// When the menu is loaded, trigger a new state so that the menu options can be rendered correctly
|
||||
intents += view.menuReadyIntent.subscribe {
|
||||
newState { it.copy() }
|
||||
}
|
||||
|
||||
// Open the phone dialer if the call button is clicked
|
||||
intents += view.callIntent
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
@@ -138,6 +151,11 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the conversation
|
||||
intents += view.deleteIntent
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
.subscribe { conversation -> deleteConversation.execute(conversation.id) }
|
||||
|
||||
// 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()) }
|
||||
|
||||
@@ -7,8 +7,8 @@ import android.view.ViewGroup
|
||||
import com.jakewharton.rxbinding2.view.RxView
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.di.appComponent
|
||||
import com.moez.QKSMS.common.util.DateFormatter
|
||||
import com.moez.QKSMS.common.util.Colors
|
||||
import com.moez.QKSMS.common.util.DateFormatter
|
||||
import com.moez.QKSMS.data.model.ConversationMessagePair
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.presentation.Navigator
|
||||
|
||||
Reference in New Issue
Block a user