convert rxjava to coroutines - renameRoom

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-11-15 13:13:13 +01:00
parent cedf1f09e9
commit 9525986dde
No known key found for this signature in database
GPG key ID: F7AA2A8B65B50220
6 changed files with 73 additions and 128 deletions

View file

@ -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()
}
}
}
}

View file

@ -1,70 +0,0 @@
/*
* Nextcloud Talk - Android Client
*
* SPDX-FileCopyrightText: 2023 Marcel Hibbe <dev@mhibbe.de>
* 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<ViewState> = MutableLiveData(
InitialState
)
val viewState: LiveData<ViewState>
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<GenericOverall> {
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
}
}

View file

@ -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()
val newRoomName = binding.conversationName.text.toString()
conversationInfoEditViewModel.renameRoom(
conversation!!.token,
newRoomName
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retry(1)
.subscribe(object : Observer<GenericOverall> {
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
}
})
}
fun saveConversationDescription() {

View file

@ -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<ViewState>
get() = _viewState
private val _renameRoomUiState = MutableLiveData<RenameRoomUiState>(RenameRoomUiState.None)
val renameRoomUiState:LiveData<RenameRoomUiState>
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<ConversationModel> {
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()
}
}

View file

@ -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

View file

@ -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)