aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CopyTextBottomSheetFragment.java
blob: 2d01d37f6e646dbcdd1aa2069610b3a0edc1d4f4 (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package ml.docilealligator.infinityforreddit.bottomsheetfragments;


import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.activities.ViewRedditGalleryActivity;
import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedRoundedBottomSheetDialogFragment;
import ml.docilealligator.infinityforreddit.databinding.FragmentCopyTextBottomSheetBinding;
import ml.docilealligator.infinityforreddit.utils.Utils;

/**
 * A simple {@link Fragment} subclass.
 */
public class CopyTextBottomSheetFragment extends LandscapeExpandedRoundedBottomSheetDialogFragment {
    public static final String EXTRA_RAW_TEXT = "ERT";
    public static final String EXTRA_MARKDOWN = "EM";

    private BaseActivity baseActivity;
    private ViewRedditGalleryActivity viewRedditGalleryActivity;
    private String markdownText;

    public CopyTextBottomSheetFragment() {
        // Required empty public constructor
    }

    /**
     * Convenience method for creating the dialog, creating and setting arguments bundle
     * and displaying the dialog
     */
    public static void show(@NonNull FragmentManager fragmentManager,
                            @Nullable String rawText, @Nullable String markdown) {
        Bundle bundle = new Bundle();
        bundle.putString(CopyTextBottomSheetFragment.EXTRA_RAW_TEXT, rawText);
        bundle.putString(CopyTextBottomSheetFragment.EXTRA_MARKDOWN, markdown);
        CopyTextBottomSheetFragment copyTextBottomSheetFragment = new CopyTextBottomSheetFragment();
        copyTextBottomSheetFragment.setArguments(bundle);
        copyTextBottomSheetFragment.show(fragmentManager, null);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        FragmentCopyTextBottomSheetBinding binding = FragmentCopyTextBottomSheetBinding.inflate(inflater, container, false);

        String rawText = getArguments().getString(EXTRA_RAW_TEXT, null);
        markdownText = getArguments().getString(EXTRA_MARKDOWN, null);

        if (rawText != null) {
            binding.copyRawTextTextViewCopyTextBottomSheetFragment.setOnClickListener(view -> {
                showCopyDialog(rawText);
                dismiss();
            });

            binding.copyAllRawTextTextViewCopyTextBottomSheetFragment.setOnClickListener(view -> {
                copyText(rawText);
                dismiss();
            });
        } else {
            binding.copyRawTextTextViewCopyTextBottomSheetFragment.setVisibility(View.GONE);
            binding.copyAllRawTextTextViewCopyTextBottomSheetFragment.setVisibility(View.GONE);
        }

        if (markdownText != null) {
            binding.copyMarkdownTextViewCopyTextBottomSheetFragment.setOnClickListener(view -> {
                showCopyDialog(markdownText);
                dismiss();
            });

            binding.copyAllMarkdownTextViewCopyTextBottomSheetFragment.setOnClickListener(view -> {
                copyText(markdownText);
                dismiss();
            });
        } else {
            binding.copyMarkdownTextViewCopyTextBottomSheetFragment.setVisibility(View.GONE);
            binding.copyAllMarkdownTextViewCopyTextBottomSheetFragment.setVisibility(View.GONE);
        }

        if (baseActivity != null && baseActivity.typeface != null) {
            Utils.setFontToAllTextViews(binding.getRoot(), baseActivity.typeface);
        } else if (viewRedditGalleryActivity != null && viewRedditGalleryActivity.typeface != null) {
            Utils.setFontToAllTextViews(binding.getRoot(), viewRedditGalleryActivity.typeface);
        }

        return binding.getRoot();
    }

    private void showCopyDialog(String text) {
        AppCompatActivity activity = baseActivity == null ? viewRedditGalleryActivity : baseActivity;
        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.copy_text_material_dialog, null);
        TextView textView = layout.findViewById(R.id.text_view_copy_text_material_dialog);
        textView.setText(text);
        new MaterialAlertDialogBuilder(activity, R.style.CopyTextMaterialAlertDialogTheme)
                .setTitle(R.string.copy_text)
                .setView(layout)
                .setPositiveButton(R.string.copy_all, (dialogInterface, i) -> copyText(text))
                .setNegativeButton(R.string.cancel, null)
                .show();
    }

    private void copyText(String text) {
        AppCompatActivity activity = baseActivity == null ? viewRedditGalleryActivity : baseActivity;
        ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard != null) {
            ClipData clip = ClipData.newPlainText("simple text", text);
            clipboard.setPrimaryClip(clip);
            if (android.os.Build.VERSION.SDK_INT < 33) {
                Toast.makeText(activity, R.string.copy_success, Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(activity, R.string.copy_link_failed, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof BaseActivity) {
            baseActivity = (BaseActivity) context;
        } else {
            viewRedditGalleryActivity = (ViewRedditGalleryActivity) context;
        }
    }
}