Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

[NEW] Added unread rooms badge besides the back button #1936

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,16 @@ class ChatRoomPresenter @Inject constructor(
}
}

fun updateUnreadRoomsBadge() {
Timber.w("reached updateUnreadRoomsBadge")
view.updateUnreadRoomsBadge(dbManager.totalUnreadRooms)
}

suspend fun getRoomAlert(roomId: String): Boolean? {
val room = dbManager.getRoom(roomId)
return room?.chatRoom?.alert
}

fun runCommand(text: String, roomId: String) {
launchUI(strategy) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package chat.rocket.android.chatroom.presentation

import androidx.lifecycle.MutableLiveData
import chat.rocket.android.chatroom.uimodel.BaseUiModel
import chat.rocket.android.chatroom.uimodel.suggestion.ChatRoomSuggestionUiModel
import chat.rocket.android.chatroom.uimodel.suggestion.CommandSuggestionUiModel
Expand Down Expand Up @@ -145,4 +146,6 @@ interface ChatRoomView : LoadingView, MessageView {

fun onRoomUpdated(roomUiModel: RoomUiModel)

fun updateUnreadRoomsBadge(totalUnreadRooms: MutableLiveData<Long>)

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import chat.rocket.android.R
import chat.rocket.android.chatroom.presentation.ChatRoomNavigator
import chat.rocket.android.server.domain.GetCurrentServerInteractor
Expand All @@ -19,6 +20,7 @@ import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.support.HasSupportFragmentInjector
import kotlinx.android.synthetic.main.app_bar_chat_room.*
import kotlinx.android.synthetic.main.unread_messages_badge.*
import javax.inject.Inject

fun Context.chatRoomIntent(
Expand Down Expand Up @@ -163,6 +165,26 @@ class ChatRoomActivity : AppCompatActivity(), HasSupportFragmentInjector {
text_room_name.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
}

fun bindUnreadRooms(totalUnreadRooms: Long) {
val textView = text_total_unread_messages
when {
totalUnreadRooms in 1..9 -> {
textView.textContent = totalUnreadRooms.toString()
textView.isVisible = true
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_white_24dp)
}
totalUnreadRooms > 9 -> {
textView.textContent = "!"
textView.isVisible = true
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_white_24dp)
}
else -> {
textView.isVisible = false
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp)
}
}
}

private fun finishActivity() {
super.onBackPressed()
overridePendingTransition(R.anim.close_enter, R.anim.close_exit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import androidx.core.text.bold
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -94,6 +96,8 @@ import kotlinx.android.synthetic.main.message_attachment_options.*
import kotlinx.android.synthetic.main.message_composer.*
import kotlinx.android.synthetic.main.message_list.*
import kotlinx.android.synthetic.main.reaction_praises_list_item.view.*
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.runBlocking
import timber.log.Timber
import java.io.File
import java.io.IOException
Expand Down Expand Up @@ -314,6 +318,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
text_message.addTextChangedListener(EmojiKeyboardPopup.EmojiTextWatcher(text_message))
presenter.updateUnreadRoomsBadge()
}

override fun onDestroyView() {
Expand Down Expand Up @@ -1051,6 +1056,25 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
view_dim.isVisible = false
}

override fun updateUnreadRoomsBadge(totalUnreadRooms: MutableLiveData<Long>) {
ui {
val updateTotalUnreadRooms = Observer<Long> { newTotalUnreadRooms ->
var alert: Boolean? = null
runBlocking(CommonPool) {
alert = presenter.getRoomAlert(chatRoomId)
}
Timber.d("newTotalUnreadRooms: ${newTotalUnreadRooms}, alert: ${alert}")
(activity as ChatRoomActivity).let {
if (alert == true)
it.bindUnreadRooms(newTotalUnreadRooms - 1)
else
it.bindUnreadRooms(newTotalUnreadRooms)
}
}
totalUnreadRooms.observe(this, updateTotalUnreadRooms)
}
}

private fun setupToolbar(toolbarTitle: String) {
with(activity as ChatRoomActivity) {
this.clearLightStatusBar()
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/chat/rocket/android/db/DatabaseManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chat.rocket.android.db

import android.app.Application
import androidx.lifecycle.MutableLiveData
import chat.rocket.android.R
import chat.rocket.android.db.model.BaseMessageEntity
import chat.rocket.android.db.model.BaseUserEntity
Expand Down Expand Up @@ -64,6 +65,9 @@ class DatabaseManager(val context: Application, val serverUrl: String) {
private val insertRooms = HashMap<String, Room>()
private val updateSubs = LinkedHashMap<String, Subscription>()
private val updateRooms = LinkedHashMap<String, Room>()
val totalUnreadRooms: MutableLiveData<Long> by lazy {
MutableLiveData<Long>()
}

fun chatRoomDao(): ChatRoomDao = database.chatRoomDao()
fun userDao(): UserDao = database.userDao()
Expand Down Expand Up @@ -553,12 +557,15 @@ class DatabaseManager(val context: Application, val serverUrl: String) {
Timber.d("Running ChatRooms transaction: remove: ${operation.toRemove} - insert: ${operation.toInsert} - update: ${operation.toUpdate}")

chatRoomDao().update(operation.toInsert, operation.toUpdate, operation.toRemove)
updateUnreadRoomsCount()
}
is Operation.InsertRooms -> {
chatRoomDao().insertOrReplace(operation.chatRooms)
updateUnreadRoomsCount()
}
is Operation.CleanInsertRooms -> {
chatRoomDao().cleanInsert(operation.chatRooms)
updateUnreadRoomsCount()
}
is Operation.InsertUsers -> {
val time = measureTimeMillis { userDao().upsert(operation.users) }
Expand All @@ -572,13 +579,27 @@ class DatabaseManager(val context: Application, val serverUrl: String) {
}
is Operation.InsertMessages -> {
messageDao().insert(operation.list)
updateUnreadRoomsCount()
}
is Operation.SaveLastSync -> {
messageDao().saveLastSync(operation.sync)
}
}.exhaustive
}
}

private fun updateUnreadRoomsCount() {
val rooms: List<chat.rocket.android.db.model.ChatRoom> = chatRoomDao().getAllSync()
var count: Long = 0
Timber.d("chatroomdao: ${rooms}")
rooms.forEach { room ->
if (room.chatRoom.alert || room.chatRoom.unread > 0) {
count++
}
}
Timber.d("Total unread rooms: ${count}")
totalUnreadRooms.postValue(count)
}
}

sealed class Operation {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M15.41,16.09l-4.58,-4.59 4.58,-4.59L14,5.5l-6,6 6,6z"/>
</vector>
23 changes: 22 additions & 1 deletion app/src/main/res/layout/app_bar_chat_room.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
android:background="@color/colorPrimary"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:navigationIcon="?android:attr/homeAsUpIndicator"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">

<TextView
android:id="@+id/text_room_name"
Expand All @@ -29,4 +36,18 @@
tools:text="general" />
</androidx.appcompat.widget.Toolbar>

<include
android:id="@+id/chatroom_unread_messages_badge"
layout="@layout/unread_messages_badge"
android:layout_width="21dp"
android:layout_height="21dp"
android:layout_marginStart="36dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.appbar.AppBarLayout>