From e0b2174fc73e74b4193bdce169d11b8f4cbf968a Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Thu, 14 Nov 2024 09:20:28 +0100 Subject: [PATCH] use one endpoint instead of two Signed-off-by: sowjanyakch --- .../java/com/nextcloud/talk/api/NcApi.java | 7 -- .../conversationinfo/GuestAccessHelper.kt | 84 +++++-------------- .../viewmodel/ConversationInfoViewModel.kt | 57 +++++++------ .../ConversationsRepositoryImpl.kt | 6 +- 4 files changed, 50 insertions(+), 104 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/api/NcApi.java b/app/src/main/java/com/nextcloud/talk/api/NcApi.java index 165171fd4..2e39a926c 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApi.java +++ b/app/src/main/java/com/nextcloud/talk/api/NcApi.java @@ -329,13 +329,6 @@ public interface NcApi { Observable unregisterDeviceForNotificationsWithProxy(@Url String url, @QueryMap Map fields); - - @FormUrlEncoded - @PUT - Observable setPassword2(@Header("Authorization") String authorization, - @Url String url, - @Field("password") String password); - @GET Observable getCapabilities(@Header("Authorization") String authorization, @Url String url); diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfo/GuestAccessHelper.kt b/app/src/main/java/com/nextcloud/talk/conversationinfo/GuestAccessHelper.kt index cb1a91def..51ce20972 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/GuestAccessHelper.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/GuestAccessHelper.kt @@ -65,23 +65,23 @@ class GuestAccessHelper( val isChecked = binding.guestAccessView.allowGuestsSwitch.isChecked binding.guestAccessView.allowGuestsSwitch.isChecked = !isChecked viewModel.allowGuests(conversation.token, !isChecked) - viewModel.allowGuestsViewState.observe(lifecycleOwner){uiState -> - when(uiState){ - is ConversationInfoViewModel.AllowGuestsUIState.Success ->{ - if(uiState.allow){ + viewModel.allowGuestsViewState.observe(lifecycleOwner) { uiState -> + when (uiState) { + is ConversationInfoViewModel.AllowGuestsUIState.Success -> { + binding.guestAccessView.allowGuestsSwitch.isChecked = uiState.allow + if (uiState.allow) { showAllOptions() - }else{ + } else { hideAllOptions() } } - is ConversationInfoViewModel.AllowGuestsUIState.Error ->{ + is ConversationInfoViewModel.AllowGuestsUIState.Error -> { val exception = uiState.message val message = context.getString(R.string.nc_guest_access_allow_failed) Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show() Log.e(TAG, exception) } - ConversationInfoViewModel.AllowGuestsUIState.None ->{ - //unused atm + ConversationInfoViewModel.AllowGuestsUIState.None -> { } } } @@ -91,8 +91,8 @@ class GuestAccessHelper( val isChecked = binding.guestAccessView.passwordProtectionSwitch.isChecked binding.guestAccessView.passwordProtectionSwitch.isChecked = !isChecked if (isChecked) { - viewModel.setPassword("",conversation.token) - passwordObserver(false,"") + viewModel.setPassword("", conversation.token) + passwordObserver() } else { showPasswordDialog() } @@ -104,36 +104,23 @@ class GuestAccessHelper( } } - private fun passwordObserver(passwordSet:Boolean, password:String){ - viewModel.passwordViewState.observe(lifecycleOwner){uiState -> - when(uiState){ - is ConversationInfoViewModel.PasswordUiState.Success ->{ - val weakPassword = password.trim().length < 8 - binding.guestAccessView.passwordProtectionSwitch.isChecked = passwordSet && weakPassword - if (weakPassword && passwordSet) { - val builder = MaterialAlertDialogBuilder(activity) - builder.apply { - setTitle(R.string.nc_guest_access_password_weak_alert_title) - setMessage(R.string.nc_weak_password) - setPositiveButton("OK") { _, _ -> - } - } - createDialog(builder) - } + private fun passwordObserver() { + viewModel.passwordViewState.observe(lifecycleOwner) { uiState -> + when (uiState) { + is ConversationInfoViewModel.PasswordUiState.Success -> { + // unused atm } - is ConversationInfoViewModel.PasswordUiState.Error ->{ + is ConversationInfoViewModel.PasswordUiState.Error -> { val exception = uiState.message val message = context.getString(R.string.nc_guest_access_password_failed) Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show() Log.e(TAG, exception) - } - is ConversationInfoViewModel.PasswordUiState.None ->{ - //unused atm + is ConversationInfoViewModel.PasswordUiState.None -> { + // unused atm } } } - } private fun showPasswordDialog() { @@ -145,14 +132,14 @@ class GuestAccessHelper( setTitle(R.string.nc_guest_access_password_dialog_title) setPositiveButton(R.string.nc_ok) { _, _ -> val password = dialogPassword.password.text.toString() - viewModel.setPassword(password, conversation.token) - passwordObserver(true, password) + viewModel.setPassword(password, conversation.token) } setNegativeButton(R.string.nc_cancel) { _, _ -> binding.guestAccessView.passwordProtectionSwitch.isChecked = false } } createDialog(builder) + passwordObserver() } private fun createDialog(builder: MaterialAlertDialogBuilder) { @@ -204,37 +191,6 @@ class GuestAccessHelper( binding.guestAccessView.resendInvitationsButton.visibility = View.GONE } - inner class PasswordResultObserver(private val setPassword: Boolean) : - Observer { - - private lateinit var passwordResult: ConversationsRepository.PasswordResult - - override fun onSubscribe(d: Disposable) = Unit - - override fun onNext(t: ConversationsRepository.PasswordResult) { - passwordResult = t - } - - override fun onError(e: Throwable) { - val message = context.getString(R.string.nc_guest_access_password_failed) - Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show() - Log.e(TAG, message, e) - } - - override fun onComplete() { - binding.guestAccessView.passwordProtectionSwitch.isChecked = passwordResult.passwordSet && setPassword - if (passwordResult.passwordIsWeak) { - val builder = MaterialAlertDialogBuilder(activity) - builder.apply { - setTitle(R.string.nc_guest_access_password_weak_alert_title) - setMessage(passwordResult.message) - setPositiveButton("OK") { _, _ -> } - } - createDialog(builder) - } - } - } - companion object { private val TAG = GuestAccessHelper::class.simpleName } 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 3dcd8dd75..c281ba4c9 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 @@ -6,6 +6,7 @@ */ package com.nextcloud.talk.conversationinfo.viewmodel +import android.annotation.SuppressLint import android.util.Log import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner @@ -245,35 +246,33 @@ class ConversationInfoViewModel @Inject constructor( }) } - - fun allowGuests(token:String,allow:Boolean){ - viewModelScope.launch{ - try{ - val allowGuestsResult = conversationsRepository.allowGuests(token,allow) - val statusCode: GenericMeta? = allowGuestsResult.ocs?.meta - val result = statusCode?.statusCode == STATUS_CODE_OK - if (result) { - _allowGuestsViewState.value = AllowGuestsUIState.Success(allow) - } - }catch(exception:Exception){ - _allowGuestsViewState.value = AllowGuestsUIState.Error(exception.message?: "") - - } + fun allowGuests(token: String, allow: Boolean) { + viewModelScope.launch { + try { + val allowGuestsResult = conversationsRepository.allowGuests(token, allow) + val statusCode: GenericMeta? = allowGuestsResult.ocs?.meta + val result = statusCode?.statusCode == STATUS_CODE_OK + if (result) { + _allowGuestsViewState.value = AllowGuestsUIState.Success(allow) + } + } catch (exception: Exception) { + _allowGuestsViewState.value = AllowGuestsUIState.Error(exception.message ?: "") + } } } - - fun setPassword(password:String, token:String){ - viewModelScope.launch{ - try{ - val setPasswordResult = conversationsRepository.setPassword(password,token) + @SuppressLint("SuspiciousIndentation") + fun setPassword(password: String, token: String) { + viewModelScope.launch { + try { + val setPasswordResult = conversationsRepository.setPassword(password, token) val statusCode: GenericMeta? = setPasswordResult.ocs?.meta val result = statusCode?.statusCode == STATUS_CODE_OK - if(result){ - _passwordViewState.value = PasswordUiState.Success(result) - } - }catch(exception:Exception){ - _passwordViewState.value = PasswordUiState.Error(exception.message?:"") + if (result) { + _passwordViewState.value = PasswordUiState.Success(result) + } + } catch (exception: Exception) { + _passwordViewState.value = PasswordUiState.Error(exception.message ?: "") } } } @@ -315,13 +314,13 @@ class ConversationInfoViewModel @Inject constructor( sealed class AllowGuestsUIState { data object None : AllowGuestsUIState() - data class Success(val allow:Boolean) : AllowGuestsUIState() + data class Success(val allow: Boolean) : AllowGuestsUIState() data class Error(val message: String) : AllowGuestsUIState() } - sealed class PasswordUiState{ - data object None:PasswordUiState() - data class Success(val result:Boolean): PasswordUiState() - data class Error(val message:String): PasswordUiState() + sealed class PasswordUiState { + data object None : PasswordUiState() + data class Success(val result: Boolean) : PasswordUiState() + data class Error(val message: String) : PasswordUiState() } } 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 db29492eb..254f0e4f7 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 @@ -38,7 +38,7 @@ class ConversationsRepositoryImpl( ) val result: GenericOverall = if (allow) { - coroutineApi.makeRoomPublic( + coroutineApi.makeRoomPublic( credentials, url ) @@ -51,7 +51,6 @@ class ConversationsRepositoryImpl( return result } - override fun resendInvitations(token: String): Observable { val apiObservable = api.resendParticipantInvitations( credentials, @@ -80,7 +79,7 @@ class ConversationsRepositoryImpl( } override suspend fun setPassword(password: String, token: String): GenericOverall { - val result = coroutineApi.setPassword2( + val result = coroutineApi.setPassword( credentials, ApiUtils.getUrlForRoomPassword( apiVersion, @@ -98,6 +97,5 @@ class ConversationsRepositoryImpl( companion object { const val STATUS_CODE_OK = 200 - const val STATUS_CODE_BAD_REQUEST = 400 } }