Browse Source

convert rxjava to coroutines - clearChatHistory

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
sowjanyakch 6 months ago
parent
commit
7a8eb3ca22

+ 6 - 0
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
 }

+ 24 - 32
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<GenericOverall> {
-                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() {

+ 27 - 0
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<ViewState>
         get() = _getCapabilitiesViewState
 
+    private val _clearChatHistoryViewState:MutableLiveData<ClearChatHistoryViewState> = MutableLiveData(ClearChatHistoryViewState.None)
+    val clearChatHistoryViewState:LiveData<ClearChatHistoryViewState>
+        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<ConversationModel> {
         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()

+ 2 - 0
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<GenericOverall>
+
+    suspend fun clearChatHistory(roomToken:String): GenericOverall
 }

+ 7 - 0
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))
     }