Commit be16d670 authored by Kiryl Vashyla's avatar Kiryl Vashyla

Chat history issues fx: on presenter and repository side

parent f15bca5b
......@@ -183,7 +183,8 @@ class ChatRoomPresenter @Inject constructor(
isBroadcast = chatIsBroadcast, isRoom = true
)
)
if (oldMessages.isNotEmpty()) {
val lastSyncDate = messagesRepository.getLastSyncDate()
if (oldMessages.isNotEmpty() && lastSyncDate != null) {
view.showMessages(oldMessages, clearDataSet)
loadMissingMessages()
} else {
......@@ -225,6 +226,15 @@ class ChatRoomPresenter @Inject constructor(
client.messages(chatRoomId, roomTypeOf(chatRoomType), offset, 30).result
}
messagesRepository.saveAll(messages)
//if success - saving last synced time
if (messages.isEmpty()) {
messagesRepository.saveLastSyncDate(System.currentTimeMillis())
} else {
//TODO sort
messagesRepository.saveLastSyncDate(messages.last().timestamp)
}
view.showMessages(
mapper.map(
messages,
......@@ -493,8 +503,14 @@ class ChatRoomPresenter @Inject constructor(
if (chatRoomId != null && chatRoomType != null) {
val roomType = roomTypeOf(chatRoomType!!)
messagesRepository.getByRoomId(chatRoomId!!)
//FIXME last message could not be sent
.sortedByDescending { it.timestamp }.firstOrNull()?.let { lastMessage ->
val instant = Instant.ofEpochMilli(lastMessage.timestamp).toString()
val lastSyncDate = messagesRepository.getLastSyncDate()
// 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
val instant = Instant.ofEpochMilli(lastSyncDate ?: 0).toString()
try {
val messages =
retryIO(description = "history($chatRoomId, $roomType, $instant)") {
......@@ -513,6 +529,9 @@ class ChatRoomPresenter @Inject constructor(
isRoom = true
))
messagesRepository.saveAll(messages.result)
//if success - saving last synced time
// TODO sort and peek the latest date
messagesRepository.saveLastSyncDate(messages.result.last().timestamp)
launchUI(strategy) {
view.showNewMessage(models, true)
......
......@@ -72,4 +72,8 @@ interface MessagesRepository {
suspend fun getAllUnsent(): List<Message>
suspend fun getUnsentByRoomId(roomId: String): List<Message>
suspend fun saveLastSyncDate(currentTimeMillis: Long)
suspend fun getLastSyncDate(): Long?
}
\ No newline at end of file
......@@ -7,6 +7,14 @@ import kotlinx.coroutines.experimental.withContext
class MemoryMessagesRepository : MessagesRepository {
private var lastSyncDate: Long? = null
override suspend fun saveLastSyncDate(it: Long) {
lastSyncDate = it
}
override suspend fun getLastSyncDate() = lastSyncDate
private val messages: HashMap<String, Message> = HashMap()
override suspend fun getById(id: String): Message? = withContext(CommonPool) {
......
......@@ -13,6 +13,28 @@ class SharedPreferencesMessagesRepository(
private val moshi: Moshi,
private val currentServerInteractor: GetCurrentServerInteractor
) : MessagesRepository {
private val KEY_LAST_SYNC_DATE = "KEY_LAST_SYNC_DATE"
override suspend fun saveLastSyncDate(currentTimeMillis: Long) {
withContext(CommonPool) {
currentServerInteractor.get()?.let {
prefs.edit().putLong(getSyncDateKey(it), currentTimeMillis).apply()
}
}
}
override suspend fun getLastSyncDate(): Long? = withContext(CommonPool) {
currentServerInteractor.get()?.also { server ->
if (!prefs.contains(getSyncDateKey(server)))
return@withContext null
//
val time = prefs.getLong(getSyncDateKey(server), -1)
return@withContext if (time == -1L) null else time
}
return@withContext null
}
private fun getSyncDateKey(it: String) = "${KEY_LAST_SYNC_DATE}_${it}"
override suspend fun getById(id: String): Message? = withContext(CommonPool) {
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