summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/fragments/DonationReminderDialog.java
blob: 08346791e4c1e39d9a961f87f7886f60335c0357 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package se.leap.bitmaskclient.base.fragments;

import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDialogFragment;

import java.text.ParseException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import se.leap.bitmaskclient.R;
import se.leap.bitmaskclient.base.utils.DateHelper;
import se.leap.bitmaskclient.base.utils.PreferenceHelper;

import static se.leap.bitmaskclient.base.models.Constants.DONATION_REMINDER_DURATION;
import static se.leap.bitmaskclient.base.models.Constants.DONATION_URL;
import static se.leap.bitmaskclient.base.models.Constants.ENABLE_DONATION;
import static se.leap.bitmaskclient.base.models.Constants.ENABLE_DONATION_REMINDER;
import static se.leap.bitmaskclient.base.models.Constants.FIRST_TIME_USER_DATE;
import static se.leap.bitmaskclient.base.models.Constants.LAST_DONATION_REMINDER_DATE;

public class DonationReminderDialog extends AppCompatDialogFragment {

    public final static String TAG = DonationReminderDialog.class.getName();
    private static boolean isShown = false;

    @BindView(R.id.btnDonate)
    Button btnDonate;

    @BindView(R.id.btnLater)
    Button btnLater;

    private Unbinder unbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.donation_reminder_dialog, null);
        unbinder = ButterKnife.bind(this, view);
        isShown = true;

        builder.setView(view);
        btnDonate.setOnClickListener(v -> {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(DONATION_URL));
            try {
                startActivity(browserIntent);
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
            }
            PreferenceHelper.lastDonationReminderDate(DateHelper.getCurrentDateString());
            dismiss();
        });
        btnLater.setOnClickListener(v -> {
            PreferenceHelper.lastDonationReminderDate(DateHelper.getCurrentDateString());
            dismiss();
        });

        return builder.create();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public static boolean isCallable(Context context) {
        if (isShown) {
            return false;
        }

        if (!ENABLE_DONATION || !ENABLE_DONATION_REMINDER) {
            return false;
        }

        if (context == null) {
            Log.e(TAG, "context is null!");
            return false;
        }
        
        String firstTimeUserDate = PreferenceHelper.getFirstTimeUserDate();
        if (firstTimeUserDate == null) {
            PreferenceHelper.firstTimeUserDate(DateHelper.getCurrentDateString());
            return false;
        }

        try {
            long diffDays;

            diffDays = DateHelper.getDateDiffToCurrentDateInDays(firstTimeUserDate);
            if (diffDays < 1) {
                return false;
            }

            String lastDonationReminderDate = PreferenceHelper.getLastDonationReminderDate();
            if (lastDonationReminderDate == null) {
                return true;
            }
            diffDays = DateHelper.getDateDiffToCurrentDateInDays(lastDonationReminderDate);
            return diffDays >= DONATION_REMINDER_DURATION;

        } catch (ParseException e) {
            e.printStackTrace();
            Log.e(TAG, e.getMessage());
            return false;
        }
    }
}