From 9a22b145020c3039d35c86bc59fd295398841334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Ander=20Pe=C3=B1alba?= Date: Fri, 16 Dec 2016 17:44:59 +0100 Subject: [PATCH] Add dialog to rate ForkHub in the Play Store --- .../mobile/core/repo/StarForkHubTask.java | 61 ++++++++++++++++--- app/src/main/res/values/strings.xml | 2 + 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/github/mobile/core/repo/StarForkHubTask.java b/app/src/main/java/com/github/mobile/core/repo/StarForkHubTask.java index c18f37e9..82ffd2c3 100644 --- a/app/src/main/java/com/github/mobile/core/repo/StarForkHubTask.java +++ b/app/src/main/java/com/github/mobile/core/repo/StarForkHubTask.java @@ -18,7 +18,9 @@ import android.accounts.Account; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.support.v7.app.AlertDialog; import android.util.Log; @@ -33,13 +35,15 @@ /** * Task to check repository starring status */ -public class StarForkHubTask extends AuthenticatedUserTask implements DialogInterface.OnClickListener { +public class StarForkHubTask extends AuthenticatedUserTask implements DialogInterface.OnClickListener { private static final String TAG = "StarForkHubTask"; private static final String PREF_START_APP_COUNT = "startAppCount"; - private static final int NUMBER_STARTS_NEEDED = 5; + private static final int NUMBER_EXECUTIONS_NEEDED_STAR = 5; + + private static final int NUMBER_EXECUTIONS_NEEDED_PLAY_STORE = 15; @Inject private StargazerService service; @@ -47,6 +51,8 @@ public class StarForkHubTask extends AuthenticatedUserTask implements D @Inject private SharedPreferences sharedPreferences; + private int numStarts; + private Repository repository; /** @@ -62,23 +68,51 @@ public StarForkHubTask(Context context) { } @Override - protected Boolean run(Account account) throws Exception { + protected Integer run(Account account) throws Exception { int numStarts = sharedPreferences.getInt(PREF_START_APP_COUNT, -1); + + if (numStarts > NUMBER_EXECUTIONS_NEEDED_PLAY_STORE) { + return 0; + } + sharedPreferences.edit() - .putInt(PREF_START_APP_COUNT, Math.min(numStarts, NUMBER_STARTS_NEEDED) + 1) + .putInt(PREF_START_APP_COUNT, numStarts + 1) .apply(); - return numStarts == NUMBER_STARTS_NEEDED && !service.isStarring(repository); + + if (numStarts == NUMBER_EXECUTIONS_NEEDED_STAR && !service.isStarring(repository)) { + return numStarts; + } + + if (numStarts == NUMBER_EXECUTIONS_NEEDED_PLAY_STORE) { + return numStarts; + } + + return 0; } @Override - protected void onSuccess(Boolean result) { - if (result) { + protected void onSuccess(Integer result) { + numStarts = result; + + switch (result) { + case NUMBER_EXECUTIONS_NEEDED_STAR: + // Star dialog new AlertDialog.Builder(context) .setMessage(context.getResources().getString(R.string.star_forkhub_dialog_text)) .setPositiveButton(context.getResources().getString(R.string.star), this) - .setNegativeButton(context.getResources().getString(android.R.string.no), this) + .setNegativeButton(context.getResources().getString(android.R.string.cancel), this) + .setCancelable(true) + .show(); + break; + case NUMBER_EXECUTIONS_NEEDED_PLAY_STORE: + // Rate dialog + new AlertDialog.Builder(context) + .setMessage(context.getResources().getString(R.string.rate_forkhub_dialog_text)) + .setPositiveButton(context.getResources().getString(R.string.rate), this) + .setNegativeButton(context.getResources().getString(android.R.string.cancel), this) .setCancelable(true) .show(); + break; } } @@ -91,8 +125,17 @@ protected void onException(Exception e) throws RuntimeException { @Override public void onClick(DialogInterface dialog, int which) { - if (which == DialogInterface.BUTTON_POSITIVE) { + if (which != DialogInterface.BUTTON_POSITIVE) { + return; + } + + switch (numStarts) { + case NUMBER_EXECUTIONS_NEEDED_STAR: new StarRepositoryTask(context, repository).start(); + break; + case NUMBER_EXECUTIONS_NEEDED_PLAY_STORE: + context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=jp.forkhub"))); + break; } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index faa7dd1a..6857ac15 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -187,6 +187,8 @@ Authentication Code Two-factor authentication is enabled for your account. Enter your authentication code to verify your identity. If you like this app, please show your appreciation by starring it. + If you like this app, please show your appreciation by rating it in the Play Store. + Rate No milestone No one is assigned is assigned