summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/fragments/DonationReminderDialog.java
diff options
context:
space:
mode:
authorcyberta <cyberta@riseup.net>2020-12-28 17:05:35 -0800
committercyberta <cyberta@riseup.net>2020-12-28 17:05:35 -0800
commitfd81f2e14e36adb59d534df257e6ba2262cc362f (patch)
tree40196410e7358768c5cf6f1847760e2d012c0f1b /app/src/main/java/se/leap/bitmaskclient/base/fragments/DonationReminderDialog.java
parent16da1eeb5180cbb4a0d916785a08ccbcd3c1d74e (diff)
parent6af193f7d3c0fa3f73f5809442d83367bf025ffd (diff)
Merge branch 'restructure_project' into 'master'
Restructure project See merge request leap/bitmask_android!117
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/base/fragments/DonationReminderDialog.java')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/base/fragments/DonationReminderDialog.java120
1 files changed, 120 insertions, 0 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/fragments/DonationReminderDialog.java b/app/src/main/java/se/leap/bitmaskclient/base/fragments/DonationReminderDialog.java
new file mode 100644
index 00000000..0277933c
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/base/fragments/DonationReminderDialog.java
@@ -0,0 +1,120 @@
+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 androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AlertDialog;
+import androidx.appcompat.app.AppCompatDialogFragment;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.Button;
+
+import java.text.ParseException;
+
+import butterknife.ButterKnife;
+import butterknife.InjectView;
+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;
+
+ @InjectView(R.id.btnDonate)
+ Button btnDonate;
+
+ @InjectView(R.id.btnLater)
+ Button btnLater;
+
+ @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);
+ ButterKnife.inject(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.putString(getContext(), LAST_DONATION_REMINDER_DATE,
+ DateHelper.getCurrentDateString());
+ dismiss();
+ });
+ btnLater.setOnClickListener(v -> {
+ PreferenceHelper.putString(getContext(), LAST_DONATION_REMINDER_DATE,
+ DateHelper.getCurrentDateString());
+ dismiss();
+ });
+
+ return builder.create();
+ }
+
+ 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.getString(context, FIRST_TIME_USER_DATE, null);
+ if (firstTimeUserDate == null) {
+ PreferenceHelper.putString(context, FIRST_TIME_USER_DATE, DateHelper.getCurrentDateString());
+ return false;
+ }
+
+ try {
+ long diffDays;
+
+ diffDays = DateHelper.getDateDiffToCurrentDateInDays(firstTimeUserDate);
+ if (diffDays < 1) {
+ return false;
+ }
+
+ String lastDonationReminderDate = PreferenceHelper.getString(context, LAST_DONATION_REMINDER_DATE, null);
+ 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;
+ }
+ }
+}