ShareViewHolder.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.nextcloud.client.account.User;
  27. import com.owncloud.android.R;
  28. import com.owncloud.android.databinding.FileDetailsShareShareItemBinding;
  29. import com.owncloud.android.lib.resources.shares.OCShare;
  30. import com.owncloud.android.ui.TextDrawable;
  31. import com.owncloud.android.utils.DisplayUtils;
  32. import com.owncloud.android.utils.theme.ThemeAvatarUtils;
  33. import androidx.annotation.DrawableRes;
  34. import androidx.annotation.NonNull;
  35. import androidx.recyclerview.widget.RecyclerView;
  36. class ShareViewHolder extends RecyclerView.ViewHolder {
  37. private FileDetailsShareShareItemBinding binding;
  38. private float avatarRadiusDimension;
  39. private User user;
  40. private Context context;
  41. public ShareViewHolder(@NonNull View itemView) {
  42. super(itemView);
  43. }
  44. public ShareViewHolder(FileDetailsShareShareItemBinding binding,
  45. User user,
  46. Context context) {
  47. this(binding.getRoot());
  48. this.binding = binding;
  49. this.user = user;
  50. this.context = context;
  51. }
  52. public void bind(OCShare share,
  53. ShareeListAdapterListener listener,
  54. DisplayUtils.AvatarGenerationListener avatarListener,
  55. String userId,
  56. float avatarRadiusDimension) {
  57. this.avatarRadiusDimension = avatarRadiusDimension;
  58. String name = share.getSharedWithDisplayName();
  59. binding.icon.setTag(null);
  60. switch (share.getShareType()) {
  61. case GROUP:
  62. name = context.getString(R.string.share_group_clarification, name);
  63. ThemeAvatarUtils.createAvatar(share.getShareType(), binding.icon, context);
  64. break;
  65. case ROOM:
  66. name = context.getString(R.string.share_room_clarification, name);
  67. ThemeAvatarUtils.createAvatar(share.getShareType(), binding.icon, context);
  68. break;
  69. case CIRCLE:
  70. ThemeAvatarUtils.createAvatar(share.getShareType(), binding.icon, context);
  71. break;
  72. case FEDERATED:
  73. name = context.getString(R.string.share_remote_clarification, name);
  74. setImage(binding.icon, share.getSharedWithDisplayName(), R.drawable.ic_user);
  75. break;
  76. case USER:
  77. binding.icon.setTag(share.getShareWith());
  78. float avatarRadius = context.getResources().getDimension(R.dimen.list_item_avatar_icon_radius);
  79. DisplayUtils.setAvatar(user,
  80. share.getShareWith(),
  81. share.getSharedWithDisplayName(),
  82. avatarListener,
  83. avatarRadius,
  84. context.getResources(),
  85. binding.icon,
  86. context);
  87. binding.icon.setOnClickListener(v -> listener.showProfileBottomSheet(user, share.getShareWith()));
  88. default:
  89. setImage(binding.icon, name, R.drawable.ic_user);
  90. break;
  91. }
  92. binding.name.setText(name);
  93. if (share.getShareWith().equalsIgnoreCase(userId) || share.getUserId().equalsIgnoreCase(userId)) {
  94. binding.overflowMenu.setVisibility(View.VISIBLE);
  95. // bind listener to edit privileges
  96. binding.overflowMenu.setOnClickListener(v -> listener.showUserOverflowMenu(share, binding.overflowMenu));
  97. } else {
  98. binding.overflowMenu.setVisibility(View.GONE);
  99. }
  100. }
  101. private void setImage(ImageView avatar, String name, @DrawableRes int fallback) {
  102. try {
  103. avatar.setImageDrawable(TextDrawable.createNamedAvatar(name, avatarRadiusDimension));
  104. } catch (StringIndexOutOfBoundsException e) {
  105. avatar.setImageResource(fallback);
  106. }
  107. }
  108. }