Skip to content

Commit

Permalink
Add dialog to rate ForkHub in the Play Store
Browse files Browse the repository at this point in the history
  • Loading branch information
jonan committed Dec 16, 2016
1 parent 3aef326 commit 9a22b14
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
61 changes: 52 additions & 9 deletions app/src/main/java/com/github/mobile/core/repo/StarForkHubTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,20 +35,24 @@
/**
* Task to check repository starring status
*/
public class StarForkHubTask extends AuthenticatedUserTask<Boolean> implements DialogInterface.OnClickListener {
public class StarForkHubTask extends AuthenticatedUserTask<Integer> 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;

@Inject
private SharedPreferences sharedPreferences;

private int numStarts;

private Repository repository;

/**
Expand All @@ -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;
}
}

Expand All @@ -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;
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@
<string name="enter_otp_code_title">Authentication Code</string>
<string name="enter_otp_code_message">Two-factor authentication is enabled for your account. Enter your authentication code to verify your identity.</string>
<string name="star_forkhub_dialog_text">If you like this app, please show your appreciation by starring it.</string>
<string name="rate_forkhub_dialog_text">If you like this app, please show your appreciation by rating it in the Play Store.</string>
<string name="rate">Rate</string>
<string name="no_milestone">No milestone</string>
<string name="unassigned">No one is assigned</string>
<string name="assigned">is assigned</string>
Expand Down

0 comments on commit 9a22b14

Please sign in to comment.