mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-21 04:29:45 +01:00
Refactor pullChatMessages
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
parent
6f4a24b28f
commit
7f77cf8c8e
@ -2313,24 +2313,70 @@ class ChatController(args: Bundle) :
|
|||||||
disposables.add(d)
|
disposables.add(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
@Suppress("Detekt.TooGenericExceptionCaught")
|
@Suppress("Detekt.TooGenericExceptionCaught")
|
||||||
override fun onNext(response: Response<*>) {
|
override fun onNext(response: Response<*>) {
|
||||||
pullChatMessagesPending = false
|
pullChatMessagesPending = false
|
||||||
|
|
||||||
if (lookIntoFuture) {
|
if (isFirstMessagesProcessing) {
|
||||||
if (response.code() == HTTP_CODE_NOT_MODIFIED) {
|
cancelNotificationsForCurrentConversation()
|
||||||
Log.d(TAG, "pullChatMessages - quasi recursive call to pullChatMessages")
|
|
||||||
pullChatMessages(true, setReadMarker, xChatLastCommonRead)
|
isFirstMessagesProcessing = false
|
||||||
} else if (response.code() == HTTP_CODE_PRECONDITION_FAILED) {
|
binding?.progressBar?.visibility = View.GONE
|
||||||
futurePreconditionFailed = true
|
|
||||||
} else {
|
binding?.messagesListView?.visibility = View.VISIBLE
|
||||||
processMessagesResponse(response, true)
|
}
|
||||||
|
|
||||||
|
when (response.code()) {
|
||||||
|
HTTP_CODE_NOT_MODIFIED -> {
|
||||||
|
Log.d(TAG, "pullChatMessages - HTTP_CODE_NOT_MODIFIED")
|
||||||
|
|
||||||
|
if (lookIntoFuture) {
|
||||||
|
Log.d(TAG, "recursive call to pullChatMessages")
|
||||||
|
pullChatMessages(true, setReadMarker, xChatLastCommonRead)
|
||||||
|
} else {
|
||||||
|
historyRead = true
|
||||||
|
pullChatMessages(true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
HTTP_CODE_PRECONDITION_FAILED -> {
|
||||||
if (response.code() == HTTP_CODE_PRECONDITION_FAILED) {
|
Log.d(TAG, "pullChatMessages - HTTP_CODE_PRECONDITION_FAILED")
|
||||||
pastPreconditionFailed = true
|
|
||||||
} else {
|
if (lookIntoFuture) {
|
||||||
processMessagesResponse(response, false)
|
futurePreconditionFailed = true
|
||||||
|
} else {
|
||||||
|
pastPreconditionFailed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
HTTP_CODE_OK -> {
|
||||||
|
Log.d(TAG, "pullChatMessages - HTTP_CODE_OK")
|
||||||
|
|
||||||
|
val chatOverall = response.body() as ChatOverall?
|
||||||
|
val chatMessageList = handleSystemMessages(chatOverall?.ocs!!.data!!)
|
||||||
|
|
||||||
|
processHeaderChatLastGiven(response, lookIntoFuture)
|
||||||
|
|
||||||
|
if (chatMessageList.isNotEmpty() &&
|
||||||
|
ChatMessage.SystemMessageType.CLEARED_CHAT == chatMessageList[0].systemMessageType
|
||||||
|
) {
|
||||||
|
adapter?.clear()
|
||||||
|
adapter?.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lookIntoFuture) {
|
||||||
|
processMessagesFromTheFuture(chatMessageList)
|
||||||
|
} else {
|
||||||
|
processMessagesNotFromTheFuture(chatMessageList)
|
||||||
|
}
|
||||||
|
|
||||||
|
val xChatLastCommonRead = response.headers()["X-Chat-Last-Common-Read"]?.let {
|
||||||
|
Integer.parseInt(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
updateReadStatusOfAllMessages(xChatLastCommonRead)
|
||||||
|
adapter?.notifyDataSetChanged()
|
||||||
|
|
||||||
|
pullChatMessages(true, true, xChatLastCommonRead)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2372,70 +2418,6 @@ class ChatController(args: Bundle) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun processMessagesResponse(
|
|
||||||
response: Response<*>,
|
|
||||||
isFromTheFuture: Boolean
|
|
||||||
) {
|
|
||||||
val xChatLastCommonRead = response.headers()["X-Chat-Last-Common-Read"]?.let {
|
|
||||||
Integer.parseInt(it)
|
|
||||||
}
|
|
||||||
|
|
||||||
processHeaderChatLastGiven(response, isFromTheFuture)
|
|
||||||
|
|
||||||
if (response.code() == HTTP_CODE_OK) {
|
|
||||||
val chatOverall = response.body() as ChatOverall?
|
|
||||||
val chatMessageList = handleSystemMessages(chatOverall?.ocs!!.data!!)
|
|
||||||
|
|
||||||
processMessages(chatMessageList, isFromTheFuture, xChatLastCommonRead)
|
|
||||||
} else if (response.code() == HTTP_CODE_NOT_MODIFIED && !isFromTheFuture) {
|
|
||||||
if (isFirstMessagesProcessing) {
|
|
||||||
cancelNotificationsForCurrentConversation()
|
|
||||||
|
|
||||||
isFirstMessagesProcessing = false
|
|
||||||
binding?.progressBar?.visibility = View.GONE
|
|
||||||
}
|
|
||||||
|
|
||||||
historyRead = true
|
|
||||||
|
|
||||||
pullChatMessages(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun processMessages(
|
|
||||||
chatMessageList: List<ChatMessage>,
|
|
||||||
isFromTheFuture: Boolean,
|
|
||||||
xChatLastCommonRead: Int?
|
|
||||||
) {
|
|
||||||
if (chatMessageList.isNotEmpty() &&
|
|
||||||
ChatMessage.SystemMessageType.CLEARED_CHAT == chatMessageList[0].systemMessageType
|
|
||||||
) {
|
|
||||||
adapter?.clear()
|
|
||||||
adapter?.notifyDataSetChanged()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isFirstMessagesProcessing) {
|
|
||||||
cancelNotificationsForCurrentConversation()
|
|
||||||
|
|
||||||
isFirstMessagesProcessing = false
|
|
||||||
binding?.progressBar?.visibility = View.GONE
|
|
||||||
|
|
||||||
binding?.messagesListView?.visibility = View.VISIBLE
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isFromTheFuture) {
|
|
||||||
processMessagesFromTheFuture(chatMessageList)
|
|
||||||
} else {
|
|
||||||
processMessagesNotFromTheFuture(chatMessageList)
|
|
||||||
}
|
|
||||||
|
|
||||||
updateReadStatusOfAllMessages(xChatLastCommonRead)
|
|
||||||
adapter?.notifyDataSetChanged()
|
|
||||||
|
|
||||||
if (validSessionId()) {
|
|
||||||
pullChatMessages(true, true, xChatLastCommonRead)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateReadStatusOfAllMessages(xChatLastCommonRead: Int?) {
|
private fun updateReadStatusOfAllMessages(xChatLastCommonRead: Int?) {
|
||||||
if (adapter != null) {
|
if (adapter != null) {
|
||||||
for (message in adapter!!.items) {
|
for (message in adapter!!.items) {
|
||||||
@ -2624,7 +2606,7 @@ class ChatController(args: Bundle) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onLoadMore(page: Int, totalItemsCount: Int) {
|
override fun onLoadMore(page: Int, totalItemsCount: Int) {
|
||||||
if (!historyRead && validSessionId()) {
|
if (!historyRead) {
|
||||||
pullChatMessages(false)
|
pullChatMessages(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user