From f9ad73ab11551e26bb486ea345bacfebd594bf12 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Mon, 5 Mar 2018 15:10:41 -0500 Subject: [PATCH] Add ability to toggle blocked state of conversation --- .../src/main/java/data/model/Conversation.kt | 1 + .../java/data/repository/MessageRepository.kt | 30 +++++++++++++++-- qksms/src/main/java/interactor/MarkBlocked.kt | 32 +++++++++++++++++++ .../src/main/java/interactor/MarkUnblocked.kt | 32 +++++++++++++++++++ .../ConversationInfoActivity.kt | 6 ++++ .../conversationinfo/ConversationInfoState.kt | 1 + .../conversationinfo/ConversationInfoView.kt | 1 + .../ConversationInfoViewModel.kt | 31 ++++++++++++++---- .../res/layout/conversation_info_activity.xml | 7 ++++ qksms/src/main/res/values/strings.xml | 3 +- 10 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 qksms/src/main/java/interactor/MarkBlocked.kt create mode 100644 qksms/src/main/java/interactor/MarkUnblocked.kt diff --git a/qksms/src/main/java/data/model/Conversation.kt b/qksms/src/main/java/data/model/Conversation.kt index d0746ad5..0510d8ee 100644 --- a/qksms/src/main/java/data/model/Conversation.kt +++ b/qksms/src/main/java/data/model/Conversation.kt @@ -27,6 +27,7 @@ open class Conversation : RealmObject() { @PrimaryKey var id: Long = 0 var recipients: RealmList = RealmList() var archived: Boolean = false + var blocked: Boolean = false var draft: String = "" fun getTitle(): String { diff --git a/qksms/src/main/java/data/repository/MessageRepository.kt b/qksms/src/main/java/data/repository/MessageRepository.kt index 44c16bde..67f37ec3 100644 --- a/qksms/src/main/java/data/repository/MessageRepository.kt +++ b/qksms/src/main/java/data/repository/MessageRepository.kt @@ -64,6 +64,7 @@ class MessageRepository @Inject constructor( val conversationFlowable = realm.where(Conversation::class.java) .equalTo("archived", archived) + .equalTo("blocked", false) .findAllAsync() .asFlowable() .filter { it.isLoaded } @@ -87,6 +88,13 @@ class MessageRepository @Inject constructor( }) } + fun getBlockedConversations(): RealmResults { + return Realm.getDefaultInstance() + .where(Conversation::class.java) + .equalTo("blocked", true) + .findAll() + } + fun getConversationAsync(threadId: Long): Conversation { return Realm.getDefaultInstance() .where(Conversation::class.java) @@ -212,7 +220,6 @@ class MessageRepository @Inject constructor( val realm = Realm.getDefaultInstance() val conversation = realm.where(Conversation::class.java) .equalTo("id", threadId) - .equalTo("archived", false) .findFirst() realm.executeTransaction { conversation?.archived = true } @@ -223,13 +230,32 @@ class MessageRepository @Inject constructor( val realm = Realm.getDefaultInstance() val conversation = realm.where(Conversation::class.java) .equalTo("id", threadId) - .equalTo("archived", true) .findFirst() realm.executeTransaction { conversation?.archived = false } realm.close() } + fun markBlocked(threadId: Long) { + val realm = Realm.getDefaultInstance() + val conversation = realm.where(Conversation::class.java) + .equalTo("id", threadId) + .findFirst() + + realm.executeTransaction { conversation?.blocked = true } + realm.close() + } + + fun markUnblocked(threadId: Long) { + val realm = Realm.getDefaultInstance() + val conversation = realm.where(Conversation::class.java) + .equalTo("id", threadId) + .findFirst() + + realm.executeTransaction { conversation?.blocked = false } + realm.close() + } + fun markAllSeen() { val realm = Realm.getDefaultInstance() val messages = realm.where(Message::class.java).equalTo("seen", false).findAll() diff --git a/qksms/src/main/java/interactor/MarkBlocked.kt b/qksms/src/main/java/interactor/MarkBlocked.kt new file mode 100644 index 00000000..3d731322 --- /dev/null +++ b/qksms/src/main/java/interactor/MarkBlocked.kt @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * This file is part of QKSMS. + * + * QKSMS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QKSMS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QKSMS. If not, see . + */ +package interactor + +import data.repository.MessageRepository +import io.reactivex.Flowable +import javax.inject.Inject + +class MarkBlocked @Inject constructor(private val messageRepo: MessageRepository) : Interactor() { + + override fun buildObservable(params: Long): Flowable { + return Flowable.just(Unit) + .doOnNext { messageRepo.markBlocked(params) } + } + +} \ No newline at end of file diff --git a/qksms/src/main/java/interactor/MarkUnblocked.kt b/qksms/src/main/java/interactor/MarkUnblocked.kt new file mode 100644 index 00000000..02f0ce11 --- /dev/null +++ b/qksms/src/main/java/interactor/MarkUnblocked.kt @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * This file is part of QKSMS. + * + * QKSMS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QKSMS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QKSMS. If not, see . + */ +package interactor + +import data.repository.MessageRepository +import io.reactivex.Flowable +import javax.inject.Inject + +class MarkUnblocked @Inject constructor(private val messageRepo: MessageRepository) : Interactor() { + + override fun buildObservable(params: Long): Flowable { + return Flowable.just(Unit) + .doOnNext { messageRepo.markUnblocked(params) } + } + +} \ No newline at end of file diff --git a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoActivity.kt b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoActivity.kt index 2b66b4d9..3fb991b8 100644 --- a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoActivity.kt +++ b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoActivity.kt @@ -35,6 +35,7 @@ class ConversationInfoActivity : QkThemedActivity(), override val viewModelClass = ConversationInfoViewModel::class override val notificationsIntent by lazy { notifications.clicks() } override val archiveIntent by lazy { archive.clicks() } + override val blockIntent by lazy { block.clicks() } override val deleteIntent by lazy { delete.clicks() } override val confirmDeleteIntent: PublishSubject = PublishSubject.create() @@ -76,6 +77,11 @@ class ConversationInfoActivity : QkThemedActivity(), false -> R.string.info_archive }) + block.title = getString(when (state.blocked) { + true -> R.string.info_unblock + false -> R.string.info_block + }) + mediaAdapter.data = state.media } diff --git a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoState.kt b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoState.kt index 4f6f4739..6e7ca858 100644 --- a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoState.kt +++ b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoState.kt @@ -24,6 +24,7 @@ import data.model.Recipient data class ConversationInfoState( val recipients: List = listOf(), val archived: Boolean = false, + val blocked: Boolean = false, val media: List = ArrayList(), val hasError: Boolean = false ) \ No newline at end of file diff --git a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoView.kt b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoView.kt index d45a3e35..55d79d9d 100644 --- a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoView.kt +++ b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoView.kt @@ -25,6 +25,7 @@ interface ConversationInfoView : QkView { val notificationsIntent: Observable val archiveIntent: Observable + val blockIntent: Observable val deleteIntent: Observable val confirmDeleteIntent: Observable diff --git a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoViewModel.kt b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoViewModel.kt index d71f908c..57b1a277 100644 --- a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoViewModel.kt +++ b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoViewModel.kt @@ -20,17 +20,13 @@ package presentation.feature.conversationinfo import android.content.Context import android.content.Intent -import com.moez.QKSMS.R import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.kotlin.autoDisposable import common.di.appComponent import common.util.extensions.asObservable -import common.util.extensions.makeToast import data.model.Conversation import data.repository.MessageRepository -import interactor.DeleteConversation -import interactor.MarkArchived -import interactor.MarkUnarchived +import interactor.* import io.reactivex.Observable import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.withLatestFrom @@ -45,6 +41,8 @@ class ConversationInfoViewModel(intent: Intent) : QkViewModel newState { it.copy(archived = archived) } } + // Update the view's blocked state whenever it changes + disposables += conversation + .map { conversation -> conversation.blocked } + .distinctUntilChanged() + .subscribe { blocked -> newState { it.copy(blocked = blocked) } } + // Load the attachments from the conversation disposables += messageRepo.getPartsForConversation(threadId) .map { parts -> parts.filter { part -> part.isImage() } } @@ -103,8 +109,19 @@ class ConversationInfoViewModel(intent: Intent) : QkViewModel when (conversation.archived) { - true -> markUnarchived.execute(conversation.id, { context.makeToast(R.string.toast_unarchived) }) - false -> markArchived.execute(conversation.id, { context.makeToast(R.string.toast_archived) }) + true -> markUnarchived.execute(conversation.id) + false -> markArchived.execute(conversation.id) + } + } + + // Toggle the blocked state of the conversation + view.blockIntent + .withLatestFrom(conversation, { _, conversation -> conversation }) + .autoDisposable(view.scope()) + .subscribe { conversation -> + when (conversation.blocked) { + true -> markUnblocked.execute(conversation.id) + false -> markBlocked.execute(conversation.id) } } diff --git a/qksms/src/main/res/layout/conversation_info_activity.xml b/qksms/src/main/res/layout/conversation_info_activity.xml index 1fa72b32..b28c692b 100644 --- a/qksms/src/main/res/layout/conversation_info_activity.xml +++ b/qksms/src/main/res/layout/conversation_info_activity.xml @@ -69,6 +69,13 @@ app:icon="@drawable/ic_folder_black_24dp" app:title="@string/info_archive" /> + + Notifications Archive Unarchive + Block + Unblock Delete conversation Default SMS app @@ -138,7 +140,6 @@ Copied Archived conversation - Moved to inbox You must unlock QKSMS+ to use this Message not sent