mirror of
https://github.com/nextcloud/talk-android.git
synced 2024-11-21 20:45:29 +03:00
Merge pull request #4300 from nextcloud/feature/4299/improveOfflineSupport
Feature/4299/improve offline support
This commit is contained in:
commit
a51875098b
6 changed files with 258 additions and 169 deletions
|
@ -183,6 +183,7 @@ import io.reactivex.Observer
|
|||
import io.reactivex.disposables.Disposable
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -416,7 +417,12 @@ class ChatActivity :
|
|||
|
||||
messageInputViewModel = ViewModelProvider(this, viewModelFactory)[MessageInputViewModel::class.java]
|
||||
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
this.lifecycleScope.launch {
|
||||
delay(DELAY_TO_SHOW_PROGRESS_BAR)
|
||||
if (adapter?.isEmpty == true) {
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
|
||||
|
||||
|
@ -1244,9 +1250,7 @@ class ChatActivity :
|
|||
|
||||
@Suppress("MagicNumber", "LongMethod")
|
||||
private fun updateTypingIndicator() {
|
||||
fun ellipsize(text: String): String {
|
||||
return DisplayUtils.ellipsize(text, TYPING_INDICATOR_MAX_NAME_LENGTH)
|
||||
}
|
||||
fun ellipsize(text: String): String = DisplayUtils.ellipsize(text, TYPING_INDICATOR_MAX_NAME_LENGTH)
|
||||
|
||||
val participantNames = ArrayList<String>()
|
||||
|
||||
|
@ -1320,10 +1324,9 @@ class ChatActivity :
|
|||
}
|
||||
}
|
||||
|
||||
private fun isTypingStatusEnabled(): Boolean {
|
||||
return webSocketInstance != null &&
|
||||
private fun isTypingStatusEnabled(): Boolean =
|
||||
webSocketInstance != null &&
|
||||
!CapabilitiesUtil.isTypingStatusPrivate(conversationUser!!)
|
||||
}
|
||||
|
||||
private fun setupSwipeToReply() {
|
||||
if (this::participantPermissions.isInitialized &&
|
||||
|
@ -1422,15 +1425,18 @@ class ChatActivity :
|
|||
}
|
||||
|
||||
fun isOneToOneConversation() =
|
||||
currentConversation != null && currentConversation?.type != null &&
|
||||
currentConversation != null &&
|
||||
currentConversation?.type != null &&
|
||||
currentConversation?.type == ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL
|
||||
|
||||
private fun isGroupConversation() =
|
||||
currentConversation != null && currentConversation?.type != null &&
|
||||
currentConversation != null &&
|
||||
currentConversation?.type != null &&
|
||||
currentConversation?.type == ConversationEnums.ConversationType.ROOM_GROUP_CALL
|
||||
|
||||
private fun isPublicConversation() =
|
||||
currentConversation != null && currentConversation?.type != null &&
|
||||
currentConversation != null &&
|
||||
currentConversation?.type != null &&
|
||||
currentConversation?.type == ConversationEnums.ConversationType.ROOM_PUBLIC_CALL
|
||||
|
||||
private fun updateRoomTimerHandler() {
|
||||
|
@ -1668,11 +1674,10 @@ class ChatActivity :
|
|||
adapter?.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
private fun isChildOfExpandableSystemMessage(chatMessage: ChatMessage): Boolean {
|
||||
return isSystemMessage(chatMessage) &&
|
||||
private fun isChildOfExpandableSystemMessage(chatMessage: ChatMessage): Boolean =
|
||||
isSystemMessage(chatMessage) &&
|
||||
!chatMessage.expandableParent &&
|
||||
chatMessage.lastItemOfExpandableGroup != 0
|
||||
}
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
override fun expandSystemMessage(chatMessageToExpand: ChatMessage) {
|
||||
|
@ -1758,12 +1763,11 @@ class ChatActivity :
|
|||
}
|
||||
}
|
||||
|
||||
fun isRecordAudioPermissionGranted(): Boolean {
|
||||
return PermissionChecker.checkSelfPermission(
|
||||
fun isRecordAudioPermissionGranted(): Boolean =
|
||||
PermissionChecker.checkSelfPermission(
|
||||
context,
|
||||
Manifest.permission.RECORD_AUDIO
|
||||
) == PERMISSION_GRANTED
|
||||
}
|
||||
|
||||
fun requestRecordAudioPermissions() {
|
||||
requestPermissions(
|
||||
|
@ -1870,11 +1874,10 @@ class ChatActivity :
|
|||
}
|
||||
}
|
||||
|
||||
private fun isReadOnlyConversation(): Boolean {
|
||||
return currentConversation?.conversationReadOnlyState != null &&
|
||||
private fun isReadOnlyConversation(): Boolean =
|
||||
currentConversation?.conversationReadOnlyState != null &&
|
||||
currentConversation?.conversationReadOnlyState ==
|
||||
ConversationEnums.ConversationReadOnlyState.CONVERSATION_READ_ONLY
|
||||
}
|
||||
|
||||
private fun checkLobbyState() {
|
||||
if (currentConversation != null &&
|
||||
|
@ -1890,7 +1893,8 @@ class ChatActivity :
|
|||
sb.append(resources!!.getText(R.string.nc_lobby_waiting))
|
||||
.append("\n\n")
|
||||
|
||||
if (currentConversation?.lobbyTimer != null && currentConversation?.lobbyTimer !=
|
||||
if (currentConversation?.lobbyTimer != null &&
|
||||
currentConversation?.lobbyTimer !=
|
||||
0L
|
||||
) {
|
||||
val timestampMS = (currentConversation?.lobbyTimer ?: 0) * DateConstants.SECOND_DIVIDER
|
||||
|
@ -2089,7 +2093,7 @@ class ChatActivity :
|
|||
if (position != null && position >= 0) {
|
||||
binding.messagesListView.scrollToPosition(position)
|
||||
} else {
|
||||
// TODO show error that we don't have that message?
|
||||
Log.d(TAG, "message $messageId that should be scrolled to was not found (scrollToMessageWithId)")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2101,6 +2105,12 @@ class ChatActivity :
|
|||
position,
|
||||
binding.messagesListView.height / 2
|
||||
)
|
||||
} else {
|
||||
Log.d(
|
||||
TAG,
|
||||
"message $messageId that should be scrolled to was not found " +
|
||||
"(scrollToAndCenterMessageWithId)"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2264,11 +2274,10 @@ class ChatActivity :
|
|||
startActivity(intent)
|
||||
}
|
||||
|
||||
private fun validSessionId(): Boolean {
|
||||
return currentConversation != null &&
|
||||
private fun validSessionId(): Boolean =
|
||||
currentConversation != null &&
|
||||
sessionIdAfterRoomJoined?.isNotEmpty() == true &&
|
||||
sessionIdAfterRoomJoined != "0"
|
||||
}
|
||||
|
||||
@Suppress("Detekt.TooGenericExceptionCaught")
|
||||
private fun cancelNotificationsForCurrentConversation() {
|
||||
|
@ -2321,14 +2330,11 @@ class ChatActivity :
|
|||
}
|
||||
}
|
||||
|
||||
private fun isActivityNotChangingConfigurations(): Boolean {
|
||||
return !isChangingConfigurations
|
||||
}
|
||||
private fun isActivityNotChangingConfigurations(): Boolean = !isChangingConfigurations
|
||||
|
||||
private fun isNotInCall(): Boolean {
|
||||
return !ApplicationWideCurrentRoomHolder.getInstance().isInCall &&
|
||||
private fun isNotInCall(): Boolean =
|
||||
!ApplicationWideCurrentRoomHolder.getInstance().isInCall &&
|
||||
!ApplicationWideCurrentRoomHolder.getInstance().isDialing
|
||||
}
|
||||
|
||||
private fun setActionBarTitle() {
|
||||
val title = binding.chatToolbar.findViewById<TextView>(R.id.chat_toolbar_title)
|
||||
|
@ -2769,11 +2775,10 @@ class ChatActivity :
|
|||
}
|
||||
}
|
||||
|
||||
private fun isSameDayNonSystemMessages(messageLeft: ChatMessage, messageRight: ChatMessage): Boolean {
|
||||
return TextUtils.isEmpty(messageLeft.systemMessage) &&
|
||||
private fun isSameDayNonSystemMessages(messageLeft: ChatMessage, messageRight: ChatMessage): Boolean =
|
||||
TextUtils.isEmpty(messageLeft.systemMessage) &&
|
||||
TextUtils.isEmpty(messageRight.systemMessage) &&
|
||||
DateFormatter.isSameDay(messageLeft.createdAt, messageRight.createdAt)
|
||||
}
|
||||
|
||||
override fun onLoadMore(page: Int, totalItemsCount: Int) {
|
||||
val id = (
|
||||
|
@ -2793,15 +2798,14 @@ class ChatActivity :
|
|||
)
|
||||
}
|
||||
|
||||
override fun format(date: Date): String {
|
||||
return if (DateFormatter.isToday(date)) {
|
||||
override fun format(date: Date): String =
|
||||
if (DateFormatter.isToday(date)) {
|
||||
resources!!.getString(R.string.nc_date_header_today)
|
||||
} else if (DateFormatter.isYesterday(date)) {
|
||||
resources!!.getString(R.string.nc_date_header_yesterday)
|
||||
} else {
|
||||
DateFormatter.format(date, DateFormatter.Template.STRING_DAY_MONTH_YEAR)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
super.onCreateOptionsMenu(menu)
|
||||
|
@ -2863,8 +2867,8 @@ class ChatActivity :
|
|||
return true
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
return when (item.itemId) {
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean =
|
||||
when (item.itemId) {
|
||||
R.id.conversation_video_call -> {
|
||||
startACall(false, false)
|
||||
true
|
||||
|
@ -2892,7 +2896,6 @@ class ChatActivity :
|
|||
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showSharedItems() {
|
||||
val intent = Intent(this, SharedItemsActivity::class.java)
|
||||
|
@ -2954,25 +2957,23 @@ class ChatActivity :
|
|||
return chatMessageMap.values.toList()
|
||||
}
|
||||
|
||||
private fun isInfoMessageAboutDeletion(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean {
|
||||
return currentMessage.value.parentMessageId != null && currentMessage.value.systemMessageType == ChatMessage
|
||||
.SystemMessageType.MESSAGE_DELETED
|
||||
}
|
||||
private fun isInfoMessageAboutDeletion(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean =
|
||||
currentMessage.value.parentMessageId != null &&
|
||||
currentMessage.value.systemMessageType == ChatMessage
|
||||
.SystemMessageType.MESSAGE_DELETED
|
||||
|
||||
private fun isReactionsMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean {
|
||||
return currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.REACTION ||
|
||||
private fun isReactionsMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean =
|
||||
currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.REACTION ||
|
||||
currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.REACTION_DELETED ||
|
||||
currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.REACTION_REVOKED
|
||||
}
|
||||
|
||||
private fun isEditMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean {
|
||||
return currentMessage.value.parentMessageId != null && currentMessage.value.systemMessageType == ChatMessage
|
||||
.SystemMessageType.MESSAGE_EDITED
|
||||
}
|
||||
private fun isEditMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean =
|
||||
currentMessage.value.parentMessageId != null &&
|
||||
currentMessage.value.systemMessageType == ChatMessage
|
||||
.SystemMessageType.MESSAGE_EDITED
|
||||
|
||||
private fun isPollVotedMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean {
|
||||
return currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.POLL_VOTED
|
||||
}
|
||||
private fun isPollVotedMessage(currentMessage: MutableMap.MutableEntry<String, ChatMessage>): Boolean =
|
||||
currentMessage.value.systemMessageType == ChatMessage.SystemMessageType.POLL_VOTED
|
||||
|
||||
private fun startACall(isVoiceOnlyCall: Boolean, callWithoutNotification: Boolean) {
|
||||
currentConversation?.let {
|
||||
|
@ -3076,9 +3077,8 @@ class ChatActivity :
|
|||
}
|
||||
}
|
||||
|
||||
private fun isSystemMessage(message: ChatMessage): Boolean {
|
||||
return ChatMessage.MessageType.SYSTEM_MESSAGE == message.getCalculateMessageType()
|
||||
}
|
||||
private fun isSystemMessage(message: ChatMessage): Boolean =
|
||||
ChatMessage.MessageType.SYSTEM_MESSAGE == message.getCalculateMessageType()
|
||||
|
||||
fun deleteMessage(message: IMessage) {
|
||||
if (!participantPermissions.hasChatPermission()) {
|
||||
|
@ -3321,20 +3321,26 @@ class ChatActivity :
|
|||
fileViewerUtils.openFileInFilesApp(link!!, keyID!!)
|
||||
}
|
||||
|
||||
private fun hasVisibleItems(message: ChatMessage): Boolean {
|
||||
return !message.isDeleted || // copy message
|
||||
message.replyable || // reply to
|
||||
message.replyable && // reply privately
|
||||
conversationUser?.userId?.isNotEmpty() == true && conversationUser!!.userId != "?" &&
|
||||
private fun hasVisibleItems(message: ChatMessage): Boolean =
|
||||
!message.isDeleted ||
|
||||
// copy message
|
||||
message.replyable ||
|
||||
// reply to
|
||||
message.replyable &&
|
||||
// reply privately
|
||||
conversationUser?.userId?.isNotEmpty() == true &&
|
||||
conversationUser!!.userId != "?" &&
|
||||
message.user.id.startsWith("users/") &&
|
||||
message.user.id.substring(ACTOR_LENGTH) != currentConversation?.actorId &&
|
||||
currentConversation?.type != ConversationEnums.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL ||
|
||||
isShowMessageDeletionButton(message) || // delete
|
||||
ChatMessage.MessageType.REGULAR_TEXT_MESSAGE == message.getCalculateMessageType() || // forward
|
||||
message.previousMessageId > NO_PREVIOUS_MESSAGE_ID && // mark as unread
|
||||
isShowMessageDeletionButton(message) ||
|
||||
// delete
|
||||
ChatMessage.MessageType.REGULAR_TEXT_MESSAGE == message.getCalculateMessageType() ||
|
||||
// forward
|
||||
message.previousMessageId > NO_PREVIOUS_MESSAGE_ID &&
|
||||
// mark as unread
|
||||
ChatMessage.MessageType.SYSTEM_MESSAGE != message.getCalculateMessageType() &&
|
||||
BuildConfig.DEBUG
|
||||
}
|
||||
|
||||
private fun setMessageAsDeleted(message: IMessage?) {
|
||||
val messageTemp = message as ChatMessage
|
||||
|
@ -3452,8 +3458,8 @@ class ChatActivity :
|
|||
return isUserAllowedByPrivileges
|
||||
}
|
||||
|
||||
override fun hasContentFor(message: ChatMessage, type: Byte): Boolean {
|
||||
return when (type) {
|
||||
override fun hasContentFor(message: ChatMessage, type: Byte): Boolean =
|
||||
when (type) {
|
||||
CONTENT_TYPE_LOCATION -> message.hasGeoLocation()
|
||||
CONTENT_TYPE_VOICE_MESSAGE -> message.isVoiceMessage
|
||||
CONTENT_TYPE_POLL -> message.isPoll()
|
||||
|
@ -3464,7 +3470,6 @@ class ChatActivity :
|
|||
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
private fun processMostRecentMessage(recent: ChatMessage, chatMessageList: List<ChatMessage>) {
|
||||
when (recent.systemMessageType) {
|
||||
|
@ -3712,5 +3717,6 @@ class ChatActivity :
|
|||
private const val CURRENT_AUDIO_POSITION_KEY = "CURRENT_AUDIO_POSITION"
|
||||
private const val CURRENT_AUDIO_WAS_PLAYING_KEY = "CURRENT_AUDIO_PLAYING"
|
||||
private const val RESUME_AUDIO_TAG = "RESUME_AUDIO_TAG"
|
||||
private const val DELAY_TO_SHOW_PROGRESS_BAR = 1000L
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,10 +56,8 @@ interface ChatMessageRepository : LifecycleAwareManager {
|
|||
* Long polls the server for any updates to the chat, if found, it synchronizes
|
||||
* the database with the server and emits the new messages to [messageFlow],
|
||||
* else it simply retries after timeout.
|
||||
*
|
||||
* [withNetworkParams] credentials and url.
|
||||
*/
|
||||
fun initMessagePolling(): Job
|
||||
fun initMessagePolling(initialMessageId: Long): Job
|
||||
|
||||
/**
|
||||
* Gets a individual message.
|
||||
|
|
|
@ -108,38 +108,101 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
override fun loadInitialMessages(withNetworkParams: Bundle): Job =
|
||||
scope.launch {
|
||||
Log.d(TAG, "---- loadInitialMessages ------------")
|
||||
|
||||
newXChatLastCommonRead = conversationModel.lastCommonReadMessage
|
||||
|
||||
val fieldMap = getFieldMap(
|
||||
lookIntoFuture = false,
|
||||
includeLastKnown = true,
|
||||
setReadMarker = true,
|
||||
lastKnown = null
|
||||
)
|
||||
withNetworkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
|
||||
withNetworkParams.putString(BundleKeys.KEY_ROOM_TOKEN, conversationModel.token)
|
||||
Log.d(TAG, "conversationModel.internalId: " + conversationModel.internalId)
|
||||
Log.d(TAG, "conversationModel.lastReadMessage:" + conversationModel.lastReadMessage)
|
||||
|
||||
sync(withNetworkParams)
|
||||
var newestMessageIdFromDb = chatDao.getNewestMessageId(internalConversationId)
|
||||
Log.d(TAG, "newestMessageIdFromDb: $newestMessageIdFromDb")
|
||||
|
||||
val newestMessageId = chatDao.getNewestMessageId(internalConversationId)
|
||||
Log.d(TAG, "newestMessageId after sync: $newestMessageId")
|
||||
val weAlreadyHaveSomeOfflineMessages = newestMessageIdFromDb > 0
|
||||
val weHaveAtLeastTheLastReadMessage = newestMessageIdFromDb >= conversationModel.lastReadMessage.toLong()
|
||||
Log.d(TAG, "weAlreadyHaveSomeOfflineMessages:$weAlreadyHaveSomeOfflineMessages")
|
||||
Log.d(TAG, "weHaveAtLeastTheLastReadMessage:$weHaveAtLeastTheLastReadMessage")
|
||||
|
||||
showLast100MessagesBeforeAndEqual(
|
||||
internalConversationId,
|
||||
chatDao.getNewestMessageId(internalConversationId)
|
||||
)
|
||||
if (weAlreadyHaveSomeOfflineMessages && weHaveAtLeastTheLastReadMessage) {
|
||||
Log.d(
|
||||
TAG,
|
||||
"Initial online request is skipped because offline messages are up to date" +
|
||||
" until lastReadMessage"
|
||||
)
|
||||
Log.d(TAG, "For messages newer than lastRead, lookIntoFuture will load them.")
|
||||
} else {
|
||||
if (!weAlreadyHaveSomeOfflineMessages) {
|
||||
Log.d(TAG, "An online request for newest 100 messages is made because offline chat is empty")
|
||||
} else {
|
||||
Log.d(
|
||||
TAG,
|
||||
"An online request for newest 100 messages is made because we don't have the lastReadMessage " +
|
||||
"(gaps could be closed by scrolling up to merge the chatblocks)"
|
||||
)
|
||||
}
|
||||
|
||||
// delay is a dirty workaround to make sure messages are added to adapter on initial load before dealing
|
||||
// with them (otherwise there is a race condition).
|
||||
delay(DELAY_TO_ENSURE_MESSAGES_ARE_ADDED)
|
||||
// set up field map to load the newest messages
|
||||
val fieldMap = getFieldMap(
|
||||
lookIntoFuture = false,
|
||||
includeLastKnown = true,
|
||||
setReadMarker = true,
|
||||
lastKnown = null
|
||||
)
|
||||
withNetworkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
|
||||
withNetworkParams.putString(BundleKeys.KEY_ROOM_TOKEN, conversationModel.token)
|
||||
|
||||
updateUiForLastCommonRead()
|
||||
updateUiForLastReadMessage(newestMessageId)
|
||||
Log.d(TAG, "Starting online request for initial loading")
|
||||
val chatMessageEntities = sync(withNetworkParams)
|
||||
if (chatMessageEntities == null) {
|
||||
Log.e(TAG, "initial loading of messages failed")
|
||||
}
|
||||
|
||||
initMessagePolling()
|
||||
newestMessageIdFromDb = chatDao.getNewestMessageId(internalConversationId)
|
||||
Log.d(TAG, "newestMessageIdFromDb after sync: $newestMessageIdFromDb")
|
||||
}
|
||||
|
||||
if (newestMessageIdFromDb.toInt() != 0) {
|
||||
val limit = getCappedMessagesAmountOfChatBlock(newestMessageIdFromDb)
|
||||
|
||||
showMessagesBeforeAndEqual(
|
||||
internalConversationId,
|
||||
newestMessageIdFromDb,
|
||||
limit
|
||||
)
|
||||
|
||||
// delay is a dirty workaround to make sure messages are added to adapter on initial load before dealing
|
||||
// with them (otherwise there is a race condition).
|
||||
delay(DELAY_TO_ENSURE_MESSAGES_ARE_ADDED)
|
||||
|
||||
updateUiForLastCommonRead()
|
||||
updateUiForLastReadMessage(newestMessageIdFromDb)
|
||||
}
|
||||
|
||||
initMessagePolling(newestMessageIdFromDb)
|
||||
}
|
||||
|
||||
private suspend fun getCappedMessagesAmountOfChatBlock(messageId: Long): Int {
|
||||
val chatBlock = getBlockOfMessage(messageId.toInt())
|
||||
|
||||
if (chatBlock != null) {
|
||||
val amountBetween = chatDao.getCountBetweenMessageIds(
|
||||
internalConversationId,
|
||||
messageId,
|
||||
chatBlock.oldestMessageId
|
||||
)
|
||||
|
||||
Log.d(TAG, "amount of messages between newestMessageId and oldest message of same ChatBlock:$amountBetween")
|
||||
val limit = if (amountBetween > DEFAULT_MESSAGES_LIMIT) {
|
||||
DEFAULT_MESSAGES_LIMIT
|
||||
} else {
|
||||
amountBetween
|
||||
}
|
||||
Log.d(TAG, "limit of messages to load for UI (max 100 to ensure performance is okay):$limit")
|
||||
return limit
|
||||
} else {
|
||||
Log.e(TAG, "No chat block found. Returning 0 as limit.")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun updateUiForLastReadMessage(newestMessageId: Long) {
|
||||
val scrollToLastRead = conversationModel.lastReadMessage.toLong() < newestMessageId
|
||||
if (scrollToLastRead) {
|
||||
|
@ -175,25 +238,25 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
val loadFromServer = hasToLoadPreviousMessagesFromServer(beforeMessageId)
|
||||
|
||||
if (loadFromServer) {
|
||||
Log.d(TAG, "Starting online request for loadMoreMessages")
|
||||
sync(withNetworkParams)
|
||||
}
|
||||
|
||||
showLast100MessagesBefore(internalConversationId, beforeMessageId)
|
||||
showMessagesBefore(internalConversationId, beforeMessageId, DEFAULT_MESSAGES_LIMIT)
|
||||
updateUiForLastCommonRead()
|
||||
}
|
||||
|
||||
override fun initMessagePolling(): Job =
|
||||
override fun initMessagePolling(initialMessageId: Long): Job =
|
||||
scope.launch {
|
||||
Log.d(TAG, "---- initMessagePolling ------------")
|
||||
|
||||
val initialMessageId = chatDao.getNewestMessageId(internalConversationId).toInt()
|
||||
Log.d(TAG, "newestMessage: $initialMessageId")
|
||||
|
||||
var fieldMap = getFieldMap(
|
||||
lookIntoFuture = true,
|
||||
includeLastKnown = false,
|
||||
setReadMarker = true,
|
||||
lastKnown = initialMessageId
|
||||
lastKnown = initialMessageId.toInt()
|
||||
)
|
||||
|
||||
val networkParams = Bundle()
|
||||
|
@ -205,6 +268,7 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
// sync database with server (This is a long blocking call because long polling (lookIntoFuture) is set)
|
||||
networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
|
||||
|
||||
Log.d(TAG, "Starting online request for long polling")
|
||||
val resultsFromSync = sync(networkParams)
|
||||
if (!resultsFromSync.isNullOrEmpty()) {
|
||||
val chatMessages = resultsFromSync.map(ChatMessageEntity::asModel)
|
||||
|
@ -240,15 +304,15 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
loadFromServer = false
|
||||
} else {
|
||||
// we know that beforeMessageId and blockForMessage.oldestMessageId are in the same block.
|
||||
// As we want the last 100 entries before beforeMessageId, we calculate if these messages are 100
|
||||
// entries apart from each other
|
||||
// As we want the last DEFAULT_MESSAGES_LIMIT entries before beforeMessageId, we calculate if these
|
||||
// messages are DEFAULT_MESSAGES_LIMIT entries apart from each other
|
||||
|
||||
val amountBetween = chatDao.getCountBetweenMessageIds(
|
||||
internalConversationId,
|
||||
beforeMessageId,
|
||||
blockForMessage.oldestMessageId
|
||||
)
|
||||
loadFromServer = amountBetween < 100
|
||||
loadFromServer = amountBetween < DEFAULT_MESSAGES_LIMIT
|
||||
|
||||
Log.d(
|
||||
TAG,
|
||||
|
@ -263,7 +327,8 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
lookIntoFuture: Boolean,
|
||||
includeLastKnown: Boolean,
|
||||
setReadMarker: Boolean,
|
||||
lastKnown: Int?
|
||||
lastKnown: Int?,
|
||||
limit: Int = DEFAULT_MESSAGES_LIMIT
|
||||
): HashMap<String, Int> {
|
||||
val fieldMap = HashMap<String, Int>()
|
||||
|
||||
|
@ -278,7 +343,7 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
}
|
||||
|
||||
fieldMap["timeout"] = if (lookIntoFuture) 30 else 0
|
||||
fieldMap["limit"] = 100
|
||||
fieldMap["limit"] = limit
|
||||
fieldMap["lookIntoFuture"] = if (lookIntoFuture) 1 else 0
|
||||
fieldMap["setReadMarker"] = if (setReadMarker) 1 else 0
|
||||
|
||||
|
@ -294,73 +359,84 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
lookIntoFuture = false,
|
||||
includeLastKnown = true,
|
||||
setReadMarker = false,
|
||||
lastKnown = messageId.toInt()
|
||||
lastKnown = messageId.toInt(),
|
||||
limit = 1
|
||||
)
|
||||
bundle.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
|
||||
|
||||
// Although only the single message will be returned, a server request will load 100 messages.
|
||||
// If this turns out to be confusion for debugging we could load set the limit to 1 for this request.
|
||||
Log.d(TAG, "Starting online request for single message (e.g. a reply)")
|
||||
sync(bundle)
|
||||
}
|
||||
return chatDao.getChatMessageForConversation(internalConversationId, messageId)
|
||||
.map(ChatMessageEntity::asModel)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@Suppress("UNCHECKED_CAST", "MagicNumber")
|
||||
private fun getMessagesFromServer(bundle: Bundle): Pair<Int, List<ChatMessageJson>>? {
|
||||
Log.d(TAG, "An online request is made!!!!!!!!!!!!!!!!!!!!")
|
||||
val fieldMap = bundle.getSerializable(BundleKeys.KEY_FIELD_MAP) as HashMap<String, Int>
|
||||
|
||||
try {
|
||||
val result = network.pullChatMessages(credentials, urlForChatting, fieldMap)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
// .timeout(3, TimeUnit.SECONDS)
|
||||
.map { it ->
|
||||
when (it.code()) {
|
||||
HTTP_CODE_OK -> {
|
||||
Log.d(TAG, "getMessagesFromServer HTTP_CODE_OK")
|
||||
newXChatLastCommonRead = it.headers()["X-Chat-Last-Common-Read"]?.let {
|
||||
Integer.parseInt(it)
|
||||
var attempts = 1
|
||||
while (attempts < 5) {
|
||||
Log.d(TAG, "message limit: " + fieldMap["limit"])
|
||||
try {
|
||||
val result = network.pullChatMessages(credentials, urlForChatting, fieldMap)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.map { it ->
|
||||
when (it.code()) {
|
||||
HTTP_CODE_OK -> {
|
||||
Log.d(TAG, "getMessagesFromServer HTTP_CODE_OK")
|
||||
newXChatLastCommonRead = it.headers()["X-Chat-Last-Common-Read"]?.let {
|
||||
Integer.parseInt(it)
|
||||
}
|
||||
|
||||
return@map Pair(
|
||||
HTTP_CODE_OK,
|
||||
(it.body() as ChatOverall).ocs!!.data!!
|
||||
)
|
||||
}
|
||||
|
||||
return@map Pair(
|
||||
HTTP_CODE_OK,
|
||||
(it.body() as ChatOverall).ocs!!.data!!
|
||||
)
|
||||
}
|
||||
HTTP_CODE_NOT_MODIFIED -> {
|
||||
Log.d(TAG, "getMessagesFromServer HTTP_CODE_NOT_MODIFIED")
|
||||
|
||||
HTTP_CODE_NOT_MODIFIED -> {
|
||||
Log.d(TAG, "getMessagesFromServer HTTP_CODE_NOT_MODIFIED")
|
||||
return@map Pair(
|
||||
HTTP_CODE_NOT_MODIFIED,
|
||||
listOf<ChatMessageJson>()
|
||||
)
|
||||
}
|
||||
|
||||
return@map Pair(
|
||||
HTTP_CODE_NOT_MODIFIED,
|
||||
listOf<ChatMessageJson>()
|
||||
)
|
||||
}
|
||||
HTTP_CODE_PRECONDITION_FAILED -> {
|
||||
Log.d(TAG, "getMessagesFromServer HTTP_CODE_PRECONDITION_FAILED")
|
||||
|
||||
HTTP_CODE_PRECONDITION_FAILED -> {
|
||||
Log.d(TAG, "getMessagesFromServer HTTP_CODE_PRECONDITION_FAILED")
|
||||
return@map Pair(
|
||||
HTTP_CODE_PRECONDITION_FAILED,
|
||||
listOf<ChatMessageJson>()
|
||||
)
|
||||
}
|
||||
|
||||
return@map Pair(
|
||||
HTTP_CODE_PRECONDITION_FAILED,
|
||||
listOf<ChatMessageJson>()
|
||||
)
|
||||
}
|
||||
|
||||
else -> {
|
||||
return@map Pair(
|
||||
HTTP_CODE_PRECONDITION_FAILED,
|
||||
listOf<ChatMessageJson>()
|
||||
)
|
||||
else -> {
|
||||
return@map Pair(
|
||||
HTTP_CODE_PRECONDITION_FAILED,
|
||||
listOf<ChatMessageJson>()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
.blockingSingle()
|
||||
return result
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Something went wrong when pulling chat messages (attempt: $attempts)", e)
|
||||
attempts++
|
||||
|
||||
val newMessageLimit = when (attempts) {
|
||||
2 -> 50
|
||||
3 -> 10
|
||||
else -> 5
|
||||
}
|
||||
.blockingSingle()
|
||||
return result
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Something went wrong when pulling chat messages", e)
|
||||
fieldMap["limit"] = newMessageLimit
|
||||
}
|
||||
}
|
||||
Log.e(TAG, "All attempts to get messages from server failed")
|
||||
return null
|
||||
}
|
||||
|
||||
|
@ -370,7 +446,12 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
return null
|
||||
}
|
||||
|
||||
val result = getMessagesFromServer(bundle) ?: return listOf()
|
||||
val result = getMessagesFromServer(bundle)
|
||||
if (result == null) {
|
||||
Log.d(TAG, "No result from server")
|
||||
return null
|
||||
}
|
||||
|
||||
var chatMessagesFromSync: List<ChatMessageEntity>? = null
|
||||
|
||||
val fieldMap = bundle.getSerializable(BundleKeys.KEY_FIELD_MAP) as HashMap<String, Int>
|
||||
|
@ -471,7 +552,7 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
ChatMessage.SystemMessageType.CLEARED_CHAT -> {
|
||||
// for lookIntoFuture just deleting everything would be fine.
|
||||
// But lets say we did not open the chat for a while and in between it was cleared.
|
||||
// We just load the last 100 messages but this don't contain the system message.
|
||||
// We just load the last messages but this don't contain the system message.
|
||||
// We scroll up and load the system message. Deleting everything is not an option as we
|
||||
// would loose the messages that we want to keep. We only want to
|
||||
// delete the messages and chatBlocks older than the system message.
|
||||
|
@ -488,13 +569,12 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
* 304 is returned when oldest message of chat was queried or when long polling request returned with no
|
||||
* modification. hasHistory is only set to false, when 304 was returned for the the oldest message
|
||||
*/
|
||||
private fun getHasHistory(statusCode: Int, lookIntoFuture: Boolean): Boolean {
|
||||
return if (statusCode == HTTP_CODE_NOT_MODIFIED) {
|
||||
private fun getHasHistory(statusCode: Int, lookIntoFuture: Boolean): Boolean =
|
||||
if (statusCode == HTTP_CODE_NOT_MODIFIED) {
|
||||
lookIntoFuture
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getBlockOfMessage(queriedMessageId: Int?): ChatBlockEntity? {
|
||||
var blockContainingQueriedMessage: ChatBlockEntity? = null
|
||||
|
@ -563,7 +643,7 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private suspend fun showLast100MessagesBeforeAndEqual(internalConversationId: String, messageId: Long) {
|
||||
private suspend fun showMessagesBeforeAndEqual(internalConversationId: String, messageId: Long, limit: Int) {
|
||||
suspend fun getMessagesBeforeAndEqual(
|
||||
messageId: Long,
|
||||
internalConversationId: String,
|
||||
|
@ -580,7 +660,7 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
val list = getMessagesBeforeAndEqual(
|
||||
messageId,
|
||||
internalConversationId,
|
||||
100
|
||||
limit
|
||||
)
|
||||
|
||||
if (list.isNotEmpty()) {
|
||||
|
@ -589,7 +669,7 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private suspend fun showLast100MessagesBefore(internalConversationId: String, messageId: Long) {
|
||||
private suspend fun showMessagesBefore(internalConversationId: String, messageId: Long, limit: Int) {
|
||||
suspend fun getMessagesBefore(
|
||||
messageId: Long,
|
||||
internalConversationId: String,
|
||||
|
@ -606,7 +686,7 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
val list = getMessagesBefore(
|
||||
messageId,
|
||||
internalConversationId,
|
||||
100
|
||||
limit
|
||||
)
|
||||
|
||||
if (list.isNotEmpty()) {
|
||||
|
@ -638,5 +718,6 @@ class OfflineFirstChatRepository @Inject constructor(
|
|||
private const val HTTP_CODE_PRECONDITION_FAILED = 412
|
||||
private const val HALF_SECOND = 500L
|
||||
private const val DELAY_TO_ENSURE_MESSAGES_ARE_ADDED: Long = 100
|
||||
private const val DEFAULT_MESSAGES_LIMIT = 100
|
||||
}
|
||||
}
|
||||
|
|
|
@ -225,7 +225,7 @@ class ChatViewModel @Inject constructor(
|
|||
|
||||
fun getRoom(user: User, token: String) {
|
||||
_getRoomViewState.value = GetRoomStartState
|
||||
conversationRepository.getConversationSettings(token)
|
||||
conversationRepository.getRoom(token)
|
||||
|
||||
// chatNetworkDataSource.getRoom(user, token)
|
||||
// .subscribeOn(Schedulers.io())
|
||||
|
|
|
@ -35,5 +35,5 @@ interface OfflineConversationsRepository {
|
|||
* Called once onStart to emit a conversation to [conversationFlow]
|
||||
* to be handled asynchronously.
|
||||
*/
|
||||
fun getConversationSettings(roomToken: String): Job
|
||||
fun getRoom(roomToken: String): Job
|
||||
}
|
||||
|
|
|
@ -56,17 +56,19 @@ class OfflineFirstConversationsRepository @Inject constructor(
|
|||
|
||||
override fun getRooms(): Job =
|
||||
scope.launch {
|
||||
val resultsFromSync = sync()
|
||||
if (!resultsFromSync.isNullOrEmpty()) {
|
||||
val conversations = resultsFromSync.map(ConversationEntity::asModel)
|
||||
_roomListFlow.emit(conversations)
|
||||
} else {
|
||||
val conversationsFromDb = getListOfConversations(user.id!!)
|
||||
_roomListFlow.emit(conversationsFromDb)
|
||||
val initialConversationModels = getListOfConversations(user.id!!)
|
||||
_roomListFlow.emit(initialConversationModels)
|
||||
|
||||
if (monitor.isOnline.first()) {
|
||||
val conversationEntitiesFromSync = getRoomsFromServer()
|
||||
if (!conversationEntitiesFromSync.isNullOrEmpty()) {
|
||||
val conversationModelsFromSync = conversationEntitiesFromSync.map(ConversationEntity::asModel)
|
||||
_roomListFlow.emit(conversationModelsFromSync)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getConversationSettings(roomToken: String): Job =
|
||||
override fun getRoom(roomToken: String): Job =
|
||||
scope.launch {
|
||||
val id = user.id!!
|
||||
val model = getConversation(id, roomToken)
|
||||
|
@ -100,7 +102,7 @@ class OfflineFirstConversationsRepository @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private suspend fun sync(): List<ConversationEntity>? {
|
||||
private suspend fun getRoomsFromServer(): List<ConversationEntity>? {
|
||||
var conversationsFromSync: List<ConversationEntity>? = null
|
||||
|
||||
if (!monitor.isOnline.first()) {
|
||||
|
@ -129,10 +131,12 @@ class OfflineFirstConversationsRepository @Inject constructor(
|
|||
}
|
||||
|
||||
private suspend fun deleteLeftConversations(conversationsFromSync: List<ConversationEntity>) {
|
||||
val conversationsFromSyncIds = conversationsFromSync.map { it.internalId }.toSet()
|
||||
val oldConversationsFromDb = dao.getConversationsForUser(user.id!!).first()
|
||||
|
||||
val conversationsToDelete = oldConversationsFromDb.filterNot { conversationsFromSync.contains(it) }
|
||||
val conversationIdsToDelete = conversationsToDelete.map { it.internalId }
|
||||
val conversationIdsToDelete = oldConversationsFromDb
|
||||
.map { it.internalId }
|
||||
.filterNot { it in conversationsFromSyncIds }
|
||||
|
||||
dao.deleteConversations(conversationIdsToDelete)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue