Add ability to mark read, and expand/collapse qk reply

This commit is contained in:
Moez Bhatti
2018-03-28 04:15:45 -04:00
parent c136920c68
commit fb97ede7aa
8 changed files with 174 additions and 10 deletions
@@ -21,6 +21,8 @@ package feature.qkreply
import android.content.res.ColorStateList
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
@@ -33,12 +35,15 @@ import common.util.extensions.setBackgroundTint
import feature.compose.MessagesAdapter
import injection.appComponent
import io.reactivex.rxkotlin.Observables
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.qkreply_activity.*
import javax.inject.Inject
class QkReplyActivity : QkThemedActivity<QkReplyViewModel>(), QkReplyView {
override val viewModelClass = QkReplyViewModel::class
override val menuItemIntent: Subject<Int> = PublishSubject.create()
@Inject lateinit var adapter: MessagesAdapter
@@ -95,12 +100,26 @@ class QkReplyActivity : QkThemedActivity<QkReplyViewModel>(), QkReplyView {
}
override fun render(state: QkReplyState) {
title = state.title
threadId.onNext(state.data?.first?.id ?: 0)
title = state.title
toolbar.menu.findItem(R.id.expand)?.isVisible = !state.expanded
toolbar.menu.findItem(R.id.collapse)?.isVisible = state.expanded
adapter.data = state.data
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.qkreply, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
menuItemIntent.onNext(item.itemId)
return true
}
override fun getAppThemeResourcesObservable() = colors.appDialogThemeResources
}
@@ -24,5 +24,6 @@ import model.Message
data class QkReplyState(
val title: String = "",
val expanded: Boolean = false,
val data: Pair<Conversation, RealmResults<Message>>? = null
)
@@ -18,6 +18,12 @@
*/
package feature.qkreply
import android.view.MenuItem
import common.base.QkView
import io.reactivex.Observable
interface QkReplyView : QkView<QkReplyState>
interface QkReplyView : QkView<QkReplyState> {
val menuItemIntent: Observable<Int>
}
@@ -19,9 +19,14 @@
package feature.qkreply
import android.content.Intent
import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.base.QkViewModel
import injection.appComponent
import interactor.MarkRead
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
import model.Conversation
import repository.MessageRepository
import util.extensions.asObservable
@@ -29,22 +34,55 @@ import javax.inject.Inject
class QkReplyViewModel(intent: Intent) : QkViewModel<QkReplyView, QkReplyState>(QkReplyState()) {
@Inject lateinit var markRead: MarkRead
@Inject lateinit var messageRepo: MessageRepository
init {
appComponent.inject(this)
val threadId = intent.getLongExtra("threadId", -1)
disposables += messageRepo.getConversationAsync(threadId)
private val conversation by lazy {
messageRepo.getConversationAsync(intent.getLongExtra("threadId", -1))
.asObservable<Conversation>()
.filter { it.isLoaded }
.filter { it.isValid }
.distinctUntilChanged()
}
init {
appComponent.inject(this)
disposables += markRead
disposables += conversation
.doOnNext { conversation -> newState { it.copy(title = conversation.getTitle()) } }
.take(1) // We only need to set the messages once
.map { conversation -> Pair(conversation, messageRepo.getUnreadMessages(conversation.id)) }
.doOnNext { data -> newState { it.copy(data = data) } }
.subscribe()
.subscribe { data -> newState { it.copy(data = data) } }
}
override fun bindView(view: QkReplyView) {
super.bindView(view)
// Mark read
view.menuItemIntent
.filter { id -> id == R.id.read }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> conversation.id }
.autoDisposable(view.scope())
.subscribe { threadId -> markRead.execute(threadId) }
// Show all messages
view.menuItemIntent
.filter { id -> id == R.id.expand }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> Pair(conversation, messageRepo.getMessages(conversation.id)) }
.autoDisposable(view.scope())
.subscribe { data -> newState { it.copy(expanded = true, data = data) } }
// Show unread messages only
view.menuItemIntent
.filter { id -> id == R.id.collapse }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> Pair(conversation, messageRepo.getUnreadMessages(conversation.id)) }
.autoDisposable(view.scope())
.subscribe { data -> newState { it.copy(expanded = false, data = data) } }
}
}
@@ -0,0 +1,27 @@
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ 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 <http://www.gnu.org/licenses/>.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M7.41,18.59L8.83,20 12,16.83 15.17,20l1.41,-1.41L12,14l-4.59,4.59zM16.59,5.41L15.17,4 12,7.17 8.83,4 7.41,5.41 12,10l4.59,-4.59z"/>
</vector>
@@ -0,0 +1,27 @@
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ 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 <http://www.gnu.org/licenses/>.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,5.83L15.17,9l1.41,-1.41L12,3 7.41,7.59 8.83,9 12,5.83zM12,18.17L8.83,15l-1.41,1.41L12,21l4.59,-4.59L15.17,15 12,18.17z"/>
</vector>
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ 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 <http://www.gnu.org/licenses/>.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/read"
android:icon="@drawable/ic_check_black_24dp"
android:title="@string/qkreply_menu_read"
app:showAsAction="ifRoom" />
<item
android:id="@+id/expand"
android:icon="@drawable/ic_unfold_more_black_24dp"
android:title="@string/qkreply_menu_expand"
app:showAsAction="ifRoom" />
<item
android:id="@+id/collapse"
android:icon="@drawable/ic_unfold_less_black_24dp"
android:title="@string/qkreply_menu_collapse"
android:visible="false"
app:showAsAction="always" />
</menu>
@@ -140,6 +140,10 @@
<string name="widget_loading">Loading…</string>
<string name="widget_more">View more conversations</string>
<string name="qkreply_menu_read">Mark read</string>
<string name="qkreply_menu_expand">Expand</string>
<string name="qkreply_menu_collapse">Collapse</string>
<string name="theme_material">Material</string>
<string name="theme_plus">QKSMS+</string>
<string name="theme_apply">Apply</string>