Skip to content

Commit 2d1a170

Browse files
committed
Some logic changes
1 parent 8e0b99a commit 2d1a170

File tree

2 files changed

+77
-55
lines changed

2 files changed

+77
-55
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ public class MainActivity extends AppCompatActivity {
1111
protected void onCreate(Bundle savedInstanceState) {
1212
super.onCreate(savedInstanceState);
1313
setContentView(R.layout.activity_main);
14-
RateDialog.with(this); // Shows the dialog each 3 days and each 7 launches (default config)
15-
//RateDialog.with(this, 2, 0); // Shows the dialog with custom config. Use 0 to get the default value
14+
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.
16+
17+
// Custom dialog instance
18+
// RateDialog rateDialog = new RateDialog(this, "custom_rate_dialog");
19+
// rateDialog.init();
1620
}
1721

1822
public void openRateDialog (View v) {
19-
RateDialog.show(this); // Shows the dialog instantly
23+
RateDialog.showDialog(this); // Shows the dialog instantly
2024
}
2125

2226
}

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

Lines changed: 70 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.marcoscg.ratedialog;
22

33
import android.app.Activity;
4+
import android.content.Context;
45
import android.content.DialogInterface;
56
import android.content.Intent;
67
import android.content.SharedPreferences;
@@ -17,52 +18,103 @@
1718

1819
public class RateDialog {
1920

21+
public static int DEFAULT_DAYS_UNTIL_PROMPT = 3;
22+
public static int DEFAULT_LAUNCHES_UNTIL_PROMPT = 7;
2023
private static String DEFAULT_DIALOG_KEY = "ratedialog";
2124

22-
private int DAYS_UNTIL_PROMPT = 3;
23-
private int LAUNCHES_UNTIL_PROMPT = 7;
24-
25-
private AlertDialog dialog;
2625
private Activity activity;
27-
private String dialogKey;
28-
2926
private SharedPreferences prefs;
30-
private SharedPreferences.Editor editor;
27+
28+
private String dialogKey;
29+
private int daysUntilPrompt;
30+
private int launchesUntilPrompt;
3131

3232
public RateDialog(Activity activity, String dialogKey) {
33-
init(activity, dialogKey, 0, 0);
33+
this(activity, dialogKey, DEFAULT_DAYS_UNTIL_PROMPT, DEFAULT_LAUNCHES_UNTIL_PROMPT);
3434
}
3535

3636
public RateDialog(Activity activity) {
37-
init(activity, DEFAULT_DIALOG_KEY, 0, 0);
37+
this(activity, DEFAULT_DIALOG_KEY);
3838
}
3939

4040
public RateDialog(Activity activity, String dialogKey, int daysUntilPrompt, int launchesUntilPrompt) {
41-
init(activity, dialogKey, daysUntilPrompt, launchesUntilPrompt);
41+
this.activity = activity;
42+
this.dialogKey = dialogKey;
43+
this.daysUntilPrompt = daysUntilPrompt;
44+
this.launchesUntilPrompt = launchesUntilPrompt;
45+
46+
prefs = activity.getSharedPreferences(dialogKey, Context.MODE_PRIVATE);
4247
}
4348

4449
public RateDialog(Activity activity, int daysUntilPrompt, int launchesUntilPrompt) {
45-
init(activity, DEFAULT_DIALOG_KEY, daysUntilPrompt, launchesUntilPrompt);
50+
this(activity, DEFAULT_DIALOG_KEY, daysUntilPrompt, launchesUntilPrompt);
4651
}
4752

4853
/**
49-
* Static methods
54+
* Init rate dialog.
55+
*
56+
* @deprecated use {@link #init()} instead.
5057
*/
58+
@Deprecated
5159
public static void with(Activity activity) {
52-
with(activity, 0, 0);
60+
init(activity);
5361
}
5462

63+
/**
64+
* Init rate dialog.
65+
*
66+
* @deprecated use {@link #init()} instead.
67+
*/
68+
@Deprecated
5569
public static void with(Activity activity, int daysUntilPrompt, int launchesUntilPrompt) {
56-
new RateDialog(activity, DEFAULT_DIALOG_KEY, daysUntilPrompt, launchesUntilPrompt);
70+
init(activity, daysUntilPrompt, launchesUntilPrompt);
71+
}
72+
73+
public static void init(Activity activity) {
74+
init(activity, DEFAULT_DAYS_UNTIL_PROMPT, DEFAULT_LAUNCHES_UNTIL_PROMPT);
75+
}
76+
77+
public static void init(Activity activity, int daysUntilPrompt, int launchesUntilPrompt) {
78+
new RateDialog(activity, DEFAULT_DIALOG_KEY, daysUntilPrompt, launchesUntilPrompt).init();
5779
}
5880

81+
/**
82+
* Show rate dialog.
83+
*
84+
* @deprecated use {@link #showDialog()} instead.
85+
*/
86+
@Deprecated
5987
public static void show(Activity activity) {
88+
showDialog(activity);
89+
}
90+
91+
public static void showDialog(Activity activity) {
6092
new RateDialog(activity, DEFAULT_DIALOG_KEY).showDialog();
6193
}
6294

6395
/**
6496
* Public methods
6597
*/
98+
public void init() {
99+
if (prefs == null || prefs.getBoolean(dialogKey + "dontshowagain", false)) { return; }
100+
101+
long launch_count = prefs.getLong(dialogKey + "launch_count", 0) + 1;
102+
prefs.edit().putLong(dialogKey + "launch_count", launch_count).apply();
103+
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();
108+
}
109+
110+
if (launch_count >= launchesUntilPrompt) {
111+
if (System.currentTimeMillis() >= date_firstLaunch +
112+
(daysUntilPrompt * 24 * 60 * 60 * 1000)) {
113+
show(activity);
114+
}
115+
}
116+
}
117+
66118
public void showDialog() {
67119
LayoutInflater inflater = activity.getLayoutInflater();
68120
View view = inflater.inflate(R.layout.dialog_layout, null);
@@ -73,7 +125,7 @@ public void showDialog() {
73125
title.setText(String.format(activity.getResources().getString(R.string.rate_dialog_title), activity.getResources().getString(R.string.app_name)));
74126
message.setText(activity.getString(R.string.rate_dialog_message, activity.getResources().getString(R.string.app_name)));
75127

76-
dialog = new AlertDialog.Builder(activity)
128+
AlertDialog dialog = new AlertDialog.Builder(activity)
77129
.setView(view)
78130
.setPositiveButton(activity.getResources().getString(R.string.rate_dialog_action_rate), new DialogInterface.OnClickListener() {
79131
public void onClick(DialogInterface dialog, int which) {
@@ -92,8 +144,7 @@ public void onClick(DialogInterface dialog, int which) {
92144
neverShow();
93145
}
94146
})
95-
.create();
96-
dialog.show();
147+
.show();
97148

98149
Typeface tf = Typeface.createFromAsset(activity.getAssets(),
99150
"medium.ttf");
@@ -115,42 +166,9 @@ public void onClick(DialogInterface dialog, int which) {
115166
/**
116167
* Private methods
117168
*/
118-
private void init(Activity activity, String dialogKey, int daysUntilPrompt, int launchesUntilPrompt) {
119-
if (daysUntilPrompt > 0)
120-
DAYS_UNTIL_PROMPT = daysUntilPrompt;
121-
if (launchesUntilPrompt > 0)
122-
LAUNCHES_UNTIL_PROMPT = launchesUntilPrompt;
123-
124-
this.activity = activity;
125-
this.dialogKey = dialogKey;
126-
prefs = activity.getSharedPreferences(dialogKey, 0);
127-
editor = prefs.edit();
128-
129-
if (prefs.getBoolean(dialogKey + "dontshowagain", false)) { return; }
130-
131-
long launch_count = prefs.getLong(dialogKey + "launch_count", 0) + 1;
132-
editor.putLong(dialogKey + "launch_count", launch_count);
133-
134-
long date_firstLaunch = prefs.getLong(dialogKey + "date_firstlaunch", 0);
135-
if (date_firstLaunch == 0) {
136-
date_firstLaunch = System.currentTimeMillis();
137-
editor.putLong(dialogKey + "date_firstlaunch", date_firstLaunch);
138-
}
139-
140-
if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
141-
if (System.currentTimeMillis() >= date_firstLaunch +
142-
(DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
143-
show(activity);
144-
}
145-
}
146-
147-
editor.apply();
148-
}
149-
150169
private void neverShow() {
151-
if (editor != null) {
152-
editor.putBoolean(dialogKey + "dontshowagain", true);
153-
editor.commit();
170+
if (prefs != null) {
171+
prefs.edit().putBoolean(dialogKey + "dontshowagain", true).apply();
154172
}
155173
}
156174

0 commit comments

Comments
 (0)