convert rxjava to coroutines - clearChatHistory

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-11-15 14:33:08 +01:00
parent 6955995654
commit 7a8eb3ca22
No known key found for this signature in database
GPG key ID: F7AA2A8B65B50220
5 changed files with 66 additions and 32 deletions

View file

@ -140,4 +140,10 @@ interface NcApiCoroutines {
@Url url: String, @Url url: String,
@Field("password") password: String @Field("password") password: String
): GenericOverall ): GenericOverall
@DELETE
suspend fun clearChatHistory(
@Header("Authorization") authorization: String,
@Url url: String
): GenericOverall
} }

View file

@ -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() { private fun setupActionBar() {
@ -670,7 +693,6 @@ class ConversationInfoActivity :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent) startActivity(intent)
} }
WorkInfo.State.FAILED -> { WorkInfo.State.FAILED -> {
val errorType = workInfo.outputData.getString("error_type") val errorType = workInfo.outputData.getString("error_type")
if (errorType == LeaveConversationWorker.ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) { if (errorType == LeaveConversationWorker.ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) {
@ -687,7 +709,6 @@ class ConversationInfoActivity :
).show() ).show()
} }
} }
else -> { else -> {
} }
} }
@ -725,36 +746,7 @@ class ConversationInfoActivity :
} }
private fun clearHistory() { private fun clearHistory() {
val apiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1)) viewModel.clearChatHistory(conversationToken)
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
}
})
} }
private fun deleteConversation() { private fun deleteConversation() {

View file

@ -18,9 +18,11 @@ import com.nextcloud.talk.chat.data.network.ChatNetworkDataSource
import com.nextcloud.talk.data.user.model.User import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.domain.ConversationModel import com.nextcloud.talk.models.domain.ConversationModel
import com.nextcloud.talk.models.json.capabilities.SpreedCapability 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.generic.GenericOverall
import com.nextcloud.talk.models.json.participants.TalkBan import com.nextcloud.talk.models.json.participants.TalkBan
import com.nextcloud.talk.repositories.conversations.ConversationsRepository 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 com.nextcloud.talk.utils.ApiUtils
import io.reactivex.Observer import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
@ -110,6 +112,10 @@ class ConversationInfoViewModel @Inject constructor(
val getCapabilitiesViewState: LiveData<ViewState> val getCapabilitiesViewState: LiveData<ViewState>
get() = _getCapabilitiesViewState get() = _getCapabilitiesViewState
private val _clearChatHistoryViewState:MutableLiveData<ClearChatHistoryViewState> = MutableLiveData(ClearChatHistoryViewState.None)
val clearChatHistoryViewState:LiveData<ClearChatHistoryViewState>
get() = _clearChatHistoryViewState
fun getRoom(user: User, token: String) { fun getRoom(user: User, token: String) {
_viewState.value = GetRoomStartState _viewState.value = GetRoomStartState
chatNetworkDataSource.getRoom(user, token) chatNetworkDataSource.getRoom(user, token)
@ -279,6 +285,21 @@ class ConversationInfoViewModel @Inject constructor(
conversationsRepository.unarchiveConversation(user.getCredentials(), url) 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> { inner class GetRoomObserver : Observer<ConversationModel> {
override fun onSubscribe(d: Disposable) { override fun onSubscribe(d: Disposable) {
// unused atm // unused atm
@ -302,6 +323,12 @@ class ConversationInfoViewModel @Inject constructor(
private val TAG = ConversationInfoViewModel::class.simpleName 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 { sealed class AllowGuestsUIState {
data object None : AllowGuestsUIState() data object None : AllowGuestsUIState()
data class Success(val allow: Boolean) : AllowGuestsUIState() data class Success(val allow: Boolean) : AllowGuestsUIState()

View file

@ -26,4 +26,6 @@ interface ConversationsRepository {
suspend fun setPassword(password: String, token: String): GenericOverall suspend fun setPassword(password: String, token: String): GenericOverall
fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable<GenericOverall> fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable<GenericOverall>
suspend fun clearChatHistory(roomToken:String): GenericOverall
} }

View file

@ -91,6 +91,13 @@ class ConversationsRepositoryImpl(
return result return result
} }
override suspend fun clearChatHistory(roomToken:String): GenericOverall {
return coroutineApi.clearChatHistory(
credentials,
ApiUtils.getUrlForChat(apiVersion(), user.baseUrl!!, roomToken)
)
}
private fun apiVersion(): Int { private fun apiVersion(): Int {
return ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4)) return ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4))
} }