ShareViewHolder.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. *
  3. * Nextcloud Android client application
  4. *
  5. * @author Tobias Kaminsky
  6. * Copyright (C) 2020 Tobias Kaminsky
  7. * Copyright (C) 2020 Nextcloud GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android.ui.adapter;
  23. import android.content.Context;
  24. import android.view.View;
  25. import android.widget.ImageView;
  26. import com.owncloud.android.R;
  27. import com.owncloud.android.databinding.FileDetailsShareShareItemBinding;
  28. import com.owncloud.android.lib.resources.shares.OCShare;
  29. import com.owncloud.android.ui.TextDrawable;
  30. import java.security.NoSuchAlgorithmException;
  31. import androidx.annotation.DrawableRes;
  32. import androidx.annotation.NonNull;
  33. import androidx.recyclerview.widget.RecyclerView;
  34. class ShareViewHolder extends RecyclerView.ViewHolder {
  35. private FileDetailsShareShareItemBinding binding;
  36. private float avatarRadiusDimension;
  37. private Context context;
  38. public ShareViewHolder(@NonNull View itemView) {
  39. super(itemView);
  40. }
  41. public ShareViewHolder(FileDetailsShareShareItemBinding binding, Context context) {
  42. this(binding.getRoot());
  43. this.binding = binding;
  44. this.context = context;
  45. }
  46. public void bind(OCShare share,
  47. ShareeListAdapterListener listener,
  48. String userId,
  49. float avatarRadiusDimension) {
  50. this.avatarRadiusDimension = avatarRadiusDimension;
  51. String name = share.getSharedWithDisplayName();
  52. switch (share.getShareType()) {
  53. case GROUP:
  54. name = context.getString(R.string.share_group_clarification, name);
  55. setImage(binding.icon, share.getSharedWithDisplayName(), R.drawable.ic_group);
  56. break;
  57. case EMAIL:
  58. name = context.getString(R.string.share_email_clarification, name);
  59. setImage(binding.icon, share.getSharedWithDisplayName(), R.drawable.ic_email);
  60. break;
  61. case ROOM:
  62. name = context.getString(R.string.share_room_clarification, name);
  63. setImage(binding.icon, share.getSharedWithDisplayName(), R.drawable.ic_chat_bubble);
  64. break;
  65. case CIRCLE:
  66. binding.icon.setImageResource(R.drawable.ic_circles);
  67. break;
  68. case FEDERATED:
  69. name = context.getString(R.string.share_remote_clarification, name);
  70. setImage(binding.icon, share.getSharedWithDisplayName(), R.drawable.ic_user);
  71. break;
  72. default:
  73. setImage(binding.icon, name, R.drawable.ic_user);
  74. break;
  75. }
  76. binding.name.setText(name);
  77. if (share.getShareWith().equalsIgnoreCase(userId) || share.getUserId().equalsIgnoreCase(userId)) {
  78. binding.overflowMenu.setVisibility(View.VISIBLE);
  79. // bind listener to edit privileges
  80. binding.overflowMenu.setOnClickListener(v -> listener.showUserOverflowMenu(share, binding.overflowMenu));
  81. } else {
  82. binding.overflowMenu.setVisibility(View.GONE);
  83. }
  84. }
  85. private void setImage(ImageView avatar, String name, @DrawableRes int fallback) {
  86. try {
  87. avatar.setImageDrawable(TextDrawable.createNamedAvatar(name, avatarRadiusDimension));
  88. } catch (NoSuchAlgorithmException | StringIndexOutOfBoundsException e) {
  89. avatar.setImageResource(fallback);
  90. }
  91. }
  92. }