Skip to content

Commit 0731cc7

Browse files
committed
refactor: run password update only after successful link edit
1 parent e818d2e commit 0731cc7

3 files changed

Lines changed: 105 additions & 64 deletions

File tree

owncloudApp/src/main/java/com/owncloud/android/presentation/spaces/links/AddPublicLinkFragment.kt

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,68 @@ class AddPublicLinkFragment: Fragment(), SetPasswordDialogFragment.SetPasswordLi
8080
secretFileDropPublicLinkRadioButton.tag = OCLinkType.CREATE_ONLY
8181
}
8282

83+
subscribeToViewModels()
84+
85+
if (editMode) { bindEditMode() }
86+
87+
binding.publicLinkPermissions.apply {
88+
canViewPublicLinkRadioButton.setOnClickListener { selectRadioButton(canViewPublicLinkRadioButton) }
89+
canViewPublicLinkLayout.setOnClickListener { selectRadioButton(canViewPublicLinkRadioButton) }
90+
canEditPublicLinkRadioButton.setOnClickListener { selectRadioButton(canEditPublicLinkRadioButton) }
91+
canEditPublicLinkLayout.setOnClickListener { selectRadioButton(canEditPublicLinkRadioButton) }
92+
secretFileDropPublicLinkRadioButton.setOnClickListener { selectRadioButton(secretFileDropPublicLinkRadioButton) }
93+
secretFileDropPublicLinkLayout.setOnClickListener { selectRadioButton(secretFileDropPublicLinkRadioButton) }
94+
}
95+
96+
binding.passwordLayout.apply {
97+
setPasswordButton.setOnClickListener {
98+
showPasswordDialog()
99+
}
100+
removePasswordButton.setOnClickListener {
101+
removePassword()
102+
}
103+
setPasswordSwitch.setOnClickListener {
104+
if (setPasswordSwitch.isChecked) showPasswordDialog() else removePassword()
105+
}
106+
}
107+
108+
binding.createPublicLinkButton.setOnClickListener {
109+
val displayName = binding.publicLinkNameEditText.text.toString().ifEmpty { getString(R.string.public_link_default_display_name) }
110+
if (editMode) {
111+
selectedPublicLink?.let { spaceLinksViewModel.editPublicLink(it.id, displayName) }
112+
} else {
113+
spaceLinksViewModel.createPublicLink(displayName)
114+
}
115+
}
116+
}
117+
118+
override fun onDestroyView() {
119+
super.onDestroyView()
120+
_binding = null
121+
}
122+
123+
override fun onCancelPassword() {
124+
if (!isPasswordEnforced && !hasPassword) {
125+
binding.passwordLayout.setPasswordSwitch.isChecked = false
126+
}
127+
}
128+
129+
override fun onSetPassword(password: String) {
130+
val normalizedPassword = password.ifBlank { null }
131+
val hasPassword = normalizedPassword != null
132+
if (!isPasswordEnforced && !hasPassword) {
133+
binding.passwordLayout.setPasswordSwitch.isChecked = false
134+
}
135+
spaceLinksViewModel.onPasswordSelected(normalizedPassword, hasPassword)
136+
}
137+
138+
private fun subscribeToViewModels() {
139+
observeAddPublicLinkUIState()
140+
observeAddLinkResult()
141+
observeEditLinkResult()
142+
}
143+
144+
private fun observeAddPublicLinkUIState() {
83145
collectLatestLifecycleFlow(spaceLinksViewModel.addPublicLinkUIState) { uiState ->
84146
uiState?.let {
85147
it.selectedExpirationDate?.let { expirationDate ->
@@ -125,7 +187,9 @@ class AddPublicLinkFragment: Fragment(), SetPasswordDialogFragment.SetPasswordLi
125187
}
126188
}
127189
}
190+
}
128191

192+
private fun observeAddLinkResult() {
129193
collectLatestLifecycleFlow(spaceLinksViewModel.addLinkResultFlow) { event ->
130194
event?.peekContent()?.let { uiResult ->
131195
when (uiResult) {
@@ -135,68 +199,23 @@ class AddPublicLinkFragment: Fragment(), SetPasswordDialogFragment.SetPasswordLi
135199
}
136200
}
137201
}
202+
}
138203

204+
private fun observeEditLinkResult() {
139205
collectLatestLifecycleFlow(spaceLinksViewModel.editLinkResultFlow) { event ->
140206
event?.peekContent()?.let { uiResult ->
141207
when (uiResult) {
142208
is UIResult.Loading -> { }
143-
is UIResult.Success -> parentFragmentManager.popBackStack()
144-
is UIResult.Error -> showErrorInSnackbar(R.string.public_link_edit_failed, uiResult.error)
209+
is UIResult.Success -> {
210+
selectedPublicLink?.let { spaceLinksViewModel.editPasswordPublicLink(it.id) }
211+
parentFragmentManager.popBackStack()
212+
}
213+
is UIResult.Error -> {
214+
showErrorInSnackbar(R.string.public_link_edit_failed, uiResult.error)
215+
}
145216
}
146217
}
147218
}
148-
149-
if (editMode) { bindEditMode() }
150-
151-
binding.publicLinkPermissions.apply {
152-
canViewPublicLinkRadioButton.setOnClickListener { selectRadioButton(canViewPublicLinkRadioButton) }
153-
canViewPublicLinkLayout.setOnClickListener { selectRadioButton(canViewPublicLinkRadioButton) }
154-
canEditPublicLinkRadioButton.setOnClickListener { selectRadioButton(canEditPublicLinkRadioButton) }
155-
canEditPublicLinkLayout.setOnClickListener { selectRadioButton(canEditPublicLinkRadioButton) }
156-
secretFileDropPublicLinkRadioButton.setOnClickListener { selectRadioButton(secretFileDropPublicLinkRadioButton) }
157-
secretFileDropPublicLinkLayout.setOnClickListener { selectRadioButton(secretFileDropPublicLinkRadioButton) }
158-
}
159-
160-
binding.passwordLayout.apply {
161-
setPasswordButton.setOnClickListener {
162-
showPasswordDialog()
163-
}
164-
removePasswordButton.setOnClickListener {
165-
removePassword()
166-
}
167-
setPasswordSwitch.setOnClickListener {
168-
if (setPasswordSwitch.isChecked) showPasswordDialog() else removePassword()
169-
}
170-
}
171-
172-
binding.createPublicLinkButton.setOnClickListener {
173-
val displayName = binding.publicLinkNameEditText.text.toString().ifEmpty { getString(R.string.public_link_default_display_name) }
174-
if (editMode) {
175-
selectedPublicLink?.let { spaceLinksViewModel.editPublicLink(it.id, displayName) }
176-
} else {
177-
spaceLinksViewModel.createPublicLink(displayName)
178-
}
179-
}
180-
}
181-
182-
override fun onDestroyView() {
183-
super.onDestroyView()
184-
_binding = null
185-
}
186-
187-
override fun onCancelPassword() {
188-
if (!isPasswordEnforced && !hasPassword) {
189-
binding.passwordLayout.setPasswordSwitch.isChecked = false
190-
}
191-
}
192-
193-
override fun onSetPassword(password: String) {
194-
val normalizedPassword = password.ifBlank { null }
195-
val hasPassword = normalizedPassword != null
196-
if (!isPasswordEnforced && !hasPassword) {
197-
binding.passwordLayout.setPasswordSwitch.isChecked = false
198-
}
199-
spaceLinksViewModel.onPasswordSelected(normalizedPassword, hasPassword)
200219
}
201220

202221
private fun selectRadioButton(selectedRadioButton: RadioButton) {

owncloudApp/src/main/java/com/owncloud/android/presentation/spaces/links/SpaceLinksViewModel.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class SpaceLinksViewModel(
6565
private val _editLinkResultFlow = MutableStateFlow<Event<UIResult<Unit>>?>(null)
6666
val editLinkResultFlow: StateFlow<Event<UIResult<Unit>>?> = _editLinkResultFlow
6767

68+
private val _editPasswordLinkResultFlow = MutableStateFlow<Event<UIResult<Unit>>?>(null)
69+
val editPasswordLinkResultFlow: StateFlow<Event<UIResult<Unit>>?> = _editPasswordLinkResultFlow
70+
6871
private var capabilities: OCCapability? = null
6972

7073
init {
@@ -132,16 +135,22 @@ class SpaceLinksViewModel(
132135
expirationDate = _addPublicLinkUIState.value?.selectedExpirationDate,
133136
)
134137
)
135-
if (_addPublicLinkUIState.value?.wasPasswordChanged == true) {
136-
viewModelScope.launch(coroutineDispatcherProvider.io) {
137-
editPasswordLinkUseCase(EditPasswordLinkUseCase.Params(
138-
accountName = accountName,
139-
spaceId = space.id,
140-
linkId = linkId,
141-
password = _addPublicLinkUIState.value?.selectedPassword
142-
))
143-
}
144-
}
138+
}
139+
}
140+
141+
fun editPasswordPublicLink(linkId: String) {
142+
if (_addPublicLinkUIState.value?.wasPasswordChanged == true) {
143+
runUseCaseWithResult(
144+
coroutineDispatcher = coroutineDispatcherProvider.io,
145+
flow = _editLinkResultFlow,
146+
useCase = editPasswordLinkUseCase,
147+
useCaseParams = EditPasswordLinkUseCase.Params(
148+
accountName = accountName,
149+
spaceId = space.id,
150+
linkId = linkId,
151+
password = _addPublicLinkUIState.value?.selectedPassword
152+
)
153+
)
145154
}
146155
}
147156

owncloudApp/src/main/java/com/owncloud/android/presentation/spaces/members/SpaceMembersFragment.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class SpaceMembersFragment : Fragment(), SpaceMembersAdapter.SpaceMembersAdapter
220220
observeAddLinkResult()
221221
observeRemoveLinkResult()
222222
observeEditLinkResult()
223+
observeEditPasswordLinkResult()
223224
}
224225

225226
private fun observeRoles() {
@@ -385,6 +386,18 @@ class SpaceMembersFragment : Fragment(), SpaceMembersAdapter.SpaceMembersAdapter
385386
}
386387
}
387388

389+
private fun observeEditPasswordLinkResult() {
390+
collectLatestLifecycleFlow(spaceLinksViewModel.editPasswordLinkResultFlow) { event ->
391+
event?.peekContent()?.let { uiResult ->
392+
when (uiResult) {
393+
is UIResult.Loading -> { }
394+
is UIResult.Success -> spaceLinksViewModel.resetViewModel()
395+
is UIResult.Error -> showErrorInSnackbar(R.string.public_link_edit_failed, uiResult.error)
396+
}
397+
}
398+
}
399+
}
400+
388401
private fun checkPermissions(spacePermissions: List<String>) {
389402
val hasCreatePermission = DRIVES_CREATE_PERMISSION in spacePermissions
390403
canRemoveMembersAndLinks = DRIVES_DELETE_PERMISSION in spacePermissions

0 commit comments

Comments
 (0)