|
@@ -12,9 +12,11 @@ import android.util.Log
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
+import androidx.lifecycle.LifecycleOwner
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
|
import com.google.android.material.snackbar.Snackbar
|
|
|
import com.nextcloud.talk.R
|
|
|
+import com.nextcloud.talk.conversationinfo.viewmodel.ConversationInfoViewModel
|
|
|
import com.nextcloud.talk.data.user.model.User
|
|
|
import com.nextcloud.talk.databinding.ActivityConversationInfoBinding
|
|
|
import com.nextcloud.talk.databinding.DialogPasswordBinding
|
|
@@ -33,9 +35,10 @@ class GuestAccessHelper(
|
|
|
private val binding: ActivityConversationInfoBinding,
|
|
|
private val conversation: ConversationModel,
|
|
|
private val spreedCapabilities: SpreedCapability,
|
|
|
- private val conversationUser: User
|
|
|
+ private val conversationUser: User,
|
|
|
+ private val viewModel: ConversationInfoViewModel,
|
|
|
+ private val lifecycleOwner: LifecycleOwner
|
|
|
) {
|
|
|
-
|
|
|
private val conversationsRepository = activity.conversationsRepository
|
|
|
private val viewThemeUtils = activity.viewThemeUtils
|
|
|
private val context = activity.context
|
|
@@ -61,19 +64,35 @@ class GuestAccessHelper(
|
|
|
binding.guestAccessView.guestAccessSettingsAllowGuest.setOnClickListener {
|
|
|
val isChecked = binding.guestAccessView.allowGuestsSwitch.isChecked
|
|
|
binding.guestAccessView.allowGuestsSwitch.isChecked = !isChecked
|
|
|
- conversationsRepository.allowGuests(
|
|
|
- conversation.token!!,
|
|
|
- !isChecked
|
|
|
- ).subscribeOn(Schedulers.io())
|
|
|
- .observeOn(AndroidSchedulers.mainThread()).subscribe(AllowGuestsResultObserver())
|
|
|
+ viewModel.allowGuests(conversation.token, !isChecked)
|
|
|
+ viewModel.allowGuestsViewState.observe(lifecycleOwner) { uiState ->
|
|
|
+ when (uiState) {
|
|
|
+ is ConversationInfoViewModel.AllowGuestsUIState.Success -> {
|
|
|
+ binding.guestAccessView.allowGuestsSwitch.isChecked = uiState.allow
|
|
|
+ if (uiState.allow) {
|
|
|
+ showAllOptions()
|
|
|
+ } else {
|
|
|
+ hideAllOptions()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ is ConversationInfoViewModel.AllowGuestsUIState.Error -> {
|
|
|
+ val exception = uiState.exception
|
|
|
+ val message = context.getString(R.string.nc_guest_access_allow_failed)
|
|
|
+ Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show()
|
|
|
+ Log.e(TAG, message, exception)
|
|
|
+ }
|
|
|
+ ConversationInfoViewModel.AllowGuestsUIState.None -> {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
binding.guestAccessView.guestAccessSettingsPasswordProtection.setOnClickListener {
|
|
|
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()
|
|
|
} else {
|
|
|
showPasswordDialog()
|
|
|
}
|
|
@@ -85,6 +104,25 @@ class GuestAccessHelper(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private fun passwordObserver() {
|
|
|
+ viewModel.passwordViewState.observe(lifecycleOwner) { uiState ->
|
|
|
+ when (uiState) {
|
|
|
+ is ConversationInfoViewModel.PasswordUiState.Success -> {
|
|
|
+ // unused atm
|
|
|
+ }
|
|
|
+ is ConversationInfoViewModel.PasswordUiState.Error -> {
|
|
|
+ val exception = uiState.exception
|
|
|
+ val message = context.getString(R.string.nc_guest_access_password_failed)
|
|
|
+ Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show()
|
|
|
+ Log.e(TAG, message, exception)
|
|
|
+ }
|
|
|
+ is ConversationInfoViewModel.PasswordUiState.None -> {
|
|
|
+ // unused atm
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private fun showPasswordDialog() {
|
|
|
val builder = MaterialAlertDialogBuilder(activity)
|
|
|
builder.apply {
|
|
@@ -94,16 +132,14 @@ 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)
|
|
|
}
|
|
|
setNegativeButton(R.string.nc_cancel) { _, _ ->
|
|
|
binding.guestAccessView.passwordProtectionSwitch.isChecked = false
|
|
|
}
|
|
|
}
|
|
|
createDialog(builder)
|
|
|
+ passwordObserver()
|
|
|
}
|
|
|
|
|
|
private fun createDialog(builder: MaterialAlertDialogBuilder) {
|
|
@@ -143,32 +179,6 @@ class GuestAccessHelper(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- inner class AllowGuestsResultObserver : Observer<ConversationsRepository.AllowGuestsResult> {
|
|
|
-
|
|
|
- private lateinit var allowGuestsResult: ConversationsRepository.AllowGuestsResult
|
|
|
-
|
|
|
- override fun onNext(t: ConversationsRepository.AllowGuestsResult) {
|
|
|
- allowGuestsResult = t
|
|
|
- }
|
|
|
-
|
|
|
- override fun onError(e: Throwable) {
|
|
|
- val message = context.getString(R.string.nc_guest_access_allow_failed)
|
|
|
- Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show()
|
|
|
- Log.e(TAG, message, e)
|
|
|
- }
|
|
|
-
|
|
|
- override fun onComplete() {
|
|
|
- binding.guestAccessView.allowGuestsSwitch.isChecked = allowGuestsResult.allow
|
|
|
- if (allowGuestsResult.allow) {
|
|
|
- showAllOptions()
|
|
|
- } else {
|
|
|
- hideAllOptions()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- override fun onSubscribe(d: Disposable) = Unit
|
|
|
- }
|
|
|
-
|
|
|
private fun showAllOptions() {
|
|
|
binding.guestAccessView.guestAccessSettingsPasswordProtection.visibility = View.VISIBLE
|
|
|
if (conversationUser.capabilities?.spreedCapability?.features?.contains("sip-support") == true) {
|
|
@@ -181,37 +191,6 @@ class GuestAccessHelper(
|
|
|
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 {
|
|
|
private val TAG = GuestAccessHelper::class.simpleName
|
|
|
}
|