diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2024-03-22 18:18:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-22 18:18:30 +0100 |
commit | 0a6b7ed69957cea9582af9e0dd861ed13795d6b5 (patch) | |
tree | 317262009e7e439a9d85c643fb1f6e864f1d4507 /app/src/main/java/de/danoeh/antennapod/dialog | |
parent | 27aa5cba96299dd14569f8a5f6da60bb88352d60 (diff) | |
download | AntennaPod-0a6b7ed69957cea9582af9e0dd861ed13795d6b5.zip |
Nicer rating dialog (#7011)
Diffstat (limited to 'app/src/main/java/de/danoeh/antennapod/dialog')
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/dialog/rating/RatingDialogFragment.java | 79 | ||||
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/dialog/rating/RatingDialogManager.java | 94 |
2 files changed, 173 insertions, 0 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/rating/RatingDialogFragment.java b/app/src/main/java/de/danoeh/antennapod/dialog/rating/RatingDialogFragment.java new file mode 100644 index 000000000..4e9da8b9a --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/dialog/rating/RatingDialogFragment.java @@ -0,0 +1,79 @@ +package de.danoeh.antennapod.dialog.rating; + +import android.app.Dialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.text.HtmlCompat; +import androidx.fragment.app.DialogFragment; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.util.IntentUtils; +import de.danoeh.antennapod.databinding.RatingDialogBinding; +import de.danoeh.antennapod.ui.common.DateFormatter; + +import java.util.Date; + +public class RatingDialogFragment extends DialogFragment { + private static final String EXTRA_TOTAL_TIME = "totalTime"; + private static final String EXTRA_OLDEST_DATE = "oldestDate"; + + public static RatingDialogFragment newInstance(long totalTime, long oldestDate) { + RatingDialogFragment fragment = new RatingDialogFragment(); + Bundle arguments = new Bundle(); + arguments.putLong(EXTRA_TOTAL_TIME, totalTime); + arguments.putLong(EXTRA_OLDEST_DATE, oldestDate); + fragment.setArguments(arguments); + return fragment; + } + + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + return new MaterialAlertDialogBuilder(getContext()) + .setView(onCreateView(getLayoutInflater(), null, savedInstanceState)) + .create(); + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, + @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + RatingDialogBinding viewBinding = RatingDialogBinding.inflate(inflater); + long totalTime = getArguments().getLong(EXTRA_TOTAL_TIME, 0); + long oldestDate = getArguments().getLong(EXTRA_OLDEST_DATE, 0); + + viewBinding.headerLabel.setText(HtmlCompat.fromHtml(getString(R.string.rating_tagline, + DateFormatter.formatAbbrev(getContext(), new Date(oldestDate)), + "<br/><b><big><big><big><big><big>", totalTime / 3600L, + "</big></big></big></big></big></b><br/>"), HtmlCompat.FROM_HTML_MODE_LEGACY)); + viewBinding.neverAgainButton.setOnClickListener(v -> { + new RatingDialogManager(getActivity()).saveRated(); + dismiss(); + }); + viewBinding.showLaterButton.setOnClickListener(v -> { + new RatingDialogManager(getActivity()).resetStartDate(); + dismiss(); + }); + viewBinding.rateButton.setOnClickListener(v -> { + IntentUtils.openInBrowser(getContext(), + "https://play.google.com/store/apps/details?id=de.danoeh.antennapod"); + new RatingDialogManager(getActivity()).saveRated(); + }); + viewBinding.contibuteButton.setOnClickListener(v -> { + IntentUtils.openInBrowser(getContext(), IntentUtils.getLocalizedWebsiteLink(getContext()) + "/contribute/"); + new RatingDialogManager(getActivity()).saveRated(); + }); + return viewBinding.getRoot(); + } + + @Override + public void onCancel(@NonNull DialogInterface dialog) { + super.onCancel(dialog); + new RatingDialogManager(getActivity()).resetStartDate(); + } +} diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/rating/RatingDialogManager.java b/app/src/main/java/de/danoeh/antennapod/dialog/rating/RatingDialogManager.java new file mode 100644 index 000000000..ffa67cc2e --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/dialog/rating/RatingDialogManager.java @@ -0,0 +1,94 @@ +package de.danoeh.antennapod.dialog.rating; + +import android.content.Context; +import android.content.SharedPreferences; + +import java.util.concurrent.TimeUnit; + +import android.util.Log; +import androidx.fragment.app.FragmentActivity; +import de.danoeh.antennapod.core.BuildConfig; +import de.danoeh.antennapod.core.storage.DBReader; +import de.danoeh.antennapod.core.storage.StatisticsItem; +import io.reactivex.Observable; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; +import io.reactivex.schedulers.Schedulers; +import kotlin.Pair; + +public class RatingDialogManager { + private static final int AFTER_DAYS = 20; + private static final String TAG = "RatingDialog"; + private static final String PREFS_NAME = "RatingPrefs"; + private static final String KEY_RATED = "KEY_WAS_RATED"; + private static final String KEY_FIRST_START_DATE = "KEY_FIRST_HIT_DATE"; + + private final SharedPreferences preferences; + private final FragmentActivity fragmentActivity; + private Disposable disposable; + + public RatingDialogManager(FragmentActivity activity) { + this.fragmentActivity = activity; + preferences = activity.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + } + + public void showIfNeeded() { + //noinspection ConstantConditions + if (isRated() || BuildConfig.DEBUG || "free".equals(BuildConfig.FLAVOR)) { + return; + } else if (!enoughTimeSinceInstall()) { + return; + } + + if (disposable != null) { + disposable.dispose(); + } + disposable = Observable.fromCallable( + () -> { + DBReader.StatisticsResult statisticsData = DBReader.getStatistics(false, 0, Long.MAX_VALUE); + long totalTime = 0; + for (StatisticsItem item : statisticsData.feedTime) { + totalTime += item.timePlayed; + } + return new Pair<>(totalTime, statisticsData.oldestDate); + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(result -> { + long totalTime = result.getFirst(); + long oldestDate = result.getSecond(); + if (totalTime < TimeUnit.SECONDS.convert(15, TimeUnit.HOURS)) { + return; + } else if (oldestDate > System.currentTimeMillis() + - TimeUnit.MILLISECONDS.convert(AFTER_DAYS, TimeUnit.DAYS)) { + return; // In case the app was opened but nothing was played + } + RatingDialogFragment.newInstance(result.getFirst(), result.getSecond()) + .show(fragmentActivity.getSupportFragmentManager(), TAG); + }, error -> Log.e(TAG, Log.getStackTraceString(error))); + } + + private boolean isRated() { + return preferences.getBoolean(KEY_RATED, false); + } + + public void saveRated() { + preferences.edit().putBoolean(KEY_RATED, true).apply(); + } + + public void resetStartDate() { + preferences.edit().putLong(KEY_FIRST_START_DATE, System.currentTimeMillis()).apply(); + } + + private boolean enoughTimeSinceInstall() { + if (preferences.getLong(KEY_FIRST_START_DATE, 0) == 0) { + resetStartDate(); + return false; + } + long now = System.currentTimeMillis(); + long firstDate = preferences.getLong(KEY_FIRST_START_DATE, now); + long diff = now - firstDate; + long diffDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS); + return diffDays >= AFTER_DAYS; + } +} |