use one endpoint instead of two

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-11-14 09:20:28 +01:00 committed by Marcel Hibbe
parent 51330cbc76
commit e0b2174fc7
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
4 changed files with 50 additions and 104 deletions

View file

@ -329,13 +329,6 @@ public interface NcApi {
Observable<Void> unregisterDeviceForNotificationsWithProxy(@Url String url, Observable<Void> unregisterDeviceForNotificationsWithProxy(@Url String url,
@QueryMap Map<String, String> fields); @QueryMap Map<String, String> fields);
@FormUrlEncoded
@PUT
Observable<GenericOverall> setPassword2(@Header("Authorization") String authorization,
@Url String url,
@Field("password") String password);
@GET @GET
Observable<CapabilitiesOverall> getCapabilities(@Header("Authorization") String authorization, @Url String url); Observable<CapabilitiesOverall> getCapabilities(@Header("Authorization") String authorization, @Url String url);

View file

@ -65,23 +65,23 @@ class GuestAccessHelper(
val isChecked = binding.guestAccessView.allowGuestsSwitch.isChecked val isChecked = binding.guestAccessView.allowGuestsSwitch.isChecked
binding.guestAccessView.allowGuestsSwitch.isChecked = !isChecked binding.guestAccessView.allowGuestsSwitch.isChecked = !isChecked
viewModel.allowGuests(conversation.token, !isChecked) viewModel.allowGuests(conversation.token, !isChecked)
viewModel.allowGuestsViewState.observe(lifecycleOwner){uiState -> viewModel.allowGuestsViewState.observe(lifecycleOwner) { uiState ->
when(uiState){ when (uiState) {
is ConversationInfoViewModel.AllowGuestsUIState.Success ->{ is ConversationInfoViewModel.AllowGuestsUIState.Success -> {
if(uiState.allow){ binding.guestAccessView.allowGuestsSwitch.isChecked = uiState.allow
if (uiState.allow) {
showAllOptions() showAllOptions()
}else{ } else {
hideAllOptions() hideAllOptions()
} }
} }
is ConversationInfoViewModel.AllowGuestsUIState.Error ->{ is ConversationInfoViewModel.AllowGuestsUIState.Error -> {
val exception = uiState.message val exception = uiState.message
val message = context.getString(R.string.nc_guest_access_allow_failed) val message = context.getString(R.string.nc_guest_access_allow_failed)
Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show() Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show()
Log.e(TAG, exception) Log.e(TAG, exception)
} }
ConversationInfoViewModel.AllowGuestsUIState.None ->{ ConversationInfoViewModel.AllowGuestsUIState.None -> {
//unused atm
} }
} }
} }
@ -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) {
viewModel.setPassword("",conversation.token) viewModel.setPassword("", conversation.token)
passwordObserver(false,"") passwordObserver()
} else { } else {
showPasswordDialog() showPasswordDialog()
} }
@ -104,36 +104,23 @@ class GuestAccessHelper(
} }
} }
private fun passwordObserver(passwordSet:Boolean, password:String){ private fun passwordObserver() {
viewModel.passwordViewState.observe(lifecycleOwner){uiState -> viewModel.passwordViewState.observe(lifecycleOwner) { uiState ->
when(uiState){ when (uiState) {
is ConversationInfoViewModel.PasswordUiState.Success ->{ is ConversationInfoViewModel.PasswordUiState.Success -> {
val weakPassword = password.trim().length < 8 // unused atm
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 ->{ is ConversationInfoViewModel.PasswordUiState.Error -> {
val exception = uiState.message val exception = uiState.message
val message = context.getString(R.string.nc_guest_access_password_failed) val message = context.getString(R.string.nc_guest_access_password_failed)
Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show() Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show()
Log.e(TAG, exception) Log.e(TAG, exception)
} }
is ConversationInfoViewModel.PasswordUiState.None ->{ is ConversationInfoViewModel.PasswordUiState.None -> {
//unused atm // unused atm
} }
} }
} }
} }
private fun showPasswordDialog() { private fun showPasswordDialog() {
@ -145,14 +132,14 @@ 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()
viewModel.setPassword(password, conversation.token) viewModel.setPassword(password, conversation.token)
passwordObserver(true, password)
} }
setNegativeButton(R.string.nc_cancel) { _, _ -> setNegativeButton(R.string.nc_cancel) { _, _ ->
binding.guestAccessView.passwordProtectionSwitch.isChecked = false binding.guestAccessView.passwordProtectionSwitch.isChecked = false
} }
} }
createDialog(builder) createDialog(builder)
passwordObserver()
} }
private fun createDialog(builder: MaterialAlertDialogBuilder) { private fun createDialog(builder: MaterialAlertDialogBuilder) {
@ -204,37 +191,6 @@ class GuestAccessHelper(
binding.guestAccessView.resendInvitationsButton.visibility = View.GONE binding.guestAccessView.resendInvitationsButton.visibility = View.GONE
} }
inner class PasswordResultObserver(private val setPassword: Boolean) :
Observer<ConversationsRepository.PasswordResult> {
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 { companion object {
private val TAG = GuestAccessHelper::class.simpleName private val TAG = GuestAccessHelper::class.simpleName
} }

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
@ -245,35 +246,33 @@ class ConversationInfoViewModel @Inject constructor(
}) })
} }
fun allowGuests(token: String, allow: Boolean) {
fun allowGuests(token:String,allow:Boolean){ viewModelScope.launch {
viewModelScope.launch{ try {
try{ val allowGuestsResult = conversationsRepository.allowGuests(token, allow)
val allowGuestsResult = conversationsRepository.allowGuests(token,allow) 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(allow)
_allowGuestsViewState.value = AllowGuestsUIState.Success(allow) }
} } catch (exception: Exception) {
}catch(exception:Exception){ _allowGuestsViewState.value = AllowGuestsUIState.Error(exception.message ?: "")
_allowGuestsViewState.value = AllowGuestsUIState.Error(exception.message?: "") }
}
} }
} }
@SuppressLint("SuspiciousIndentation")
fun setPassword(password:String, token:String){ fun setPassword(password: String, token: String) {
viewModelScope.launch{ viewModelScope.launch {
try{ try {
val setPasswordResult = conversationsRepository.setPassword(password,token) val setPasswordResult = conversationsRepository.setPassword(password, token)
val statusCode: GenericMeta? = setPasswordResult.ocs?.meta val statusCode: GenericMeta? = setPasswordResult.ocs?.meta
val result = statusCode?.statusCode == STATUS_CODE_OK val result = statusCode?.statusCode == STATUS_CODE_OK
if(result){ if (result) {
_passwordViewState.value = PasswordUiState.Success(result) _passwordViewState.value = PasswordUiState.Success(result)
} }
}catch(exception:Exception){ } catch (exception: Exception) {
_passwordViewState.value = PasswordUiState.Error(exception.message?:"") _passwordViewState.value = PasswordUiState.Error(exception.message ?: "")
} }
} }
} }
@ -315,13 +314,13 @@ class ConversationInfoViewModel @Inject constructor(
sealed class AllowGuestsUIState { sealed class AllowGuestsUIState {
data object None : 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() data class Error(val message: String) : AllowGuestsUIState()
} }
sealed class PasswordUiState{ sealed class PasswordUiState {
data object None:PasswordUiState() data object None : PasswordUiState()
data class Success(val result:Boolean): PasswordUiState() data class Success(val result: Boolean) : PasswordUiState()
data class Error(val message:String): PasswordUiState() data class Error(val message: String) : PasswordUiState()
} }
} }

View file

@ -38,7 +38,7 @@ class ConversationsRepositoryImpl(
) )
val result: GenericOverall = if (allow) { val result: GenericOverall = if (allow) {
coroutineApi.makeRoomPublic( coroutineApi.makeRoomPublic(
credentials, credentials,
url url
) )
@ -51,7 +51,6 @@ class ConversationsRepositoryImpl(
return result return result
} }
override fun resendInvitations(token: String): Observable<ResendInvitationsResult> { override fun resendInvitations(token: String): Observable<ResendInvitationsResult> {
val apiObservable = api.resendParticipantInvitations( val apiObservable = api.resendParticipantInvitations(
credentials, credentials,
@ -80,7 +79,7 @@ class ConversationsRepositoryImpl(
} }
override suspend fun setPassword(password: String, token: String): GenericOverall { override suspend fun setPassword(password: String, token: String): GenericOverall {
val result = coroutineApi.setPassword2( val result = coroutineApi.setPassword(
credentials, credentials,
ApiUtils.getUrlForRoomPassword( ApiUtils.getUrlForRoomPassword(
apiVersion, apiVersion,
@ -98,6 +97,5 @@ class ConversationsRepositoryImpl(
companion object { companion object {
const val STATUS_CODE_OK = 200 const val STATUS_CODE_OK = 200
const val STATUS_CODE_BAD_REQUEST = 400
} }
} }