diff options
author | stefan-niedermann <info@niedermann.it> | 2020-02-24 23:05:45 +0300 |
---|---|---|
committer | stefan-niedermann <info@niedermann.it> | 2020-02-24 23:05:45 +0300 |
commit | 457e9948c23f2468293675303ad6e15f1da15a23 (patch) | |
tree | 7c80c5091e6fc458f8b417c29c50c6d92c375a69 | |
parent | 8dbc3d6e53175a672d05a11cdebe7db9c8ba48f7 (diff) |
Display image attachments directly
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/card/AttachmentAdapter.java | 113 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardAttachmentsFragment.java | 17 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/SquareRelativeLayout.java | 34 | ||||
-rw-r--r-- | app/src/main/res/layout/fragment_card_edit_tab_attachments.xml | 3 | ||||
-rw-r--r-- | app/src/main/res/layout/item_attachment_default.xml (renamed from app/src/main/res/layout/item_attachment.xml) | 2 | ||||
-rw-r--r-- | app/src/main/res/layout/item_attachment_image.xml | 25 |
6 files changed, 157 insertions, 37 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/AttachmentAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/AttachmentAdapter.java index 74a00677e..dad498a62 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/AttachmentAdapter.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/AttachmentAdapter.java @@ -16,6 +16,7 @@ import androidx.appcompat.widget.AppCompatImageView; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; +import com.bumptech.glide.load.resource.bitmap.CenterCrop; import java.util.List; @@ -30,6 +31,9 @@ import it.niedermann.nextcloud.deck.util.DeleteDialogBuilder; public class AttachmentAdapter extends RecyclerView.Adapter<AttachmentAdapter.AttachmentViewHolder> { + public static final int VIEW_TYPE_DEFAULT = 2; + public static final int VIEW_TYPE_IMAGE = 1; + private final Account account; private final long cardRemoteId; @NonNull @@ -50,56 +54,82 @@ public class AttachmentAdapter extends RecyclerView.Adapter<AttachmentAdapter.At @Override public AttachmentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { this.context = parent.getContext(); - View v = LayoutInflater.from(context).inflate(R.layout.item_attachment, parent, false); - return new AttachmentViewHolder(v); + //noinspection SwitchStatementWithTooFewBranches + switch (viewType) { + case VIEW_TYPE_IMAGE: { + return new ImageAttachmentViewHolder(LayoutInflater.from(context).inflate(R.layout.item_attachment_image, parent, false)); + } + default: { + return new DefaultAttachmentViewHolder(LayoutInflater.from(context).inflate(R.layout.item_attachment_default, parent, false)); + } + } } @Override public void onBindViewHolder(@NonNull AttachmentViewHolder holder, int position) { Attachment attachment = attachments.get(position); + int viewType = getItemViewType(position); holder.notSyncedYet.setVisibility(attachment.getStatusEnum() == DBStatus.UP_TO_DATE ? View.GONE : View.VISIBLE); - holder.filename.getRootView().setOnClickListener((event) -> { - Intent openURL = new Intent(android.content.Intent.ACTION_VIEW); - openURL.setData(Uri.parse(account.getUrl() + "/index.php/apps/deck/cards/" + cardRemoteId + "/attachment/" + attachment.getId())); - context.startActivity(openURL); - }); - if (attachment.getMimetype() != null) { if (attachment.getMimetype().startsWith("image")) { // TODO Glide is currently not yet able to use SSO and fails on authentication String uri = account.getUrl() + "/index.php/apps/deck/cards/" + cardRemoteId + "/attachment/" + attachment.getId(); Glide.with(context) .load(uri) + .transform(new CenterCrop()) .error(R.drawable.ic_image_grey600_24dp) - .into(holder.filetype); - holder.filetype.setImageResource(R.drawable.ic_image_grey600_24dp); - holder.filename.getRootView().setOnClickListener((v) -> AttachmentDialogFragment.newInstance(uri, attachment.getBasename()).show(((AppCompatActivity) context).getSupportFragmentManager(), "asdf")); + .into(holder.preview); + holder.preview.setImageResource(R.drawable.ic_image_grey600_24dp); + holder.preview.getRootView().setOnClickListener((v) -> AttachmentDialogFragment.newInstance(uri, attachment.getBasename()).show(((AppCompatActivity) context).getSupportFragmentManager(), "preview")); } else if (attachment.getMimetype().startsWith("audio")) { - holder.filetype.setImageResource(R.drawable.ic_music_note_grey600_24dp); + holder.preview.setImageResource(R.drawable.ic_music_note_grey600_24dp); } else if (attachment.getMimetype().startsWith("video")) { - holder.filetype.setImageResource(R.drawable.ic_local_movies_grey600_24dp); + holder.preview.setImageResource(R.drawable.ic_local_movies_grey600_24dp); } } - holder.filename.setText(attachment.getBasename()); - holder.filesize.setText(Formatter.formatFileSize(context, attachment.getFilesize())); - if (attachment.getLastModifiedLocal() != null) { - holder.modified.setText(DateUtil.getRelativeDateTimeString(context, attachment.getLastModifiedLocal().getTime())); - holder.modified.setVisibility(View.VISIBLE); - } else if (attachment.getLastModified() != null) { - holder.modified.setText(DateUtil.getRelativeDateTimeString(context, attachment.getLastModified().getTime())); - holder.modified.setVisibility(View.VISIBLE); - } else { - holder.modified.setVisibility(View.GONE); + + //noinspection SwitchStatementWithTooFewBranches + switch (viewType) { + case VIEW_TYPE_IMAGE: { + ImageAttachmentViewHolder imageHolder = (ImageAttachmentViewHolder) holder; + break; + } + default: { + DefaultAttachmentViewHolder defaultHolder = (DefaultAttachmentViewHolder) holder; + defaultHolder.filename.getRootView().setOnClickListener((event) -> { + Intent openURL = new Intent(android.content.Intent.ACTION_VIEW); + openURL.setData(Uri.parse(account.getUrl() + "/index.php/apps/deck/cards/" + cardRemoteId + "/attachment/" + attachment.getId())); + context.startActivity(openURL); + }); + defaultHolder.filename.setText(attachment.getBasename()); + defaultHolder.filesize.setText(Formatter.formatFileSize(context, attachment.getFilesize())); + if (attachment.getLastModifiedLocal() != null) { + defaultHolder.modified.setText(DateUtil.getRelativeDateTimeString(context, attachment.getLastModifiedLocal().getTime())); + defaultHolder.modified.setVisibility(View.VISIBLE); + } else if (attachment.getLastModified() != null) { + defaultHolder.modified.setText(DateUtil.getRelativeDateTimeString(context, attachment.getLastModified().getTime())); + defaultHolder.modified.setVisibility(View.VISIBLE); + } else { + defaultHolder.modified.setVisibility(View.GONE); + } + defaultHolder.deleteButton.setOnClickListener((v) -> { + new DeleteDialogBuilder(context) + .setTitle(context.getString(R.string.delete_something, attachment.getFilename())) + .setMessage(R.string.attachment_delete_message) + .setNegativeButton(android.R.string.cancel, null) + .setPositiveButton(R.string.simple_delete, (dialog, which) -> attachmentDeletedListener.onAttachmentDeleted(attachment)) + .show(); + }); + break; + } } - holder.deleteButton.setOnClickListener((v) -> { - new DeleteDialogBuilder(context) - .setTitle(context.getString(R.string.delete_something, attachment.getFilename())) - .setMessage(R.string.attachment_delete_message) - .setNegativeButton(android.R.string.cancel, null) - .setPositiveButton(R.string.simple_delete, (dialog, which) -> attachmentDeletedListener.onAttachmentDeleted(attachment)) - .show(); - }); + } + + @Override + public int getItemViewType(int position) { + String mimeType = attachments.get(position).getMimetype(); + return (mimeType != null && mimeType.startsWith("image")) ? VIEW_TYPE_IMAGE : VIEW_TYPE_DEFAULT; } @Override @@ -108,10 +138,17 @@ public class AttachmentAdapter extends RecyclerView.Adapter<AttachmentAdapter.At } static class AttachmentViewHolder extends RecyclerView.ViewHolder { + @BindView(R.id.preview) + AppCompatImageView preview; @BindView(R.id.not_synced_yet) AppCompatImageView notSyncedYet; - @BindView(R.id.filetype) - AppCompatImageView filetype; + + AttachmentViewHolder(@NonNull View itemView) { + super(itemView); + } + } + + static class DefaultAttachmentViewHolder extends AttachmentViewHolder { @BindView(R.id.filename) TextView filename; @BindView(R.id.filesize) @@ -121,7 +158,15 @@ public class AttachmentAdapter extends RecyclerView.Adapter<AttachmentAdapter.At @BindView(R.id.deleteButton) ImageButton deleteButton; - private AttachmentViewHolder(View view) { + private DefaultAttachmentViewHolder(View view) { + super(view); + ButterKnife.bind(this, view); + } + } + + static class ImageAttachmentViewHolder extends AttachmentViewHolder { + + private ImageAttachmentViewHolder(View view) { super(view); ButterKnife.bind(this, view); } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardAttachmentsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardAttachmentsFragment.java index a26bac420..563b8ce2c 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardAttachmentsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardAttachmentsFragment.java @@ -16,6 +16,7 @@ import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.floatingactionbutton.FloatingActionButton; @@ -83,6 +84,22 @@ public class CardAttachmentsFragment extends Fragment implements AttachmentAdapt syncManager.readAccount(accountId).observe(getViewLifecycleOwner(), (Account account) -> { RecyclerView.Adapter adapter = new AttachmentAdapter(this, account, fullCard.getCard().getId(), fullCard.getAttachments()); attachmentsList.setAdapter(adapter); + GridLayoutManager glm = new GridLayoutManager(getActivity(), 3); + + glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { + @Override + public int getSpanSize(int position) { + switch (adapter.getItemViewType(position)) { + case AttachmentAdapter.VIEW_TYPE_IMAGE: + return 1; + case AttachmentAdapter.VIEW_TYPE_DEFAULT: + return 3; + default: + return 1; + } + } + }); + attachmentsList.setLayoutManager(glm); }); } }); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/SquareRelativeLayout.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/SquareRelativeLayout.java new file mode 100644 index 000000000..86a48d6d2 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/SquareRelativeLayout.java @@ -0,0 +1,34 @@ +package it.niedermann.nextcloud.deck.ui.widget; + +import android.annotation.TargetApi; +import android.content.Context; +import android.os.Build; +import android.util.AttributeSet; +import android.widget.RelativeLayout; + +public class SquareRelativeLayout extends RelativeLayout { + + public SquareRelativeLayout(Context context) { + super(context); + } + + public SquareRelativeLayout(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + // Set a square layout. + super.onMeasure(widthMeasureSpec, widthMeasureSpec); + } + +}
\ No newline at end of file diff --git a/app/src/main/res/layout/fragment_card_edit_tab_attachments.xml b/app/src/main/res/layout/fragment_card_edit_tab_attachments.xml index 42b21612a..93d76fb53 100644 --- a/app/src/main/res/layout/fragment_card_edit_tab_attachments.xml +++ b/app/src/main/res/layout/fragment_card_edit_tab_attachments.xml @@ -13,8 +13,7 @@ android:layout_height="match_parent" android:scrollbars="vertical" android:visibility="gone" - app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" - tools:listitem="@layout/item_attachment" + tools:listitem="@layout/item_attachment_default" tools:visibility="visible" /> <it.niedermann.nextcloud.deck.ui.helper.emptycontentview.EmptyContentView diff --git a/app/src/main/res/layout/item_attachment.xml b/app/src/main/res/layout/item_attachment_default.xml index 5fe90e029..26798c547 100644 --- a/app/src/main/res/layout/item_attachment.xml +++ b/app/src/main/res/layout/item_attachment_default.xml @@ -15,7 +15,7 @@ android:layout_marginRight="@dimen/standard_half_margin"> <androidx.appcompat.widget.AppCompatImageView - android:id="@+id/filetype" + android:id="@+id/preview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" diff --git a/app/src/main/res/layout/item_attachment_image.xml b/app/src/main/res/layout/item_attachment_image.xml new file mode 100644 index 000000000..c7f63d998 --- /dev/null +++ b/app/src/main/res/layout/item_attachment_image.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="utf-8"?> + +<it.niedermann.nextcloud.deck.ui.widget.SquareRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <androidx.appcompat.widget.AppCompatImageView + android:id="@+id/preview" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_gravity="center" + app:srcCompat="@drawable/ic_image_grey600_24dp" /> + + <androidx.appcompat.widget.AppCompatImageView + android:id="@+id/not_synced_yet" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="bottom|end" + android:translationY="-8dp" + android:visibility="gone" + app:srcCompat="@drawable/ic_sync_blue_24dp" + tools:visibility="visible" /> +</it.niedermann.nextcloud.deck.ui.widget.SquareRelativeLayout>
\ No newline at end of file |