Skip to content

Commit

Permalink
"Delete All Messages" now works without breaking the initMessagePolling
Browse files Browse the repository at this point in the history
Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 committed Aug 29, 2024
1 parent 5440b84 commit 10e2d32
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class ChatActivity :

private val onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
chatViewModel.handleChatOnBackPress()
if (currentlyPlayedVoiceMessage != null) {
stopMediaPlayer(currentlyPlayedVoiceMessage!!)
}
Expand Down Expand Up @@ -2699,7 +2700,7 @@ class ChatActivity :
withUrl = urlForChatting,
withCredentials = credentials!!,
withMessageLimit = MESSAGE_PULL_LIMIT,
roomToken = currentConversation!!.token!!
roomToken = currentConversation!!.token
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ interface ChatMessageRepository : LifecycleAwareManager {
* Gets a individual message.
*/
suspend fun getMessage(messageId: Long, bundle: Bundle): Flow<ChatMessage>

/**
* Destroys unused resources.
*/
fun handleChatOnBackPress()
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down Expand Up @@ -184,8 +185,8 @@ class OfflineFirstChatRepository @Inject constructor(

val networkParams = Bundle()

while (!itIsPaused) {
if (!monitor.isOnline.first()) Thread.sleep(500)
while (true) {
if (!monitor.isOnline.first() || itIsPaused) Thread.sleep(500)

// sync database with server (This is a long blocking call because long polling (lookIntoFuture) is set)
networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
Expand Down Expand Up @@ -609,6 +610,10 @@ class OfflineFirstChatRepository @Inject constructor(
// unused atm
}

override fun handleChatOnBackPress() {
scope.cancel()
}

companion object {
val TAG = OfflineFirstChatRepository::class.simpleName
private const val HTTP_CODE_OK: Int = 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ class ChatViewModel @Inject constructor(
_getCapabilitiesViewState.value = GetCapabilitiesStartState
}

fun handleChatOnBackPress() {
chatRepository.handleChatOnBackPress()
}

suspend fun getMessageById(url: String, conversationModel: ConversationModel, messageId: Long): Flow<ChatMessage> =
flow {
val bundle = Bundle()
Expand All @@ -632,7 +636,7 @@ class ChatViewModel @Inject constructor(
BundleKeys.KEY_CREDENTIALS,
userProvider.currentUser.blockingGet().getCredentials()
)
bundle.putString(BundleKeys.KEY_ROOM_TOKEN, conversationModel.token!!)
bundle.putString(BundleKeys.KEY_ROOM_TOKEN, conversationModel.token)

val message = chatRepository.getMessage(messageId, bundle)
emit(message.first())
Expand Down

0 comments on commit 10e2d32

Please sign in to comment.