SyncedFolderAdapter.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * Copyright (C) 2016 Andy Scherzinger
  6. * Copyright (C) 2016 Nextcloud
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.adapter;
  22. import android.content.Context;
  23. import android.support.annotation.Nullable;
  24. import android.view.LayoutInflater;
  25. import android.view.View;
  26. import android.view.ViewGroup;
  27. import android.widget.ImageButton;
  28. import android.widget.ImageView;
  29. import android.widget.LinearLayout;
  30. import android.widget.RelativeLayout;
  31. import android.widget.TextView;
  32. import com.afollestad.sectionedrecyclerview.SectionedRecyclerViewAdapter;
  33. import com.afollestad.sectionedrecyclerview.SectionedViewHolder;
  34. import com.owncloud.android.R;
  35. import com.owncloud.android.datamodel.MediaFolderType;
  36. import com.owncloud.android.datamodel.SyncedFolderDisplayItem;
  37. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  38. import com.owncloud.android.utils.ThemeUtils;
  39. import java.io.File;
  40. import java.util.ArrayList;
  41. import java.util.List;
  42. import butterknife.BindView;
  43. import butterknife.ButterKnife;
  44. /**
  45. * Adapter to display all auto-synced folders and/or instant upload media folders.
  46. */
  47. public class SyncedFolderAdapter extends SectionedRecyclerViewAdapter<SyncedFolderAdapter.MainViewHolder> {
  48. private final Context mContext;
  49. private final int mGridWidth;
  50. private final int mGridTotal;
  51. private final ClickListener mListener;
  52. private final List<SyncedFolderDisplayItem> mSyncFolderItems;
  53. private final boolean mLight;
  54. public SyncedFolderAdapter(Context context, int gridWidth, ClickListener listener, boolean light) {
  55. mContext = context;
  56. mGridWidth = gridWidth;
  57. mGridTotal = gridWidth * 2;
  58. mListener = listener;
  59. mSyncFolderItems = new ArrayList<>();
  60. mLight = light;
  61. shouldShowHeadersForEmptySections(true);
  62. }
  63. public void setSyncFolderItems(List<SyncedFolderDisplayItem> syncFolderItems) {
  64. mSyncFolderItems.clear();
  65. mSyncFolderItems.addAll(syncFolderItems);
  66. }
  67. public void setSyncFolderItem(int location, SyncedFolderDisplayItem syncFolderItem) {
  68. mSyncFolderItems.set(location, syncFolderItem);
  69. notifyDataSetChanged();
  70. }
  71. public void addSyncFolderItem(SyncedFolderDisplayItem syncFolderItem) {
  72. mSyncFolderItems.add(syncFolderItem);
  73. notifyDataSetChanged();
  74. }
  75. public void removeItem(int section) {
  76. mSyncFolderItems.remove(section);
  77. notifyDataSetChanged();
  78. }
  79. @Override
  80. public int getSectionCount() {
  81. return mSyncFolderItems.size();
  82. }
  83. @Override
  84. public int getItemCount(int section) {
  85. if (mSyncFolderItems.get(section).getFilePaths() != null) {
  86. return mSyncFolderItems.get(section).getFilePaths().size();
  87. } else {
  88. return 1;
  89. }
  90. }
  91. public SyncedFolderDisplayItem get(int section) {
  92. return mSyncFolderItems.get(section);
  93. }
  94. /**
  95. * returns the section of a synced folder for the given local path and type.
  96. *
  97. * @param localPath the local path of the synced folder
  98. * @param type the of the synced folder
  99. * @return the section index of the looked up synced folder, <code>-1</code> if not present
  100. */
  101. public int getSectionByLocalPathAndType(String localPath, int type) {
  102. for (int i = 0; i < mSyncFolderItems.size(); i++) {
  103. if (mSyncFolderItems.get(i).getLocalPath().equalsIgnoreCase(localPath) &&
  104. mSyncFolderItems.get(i).getType().getId().equals(type)) {
  105. return i;
  106. }
  107. }
  108. return -1;
  109. }
  110. @Override
  111. public void onBindHeaderViewHolder(final MainViewHolder holder, final int section, boolean expanded) {
  112. holder.mainHeaderContainer.setVisibility(View.VISIBLE);
  113. holder.title.setText(mSyncFolderItems.get(section).getFolderName());
  114. if (MediaFolderType.VIDEO == mSyncFolderItems.get(section).getType()) {
  115. holder.type.setImageResource(R.drawable.video_32dp);
  116. } else if (MediaFolderType.IMAGE == mSyncFolderItems.get(section).getType()) {
  117. holder.type.setImageResource(R.drawable.image_32dp);
  118. } else {
  119. holder.type.setImageResource(R.drawable.folder_star_32dp);
  120. }
  121. holder.syncStatusButton.setVisibility(View.VISIBLE);
  122. holder.syncStatusButton.setTag(section);
  123. holder.syncStatusButton.setOnClickListener(v -> {
  124. mSyncFolderItems.get(section).setEnabled(!mSyncFolderItems.get(section).isEnabled());
  125. setSyncButtonActiveIcon(holder.syncStatusButton, mSyncFolderItems.get(section).isEnabled());
  126. mListener.onSyncStatusToggleClick(section, mSyncFolderItems.get(section));
  127. });
  128. setSyncButtonActiveIcon(holder.syncStatusButton, mSyncFolderItems.get(section).isEnabled());
  129. holder.syncStatusButton.setVisibility(View.VISIBLE);
  130. holder.syncStatusButton.setTag(section);
  131. holder.syncStatusButton.setOnClickListener(v -> {
  132. mSyncFolderItems.get(section).setEnabled(!mSyncFolderItems.get(section).isEnabled());
  133. setSyncButtonActiveIcon(holder.syncStatusButton, mSyncFolderItems.get(section).isEnabled());
  134. mListener.onSyncStatusToggleClick(section, mSyncFolderItems.get(section));
  135. });
  136. setSyncButtonActiveIcon(holder.syncStatusButton, mSyncFolderItems.get(section).isEnabled());
  137. if (mLight) {
  138. holder.menuButton.setVisibility(View.GONE);
  139. } else {
  140. holder.menuButton.setVisibility(View.VISIBLE);
  141. holder.menuButton.setTag(section);
  142. holder.menuButton.setOnClickListener(v -> mListener.onSyncFolderSettingsClick(section,
  143. mSyncFolderItems.get(section)));
  144. }
  145. }
  146. @Override
  147. public void onBindFooterViewHolder(MainViewHolder holder, int section) {
  148. // not needed
  149. }
  150. @Override
  151. public void onBindViewHolder(MainViewHolder holder, int section, int relativePosition, int absolutePosition) {
  152. if (mSyncFolderItems.get(section).getFilePaths() != null) {
  153. File file = new File(mSyncFolderItems.get(section).getFilePaths().get(relativePosition));
  154. ThumbnailsCacheManager.MediaThumbnailGenerationTask task =
  155. new ThumbnailsCacheManager.MediaThumbnailGenerationTask(holder.image, mContext);
  156. ThumbnailsCacheManager.AsyncMediaThumbnailDrawable asyncDrawable =
  157. new ThumbnailsCacheManager.AsyncMediaThumbnailDrawable(
  158. mContext.getResources(),
  159. ThumbnailsCacheManager.mDefaultImg,
  160. task
  161. );
  162. holder.image.setImageDrawable(asyncDrawable);
  163. task.execute(file);
  164. // set proper tag
  165. holder.image.setTag(file.hashCode());
  166. holder.itemView.setTag(relativePosition % mGridWidth);
  167. if (mSyncFolderItems.get(section).getNumberOfFiles() > mGridTotal && relativePosition >= mGridTotal - 1) {
  168. holder.counterValue.setText(Long.toString(mSyncFolderItems.get(section).getNumberOfFiles() - mGridTotal));
  169. holder.counterBar.setVisibility(View.VISIBLE);
  170. holder.thumbnailDarkener.setVisibility(View.VISIBLE);
  171. } else {
  172. holder.counterBar.setVisibility(View.GONE);
  173. holder.thumbnailDarkener.setVisibility(View.GONE);
  174. }
  175. }
  176. }
  177. @Override
  178. public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  179. View v = LayoutInflater.from(parent.getContext()).inflate(
  180. viewType == VIEW_TYPE_HEADER ?
  181. R.layout.synced_folders_item_header : R.layout.grid_sync_item, parent, false);
  182. return new MainViewHolder(v);
  183. }
  184. public interface ClickListener {
  185. void onSyncStatusToggleClick(int section, SyncedFolderDisplayItem syncedFolderDisplayItem);
  186. void onSyncFolderSettingsClick(int section, SyncedFolderDisplayItem syncedFolderDisplayItem);
  187. }
  188. static class MainViewHolder extends SectionedViewHolder {
  189. @Nullable
  190. @BindView(R.id.thumbnail)
  191. public ImageView image;
  192. @Nullable
  193. @BindView(R.id.title)
  194. public TextView title;
  195. @Nullable
  196. @BindView(R.id.type)
  197. public ImageView type;
  198. @Nullable
  199. @BindView(R.id.settingsButton)
  200. public ImageButton menuButton;
  201. @Nullable
  202. @BindView(R.id.syncStatusButton)
  203. public ImageButton syncStatusButton;
  204. @Nullable
  205. @BindView(R.id.counterLayout)
  206. public LinearLayout counterBar;
  207. @Nullable
  208. @BindView(R.id.counter)
  209. public TextView counterValue;
  210. @Nullable
  211. @BindView(R.id.thumbnailDarkener)
  212. public ImageView thumbnailDarkener;
  213. @Nullable
  214. @BindView(R.id.header_container)
  215. public RelativeLayout mainHeaderContainer;
  216. private MainViewHolder(View itemView) {
  217. super(itemView);
  218. ButterKnife.bind(this, itemView);
  219. }
  220. }
  221. private void setSyncButtonActiveIcon(ImageButton syncStatusButton, boolean enabled) {
  222. if (enabled) {
  223. syncStatusButton.setImageDrawable(ThemeUtils.tintDrawable(R.drawable.ic_cloud_sync_on,
  224. ThemeUtils.primaryColor(mContext)));
  225. } else {
  226. syncStatusButton.setImageResource(R.drawable.ic_cloud_sync_off);
  227. }
  228. }
  229. }