TemplateAdapter.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 android.widget.ImageView;
  31. import android.widget.TextView;
  32. import com.bumptech.glide.Glide;
  33. import com.nextcloud.client.account.CurrentAccountProvider;
  34. import com.nextcloud.client.network.ClientFactory;
  35. import com.owncloud.android.R;
  36. import com.owncloud.android.lib.common.Template;
  37. import com.owncloud.android.lib.common.TemplateList;
  38. import com.owncloud.android.utils.MimeTypeUtil;
  39. import com.owncloud.android.utils.glide.CustomGlideStreamLoader;
  40. import androidx.annotation.NonNull;
  41. import androidx.recyclerview.widget.RecyclerView;
  42. import butterknife.BindView;
  43. import butterknife.ButterKnife;
  44. /**
  45. * Adapter for handling Templates, used to create files out of it via RichDocuments app
  46. */
  47. public class TemplateAdapter extends RecyclerView.Adapter<TemplateAdapter.ViewHolder> {
  48. private TemplateList templateList = new TemplateList();
  49. private ClickListener clickListener;
  50. private Context context;
  51. private CurrentAccountProvider currentAccountProvider;
  52. private ClientFactory clientFactory;
  53. private String mimetype;
  54. public TemplateAdapter(
  55. String mimetype,
  56. ClickListener clickListener,
  57. Context context,
  58. CurrentAccountProvider currentAccountProvider,
  59. ClientFactory clientFactory
  60. ) {
  61. this.mimetype = mimetype;
  62. this.clickListener = clickListener;
  63. this.context = context;
  64. this.currentAccountProvider = currentAccountProvider;
  65. this.clientFactory = clientFactory;
  66. }
  67. @NonNull
  68. @Override
  69. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  70. return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.template_button, parent, false));
  71. }
  72. @Override
  73. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  74. holder.setData(templateList.getTemplateList().get(position));
  75. }
  76. public void setTemplateList(TemplateList templateList) {
  77. this.templateList = templateList;
  78. }
  79. @Override
  80. public int getItemCount() {
  81. return templateList.getTemplateList().size();
  82. }
  83. public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  84. @BindView(R.id.name)
  85. public TextView name;
  86. @BindView(R.id.thumbnail)
  87. public ImageView thumbnail;
  88. private Template template;
  89. public ViewHolder(View itemView) {
  90. super(itemView);
  91. ButterKnife.bind(this, itemView);
  92. itemView.setOnClickListener(this);
  93. }
  94. @Override
  95. public void onClick(View v) {
  96. if (clickListener != null) {
  97. clickListener.onClick(template);
  98. }
  99. }
  100. public void setData(Template template) {
  101. this.template = template;
  102. Drawable placeholder = MimeTypeUtil.getFileTypeIcon(mimetype,
  103. template.getTitle(),
  104. currentAccountProvider.getUser().toPlatformAccount(),
  105. context);
  106. Glide.with(context).using(new CustomGlideStreamLoader(currentAccountProvider, clientFactory))
  107. .load(template.getPreview())
  108. .placeholder(placeholder)
  109. .error(placeholder)
  110. .into(thumbnail);
  111. name.setText(template.getTitle());
  112. }
  113. }
  114. public interface ClickListener {
  115. void onClick(Template template);
  116. }
  117. }