Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AttachmentAdapter.java « card « ui « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1a01d1dbc671b7d43439168fcddc8a500a6e3e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package it.niedermann.nextcloud.deck.ui.card;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.format.DateUtils;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.model.Attachment;
import it.niedermann.nextcloud.deck.util.DeleteDialogBuilder;

public class AttachmentAdapter extends RecyclerView.Adapter<AttachmentAdapter.AttachmentViewHolder> {

    private final Account account;
    private final long cardRemoteId;
    @NonNull
    private List<Attachment> attachments;
    @NonNull
    private AttachmentDeletedListener attachmentDeletedListener;
    private Context context;

    AttachmentAdapter(@NonNull AttachmentDeletedListener attachmentDeletedListener, @NonNull Account account, long cardRemoteId, @NonNull List<Attachment> attachments) {
        super();
        this.attachmentDeletedListener = attachmentDeletedListener;
        this.attachments = attachments;
        this.account = account;
        this.cardRemoteId = cardRemoteId;
    }

    @NonNull
    @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);
    }

    @Override
    public void onBindViewHolder(@NonNull AttachmentViewHolder holder, int position) {
        Attachment attachment = attachments.get(position);
        if(attachment.getMimetype().startsWith("image")) {
            holder.filetype.setImageResource(R.drawable.ic_image_grey600_24dp);
        } else if(attachment.getMimetype().startsWith("audio")) {
            holder.filetype.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.filename.setText(attachment.getFilename());
        holder.filesize.setText(Formatter.formatFileSize(context, attachment.getFilesize()));
        holder.modified.setText(DateUtils.getRelativeTimeSpanString(context, attachment.getLastModified().getTime()));
        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);
        });
        holder.deleteButton.setOnClickListener((v) -> {
            new DeleteDialogBuilder(context)
                    .setTitle(context.getString(R.string.delete_attachment, attachment.getFilename()))
                    .setMessage(R.string.attachment_delete_message)
                    .setNegativeButton(R.string.simple_cancel, null)
                    .setPositiveButton(R.string.simple_delete, (dialog, which) -> attachmentDeletedListener.onAttachmentDeleted(attachment))
                    .show();
        });
    }

    @Override
    public int getItemCount() {
        return attachments.size();
    }

    static class AttachmentViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.filetype)
        AppCompatImageView filetype;
        @BindView(R.id.filename)
        TextView filename;
        @BindView(R.id.filesize)
        TextView filesize;
        @BindView(R.id.modified)
        TextView modified;
        @BindView(R.id.deleteButton)
        ImageButton deleteButton;

        private AttachmentViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface AttachmentDeletedListener {
        void onAttachmentDeleted(Attachment attachment);
    }
}