From 228c73ce609a25d4ac770848088fd81655b77339 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Thu, 14 Nov 2024 07:38:46 +0100 Subject: [PATCH] test coroutines Signed-off-by: sowjanyakch --- .../java/com/nextcloud/talk/api/NcApi.java | 2 +- .../com/nextcloud/talk/api/NcApiCoroutines.kt | 8 ++++ .../conversationinfo/GuestAccessHelper.kt | 44 ++++++++++++++++--- .../viewmodel/ConversationInfoViewModel.kt | 31 ++++++++++++- .../conversations/ConversationsRepository.kt | 4 +- .../ConversationsRepositoryImpl.kt | 38 ++++++---------- app/src/main/res/values/strings.xml | 1 + 7 files changed, 91 insertions(+), 37 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 af11d231f..165171fd4 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApi.java +++ b/app/src/main/java/com/nextcloud/talk/api/NcApi.java @@ -332,7 +332,7 @@ public interface NcApi { @FormUrlEncoded @PUT - Observable> setPassword2(@Header("Authorization") String authorization, + Observable setPassword2(@Header("Authorization") String authorization, @Url String url, @Field("password") String password); diff --git a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt index a84b2ca6d..cee5671f6 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt +++ b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt @@ -132,4 +132,12 @@ interface NcApiCoroutines { @Url url: String, @Body body: RequestBody ): GenericOverall + + @FormUrlEncoded + @PUT + suspend fun setPassword2( + @Header("Authorization") authorization: String, + @Url url: String, + @Field("password") password: String + ): GenericOverall } 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 414d2a089..cb1a91def 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/GuestAccessHelper.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/GuestAccessHelper.kt @@ -68,7 +68,7 @@ class GuestAccessHelper( viewModel.allowGuestsViewState.observe(lifecycleOwner){uiState -> when(uiState){ is ConversationInfoViewModel.AllowGuestsUIState.Success ->{ - if(uiState.result){ + if(uiState.allow){ showAllOptions() }else{ hideAllOptions() @@ -91,8 +91,8 @@ class GuestAccessHelper( val isChecked = binding.guestAccessView.passwordProtectionSwitch.isChecked binding.guestAccessView.passwordProtectionSwitch.isChecked = !isChecked if (isChecked) { - conversationsRepository.password("", conversation.token!!).subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()).subscribe(PasswordResultObserver(false)) + viewModel.setPassword("",conversation.token) + passwordObserver(false,"") } else { showPasswordDialog() } @@ -104,6 +104,38 @@ 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) + } + } + 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 + } + } + } + + } + private fun showPasswordDialog() { val builder = MaterialAlertDialogBuilder(activity) builder.apply { @@ -113,10 +145,8 @@ class GuestAccessHelper( setTitle(R.string.nc_guest_access_password_dialog_title) setPositiveButton(R.string.nc_ok) { _, _ -> val password = dialogPassword.password.text.toString() - conversationsRepository.password(password, conversation.token!!) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(PasswordResultObserver(true)) + viewModel.setPassword(password, conversation.token) + passwordObserver(true, password) } setNegativeButton(R.string.nc_cancel) { _, _ -> binding.guestAccessView.passwordProtectionSwitch.isChecked = false 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 aad7bcb57..4ba0d0160 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 @@ -103,6 +104,10 @@ class ConversationInfoViewModel @Inject constructor( val allowGuestsViewState: LiveData get() = _allowGuestsViewState + private val _passwordViewState = MutableLiveData(PasswordUiState.None) + val passwordViewState: LiveData + get() = _passwordViewState + private val _getCapabilitiesViewState: MutableLiveData = MutableLiveData(GetCapabilitiesStartState) val getCapabilitiesViewState: LiveData get() = _getCapabilitiesViewState @@ -249,7 +254,7 @@ class ConversationInfoViewModel @Inject constructor( val statusCode: GenericMeta? = allowGuestsResult.ocs?.meta val result = (statusCode?.statusCode == STATUS_CODE_OK) if (result) { - _allowGuestsViewState.value = AllowGuestsUIState.Success(result) + _allowGuestsViewState.value = AllowGuestsUIState.Success(allow) } }catch(exception:Exception){ _allowGuestsViewState.value = AllowGuestsUIState.Error(exception.message?: "") @@ -258,6 +263,22 @@ class ConversationInfoViewModel @Inject constructor( } } + + 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?:"") + } + } + } + suspend fun archiveConversation(user: User, token: String) { val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1)) val url = ApiUtils.getUrlForArchive(apiVersion, user.baseUrl, token) @@ -295,7 +316,13 @@ class ConversationInfoViewModel @Inject constructor( sealed class AllowGuestsUIState { data object None : AllowGuestsUIState() - data class Success(val result: 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() + } } diff --git a/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt b/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt index 4a858a62d..d4ca0f490 100644 --- a/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/repositories/conversations/ConversationsRepository.kt @@ -20,8 +20,6 @@ interface ConversationsRepository { val message: String ) - fun password(password: String, token: String): Observable - data class ResendInvitationsResult( val successful: Boolean ) @@ -31,5 +29,7 @@ interface ConversationsRepository { suspend fun unarchiveConversation(credentials: String, url: String): GenericOverall + suspend fun setPassword(password: String, token: String): GenericOverall + fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable } 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 4967f1051..db29492eb 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 @@ -7,13 +7,10 @@ */ package com.nextcloud.talk.repositories.conversations -import com.bluelinelabs.logansquare.LoganSquare import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.api.NcApiCoroutines import com.nextcloud.talk.data.user.model.User -import com.nextcloud.talk.models.json.conversations.password.PasswordOverall import com.nextcloud.talk.models.json.generic.GenericOverall -import com.nextcloud.talk.repositories.conversations.ConversationsRepository.PasswordResult import com.nextcloud.talk.repositories.conversations.ConversationsRepository.ResendInvitationsResult import com.nextcloud.talk.utils.ApiUtils import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew @@ -55,28 +52,6 @@ class ConversationsRepositoryImpl( } -override fun password(password: String, token: String): Observable { - val apiObservable = api.setPassword2( - credentials, - ApiUtils.getUrlForRoomPassword( - apiVersion(), - user.baseUrl!!, - token - ), - password - ) - return apiObservable.map { - val passwordPolicyMessage = if (it.code() == STATUS_CODE_BAD_REQUEST) { - LoganSquare.parse(it.errorBody()!!.string(), PasswordOverall::class.java).ocs!!.data!! - .message!! - } else { - "" - } - - PasswordResult(it.isSuccessful, passwordPolicyMessage.isNotEmpty(), passwordPolicyMessage) - } - } - override fun resendInvitations(token: String): Observable { val apiObservable = api.resendParticipantInvitations( credentials, @@ -104,6 +79,19 @@ override fun password(password: String, token: String): ObservableInvitations were sent out again. Invitations were not send due to an error. Share link + Password must be atleast 8 characters length Send message