Skip to content

Commit 6005b9d

Browse files
committed
feat: show trashbin icon when user has permission
Signed-off-by: Jorge Aguado Recio <jaguado@izertis.com>
1 parent 9c05c6b commit 6005b9d

5 files changed

Lines changed: 62 additions & 4 deletions

File tree

owncloudApp/src/main/java/com/owncloud/android/presentation/sharing/GraphShareFragment.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class GraphShareFragment : Fragment() {
5555

5656
private var roles: List<OCRole> = emptyList()
5757
private var listener: GraphShareFragmentListener? = null
58+
private var canRemoveShares: Boolean = false
5859

5960
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
6061
_binding = MembersFragmentBinding.inflate(inflater, container, false)
@@ -107,6 +108,7 @@ class GraphShareFragment : Fragment() {
107108
private fun subscribeToViewModels() {
108109
observeRoles()
109110
observeShares()
111+
observeSpacePermissions()
110112
observeAddShareResult()
111113
}
112114

@@ -139,7 +141,7 @@ class GraphShareFragment : Fragment() {
139141
val hasMembers = it.members.isNotEmpty()
140142
binding.membersRecyclerView.isVisible = hasMembers
141143
binding.noSharesMessage.isVisible = !hasMembers
142-
graphSharesAdapter.setShares(it.members, it.roles)
144+
graphSharesAdapter.setShares(it.members, it.roles, canRemoveShares)
143145
binding.swipeRefreshMembers.isRefreshing = false
144146
}
145147
}
@@ -154,6 +156,28 @@ class GraphShareFragment : Fragment() {
154156
}
155157
}
156158

159+
private fun observeSpacePermissions() {
160+
collectLatestLifecycleFlow(graphShareViewModel.spacePermissions) { event ->
161+
event?.let {
162+
when (val uiResult = event.peekContent()) {
163+
is UIResult.Success -> {
164+
uiResult.data?.let { spacePermissions ->
165+
checkPermissions(spacePermissions)
166+
}
167+
}
168+
is UIResult.Loading -> { }
169+
is UIResult.Error -> {
170+
Timber.e(uiResult.error, "Failed to retrieve space permissions")
171+
}
172+
}
173+
}
174+
}
175+
}
176+
177+
private fun checkPermissions(spacePermissions: List<String>) {
178+
canRemoveShares = DRIVES_DELETE_PERMISSION in spacePermissions
179+
}
180+
157181
private fun observeAddShareResult() {
158182
collectLatestLifecycleFlow(graphShareViewModel.addShareResultFlow) { event ->
159183
event?.peekContent()?.let { uiResult ->
@@ -176,6 +200,7 @@ class GraphShareFragment : Fragment() {
176200
companion object {
177201
private const val ARG_FILE = "FILE"
178202
private const val ARG_ACCOUNT_NAME = "ACCOUNT_NAME"
203+
private const val DRIVES_DELETE_PERMISSION = "libre.graph/driveItem/permissions/delete"
179204

180205
fun newInstance(file: OCFile, accountName: String): GraphShareFragment {
181206
val args = Bundle().apply {

owncloudApp/src/main/java/com/owncloud/android/presentation/sharing/GraphShareViewModel.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import com.owncloud.android.domain.sharing.shares.usecases.AddGraphShareAsyncUse
3535
import com.owncloud.android.domain.sharing.shares.usecases.GetGraphSharesAsyncUseCase
3636
import com.owncloud.android.domain.sharing.shares.model.OCPermissions
3737
import com.owncloud.android.domain.user.usecases.GetUserIdAsyncUseCase
38+
import com.owncloud.android.domain.spaces.usecases.GetSpacePermissionsAsyncUseCase
3839
import com.owncloud.android.domain.utils.Event
3940
import com.owncloud.android.extensions.ViewModelExt.runUseCaseWithResult
4041
import com.owncloud.android.presentation.common.UIResult
@@ -55,6 +56,7 @@ class GraphShareViewModel(
5556
private val getStoredCapabilitiesUseCase: GetStoredCapabilitiesUseCase,
5657
private val searchMembersUseCase: SearchMembersUseCase,
5758
private val getUserIdAsyncUseCase: GetUserIdAsyncUseCase,
59+
private val getSpacePermissionsAsyncUseCase: GetSpacePermissionsAsyncUseCase,
5860
private val accountName: String,
5961
private val file: OCFile,
6062
private val coroutineDispatcherProvider: CoroutinesDispatcherProvider,
@@ -81,6 +83,9 @@ class GraphShareViewModel(
8183
private var searchJob: Job? = null
8284
var capabilities: OCCapability? = null
8385

86+
private val _spacePermissions = MutableStateFlow<Event<UIResult<List<String>>>?>(null)
87+
val spacePermissions: StateFlow<Event<UIResult<List<String>>>?> = _spacePermissions
88+
8489
init {
8590
runUseCaseWithResult(
8691
coroutineDispatcher = coroutineDispatcherProvider.io,
@@ -98,6 +103,24 @@ class GraphShareViewModel(
98103
viewModelScope.launch(coroutineDispatcherProvider.io) {
99104
capabilities = getStoredCapabilitiesUseCase(GetStoredCapabilitiesUseCase.Params(accountName))
100105
}
106+
getSpacePermissions()
107+
}
108+
109+
fun getSpacePermissions() {
110+
val spaceId = file.spaceId
111+
if (spaceId == null) {
112+
_spacePermissions.update { Event(UIResult.Error(error = IncompleteFileDataException())) }
113+
return
114+
}
115+
116+
runUseCaseWithResult(
117+
coroutineDispatcher = coroutineDispatcherProvider.io,
118+
flow = _spacePermissions,
119+
useCase = getSpacePermissionsAsyncUseCase,
120+
useCaseParams = GetSpacePermissionsAsyncUseCase.Params(accountName = accountName, spaceId = spaceId),
121+
showLoading = false,
122+
requiresConnection = true
123+
)
101124
}
102125

103126
fun getGraphShares() {

owncloudApp/src/main/java/com/owncloud/android/presentation/sharing/GraphSharesAdapter.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class GraphSharesAdapter : RecyclerView.Adapter<GraphSharesAdapter.GraphShareVie
3737

3838
private var shares: List<MemberPermission> = emptyList()
3939
private var rolesMap: Map<String, String> = emptyMap()
40+
private var canRemoveShares = false
4041

4142
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GraphShareViewHolder {
4243
val inflater = LayoutInflater.from(parent.context)
@@ -58,6 +59,11 @@ class GraphSharesAdapter : RecyclerView.Adapter<GraphSharesAdapter.GraphShareVie
5859
)
5960
memberRole.text = roleNames.joinToString(", ")
6061

62+
removeMemberButton.apply {
63+
contentDescription = holder.itemView.context.getString(R.string.content_description_remove_share_button, share.displayName)
64+
isVisible = canRemoveShares
65+
}
66+
6167
val hasExpirationDate = share.expirationDateTime != null
6268
expirationCalendarIcon.isVisible = hasExpirationDate
6369
expirationDate.isVisible = hasExpirationDate
@@ -71,13 +77,15 @@ class GraphSharesAdapter : RecyclerView.Adapter<GraphSharesAdapter.GraphShareVie
7177

7278
override fun getItemCount(): Int = shares.size
7379

74-
fun setShares(shares: List<MemberPermission>, roles: List<OCRole>) {
80+
fun setShares(shares: List<MemberPermission>, roles: List<OCRole>, canRemoveShares: Boolean) {
81+
val hasUserPermissionsChanged = this.canRemoveShares != canRemoveShares
82+
this.canRemoveShares = canRemoveShares
7583
this.rolesMap = roles.associate { it.id to it.displayName }
7684
val sortedShares = shares.sortedWith(
7785
compareBy<MemberPermission> { it.isGroup }
7886
.thenBy { it.displayName.lowercase() }
7987
)
80-
val diffResult = DiffUtil.calculateDiff(GraphSharesDiffUtil(this.shares, sortedShares))
88+
val diffResult = DiffUtil.calculateDiff(GraphSharesDiffUtil(this.shares, sortedShares, hasUserPermissionsChanged))
8189
this.shares = sortedShares
8290
diffResult.dispatchUpdatesTo(this)
8391
}

owncloudApp/src/main/java/com/owncloud/android/presentation/sharing/GraphSharesDiffUtil.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.owncloud.android.domain.sharing.shares.model.MemberPermission
2626
class GraphSharesDiffUtil(
2727
private val oldList: List<MemberPermission>,
2828
private val newList: List<MemberPermission>,
29+
private val hasUserPermissionsChanged: Boolean = false,
2930
) : DiffUtil.Callback() {
3031

3132
override fun getOldListSize(): Int = oldList.size
@@ -36,5 +37,5 @@ class GraphSharesDiffUtil(
3637
oldList[oldItemPosition].id == newList[newItemPosition].id
3738

3839
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
39-
oldList[oldItemPosition] == newList[newItemPosition]
40+
oldList[oldItemPosition] == newList[newItemPosition] && !hasUserPermissionsChanged
4041
}

owncloudApp/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@
729729
<string name="content_description_remove_password_button">Remove password</string>
730730
<string name="content_description_generate_password_button">Generate password</string>
731731
<string name="content_description_copy_password_button">Copy password</string>
732+
<string name="content_description_remove_share_button">Remove share %1$s</string>
732733

733734
<string name="create_shortcut_dialog_title">Create a shortcut</string>
734735
<string name="create_shortcut_dialog_url">URL</string>

0 commit comments

Comments
 (0)