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

DeleteAttachmentDialogFragment.java « attachments « 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: 270c0a138adca958fabf30e7ed7476d60474a3fc (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
package it.niedermann.nextcloud.deck.ui.card.attachments;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.model.Attachment;
import it.niedermann.nextcloud.deck.ui.branding.DeleteAlertDialogBuilder;

public class DeleteAttachmentDialogFragment extends DialogFragment {

    private static final String KEY_ATTACHMENT = "attachment";

    private AttachmentDeletedListener deleteAttachmentListener;
    private Attachment attachment;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (getParentFragment() instanceof AttachmentDeletedListener) {
            this.deleteAttachmentListener = (AttachmentDeletedListener) getParentFragment();
        } else if (context instanceof AttachmentDeletedListener) {
            this.deleteAttachmentListener = (AttachmentDeletedListener) context;
        } else {
            throw new ClassCastException("Context or parent fragment must implement " + AttachmentDeletedListener.class.getCanonicalName());
        }

        if (getArguments() == null || !getArguments().containsKey(KEY_ATTACHMENT)) {
            throw new IllegalArgumentException("Please provide at least " + KEY_ATTACHMENT + " as an argument");
        } else {
            this.attachment = (Attachment) getArguments().getSerializable(KEY_ATTACHMENT);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new DeleteAlertDialogBuilder(requireContext())
                .setTitle(getString(R.string.delete_something, attachment.getFilename()))
                .setMessage(R.string.attachment_delete_message)
                .setPositiveButton(R.string.simple_delete, (dialog, whichButton) -> deleteAttachmentListener.onAttachmentDeleted(attachment))
                .setNeutralButton(android.R.string.cancel, null);
        return builder.create();
    }

    public static DialogFragment newInstance(Attachment attachment) {
        final DeleteAttachmentDialogFragment dialog = new DeleteAttachmentDialogFragment();

        final Bundle args = new Bundle();
        args.putSerializable(KEY_ATTACHMENT, attachment);
        dialog.setArguments(args);

        return dialog;
    }
}