forked from upstream-mirrors/quik
Allow deleting conversation from menu
This commit is contained in:
@@ -4,6 +4,7 @@ import android.content.ContentValues
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.BaseColumns
|
import android.provider.BaseColumns
|
||||||
|
import android.provider.Telephony
|
||||||
import android.provider.Telephony.Sms
|
import android.provider.Telephony.Sms
|
||||||
import android.provider.Telephony.TextBasedSmsColumns
|
import android.provider.Telephony.TextBasedSmsColumns
|
||||||
import com.moez.QKSMS.common.util.MessageUtils
|
import com.moez.QKSMS.common.util.MessageUtils
|
||||||
@@ -239,6 +240,23 @@ class MessageRepository @Inject constructor(
|
|||||||
realm.close()
|
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? {
|
fun getConversationFromCp(threadId: Long): Conversation? {
|
||||||
var conversation: Conversation? = null
|
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 queryChangedIntent: Observable<CharSequence> by lazy { chipsAdapter.textChanges }
|
||||||
override val chipSelectedIntent: Subject<Contact> by lazy { contactsAdapter.contactSelected }
|
override val chipSelectedIntent: Subject<Contact> by lazy { contactsAdapter.contactSelected }
|
||||||
override val chipDeletedIntent: Subject<Contact> by lazy { chipsAdapter.chipDeleted }
|
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 callIntent: Subject<Unit> = PublishSubject.create()
|
||||||
override val archiveIntent: 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 copyTextIntent: Subject<Message> = PublishSubject.create()
|
||||||
override val forwardMessageIntent: Subject<Message> = PublishSubject.create()
|
override val forwardMessageIntent: Subject<Message> = PublishSubject.create()
|
||||||
override val deleteMessageIntent: 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 contactsAdapter by lazy { ContactAdapter(this) }
|
||||||
private val messageAdapter by lazy { MessagesAdapter() }
|
private val messageAdapter by lazy { MessagesAdapter() }
|
||||||
|
|
||||||
private var pendingCallVisibility = false
|
|
||||||
private var pendingArchiveVisibility = false
|
|
||||||
private var pendingArchiveTitle = ""
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
appComponent.inject(this)
|
appComponent.inject(this)
|
||||||
}
|
}
|
||||||
@@ -87,26 +85,27 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun render(state: ComposeState) {
|
override fun render(state: ComposeState) {
|
||||||
|
if (state.hasError) {
|
||||||
|
finish()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
toolbarTitle.setVisible(!state.editingMode)
|
toolbarTitle.setVisible(!state.editingMode)
|
||||||
chips.setVisible(state.editingMode)
|
chips.setVisible(state.editingMode)
|
||||||
contacts.setVisible(state.contactsVisible)
|
contacts.setVisible(state.contactsVisible)
|
||||||
composeBar.setVisible(!state.contactsVisible)
|
composeBar.setVisible(!state.contactsVisible)
|
||||||
|
|
||||||
menu?.findItem(R.id.call).let { menuItem ->
|
menu?.findItem(R.id.call)?.run {
|
||||||
if (menuItem == null) {
|
isVisible = !state.editingMode
|
||||||
pendingCallVisibility = !state.editingMode
|
|
||||||
} else {
|
|
||||||
menuItem.isVisible = !state.editingMode
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
menu?.findItem(R.id.archive).let { menuItem ->
|
menu?.findItem(R.id.archive)?.run {
|
||||||
if (menuItem == null) {
|
isVisible = !state.editingMode
|
||||||
pendingArchiveVisibility = !state.editingMode
|
setTitle(if (state.archived) R.string.menu_unarchive else R.string.menu_archive)
|
||||||
} else {
|
}
|
||||||
menuItem.isVisible = !state.editingMode
|
|
||||||
menuItem.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()) {
|
if (chipsAdapter.data.isEmpty() && state.selectedContacts.isNotEmpty()) {
|
||||||
@@ -163,28 +162,20 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
|||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||||
menuInflater.inflate(R.menu.compose, menu)
|
menuInflater.inflate(R.menu.compose, menu)
|
||||||
menu?.findItem(R.id.call)?.isVisible = pendingCallVisibility
|
val result = super.onCreateOptionsMenu(menu)
|
||||||
menu?.findItem(R.id.archive)?.run {
|
menuReadyIntent.onNext(Unit)
|
||||||
isVisible = pendingArchiveVisibility
|
return result
|
||||||
title = pendingArchiveTitle
|
|
||||||
}
|
|
||||||
return super.onCreateOptionsMenu(menu)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
return when (item.itemId) {
|
when (item.itemId) {
|
||||||
R.id.call -> {
|
R.id.call -> callIntent.onNext(Unit)
|
||||||
callIntent.onNext(Unit)
|
R.id.archive -> archiveIntent.onNext(Unit)
|
||||||
true
|
R.id.delete -> deleteIntent.onNext(Unit)
|
||||||
}
|
else -> return super.onOptionsItemSelected(item)
|
||||||
|
|
||||||
R.id.archive -> {
|
|
||||||
archiveIntent.onNext(Unit)
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> super.onOptionsItemSelected(item)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ import com.moez.QKSMS.data.model.Message
|
|||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
|
|
||||||
data class ComposeState(
|
data class ComposeState(
|
||||||
|
val hasError: Boolean = false,
|
||||||
val editingMode: Boolean = false,
|
val editingMode: Boolean = false,
|
||||||
val contacts: List<Contact> = ArrayList(),
|
val contacts: List<Contact> = ArrayList(),
|
||||||
val contactsVisible: Boolean = false,
|
val contactsVisible: Boolean = false,
|
||||||
|
|||||||
@@ -11,8 +11,10 @@ interface ComposeView : QkView<ComposeState> {
|
|||||||
val queryChangedIntent: Observable<CharSequence>
|
val queryChangedIntent: Observable<CharSequence>
|
||||||
val chipSelectedIntent: Subject<Contact>
|
val chipSelectedIntent: Subject<Contact>
|
||||||
val chipDeletedIntent: Subject<Contact>
|
val chipDeletedIntent: Subject<Contact>
|
||||||
|
val menuReadyIntent: Subject<Unit>
|
||||||
val callIntent: Subject<Unit>
|
val callIntent: Subject<Unit>
|
||||||
val archiveIntent: Subject<Unit>
|
val archiveIntent: Subject<Unit>
|
||||||
|
val deleteIntent: Subject<Unit>
|
||||||
val copyTextIntent: Subject<Message>
|
val copyTextIntent: Subject<Message>
|
||||||
val forwardMessageIntent: Subject<Message>
|
val forwardMessageIntent: Subject<Message>
|
||||||
val deleteMessageIntent: 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 navigator: Navigator
|
||||||
@Inject lateinit var markArchived: MarkArchived
|
@Inject lateinit var markArchived: MarkArchived
|
||||||
@Inject lateinit var markUnarchived: MarkUnarchived
|
@Inject lateinit var markUnarchived: MarkUnarchived
|
||||||
|
@Inject lateinit var deleteConversation: DeleteConversation
|
||||||
@Inject lateinit var sendMessage: SendMessage
|
@Inject lateinit var sendMessage: SendMessage
|
||||||
@Inject lateinit var markRead: MarkRead
|
@Inject lateinit var markRead: MarkRead
|
||||||
@Inject lateinit var deleteMessage: DeleteMessage
|
@Inject lateinit var deleteMessage: DeleteMessage
|
||||||
@@ -54,11 +55,17 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
.flatMapMaybe { addresses -> messageRepo.getOrCreateConversation(addresses) }
|
.flatMapMaybe { addresses -> messageRepo.getOrCreateConversation(addresses) }
|
||||||
|
|
||||||
// Merges two potential conversation sources (threadId from constructor and contact selection) into a single
|
// 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)
|
conversation = messageRepo.getConversationAsync(threadId)
|
||||||
.asObservable<Conversation>()
|
.asObservable<Conversation>()
|
||||||
.filter { conversation -> conversation.isLoaded }
|
.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)
|
.mergeWith(selectedConversation)
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
.doOnNext { conversation ->
|
.doOnNext { conversation ->
|
||||||
@@ -69,6 +76,7 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
disposables += markRead
|
disposables += markRead
|
||||||
disposables += markArchived
|
disposables += markArchived
|
||||||
disposables += markUnarchived
|
disposables += markUnarchived
|
||||||
|
disposables += deleteConversation
|
||||||
disposables += conversation.subscribe()
|
disposables += conversation.subscribe()
|
||||||
|
|
||||||
// When the conversation changes, update the messages for the adapter
|
// When the conversation changes, update the messages for the adapter
|
||||||
@@ -121,6 +129,11 @@ class ComposeViewModel(threadId: Long) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
})
|
})
|
||||||
.subscribe()
|
.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
|
// Open the phone dialer if the call button is clicked
|
||||||
intents += view.callIntent
|
intents += view.callIntent
|
||||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
.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
|
// Enable the send button when there is text input into the new message body, disable otherwise
|
||||||
intents += view.textChangedIntent.subscribe { text ->
|
intents += view.textChangedIntent.subscribe { text ->
|
||||||
newState { it.copy(draft = text.toString(), canSend = text.isNotEmpty()) }
|
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.jakewharton.rxbinding2.view.RxView
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.moez.QKSMS.common.di.appComponent
|
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.Colors
|
||||||
|
import com.moez.QKSMS.common.util.DateFormatter
|
||||||
import com.moez.QKSMS.data.model.ConversationMessagePair
|
import com.moez.QKSMS.data.model.ConversationMessagePair
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
import com.moez.QKSMS.presentation.Navigator
|
import com.moez.QKSMS.presentation.Navigator
|
||||||
|
|||||||
@@ -15,4 +15,10 @@
|
|||||||
android:visible="false"
|
android:visible="false"
|
||||||
app:showAsAction="never" />
|
app:showAsAction="never" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/delete"
|
||||||
|
android:title="@string/menu_delete"
|
||||||
|
android:visible="false"
|
||||||
|
app:showAsAction="never" />
|
||||||
|
|
||||||
</menu>
|
</menu>
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
<string name="menu_call">Call</string>
|
<string name="menu_call">Call</string>
|
||||||
<string name="menu_archive">Archive</string>
|
<string name="menu_archive">Archive</string>
|
||||||
<string name="menu_unarchive">Unarchive</string>
|
<string name="menu_unarchive">Unarchive</string>
|
||||||
|
<string name="menu_delete">Delete</string>
|
||||||
|
|
||||||
<string name="compose_hint">Write a message</string>
|
<string name="compose_hint">Write a message</string>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user