Skip to content

Commit

Permalink
fixed deletion bug, implemented Marcel's suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 committed Oct 4, 2024
1 parent 48ec227 commit ba0448d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ class MessageInputFragment : Fragment() {
}

viewLifecycleOwner.lifecycleScope.launch {
var wasOnline = true
networkMonitor.isOnline.onEach { isOnline ->
var wasOnline: Boolean // TODO maye redo this logic, seems it might be misfiring
networkMonitor.isOnline
.onEach { isOnline ->
wasOnline = !binding.fragmentConnectionLost.isShown
val connectionGained = (!wasOnline && isOnline)
wasOnline = !binding.fragmentMessageInputView.isShown
Log.d(TAG, "isOnline: $isOnline\nwasOnline: $wasOnline\nconnectionGained: $connectionGained")
// delay(2000)
handleMessageQueue(isOnline)
handleUI(isOnline, connectionGained)
}.collect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import java.lang.Thread.sleep
import java.util.concurrent.TimeUnit
import javax.inject.Inject

class OfflineFirstChatRepository @Inject constructor(
Expand Down Expand Up @@ -312,64 +310,55 @@ class OfflineFirstChatRepository @Inject constructor(
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>

var attempts = 0
while (attempts < 2) {
sleep(100)
try {
val result = network.pullChatMessages(credentials, urlForChatting, fieldMap)
.subscribeOn(Schedulers.io())
.timeout(5, TimeUnit.SECONDS)
.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!!
)
try {
val result = network.pullChatMessages(credentials, urlForChatting, fieldMap)
.subscribeOn(Schedulers.io())
.retry(2)
.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)
}

HTTP_CODE_NOT_MODIFIED -> {
Log.d(TAG, "getMessagesFromServer HTTP_CODE_NOT_MODIFIED")
return@map Pair(
HTTP_CODE_OK,
(it.body() as ChatOverall).ocs!!.data!!
)
}

return@map Pair(
HTTP_CODE_NOT_MODIFIED,
listOf<ChatMessageJson>()
)
}
HTTP_CODE_NOT_MODIFIED -> {
Log.d(TAG, "getMessagesFromServer HTTP_CODE_NOT_MODIFIED")

HTTP_CODE_PRECONDITION_FAILED -> {
Log.d(TAG, "getMessagesFromServer HTTP_CODE_PRECONDITION_FAILED")
return@map Pair(
HTTP_CODE_NOT_MODIFIED,
listOf<ChatMessageJson>()
)
}

return@map Pair(
HTTP_CODE_PRECONDITION_FAILED,
listOf<ChatMessageJson>()
)
}
HTTP_CODE_PRECONDITION_FAILED -> {
Log.d(TAG, "getMessagesFromServer HTTP_CODE_PRECONDITION_FAILED")

else -> {
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>()
)
}
}
.blockingSingle()
return result
} catch (e: Exception) {
Log.e(TAG, "attempt: $attempts Something went wrong when pulling chat messages", e)
attempts++
if (attempts == 2) {
// TODO show user message failed
}
}
.blockingSingle()
return result
} catch (e: Exception) {
Log.e(TAG, "Something went wrong when pulling chat messages", e)
}
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class MessageInputViewModel @Inject constructor(
if (isQueueing) {
val tempID = System.currentTimeMillis().toInt()
val qMsg = QueuedMessage(tempID, message, displayName, replyTo, sendWithoutNotification)
messageQueue = dataStore.getMessageQueue(roomToken)
messageQueue.add(qMsg)
dataStore.saveMessageQueue(roomToken, messageQueue)
_messageQueueSizeFlow.update { messageQueue.size }
Expand Down Expand Up @@ -257,7 +258,7 @@ class MessageInputViewModel @Inject constructor(
val queue = dataStore.getMessageQueue(roomToken)
dataStore.saveMessageQueue(roomToken, null) // empties the queue
while (queue.size > 0) {
val msg = queue.removeFirst()
val msg = queue.removeAt(0)
sendChatMessage(
roomToken,
credentials,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject
import javax.inject.Singleton
Expand Down Expand Up @@ -73,6 +74,7 @@ class NetworkMonitorImpl @Inject constructor(
}
}
}
.distinctUntilChanged()
.flowOn(Dispatchers.IO)
.conflate()

Expand Down

0 comments on commit ba0448d

Please sign in to comment.