test coroutines

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-11-14 07:38:46 +01:00 committed by Marcel Hibbe
parent 79dfac6ac0
commit 228c73ce60
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
7 changed files with 91 additions and 37 deletions

View file

@ -332,7 +332,7 @@ public interface NcApi {
@FormUrlEncoded @FormUrlEncoded
@PUT @PUT
Observable<Response<GenericOverall>> setPassword2(@Header("Authorization") String authorization, Observable<GenericOverall> setPassword2(@Header("Authorization") String authorization,
@Url String url, @Url String url,
@Field("password") String password); @Field("password") String password);

View file

@ -132,4 +132,12 @@ interface NcApiCoroutines {
@Url url: String, @Url url: String,
@Body body: RequestBody @Body body: RequestBody
): GenericOverall ): GenericOverall
@FormUrlEncoded
@PUT
suspend fun setPassword2(
@Header("Authorization") authorization: String,
@Url url: String,
@Field("password") password: String
): GenericOverall
} }

View file

@ -68,7 +68,7 @@ class GuestAccessHelper(
viewModel.allowGuestsViewState.observe(lifecycleOwner){uiState -> viewModel.allowGuestsViewState.observe(lifecycleOwner){uiState ->
when(uiState){ when(uiState){
is ConversationInfoViewModel.AllowGuestsUIState.Success ->{ is ConversationInfoViewModel.AllowGuestsUIState.Success ->{
if(uiState.result){ if(uiState.allow){
showAllOptions() showAllOptions()
}else{ }else{
hideAllOptions() hideAllOptions()
@ -91,8 +91,8 @@ class GuestAccessHelper(
val isChecked = binding.guestAccessView.passwordProtectionSwitch.isChecked val isChecked = binding.guestAccessView.passwordProtectionSwitch.isChecked
binding.guestAccessView.passwordProtectionSwitch.isChecked = !isChecked binding.guestAccessView.passwordProtectionSwitch.isChecked = !isChecked
if (isChecked) { if (isChecked) {
conversationsRepository.password("", conversation.token!!).subscribeOn(Schedulers.io()) viewModel.setPassword("",conversation.token)
.observeOn(AndroidSchedulers.mainThread()).subscribe(PasswordResultObserver(false)) passwordObserver(false,"")
} else { } else {
showPasswordDialog() 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() { private fun showPasswordDialog() {
val builder = MaterialAlertDialogBuilder(activity) val builder = MaterialAlertDialogBuilder(activity)
builder.apply { builder.apply {
@ -113,10 +145,8 @@ class GuestAccessHelper(
setTitle(R.string.nc_guest_access_password_dialog_title) setTitle(R.string.nc_guest_access_password_dialog_title)
setPositiveButton(R.string.nc_ok) { _, _ -> setPositiveButton(R.string.nc_ok) { _, _ ->
val password = dialogPassword.password.text.toString() val password = dialogPassword.password.text.toString()
conversationsRepository.password(password, conversation.token!!) viewModel.setPassword(password, conversation.token)
.subscribeOn(Schedulers.io()) passwordObserver(true, password)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(PasswordResultObserver(true))
} }
setNegativeButton(R.string.nc_cancel) { _, _ -> setNegativeButton(R.string.nc_cancel) { _, _ ->
binding.guestAccessView.passwordProtectionSwitch.isChecked = false binding.guestAccessView.passwordProtectionSwitch.isChecked = false

View file

@ -6,6 +6,7 @@
*/ */
package com.nextcloud.talk.conversationinfo.viewmodel package com.nextcloud.talk.conversationinfo.viewmodel
import android.annotation.SuppressLint
import android.util.Log import android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
@ -103,6 +104,10 @@ class ConversationInfoViewModel @Inject constructor(
val allowGuestsViewState: LiveData<AllowGuestsUIState> val allowGuestsViewState: LiveData<AllowGuestsUIState>
get() = _allowGuestsViewState get() = _allowGuestsViewState
private val _passwordViewState = MutableLiveData<PasswordUiState>(PasswordUiState.None)
val passwordViewState: LiveData<PasswordUiState>
get() = _passwordViewState
private val _getCapabilitiesViewState: MutableLiveData<ViewState> = MutableLiveData(GetCapabilitiesStartState) private val _getCapabilitiesViewState: MutableLiveData<ViewState> = MutableLiveData(GetCapabilitiesStartState)
val getCapabilitiesViewState: LiveData<ViewState> val getCapabilitiesViewState: LiveData<ViewState>
get() = _getCapabilitiesViewState get() = _getCapabilitiesViewState
@ -249,7 +254,7 @@ class ConversationInfoViewModel @Inject constructor(
val statusCode: GenericMeta? = allowGuestsResult.ocs?.meta val statusCode: GenericMeta? = allowGuestsResult.ocs?.meta
val result = (statusCode?.statusCode == STATUS_CODE_OK) val result = (statusCode?.statusCode == STATUS_CODE_OK)
if (result) { if (result) {
_allowGuestsViewState.value = AllowGuestsUIState.Success(result) _allowGuestsViewState.value = AllowGuestsUIState.Success(allow)
} }
}catch(exception:Exception){ }catch(exception:Exception){
_allowGuestsViewState.value = AllowGuestsUIState.Error(exception.message?: "") _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) { suspend fun archiveConversation(user: User, token: String) {
val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1)) val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1))
val url = ApiUtils.getUrlForArchive(apiVersion, user.baseUrl, token) val url = ApiUtils.getUrlForArchive(apiVersion, user.baseUrl, token)
@ -295,7 +316,13 @@ class ConversationInfoViewModel @Inject constructor(
sealed class AllowGuestsUIState { sealed class AllowGuestsUIState {
data object None : 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() 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()
}
} }

View file

@ -20,8 +20,6 @@ interface ConversationsRepository {
val message: String val message: String
) )
fun password(password: String, token: String): Observable<PasswordResult>
data class ResendInvitationsResult( data class ResendInvitationsResult(
val successful: Boolean val successful: Boolean
) )
@ -31,5 +29,7 @@ interface ConversationsRepository {
suspend fun unarchiveConversation(credentials: String, url: String): GenericOverall 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<GenericOverall> fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable<GenericOverall>
} }

View file

@ -7,13 +7,10 @@
*/ */
package com.nextcloud.talk.repositories.conversations package com.nextcloud.talk.repositories.conversations
import com.bluelinelabs.logansquare.LoganSquare
import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.api.NcApiCoroutines import com.nextcloud.talk.api.NcApiCoroutines
import com.nextcloud.talk.data.user.model.User 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.models.json.generic.GenericOverall
import com.nextcloud.talk.repositories.conversations.ConversationsRepository.PasswordResult
import com.nextcloud.talk.repositories.conversations.ConversationsRepository.ResendInvitationsResult import com.nextcloud.talk.repositories.conversations.ConversationsRepository.ResendInvitationsResult
import com.nextcloud.talk.utils.ApiUtils import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
@ -55,28 +52,6 @@ class ConversationsRepositoryImpl(
} }
override fun password(password: String, token: String): Observable<PasswordResult> {
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<ResendInvitationsResult> { override fun resendInvitations(token: String): Observable<ResendInvitationsResult> {
val apiObservable = api.resendParticipantInvitations( val apiObservable = api.resendParticipantInvitations(
credentials, credentials,
@ -104,6 +79,19 @@ override fun password(password: String, token: String): Observable<PasswordResul
return api.setConversationReadOnly(credentials, url, state) return api.setConversationReadOnly(credentials, url, state)
} }
override suspend fun setPassword(password: String, token: String): GenericOverall {
val result = coroutineApi.setPassword2(
credentials,
ApiUtils.getUrlForRoomPassword(
apiVersion,
user.baseUrl!!,
token
),
password
)
return result
}
private fun apiVersion(): Int { private fun apiVersion(): Int {
return ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4)) return ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4))
} }

View file

@ -455,6 +455,7 @@ How to translate with transifex:
<string name="nc_guest_access_resend_invitations_successful">Invitations were sent out again.</string> <string name="nc_guest_access_resend_invitations_successful">Invitations were sent out again.</string>
<string name="nc_guest_access_resend_invitations_failed">Invitations were not send due to an error.</string> <string name="nc_guest_access_resend_invitations_failed">Invitations were not send due to an error.</string>
<string name="nc_share_link">Share link</string> <string name="nc_share_link">Share link</string>
<string name="nc_weak_password">Password must be atleast 8 characters length</string>
<!-- Content descriptions --> <!-- Content descriptions -->
<string name="nc_description_send_message_button">Send message</string> <string name="nc_description_send_message_button">Send message</string>