Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 authored and mahibi committed Dec 3, 2024
1 parent 3bca62d commit c7da8f9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class ConversationsListActivity :
if (adapter == null) {
adapter = FlexibleAdapter(conversationItems, this, true)
} else {
binding.loadingContent?.visibility = View.GONE
binding.loadingContent.visibility = View.GONE
}
adapter!!.addListener(this)
prepareViews()
Expand Down Expand Up @@ -387,6 +387,9 @@ class ConversationsListActivity :
lifecycleScope.launch {
conversationsListViewModel.getRoomsFlow
.onEach { list ->
// Refreshes conversation messages in the background asynchronously
list.refreshMessages()

// Update Conversations
conversationItems.clear()
conversationItemsWithHeader.clear()
Expand Down Expand Up @@ -460,6 +463,34 @@ class ConversationsListActivity :
updateFilterConversationButtonColor()
}

private fun List<ConversationModel>.refreshMessages() {
val previous = conversationItems.associate {
(it as ConversationItem)
val unreadMessages = it.model.unreadMessages
val roomToken = it.model.token
Pair(roomToken, unreadMessages)
}

val current = this.associateWith { model ->
val unreadMessages = model.unreadMessages
unreadMessages
}

val result = current.map { (model, unreadMessages) ->
val previousUnreadMessages = previous[model.token] // Check if this conversation exists in last list
previousUnreadMessages?.let {
Pair(model, unreadMessages - previousUnreadMessages)
}
}.filterNotNull()

val url = userManager.currentUser.blockingGet().baseUrl!!
for (pair in result) {
if (pair.second > 0) {
conversationsListViewModel.updateRoomMessages(pair.first, pair.second, credentials!!, url)
}
}
}

private fun filter(conversation: ConversationModel): Boolean {
var result = true
for ((k, v) in filterState) {
Expand Down Expand Up @@ -688,7 +719,7 @@ class ConversationsListActivity :
if (!hasFilterEnabled()) filterableConversationItems = searchableConversationItems
adapter!!.updateDataSet(filterableConversationItems, false)
adapter!!.showAllHeaders()
binding.swipeRefreshLayoutView?.isEnabled = false
binding.swipeRefreshLayoutView.isEnabled = false
searchBehaviorSubject.onNext(true)
return true
}
Expand All @@ -702,9 +733,9 @@ class ConversationsListActivity :
// cancel any pending searches
searchHelper!!.cancelSearch()
}
binding.swipeRefreshLayoutView?.isRefreshing = false
binding.swipeRefreshLayoutView.isRefreshing = false
searchBehaviorSubject.onNext(false)
binding.swipeRefreshLayoutView?.isEnabled = true
binding.swipeRefreshLayoutView.isEnabled = true
searchView!!.onActionViewCollapsed()

binding.conversationListAppbar.stateListAnimator = AnimatorInflater.loadStateListAnimator(
Expand All @@ -717,7 +748,7 @@ class ConversationsListActivity :
viewThemeUtils.platform.resetStatusBar(this@ConversationsListActivity)
}

val layoutManager = binding.recyclerView?.layoutManager as SmoothScrollLinearLayoutManager?
val layoutManager = binding.recyclerView.layoutManager as SmoothScrollLinearLayoutManager?
layoutManager?.scrollToPositionWithOffset(0, 0)
return true
}
Expand Down Expand Up @@ -810,18 +841,18 @@ class ConversationsListActivity :

private fun initOverallLayout(isConversationListNotEmpty: Boolean) {
if (isConversationListNotEmpty) {
if (binding.emptyLayout?.visibility != View.GONE) {
binding.emptyLayout?.visibility = View.GONE
if (binding.emptyLayout.visibility != View.GONE) {
binding.emptyLayout.visibility = View.GONE
}
if (binding.swipeRefreshLayoutView?.visibility != View.VISIBLE) {
binding.swipeRefreshLayoutView?.visibility = View.VISIBLE
if (binding.swipeRefreshLayoutView.visibility != View.VISIBLE) {
binding.swipeRefreshLayoutView.visibility = View.VISIBLE
}
} else {
if (binding.emptyLayout?.visibility != View.VISIBLE) {
binding.emptyLayout?.visibility = View.VISIBLE
if (binding.emptyLayout.visibility != View.VISIBLE) {
binding.emptyLayout.visibility = View.VISIBLE
}
if (binding.swipeRefreshLayoutView?.visibility != View.GONE) {
binding.swipeRefreshLayoutView?.visibility = View.GONE
if (binding.swipeRefreshLayoutView.visibility != View.GONE) {
binding.swipeRefreshLayoutView.visibility = View.GONE
}
}
}
Expand Down Expand Up @@ -1001,24 +1032,24 @@ class ConversationsListActivity :
}
}
})
binding.recyclerView?.setOnTouchListener { v: View, _: MotionEvent? ->
binding.recyclerView.setOnTouchListener { v: View, _: MotionEvent? ->
if (!isDestroyed) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(v.windowToken, 0)
}
false
}
binding.swipeRefreshLayoutView?.setOnRefreshListener {
binding.swipeRefreshLayoutView.setOnRefreshListener {
fetchRooms()
fetchPendingInvitations()
}
binding.swipeRefreshLayoutView?.let { viewThemeUtils.androidx.themeSwipeRefreshLayout(it) }
binding.emptyLayout?.setOnClickListener { showNewConversationsScreen() }
binding.floatingActionButton?.setOnClickListener {
binding.swipeRefreshLayoutView.let { viewThemeUtils.androidx.themeSwipeRefreshLayout(it) }
binding.emptyLayout.setOnClickListener { showNewConversationsScreen() }
binding.floatingActionButton.setOnClickListener {
run(context)
showNewConversationsScreen()
}
binding.floatingActionButton?.let { viewThemeUtils.material.themeFAB(it) }
binding.floatingActionButton.let { viewThemeUtils.material.themeFAB(it) }

binding.switchAccountButton.setOnClickListener {
if (resources != null && resources!!.getBoolean(R.bool.multiaccount_support)) {
Expand Down Expand Up @@ -1190,7 +1221,7 @@ class ConversationsListActivity :

@SuppressLint("CheckResult") // handled by helper
private fun startMessageSearch(search: String?) {
binding.swipeRefreshLayoutView?.isRefreshing = true
binding.swipeRefreshLayoutView.isRefreshing = true
searchHelper?.startMessageSearch(search!!)
?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
Expand Down Expand Up @@ -1435,8 +1466,8 @@ class ConversationsListActivity :
filesToShare?.forEach {
UploadAndShareFilesWorker.upload(
it,
selectedConversation!!.token!!,
selectedConversation!!.displayName!!,
selectedConversation!!.token,
selectedConversation!!.displayName,
null
)
}
Expand Down Expand Up @@ -1909,15 +1940,15 @@ class ConversationsListActivity :
}
// add unified search result at the end of the list
adapter!!.addItems(adapter!!.mainItemCount + adapter!!.scrollableHeaders.size, adapterItems)
binding.recyclerView?.scrollToPosition(0)
binding.recyclerView.scrollToPosition(0)
}
}
binding.swipeRefreshLayoutView?.isRefreshing = false
binding.swipeRefreshLayoutView.isRefreshing = false
}

private fun onMessageSearchError(throwable: Throwable) {
handleHttpExceptions(throwable)
binding.swipeRefreshLayoutView?.isRefreshing = false
binding.swipeRefreshLayoutView.isRefreshing = false
showErrorDialog()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.nextcloud.talk.chat.data.ChatMessageRepository
import com.nextcloud.talk.conversationlist.data.OfflineConversationsRepository
import com.nextcloud.talk.invitation.data.InvitationsModel
import com.nextcloud.talk.invitation.data.InvitationsRepository
import com.nextcloud.talk.models.domain.ConversationModel
import com.nextcloud.talk.users.UserManager
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
Expand All @@ -24,6 +26,7 @@ import javax.inject.Inject

class ConversationsListViewModel @Inject constructor(
private val repository: OfflineConversationsRepository,
private val chatRepository: ChatMessageRepository,
var userManager: UserManager
) :
ViewModel() {
Expand Down Expand Up @@ -84,6 +87,10 @@ class ConversationsListViewModel @Inject constructor(
repository.getRooms()
}

fun updateRoomMessages(model: ConversationModel, limit: Int, credentials: String, baseUrl: String) {
// TODO need to edit fetch init messages to include a limit this is rough
}

inner class FederatedInvitationsObserver : Observer<InvitationsModel> {
override fun onSubscribe(d: Disposable) {
// unused atm
Expand Down

0 comments on commit c7da8f9

Please sign in to comment.