From 9525986dde2361ac979cba36d6404e0efb5b427d Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Fri, 15 Nov 2024 13:13:13 +0100 Subject: [PATCH] convert rxjava to coroutines - renameRoom Signed-off-by: sowjanyakch --- .../RenameConversationDialogFragment.kt | 24 ++++--- .../viewmodel/RenameConversationViewModel.kt | 70 ------------------- .../ConversationInfoEditActivity.kt | 69 ++++++++---------- .../ConversationInfoEditViewModel.kt | 29 ++++++++ .../talk/dagger/modules/RepositoryModule.kt | 3 +- .../talk/dagger/modules/ViewModelModule.kt | 6 -- 6 files changed, 73 insertions(+), 128 deletions(-) delete mode 100644 app/src/main/java/com/nextcloud/talk/conversation/viewmodel/RenameConversationViewModel.kt diff --git a/app/src/main/java/com/nextcloud/talk/conversation/RenameConversationDialogFragment.kt b/app/src/main/java/com/nextcloud/talk/conversation/RenameConversationDialogFragment.kt index 71ce7d09b..e5ddcdfa1 100644 --- a/app/src/main/java/com/nextcloud/talk/conversation/RenameConversationDialogFragment.kt +++ b/app/src/main/java/com/nextcloud/talk/conversation/RenameConversationDialogFragment.kt @@ -27,7 +27,7 @@ import com.google.android.material.snackbar.Snackbar import com.nextcloud.android.common.ui.theme.utils.ColorRole import com.nextcloud.talk.R import com.nextcloud.talk.application.NextcloudTalkApplication -import com.nextcloud.talk.conversation.viewmodel.RenameConversationViewModel +import com.nextcloud.talk.conversationinfoedit.viewmodel.ConversationInfoEditViewModel import com.nextcloud.talk.conversationlist.ConversationsListActivity import com.nextcloud.talk.databinding.DialogRenameConversationBinding import com.nextcloud.talk.events.ConversationsListFetchDataEvent @@ -49,7 +49,7 @@ class RenameConversationDialogFragment : DialogFragment() { lateinit var eventBus: EventBus private lateinit var binding: DialogRenameConversationBinding - private lateinit var viewModel: RenameConversationViewModel + private lateinit var viewModel: ConversationInfoEditViewModel private var emojiPopup: EmojiPopup? = null @@ -61,7 +61,7 @@ class RenameConversationDialogFragment : DialogFragment() { NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) - viewModel = ViewModelProvider(this, viewModelFactory)[RenameConversationViewModel::class.java] + viewModel = ViewModelProvider(this, viewModelFactory)[ConversationInfoEditViewModel::class.java] roomToken = arguments?.getString(KEY_ROOM_TOKEN)!! initialName = arguments?.getString(INITIAL_NAME)!! } @@ -102,7 +102,7 @@ class RenameConversationDialogFragment : DialogFragment() { val positiveButton = (dialog as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE) positiveButton.isEnabled = false positiveButton.setOnClickListener { - viewModel.renameConversation(roomToken, binding.textEdit.text.toString()) + viewModel.renameRoom(roomToken, binding.textEdit.text.toString()) } themeDialog() @@ -169,13 +169,17 @@ class RenameConversationDialogFragment : DialogFragment() { } private fun setupStateObserver() { - viewModel.viewState.observe(viewLifecycleOwner) { state -> + viewModel.renameRoomUiState.observe(viewLifecycleOwner) { state -> when (state) { - is RenameConversationViewModel.InitialState -> {} - is RenameConversationViewModel.RenamingState -> {} - is RenameConversationViewModel.RenamingSuccessState -> handleSuccess() - is RenameConversationViewModel.RenamingFailedState -> showError() - else -> {} + is ConversationInfoEditViewModel.RenameRoomUiState.None ->{ + + } + is ConversationInfoEditViewModel.RenameRoomUiState.Success ->{ + handleSuccess() + } + is ConversationInfoEditViewModel.RenameRoomUiState.Error ->{ + showError() + } } } } diff --git a/app/src/main/java/com/nextcloud/talk/conversation/viewmodel/RenameConversationViewModel.kt b/app/src/main/java/com/nextcloud/talk/conversation/viewmodel/RenameConversationViewModel.kt deleted file mode 100644 index c998f91f0..000000000 --- a/app/src/main/java/com/nextcloud/talk/conversation/viewmodel/RenameConversationViewModel.kt +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Nextcloud Talk - Android Client - * - * SPDX-FileCopyrightText: 2023 Marcel Hibbe - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package com.nextcloud.talk.conversation.viewmodel - -import android.util.Log -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.ViewModel -import com.nextcloud.talk.conversation.repository.ConversationRepository -import com.nextcloud.talk.models.json.generic.GenericOverall -import io.reactivex.Observer -import io.reactivex.android.schedulers.AndroidSchedulers -import io.reactivex.disposables.Disposable -import io.reactivex.schedulers.Schedulers -import javax.inject.Inject - -class RenameConversationViewModel @Inject constructor(private val repository: ConversationRepository) : ViewModel() { - - sealed class ViewState - object InitialState : ViewState() - object RenamingState : ViewState() - object RenamingSuccessState : ViewState() - object RenamingFailedState : ViewState() - - private val _viewState: MutableLiveData = MutableLiveData( - InitialState - ) - val viewState: LiveData - get() = _viewState - - fun renameConversation(roomToken: String, roomNameNew: String) { - _viewState.value = RenamingState - - repository.renameConversation( - roomToken, - roomNameNew - ) - .subscribeOn(Schedulers.io()) - ?.observeOn(AndroidSchedulers.mainThread()) - ?.subscribe(RenameConversationObserver()) - } - - inner class RenameConversationObserver : Observer { - - lateinit var genericOverall: GenericOverall - - override fun onSubscribe(d: Disposable) = Unit - - override fun onNext(response: GenericOverall) { - genericOverall = response - } - - override fun onError(e: Throwable) { - Log.e(TAG, "Failed to rename conversation", e) - _viewState.value = RenamingFailedState - } - - override fun onComplete() { - _viewState.value = RenamingSuccessState - } - } - - companion object { - private val TAG = RenameConversationViewModel::class.java.simpleName - } -} diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfoedit/ConversationInfoEditActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationinfoedit/ConversationInfoEditActivity.kt index 24dba4f13..61ac94263 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfoedit/ConversationInfoEditActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfoedit/ConversationInfoEditActivity.kt @@ -172,6 +172,29 @@ class ConversationInfoEditActivity : BaseActivity() { else -> {} } } + conversationInfoEditViewModel.renameRoomUiState.observe(this){uiState -> + when(uiState){ + is ConversationInfoEditViewModel.RenameRoomUiState.None ->{ + + } + is ConversationInfoEditViewModel.RenameRoomUiState.Success ->{ + if (CapabilitiesUtil.isConversationDescriptionEndpointAvailable(spreedCapabilities)) { + saveConversationDescription() + } else { + finish() + } + } + is ConversationInfoEditViewModel.RenameRoomUiState.Error ->{ + Snackbar.make( + binding.root, + context.getString(R.string.default_error_msg), + Snackbar.LENGTH_LONG + ).show() + Log.e(TAG, "Error while saving conversation name", uiState.exception) + } + } + } + } private fun setupAvatarOptions() { @@ -236,47 +259,11 @@ class ConversationInfoEditActivity : BaseActivity() { } private fun saveConversationNameAndDescription() { - val apiVersion = - ApiUtils.getConversationApiVersion(conversationUser, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1)) - - ncApi.renameRoom( - credentials, - ApiUtils.getUrlForRoom( - apiVersion, - conversationUser.baseUrl!!, - conversation!!.token - ), - binding.conversationName.text.toString() - ) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .retry(1) - .subscribe(object : Observer { - override fun onSubscribe(d: Disposable) { - // unused atm - } - - override fun onNext(genericOverall: GenericOverall) { - if (CapabilitiesUtil.isConversationDescriptionEndpointAvailable(spreedCapabilities)) { - saveConversationDescription() - } else { - finish() - } - } - - override fun onError(e: Throwable) { - Snackbar.make( - binding.root, - context.getString(R.string.default_error_msg), - Snackbar.LENGTH_LONG - ).show() - Log.e(TAG, "Error while saving conversation name", e) - } - - override fun onComplete() { - // unused atm - } - }) + val newRoomName = binding.conversationName.text.toString() + conversationInfoEditViewModel.renameRoom( + conversation!!.token, + newRoomName + ) } fun saveConversationDescription() { diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfoedit/viewmodel/ConversationInfoEditViewModel.kt b/app/src/main/java/com/nextcloud/talk/conversationinfoedit/viewmodel/ConversationInfoEditViewModel.kt index ff8af5955..a23df454f 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfoedit/viewmodel/ConversationInfoEditViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfoedit/viewmodel/ConversationInfoEditViewModel.kt @@ -10,14 +10,18 @@ import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.nextcloud.talk.chat.data.network.ChatNetworkDataSource import com.nextcloud.talk.conversationinfoedit.data.ConversationInfoEditRepository import com.nextcloud.talk.data.user.model.User import com.nextcloud.talk.models.domain.ConversationModel +import com.nextcloud.talk.models.json.generic.GenericMeta +import com.nextcloud.talk.repositories.conversations.ConversationsRepositoryImpl.Companion.STATUS_CODE_OK import io.reactivex.Observer import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.launch import java.io.File import javax.inject.Inject @@ -42,6 +46,10 @@ class ConversationInfoEditViewModel @Inject constructor( val viewState: LiveData get() = _viewState + private val _renameRoomUiState = MutableLiveData(RenameRoomUiState.None) + val renameRoomUiState:LiveData + get() = _renameRoomUiState + fun getRoom(user: User, token: String) { _viewState.value = GetRoomStartState repository.getRoom(user, token) @@ -64,6 +72,21 @@ class ConversationInfoEditViewModel @Inject constructor( ?.subscribe(DeleteConversationAvatarObserver()) } + fun renameRoom(roomToken:String, newRoomName:String){ + viewModelScope.launch{ + try{ + val renameRoomResult = conversationInfoEditRepository.renameConversation(roomToken, newRoomName) + val statusCode:GenericMeta? = renameRoomResult.ocs?.meta + val result = statusCode?.statusCode == STATUS_CODE_OK + if(result){ + _renameRoomUiState.value = RenameRoomUiState.Success(result) + } + }catch(exception:Exception){ + _renameRoomUiState.value = RenameRoomUiState.Error(exception) + } + } + } + inner class GetRoomObserver : Observer { override fun onSubscribe(d: Disposable) { // unused atm @@ -124,4 +147,10 @@ class ConversationInfoEditViewModel @Inject constructor( companion object { private val TAG = ConversationInfoEditViewModel::class.simpleName } + + sealed class RenameRoomUiState{ + data object None: RenameRoomUiState() + data class Success(val result:Boolean): RenameRoomUiState() + data class Error(val exception:Exception): RenameRoomUiState() + } } diff --git a/app/src/main/java/com/nextcloud/talk/dagger/modules/RepositoryModule.kt b/app/src/main/java/com/nextcloud/talk/dagger/modules/RepositoryModule.kt index 1926cbf0e..352988960 100644 --- a/app/src/main/java/com/nextcloud/talk/dagger/modules/RepositoryModule.kt +++ b/app/src/main/java/com/nextcloud/talk/dagger/modules/RepositoryModule.kt @@ -159,9 +159,10 @@ class RepositoryModule { @Provides fun provideConversationInfoEditRepository( ncApi: NcApi, + ncApiCoroutines: NcApiCoroutines, userProvider: CurrentUserProviderNew ): ConversationInfoEditRepository { - return ConversationInfoEditRepositoryImpl(ncApi, userProvider) + return ConversationInfoEditRepositoryImpl(ncApi, ncApiCoroutines,userProvider) } @Provides diff --git a/app/src/main/java/com/nextcloud/talk/dagger/modules/ViewModelModule.kt b/app/src/main/java/com/nextcloud/talk/dagger/modules/ViewModelModule.kt index f1ac935fe..e4c760050 100644 --- a/app/src/main/java/com/nextcloud/talk/dagger/modules/ViewModelModule.kt +++ b/app/src/main/java/com/nextcloud/talk/dagger/modules/ViewModelModule.kt @@ -13,7 +13,6 @@ import com.nextcloud.talk.chat.viewmodels.ChatViewModel import com.nextcloud.talk.contacts.ContactsViewModel import com.nextcloud.talk.chat.viewmodels.MessageInputViewModel import com.nextcloud.talk.conversation.viewmodel.ConversationViewModel -import com.nextcloud.talk.conversation.viewmodel.RenameConversationViewModel import com.nextcloud.talk.conversationcreation.ConversationCreationViewModel import com.nextcloud.talk.conversationinfo.viewmodel.ConversationInfoViewModel import com.nextcloud.talk.conversationinfoedit.viewmodel.ConversationInfoEditViewModel @@ -138,11 +137,6 @@ abstract class ViewModelModule { @ViewModelKey(ConversationInfoEditViewModel::class) abstract fun conversationInfoEditViewModel(viewModel: ConversationInfoEditViewModel): ViewModel - @Binds - @IntoMap - @ViewModelKey(RenameConversationViewModel::class) - abstract fun renameConversationViewModel(viewModel: RenameConversationViewModel): ViewModel - @Binds @IntoMap @ViewModelKey(ConversationViewModel::class)