Commit 88caa298 authored by Kiryl Vashyla's avatar Kiryl Vashyla

PR #1615 fix. Chat synced time associated with roomId

parent 79d91e89
...@@ -184,7 +184,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -184,7 +184,7 @@ class ChatRoomPresenter @Inject constructor(
isBroadcast = chatIsBroadcast, isRoom = true isBroadcast = chatIsBroadcast, isRoom = true
) )
) )
val lastSyncDate = messagesRepository.getLastSyncDate() val lastSyncDate = messagesRepository.getLastSyncDate(chatRoomId)
if (oldMessages.isNotEmpty() && lastSyncDate != null) { if (oldMessages.isNotEmpty() && lastSyncDate != null) {
view.showMessages(oldMessages, clearDataSet) view.showMessages(oldMessages, clearDataSet)
loadMissingMessages() loadMissingMessages()
...@@ -228,13 +228,16 @@ class ChatRoomPresenter @Inject constructor( ...@@ -228,13 +228,16 @@ class ChatRoomPresenter @Inject constructor(
} }
messagesRepository.saveAll(messages) messagesRepository.saveAll(messages)
//if success - saving last synced time //we are saving last sync date of latest synced chat room message
if (messages.isEmpty()) { if (offset == 0L) {
//chat history is empty - just saving current date //if success - saving last synced time
messagesRepository.saveLastSyncDate(System.currentTimeMillis()) if (messages.isEmpty()) {
} else { //chat history is empty - just saving current date
//assume that BE returns ordered messages, the first message is the latest one messagesRepository.saveLastSyncDate(chatRoomId, System.currentTimeMillis())
messagesRepository.saveLastSyncDate(messages.first().timestamp) } else {
//assume that BE returns ordered messages, the first message is the latest one
messagesRepository.saveLastSyncDate(chatRoomId, messages.first().timestamp)
}
} }
view.showMessages( view.showMessages(
...@@ -494,9 +497,9 @@ class ChatRoomPresenter @Inject constructor( ...@@ -494,9 +497,9 @@ class ChatRoomPresenter @Inject constructor(
private fun loadMissingMessages() { private fun loadMissingMessages() {
launch(parent = strategy.jobs) { launch(parent = strategy.jobs) {
if (chatRoomId != null && chatRoomType != null) { chatRoomId?.let { chatRoomId ->
val roomType = roomTypeOf(chatRoomType!!) val roomType = roomTypeOf(chatRoomType)
val lastSyncDate = messagesRepository.getLastSyncDate() val lastSyncDate = messagesRepository.getLastSyncDate(chatRoomId)
// lastSyncDate or 0. LastSyncDate could be in case when we sent some messages offline(and saved them locally), // lastSyncDate or 0. LastSyncDate could be in case when we sent some messages offline(and saved them locally),
// but never has obtained chatMessages(or history) from remote. In this case we should sync all chat history from beginning // but never has obtained chatMessages(or history) from remote. In this case we should sync all chat history from beginning
val instant = Instant.ofEpochMilli(lastSyncDate ?: 0).toString() val instant = Instant.ofEpochMilli(lastSyncDate ?: 0).toString()
...@@ -521,7 +524,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -521,7 +524,7 @@ class ChatRoomPresenter @Inject constructor(
messagesRepository.saveAll(messages.result) messagesRepository.saveAll(messages.result)
//if success - saving last synced time //if success - saving last synced time
//assume that BE returns ordered messages, the first message is the latest one //assume that BE returns ordered messages, the first message is the latest one
messagesRepository.saveLastSyncDate(messages.result.first().timestamp) messagesRepository.saveLastSyncDate(chatRoomId, messages.result.first().timestamp)
launchUI(strategy) { launchUI(strategy) {
view.showNewMessage(models, true) view.showNewMessage(models, true)
......
...@@ -73,7 +73,21 @@ interface MessagesRepository { ...@@ -73,7 +73,21 @@ interface MessagesRepository {
suspend fun getUnsentByRoomId(roomId: String): List<Message> suspend fun getUnsentByRoomId(roomId: String): List<Message>
suspend fun saveLastSyncDate(currentTimeMillis: Long) /**
* Save time of the latest room messages sync.
* Call this fun only when the latest messages list being received via /history or /messages network calls
*
* @param rid The id of the room the messages are.
* @param timeMillis time of room messages sync or the latest room message timestamp(which came with /history request)
*/
suspend fun saveLastSyncDate(rid: String, timeMillis: Long)
suspend fun getLastSyncDate(): Long? /**
* Get time when the room chat history has been loaded last time.
*
* @param rid The id of the room the messages are.
*
* @return Last Sync time or Null.
*/
suspend fun getLastSyncDate(rid: String): Long?
} }
\ No newline at end of file
...@@ -7,15 +7,15 @@ import kotlinx.coroutines.experimental.withContext ...@@ -7,15 +7,15 @@ import kotlinx.coroutines.experimental.withContext
class MemoryMessagesRepository : MessagesRepository { class MemoryMessagesRepository : MessagesRepository {
private var lastSyncDate: Long? = null private var lastSyncDates: HashMap<String, Long> = HashMap()
override suspend fun saveLastSyncDate(currentTimeMillis: Long) { private val messages: HashMap<String, Message> = HashMap()
lastSyncDate = currentTimeMillis
}
override suspend fun getLastSyncDate() = lastSyncDate override suspend fun saveLastSyncDate(rid: String, timeMillis: Long) {
lastSyncDates[rid] = timeMillis
}
private val messages: HashMap<String, Message> = HashMap() override suspend fun getLastSyncDate(rid: String) = lastSyncDates[rid]
override suspend fun getById(id: String): Message? = withContext(CommonPool) { override suspend fun getById(id: String): Message? = withContext(CommonPool) {
return@withContext messages[id] return@withContext messages[id]
......
...@@ -15,25 +15,25 @@ class SharedPreferencesMessagesRepository( ...@@ -15,25 +15,25 @@ class SharedPreferencesMessagesRepository(
) : MessagesRepository { ) : MessagesRepository {
private val KEY_LAST_SYNC_DATE = "KEY_LAST_SYNC_DATE" private val KEY_LAST_SYNC_DATE = "KEY_LAST_SYNC_DATE"
override suspend fun saveLastSyncDate(currentTimeMillis: Long) { override suspend fun saveLastSyncDate(rid: String, timeMillis: Long) {
withContext(CommonPool) { withContext(CommonPool) {
currentServerInteractor.get()?.let { currentServerInteractor.get()?.let {
prefs.edit().putLong(getSyncDateKey(it), currentTimeMillis).apply() prefs.edit().putLong(getSyncDateKey(it, rid), timeMillis).apply()
} }
} }
} }
override suspend fun getLastSyncDate(): Long? = withContext(CommonPool) { override suspend fun getLastSyncDate(rid: String): Long? = withContext(CommonPool) {
currentServerInteractor.get()?.also { server -> currentServerInteractor.get()?.also { server ->
if (!prefs.contains(getSyncDateKey(server))) if (!prefs.contains(getSyncDateKey(server, rid)))
return@withContext null return@withContext null
val time = prefs.getLong(getSyncDateKey(server), -1) val time = prefs.getLong(getSyncDateKey(server, rid), -1)
return@withContext if (time == -1L) null else time return@withContext if (time == -1L) null else time
} }
return@withContext null return@withContext null
} }
private fun getSyncDateKey(it: String) = "${KEY_LAST_SYNC_DATE}_${it}" private fun getSyncDateKey(server: String, rid: String) = "${KEY_LAST_SYNC_DATE}_$server $rid"
override suspend fun getById(id: String): Message? = withContext(CommonPool) { override suspend fun getById(id: String): Message? = withContext(CommonPool) {
currentServerInteractor.get()?.also { server -> currentServerInteractor.get()?.also { server ->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment