TemplateAdapter.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * @author Chris Narkiewicz
  6. *
  7. * Copyright (C) 2018 Tobias Kaminsky
  8. * Copyright (C) 2018 Nextcloud GmbH.
  9. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package com.owncloud.android.ui.adapter;
  25. import android.content.Context;
  26. import android.graphics.drawable.Drawable;
  27. import android.view.LayoutInflater;
  28. import android.view.View;
  29. import android.view.ViewGroup;
  30. import com.bumptech.glide.Glide;
  31. import com.nextcloud.client.account.CurrentAccountProvider;
  32. import com.nextcloud.client.network.ClientFactory;
  33. import com.owncloud.android.R;
  34. import com.owncloud.android.databinding.TemplateButtonBinding;
  35. import com.owncloud.android.lib.common.Template;
  36. import com.owncloud.android.lib.common.TemplateList;
  37. import com.owncloud.android.utils.MimeTypeUtil;
  38. import com.owncloud.android.utils.ThemeUtils;
  39. import com.owncloud.android.utils.glide.CustomGlideStreamLoader;
  40. import androidx.annotation.NonNull;
  41. import androidx.recyclerview.widget.RecyclerView;
  42. /**
  43. * Adapter for handling Templates, used to create files out of it via RichDocuments app
  44. */
  45. public class TemplateAdapter extends RecyclerView.Adapter<TemplateAdapter.ViewHolder> {
  46. private TemplateList templateList = new TemplateList();
  47. private ClickListener clickListener;
  48. private Context context;
  49. private CurrentAccountProvider currentAccountProvider;
  50. private ClientFactory clientFactory;
  51. private String mimetype;
  52. private Template selectedTemplate;
  53. private final int colorSelected;
  54. private final int colorUnselected;
  55. public TemplateAdapter(
  56. String mimetype,
  57. ClickListener clickListener,
  58. Context context,
  59. CurrentAccountProvider currentAccountProvider,
  60. ClientFactory clientFactory
  61. ) {
  62. this.mimetype = mimetype;
  63. this.clickListener = clickListener;
  64. this.context = context;
  65. this.currentAccountProvider = currentAccountProvider;
  66. this.clientFactory = clientFactory;
  67. colorSelected = ThemeUtils.primaryColor(context, true);
  68. colorUnselected = context.getResources().getColor(R.color.grey_200);
  69. }
  70. @NonNull
  71. @Override
  72. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  73. return new TemplateAdapter.ViewHolder(
  74. TemplateButtonBinding.inflate(LayoutInflater.from(parent.getContext()),
  75. parent,
  76. false)
  77. );
  78. }
  79. @Override
  80. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  81. holder.setData(templateList.getTemplateList().get(position));
  82. }
  83. public void setTemplateList(TemplateList templateList) {
  84. this.templateList = templateList;
  85. }
  86. public void setTemplateAsActive(Template template) {
  87. selectedTemplate = template;
  88. notifyDataSetChanged();
  89. }
  90. public Template getSelectedTemplate() {
  91. return selectedTemplate;
  92. }
  93. @Override
  94. public int getItemCount() {
  95. return templateList.getTemplateList().size();
  96. }
  97. public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  98. private final TemplateButtonBinding binding;
  99. private Template template;
  100. public ViewHolder(@NonNull TemplateButtonBinding binding) {
  101. super(binding.getRoot());
  102. this.binding = binding;
  103. itemView.setOnClickListener(this);
  104. }
  105. @Override
  106. public void onClick(View v) {
  107. if (clickListener != null) {
  108. clickListener.onClick(template);
  109. }
  110. }
  111. public void setData(Template template) {
  112. this.template = template;
  113. Drawable placeholder = MimeTypeUtil.getFileTypeIcon(mimetype,
  114. template.getTitle(),
  115. currentAccountProvider.getUser(),
  116. context);
  117. Glide.with(context).using(new CustomGlideStreamLoader(currentAccountProvider, clientFactory))
  118. .load(template.getPreview())
  119. .placeholder(placeholder)
  120. .error(placeholder)
  121. .into(binding.template);
  122. binding.templateName.setText(template.getTitle());
  123. if (template == selectedTemplate) {
  124. binding.templateContainer.setStrokeColor(colorSelected);
  125. } else {
  126. binding.templateContainer.setStrokeColor(colorUnselected);
  127. }
  128. }
  129. }
  130. public interface ClickListener {
  131. void onClick(Template template);
  132. }
  133. }