Skip to content

Commit bdc043b

Browse files
committed
Issue #3 was fixed
1 parent 1f5c1e2 commit bdc043b

File tree

3 files changed

+59
-47
lines changed

3 files changed

+59
-47
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/marcoscg/ratedialogsample/MainActivity.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ protected void onCreate(Bundle savedInstanceState) {
1212
super.onCreate(savedInstanceState);
1313
setContentView(R.layout.activity_main);
1414
RateDialog.init(this); // Shows the dialog each 3 days and each 7 launches (default config)
15-
//RateDialog.init(this, 2, RateDialog.DEFAULT_LAUNCHES_UNTIL_PROMPT); // Shows the dialog with custom config.
15+
// RateDialog.init(this, 2, RateDialog.DEFAULT_LAUNCHES_UNTIL_PROMPT); // Shows the dialog with custom config.
1616

1717
// Custom dialog instance
18-
// RateDialog rateDialog = new RateDialog(this, "custom_rate_dialog");
18+
// RateDialog rateDialog = new RateDialog(this, "custom_rate_dialog", 0 , 3);
1919
// rateDialog.init();
2020
}
2121

2222
public void openRateDialog (View v) {
23-
RateDialog.showDialog(this); // Shows the dialog instantly
23+
RateDialog rateDialog = new RateDialog(this, "custom_rate_dialog_key");
24+
rateDialog.showDialog();
2425
}
2526

2627
}

ratedialog/src/main/java/com/marcoscg/ratedialog/RateDialog.java

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import android.content.DialogInterface;
66
import android.content.Intent;
77
import android.content.SharedPreferences;
8+
import android.content.res.Resources;
89
import android.graphics.Typeface;
910
import android.net.Uri;
1011
import android.os.Build;
1112
import android.support.v7.app.AlertDialog;
12-
import android.util.DisplayMetrics;
1313
import android.view.LayoutInflater;
1414
import android.view.View;
1515
import android.widget.Button;
@@ -20,12 +20,12 @@ public class RateDialog {
2020

2121
public static int DEFAULT_DAYS_UNTIL_PROMPT = 3;
2222
public static int DEFAULT_LAUNCHES_UNTIL_PROMPT = 7;
23-
private static String DEFAULT_DIALOG_KEY = "ratedialog";
23+
private static String DEFAULT_DIALOG_KEY = "default_rate_dialog_key";
2424

2525
private Activity activity;
26+
private AlertDialog alertDialog;
2627
private SharedPreferences prefs;
2728

28-
private String dialogKey;
2929
private int daysUntilPrompt;
3030
private int launchesUntilPrompt;
3131

@@ -39,11 +39,12 @@ public RateDialog(Activity activity) {
3939

4040
public RateDialog(Activity activity, String dialogKey, int daysUntilPrompt, int launchesUntilPrompt) {
4141
this.activity = activity;
42-
this.dialogKey = dialogKey;
4342
this.daysUntilPrompt = daysUntilPrompt;
4443
this.launchesUntilPrompt = launchesUntilPrompt;
4544

46-
prefs = activity.getSharedPreferences(dialogKey, Context.MODE_PRIVATE);
45+
prefs = activity.getSharedPreferences(dialogKey + "_rdp", Context.MODE_PRIVATE);
46+
47+
setupDialog();
4748
}
4849

4950
public RateDialog(Activity activity, int daysUntilPrompt, int launchesUntilPrompt) {
@@ -96,26 +97,43 @@ public static void showDialog(Activity activity) {
9697
* Public methods
9798
*/
9899
public void init() {
99-
if (prefs == null || prefs.getBoolean(dialogKey + "dontshowagain", false)) { return; }
100+
if (prefs == null || prefs.getBoolean("do_not_show_again", false)) {
101+
return;
102+
}
100103

101-
long launch_count = prefs.getLong(dialogKey + "launch_count", 0) + 1;
102-
prefs.edit().putLong(dialogKey + "launch_count", launch_count).apply();
104+
long launchCount = prefs.getLong("launch_count", 0) + 1;
105+
prefs.edit().putLong("launch_count", launchCount).apply();
103106

104-
long date_firstLaunch = prefs.getLong(dialogKey + "date_firstlaunch", 0);
105-
if (date_firstLaunch == 0) {
106-
date_firstLaunch = System.currentTimeMillis();
107-
prefs.edit().putLong(dialogKey + "date_firstlaunch", date_firstLaunch).apply();
107+
long dateFirstLaunch = prefs.getLong("date_first_launch", 0);
108+
if (dateFirstLaunch == 0) {
109+
dateFirstLaunch = System.currentTimeMillis();
110+
prefs.edit().putLong("date_first_launch", dateFirstLaunch).apply();
108111
}
109112

110-
if (launch_count >= launchesUntilPrompt) {
111-
if (System.currentTimeMillis() >= date_firstLaunch +
113+
if (launchCount >= launchesUntilPrompt) {
114+
if (System.currentTimeMillis() >= dateFirstLaunch +
112115
(daysUntilPrompt * 24 * 60 * 60 * 1000)) {
113-
show(activity);
116+
showDialog();
114117
}
115118
}
116119
}
117120

118121
public void showDialog() {
122+
if (alertDialog != null) {
123+
alertDialog.show();
124+
}
125+
}
126+
127+
/**
128+
* Private methods
129+
*/
130+
private void neverShow() {
131+
if (prefs != null) {
132+
prefs.edit().putBoolean("do_not_show_again", true).apply();
133+
}
134+
}
135+
136+
private void setupDialog() {
119137
LayoutInflater inflater = activity.getLayoutInflater();
120138
View view = inflater.inflate(R.layout.dialog_layout, null);
121139

@@ -125,7 +143,7 @@ public void showDialog() {
125143
title.setText(String.format(activity.getResources().getString(R.string.rate_dialog_title), activity.getResources().getString(R.string.app_name)));
126144
message.setText(activity.getString(R.string.rate_dialog_message, activity.getResources().getString(R.string.app_name)));
127145

128-
AlertDialog dialog = new AlertDialog.Builder(activity)
146+
alertDialog = new AlertDialog.Builder(activity)
129147
.setView(view)
130148
.setPositiveButton(activity.getResources().getString(R.string.rate_dialog_action_rate), new DialogInterface.OnClickListener() {
131149
public void onClick(DialogInterface dialog, int which) {
@@ -134,6 +152,7 @@ public void onClick(DialogInterface dialog, int which) {
134152
} catch (android.content.ActivityNotFoundException anfe) {
135153
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + activity.getPackageName())));
136154
}
155+
137156
neverShow();
138157
Toast.makeText(activity, activity.getResources().getString(R.string.rate_dialog_thank_you), Toast.LENGTH_SHORT).show();
139158
}
@@ -144,39 +163,30 @@ public void onClick(DialogInterface dialog, int which) {
144163
neverShow();
145164
}
146165
})
147-
.show();
148-
149-
Typeface tf = Typeface.createFromAsset(activity.getAssets(),
150-
"medium.ttf");
151-
152-
Button positivebt = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
153-
Button neutralbt = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
154-
Button negativebt = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
155-
156-
positivebt.setPadding(dpToPx(12, activity),0,dpToPx(12, activity),0);
157-
positivebt.setTypeface(tf);
158-
159-
neutralbt.setPadding(dpToPx(12, activity),0,dpToPx(12, activity),0);
160-
neutralbt.setTypeface(tf);
161-
162-
negativebt.setPadding(dpToPx(12, activity),0,dpToPx(12, activity),0);
163-
negativebt.setTypeface(tf);
166+
.create();
167+
168+
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
169+
@Override
170+
public void onShow(DialogInterface dialog) {
171+
setButtonStyle(alertDialog.getButton(AlertDialog.BUTTON_POSITIVE));
172+
setButtonStyle(alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL));
173+
setButtonStyle(alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE));
174+
}
175+
});
164176
}
165177

166-
/**
167-
* Private methods
168-
*/
169-
private void neverShow() {
170-
if (prefs != null) {
171-
prefs.edit().putBoolean(dialogKey + "dontshowagain", true).apply();
178+
private void setButtonStyle(Button button) {
179+
if (Build.VERSION.SDK_INT >= 21) {
180+
button.setPadding(dpToPx(14),0, dpToPx(14),0);
181+
} else {
182+
button.setPadding(dpToPx(12),0, dpToPx(12),0);
172183
}
184+
185+
button.setTypeface(Typeface.createFromAsset(activity.getAssets(), "medium.ttf"));
173186
}
174187

175-
private static int dpToPx(int dp, Activity activity) {
176-
if (Build.VERSION.SDK_INT >= 21)
177-
dp = dp+2;
178-
DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
179-
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
188+
private int dpToPx(int dp) {
189+
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
180190
}
181191

182192
}

0 commit comments

Comments
 (0)