|
| 1 | +/** |
| 2 | + * ownCloud Android client application |
| 3 | + * |
| 4 | + * @author Jorge Aguado Recio |
| 5 | + * |
| 6 | + * Copyright (C) 2026 ownCloud GmbH. |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License version 2, |
| 10 | + * as published by the Free Software Foundation. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.owncloud.android.presentation.sharing |
| 22 | + |
| 23 | +import android.view.LayoutInflater |
| 24 | +import android.view.View |
| 25 | +import android.view.ViewGroup |
| 26 | +import androidx.core.view.isVisible |
| 27 | +import androidx.recyclerview.widget.DiffUtil |
| 28 | +import androidx.recyclerview.widget.RecyclerView |
| 29 | +import com.owncloud.android.R |
| 30 | +import com.owncloud.android.databinding.MemberItemBinding |
| 31 | +import com.owncloud.android.domain.sharing.shares.model.OCShare |
| 32 | +import com.owncloud.android.domain.sharing.shares.model.ShareType |
| 33 | +import com.owncloud.android.utils.DisplayUtils |
| 34 | +import com.owncloud.android.utils.PreferenceUtils |
| 35 | + |
| 36 | +class OcisSharesAdapter( |
| 37 | + private val listener: OcisSharesAdapterListener, |
| 38 | +) : RecyclerView.Adapter<OcisSharesAdapter.OcisShareViewHolder>() { |
| 39 | + |
| 40 | + private var shares: List<OCShare> = emptyList() |
| 41 | + private var canRemoveShares = false |
| 42 | + private var canEditShares = false |
| 43 | + |
| 44 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OcisShareViewHolder { |
| 45 | + val inflater = LayoutInflater.from(parent.context) |
| 46 | + val view = inflater.inflate(R.layout.member_item, parent, false) |
| 47 | + view.filterTouchesWhenObscured = PreferenceUtils.shouldDisallowTouchesWithOtherVisibleWindows(parent.context) |
| 48 | + return OcisShareViewHolder(view) |
| 49 | + } |
| 50 | + |
| 51 | + override fun onBindViewHolder(holder: OcisShareViewHolder, position: Int) { |
| 52 | + val share = shares[position] |
| 53 | + |
| 54 | + holder.binding.apply { |
| 55 | + val isGroup = share.shareType == ShareType.GROUP |
| 56 | + memberIcon.setImageResource(if (isGroup) R.drawable.ic_group else R.drawable.ic_user) |
| 57 | + memberName.text = share.sharedWithDisplayName ?: share.shareWith.orEmpty() |
| 58 | + memberName.contentDescription = holder.itemView.context.getString( |
| 59 | + if (isGroup) R.string.content_description_member_group else R.string.content_description_member_user, |
| 60 | + memberName.text |
| 61 | + ) |
| 62 | + memberRole.text = "" |
| 63 | + |
| 64 | + val hasExpirationDate = share.expirationDate != 0L |
| 65 | + expirationCalendarIcon.isVisible = hasExpirationDate |
| 66 | + expirationDate.isVisible = hasExpirationDate |
| 67 | + if (hasExpirationDate) { |
| 68 | + expirationDate.text = DisplayUtils.displayDateToHumanReadable(share.expirationDate.toString()) |
| 69 | + expirationDate.contentDescription = |
| 70 | + holder.itemView.context.getString(R.string.content_description_member_expiration_date, expirationDate.text) |
| 71 | + } |
| 72 | + |
| 73 | + editMemberButton.apply { |
| 74 | + contentDescription = holder.itemView.context.getString( |
| 75 | + R.string.content_description_edit_member_button, memberName.text |
| 76 | + ) |
| 77 | + isVisible = canEditShares |
| 78 | + setOnClickListener { listener.onEditShare(share) } |
| 79 | + } |
| 80 | + |
| 81 | + removeMemberButton.apply { |
| 82 | + contentDescription = holder.itemView.context.getString( |
| 83 | + R.string.content_description_remove_member_button, memberName.text |
| 84 | + ) |
| 85 | + isVisible = canRemoveShares |
| 86 | + setOnClickListener { listener.onRemoveShare(share) } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + override fun getItemCount(): Int = shares.size |
| 92 | + |
| 93 | + fun setShares( |
| 94 | + shares: List<OCShare>, |
| 95 | + canRemoveShares: Boolean, |
| 96 | + canEditShares: Boolean, |
| 97 | + ) { |
| 98 | + val hasPermissionsChanged = this.canEditShares != canEditShares || this.canRemoveShares != canRemoveShares |
| 99 | + this.canRemoveShares = canRemoveShares |
| 100 | + this.canEditShares = canEditShares |
| 101 | + val sortedShares = shares.sortedBy { it.sharedWithDisplayName ?: it.shareWith.orEmpty() } |
| 102 | + val diffResult = DiffUtil.calculateDiff(OcisSharesDiffUtil(this.shares, sortedShares, hasPermissionsChanged)) |
| 103 | + this.shares = sortedShares |
| 104 | + diffResult.dispatchUpdatesTo(this) |
| 105 | + } |
| 106 | + |
| 107 | + class OcisShareViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { |
| 108 | + val binding = MemberItemBinding.bind(itemView) |
| 109 | + } |
| 110 | + |
| 111 | + interface OcisSharesAdapterListener { |
| 112 | + fun onEditShare(share: OCShare) |
| 113 | + fun onRemoveShare(share: OCShare) |
| 114 | + } |
| 115 | +} |
0 commit comments