From 7a8eb3ca22e995755adb92b7bfd93765e350f0ec Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Fri, 15 Nov 2024 14:33:08 +0100 Subject: [PATCH] convert rxjava to coroutines - clearChatHistory Signed-off-by: sowjanyakch --- .../com/nextcloud/talk/api/NcApiCoroutines.kt | 6 ++ .../ConversationInfoActivity.kt | 56 ++++++++----------- .../viewmodel/ConversationInfoViewModel.kt | 27 +++++++++ .../conversations/ConversationsRepository.kt | 2 + .../ConversationsRepositoryImpl.kt | 7 +++ 5 files changed, 66 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt index cee5671f6..8c94e9a9b 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt +++ b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt @@ -140,4 +140,10 @@ interface NcApiCoroutines { @Url url: String, @Field("password") password: String ): GenericOverall + + @DELETE + suspend fun clearChatHistory( + @Header("Authorization") authorization: String, + @Url url: String + ): GenericOverall } diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt index 9d40b112d..3a0593502 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt @@ -269,6 +269,29 @@ class ConversationInfoActivity : } } } + + viewModel.clearChatHistoryViewState.observe(this){uiState -> + when(uiState){ + is ConversationInfoViewModel.ClearChatHistoryViewState.None ->{ + + } + is ConversationInfoViewModel.ClearChatHistoryViewState.Success ->{ + Snackbar.make( + binding.root, + context.getString(R.string.nc_clear_history_success), + Snackbar.LENGTH_LONG + ).show() + + + } + is ConversationInfoViewModel.ClearChatHistoryViewState.Error ->{ + Snackbar.make(binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show() + Log.e(TAG, "failed to clear chat history", uiState.exception) + + } + } + + } } private fun setupActionBar() { @@ -670,7 +693,6 @@ class ConversationInfoActivity : intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) startActivity(intent) } - WorkInfo.State.FAILED -> { val errorType = workInfo.outputData.getString("error_type") if (errorType == LeaveConversationWorker.ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) { @@ -687,7 +709,6 @@ class ConversationInfoActivity : ).show() } } - else -> { } } @@ -725,36 +746,7 @@ class ConversationInfoActivity : } private fun clearHistory() { - val apiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1)) - - ncApi.clearChatHistory( - credentials, - ApiUtils.getUrlForChat(apiVersion, conversationUser.baseUrl!!, conversationToken) - ) - ?.subscribeOn(Schedulers.io()) - ?.observeOn(AndroidSchedulers.mainThread()) - ?.subscribe(object : Observer { - override fun onSubscribe(d: Disposable) { - // unused atm - } - - override fun onNext(genericOverall: GenericOverall) { - Snackbar.make( - binding.root, - context.getString(R.string.nc_clear_history_success), - Snackbar.LENGTH_LONG - ).show() - } - - override fun onError(e: Throwable) { - Snackbar.make(binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show() - Log.e(TAG, "failed to clear chat history", e) - } - - override fun onComplete() { - // unused atm - } - }) + viewModel.clearChatHistory(conversationToken) } private fun deleteConversation() { diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfo/viewmodel/ConversationInfoViewModel.kt b/app/src/main/java/com/nextcloud/talk/conversationinfo/viewmodel/ConversationInfoViewModel.kt index efad9cfc4..e746ca486 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/viewmodel/ConversationInfoViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/viewmodel/ConversationInfoViewModel.kt @@ -18,9 +18,11 @@ import com.nextcloud.talk.chat.data.network.ChatNetworkDataSource import com.nextcloud.talk.data.user.model.User import com.nextcloud.talk.models.domain.ConversationModel import com.nextcloud.talk.models.json.capabilities.SpreedCapability +import com.nextcloud.talk.models.json.generic.GenericMeta import com.nextcloud.talk.models.json.generic.GenericOverall import com.nextcloud.talk.models.json.participants.TalkBan import com.nextcloud.talk.repositories.conversations.ConversationsRepository +import com.nextcloud.talk.repositories.conversations.ConversationsRepositoryImpl.Companion.STATUS_CODE_OK import com.nextcloud.talk.utils.ApiUtils import io.reactivex.Observer import io.reactivex.android.schedulers.AndroidSchedulers @@ -110,6 +112,10 @@ class ConversationInfoViewModel @Inject constructor( val getCapabilitiesViewState: LiveData get() = _getCapabilitiesViewState + private val _clearChatHistoryViewState:MutableLiveData = MutableLiveData(ClearChatHistoryViewState.None) + val clearChatHistoryViewState:LiveData + get() = _clearChatHistoryViewState + fun getRoom(user: User, token: String) { _viewState.value = GetRoomStartState chatNetworkDataSource.getRoom(user, token) @@ -279,6 +285,21 @@ class ConversationInfoViewModel @Inject constructor( conversationsRepository.unarchiveConversation(user.getCredentials(), url) } + fun clearChatHistory(roomToken:String){ + viewModelScope.launch{ + try{ + val clearChatResult = conversationsRepository.clearChatHistory(roomToken) + val statusCode: GenericMeta? = clearChatResult.ocs?.meta + val result = statusCode?.statusCode == STATUS_CODE_OK + if (result) { + _clearChatHistoryViewState.value = ClearChatHistoryViewState.Success + } + }catch(exception:Exception){ + _clearChatHistoryViewState.value = ClearChatHistoryViewState.Error(exception) + } + } + } + inner class GetRoomObserver : Observer { override fun onSubscribe(d: Disposable) { // unused atm @@ -302,6 +323,12 @@ class ConversationInfoViewModel @Inject constructor( private val TAG = ConversationInfoViewModel::class.simpleName } + sealed class ClearChatHistoryViewState{ + data object None: ClearChatHistoryViewState() + data object Success:ClearChatHistoryViewState() + data class Error(val exception:Exception):ClearChatHistoryViewState() + } + sealed class AllowGuestsUIState { data object None : AllowGuestsUIState() data class Success(val allow: Boolean) : AllowGuestsUIState() diff --git a/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt b/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt index e4114bfaf..f20d0dbdf 100644 --- a/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt @@ -26,4 +26,6 @@ interface ConversationsRepository { suspend fun setPassword(password: String, token: String): GenericOverall fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable + + suspend fun clearChatHistory(roomToken:String): GenericOverall } diff --git a/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepositoryImpl.kt b/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepositoryImpl.kt index 254f0e4f7..fde1039f5 100644 --- a/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepositoryImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepositoryImpl.kt @@ -91,6 +91,13 @@ class ConversationsRepositoryImpl( return result } + override suspend fun clearChatHistory(roomToken:String): GenericOverall { + return coroutineApi.clearChatHistory( + credentials, + ApiUtils.getUrlForChat(apiVersion(), user.baseUrl!!, roomToken) + ) + } + private fun apiVersion(): Int { return ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4)) }