Skip to content

Commit

Permalink
implement "Unread messages" popup with normal button
Browse files Browse the repository at this point in the history
replace com.nextcloud.ui.popupbubble.PopupBubble with MaterialButton.

com.nextcloud.ui.popupbubble.PopupBubble was forked from
https://github.com/webianks/PopupBubble
which is quite outdated.

com.nextcloud.ui.popupbubble.PopupBubble is still used in ConversationsListActivity but there it should also be removed.

Removing this recycler view stuff will also help a bit to switch to JetpackCompose

Signed-off-by: Marcel Hibbe <[email protected]>
  • Loading branch information
mahibi committed Oct 29, 2024
1 parent 180dd03 commit f4340f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public UnreadNoticeMessageViewHolder(View itemView, Object payload) {

@Override
public void viewDetached() {
// messagesListAdapter.deleteById("-1");
}

@Override
Expand Down
65 changes: 25 additions & 40 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ class ChatActivity :
var mentionAutocomplete: Autocomplete<*>? = null
var layoutManager: LinearLayoutManager? = null
var pullChatMessagesPending = false
var newMessagesCount = 0
var startCallFromNotification: Boolean = false
var startCallFromRoomSwitch: Boolean = false

Expand Down Expand Up @@ -758,8 +757,8 @@ class ChatActivity :

removeUnreadMessagesMarker()

if (binding.unreadMessagesPopup.isShown == true) {
binding.unreadMessagesPopup.hide()
if (binding.unreadMessagesPopup.isShown) {
binding.unreadMessagesPopup.visibility = View.GONE
}
binding.messagesListView.smoothScrollToPosition(0)
}
Expand All @@ -770,8 +769,8 @@ class ChatActivity :
if (code.toString().startsWith("2")) {
myFirstMessage = state.message

if (binding.unreadMessagesPopup.isShown == true) {
binding.unreadMessagesPopup.hide()
if (binding.unreadMessagesPopup.isShown) {
binding.unreadMessagesPopup.visibility = View.GONE
}

binding.messagesListView.smoothScrollToPosition(0)
Expand Down Expand Up @@ -1016,7 +1015,7 @@ class ChatActivity :
}

private fun removeUnreadMessagesMarker() {
val index = adapter?.getMessagePositionById("-1")
val index = adapter?.getMessagePositionById(UNREAD_MESSAGES_MARKER_ID.toString())
if (index != null && index != -1) {
adapter?.items?.removeAt(index)
}
Expand All @@ -1041,22 +1040,9 @@ class ChatActivity :

setupSwipeToReply()

binding.unreadMessagesPopup.setRecyclerView(binding.messagesListView)

binding.unreadMessagesPopup.setPopupBubbleListener { _ ->
if (newMessagesCount != 0) {
val scrollPosition = if (newMessagesCount - 1 < 0) {
0
} else {
newMessagesCount - 1
}
Handler().postDelayed(
{
binding.messagesListView.smoothScrollToPosition(scrollPosition)
},
NEW_MESSAGES_POPUP_BUBBLE_DELAY
)
}
binding.unreadMessagesPopup.setOnClickListener {
binding.messagesListView.smoothScrollToPosition(0)
binding.unreadMessagesPopup.visibility = View.GONE
}

binding.scrollDownButton.setOnClickListener {
Expand All @@ -1075,21 +1061,14 @@ class ChatActivity :
super.onScrollStateChanged(recyclerView, newState)

if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
if (layoutManager!!.findFirstCompletelyVisibleItemPosition() > 0 &&
!binding.unreadMessagesPopup.isShown
) {
binding.scrollDownButton.visibility = View.VISIBLE
} else {
if (isScrolledToBottom()) {
binding.unreadMessagesPopup.visibility = View.GONE
binding.scrollDownButton.visibility = View.GONE
}

if (newMessagesCount != 0 && layoutManager != null) {
if (layoutManager!!.findFirstCompletelyVisibleItemPosition() < newMessagesCount) {
newMessagesCount = 0

if (binding.unreadMessagesPopup.isShown) {
binding.unreadMessagesPopup.hide()
}
} else {
if (binding.unreadMessagesPopup.isShown) {
binding.scrollDownButton.visibility = View.GONE
} else {
binding.scrollDownButton.visibility = View.VISIBLE
}
}
}
Expand Down Expand Up @@ -2677,7 +2656,7 @@ class ChatActivity :
scrollToBottom = true
} else {
scrollToBottom = false
binding.unreadMessagesPopup.show()
binding.unreadMessagesPopup.visibility = View.VISIBLE
// here we have the problem that the chat jumps for every update
}
}
Expand All @@ -2698,13 +2677,18 @@ class ChatActivity :
it.addToStart(chatMessage, scrollToBottom)
}
}

// workaround to jump back to unread messages marker
if (setUnreadMessagesMarker) {
scrollToFirstUnreadMessage()
}
}

private fun isScrolledToBottom() = layoutManager?.findFirstVisibleItemPosition() == 0

private fun setUnreadMessageMarker(chatMessageList: List<ChatMessage>) {
val unreadChatMessage = ChatMessage()
unreadChatMessage.jsonMessageId = -1
unreadChatMessage.jsonMessageId = UNREAD_MESSAGES_MARKER_ID
unreadChatMessage.actorId = "-1"
unreadChatMessage.timestamp = chatMessageList[0].timestamp
unreadChatMessage.message = context.getString(R.string.nc_new_messages)
Expand Down Expand Up @@ -2736,7 +2720,7 @@ class ChatActivity :

private fun scrollToFirstUnreadMessage() {
adapter?.let {
scrollToAndCenterMessageWithId("-1")
scrollToAndCenterMessageWithId(UNREAD_MESSAGES_MARKER_ID.toString())
}
}

Expand Down Expand Up @@ -3552,7 +3536,7 @@ class ChatActivity :
CONTENT_TYPE_POLL -> message.isPoll()
CONTENT_TYPE_LINK_PREVIEW -> message.isLinkPreview()
CONTENT_TYPE_SYSTEM_MESSAGE -> !TextUtils.isEmpty(message.systemMessage)
CONTENT_TYPE_UNREAD_NOTICE_MESSAGE -> message.id == "-1"
CONTENT_TYPE_UNREAD_NOTICE_MESSAGE -> message.id == UNREAD_MESSAGES_MARKER_ID.toString()
CONTENT_TYPE_CALL_STARTED -> message.id == "-2"
CONTENT_TYPE_TEMP -> message.id == "-3"
CONTENT_TYPE_DECK_CARD -> message.isDeckCard()
Expand Down Expand Up @@ -3762,6 +3746,7 @@ class ChatActivity :
private const val CONTENT_TYPE_LINK_PREVIEW: Byte = 7
private const val CONTENT_TYPE_DECK_CARD: Byte = 8
private const val CONTENT_TYPE_TEMP: Byte = 9
private const val UNREAD_MESSAGES_MARKER_ID = -1
private const val NEW_MESSAGES_POPUP_BUBBLE_DELAY: Long = 200
private const val GET_ROOM_INFO_DELAY_NORMAL: Long = 30000
private const val GET_ROOM_INFO_DELAY_LOBBY: Long = 5000
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/layout/activity_chat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@
app:textAutoLink="all"
tools:visibility="visible" />

<com.nextcloud.ui.popupbubble.PopupBubble
<com.google.android.material.button.MaterialButton
android:id="@+id/unreadMessagesPopup"
style="@style/Widget.AppTheme.Button.ElevatedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/typing_indicator_wrapper"
Expand All @@ -172,6 +173,8 @@
android:minHeight="@dimen/min_size_clickable_area"
android:text="@string/nc_new_messages"
android:theme="@style/Button.Primary"
android:visibility="gone"
tools:visibility="visible"
app:background="@color/colorPrimary"
app:cornerRadius="@dimen/button_corner_radius"
app:icon="@drawable/ic_baseline_arrow_downward_24px" />
Expand Down

0 comments on commit f4340f3

Please sign in to comment.