-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
322 lines (284 loc) · 9.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* Feedback reminder for your application
*
* Copyright (C) 2016 Tkachenko Andrey
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
var TAG = "com.falkolab.rateme",
USAGE_TIMEOUT_KEY = TAG + ".timeout",
LAUNCH_COUNTER_KEY = TAG + ".launches",
OFF_KEY = TAG + '.off',
CRASHED_KEY = TAG + '.crashed',
// initial launch of app
initialLaunchPerformed = false,
promptDelay = 0, // seconds
timeUntilPrompt = 0, // seconds
launchesUntilPrompt = 0, // run times of app
showIfAppHasCrashed = true,
appId,
timerInterval = 60, // seconds
_timer,
_dialogTimeout;
Ti.App.addEventListener('uncaughtException', function() {
Ti.App.Properties.setBool(CRASHED_KEY, true);
});
if(Titanium.Platform.osname == 'android') {
exports.storeName = 'Google Play';
appId = Ti.App.id; // default value
} else if(Titanium.Platform.osname == 'iphone') {
exports.storeName = 'App Store';
} else {
exports.storeName = 'Store';
}
/**
* Rater module initializer
* @param {Object} opts configuraion object.
* @param {number} opts.promptDelay delay before show reminder dialog (seconds).
* @param {number} opts.timeUntilPrompt app run time in seconds before show reminder dialog.
* @param {number} opts.launchesUntilPrompt app launch times before show reminder dialog.
* @param {number} opts.timerInterval internal timer period in seconds.
* @param {string} opts.appId application id. `Ti.App.id` for Android or id from App Store for iOS.
* @param {bool} opts.showIfAppHasCrashed whether or not to show reminder dialog if app was crashed before.
*/
exports.init = function(opts) {
if(!_.isUndefined(opts.promptDelay)) {
promptDelay = opts.promptDelay;
}
if(!_.isUndefined(opts.timerInterval)) {
timerInterval = opts.timerInterval;
}
if(!_.isUndefined(opts.showIfAppHasCrashed)) {
showIfAppHasCrashed = opts.showIfAppHasCrashed;
}
if(opts.appId) {
appId = opts.appId;
if(Ti.Platform.name == 'iPhone OS' && appId.lastIndexOf('id', 0) === 0) {
appId = appId.substring(2);
}
}
if(!appId) {
throw "Unknown app store id!";
}
// app usage timeout in minutes
if(!_.isUndefined(opts.timeUntilPrompt)) {
timeUntilPrompt = opts.timeUntilPrompt;
if(this.getRemainedTimeout() === 0) {
Ti.App.Properties.setInt(USAGE_TIMEOUT_KEY, timeUntilPrompt);
}
}
if(!_.isUndefined(opts.launchesUntilPrompt)) {
launchesUntilPrompt = opts.launchesUntilPrompt;
if(this.getLaunchesUntilPrompt() === 0) {
Ti.App.Properties.setInt(LAUNCH_COUNTER_KEY, launchesUntilPrompt);
//this launch
decrementLauchCounter();
}
}
resume();
};
function resume() {
if(exports.isOff()) return;
var timeoutValue = exports.getRemainedTimeout();
if(timeoutValue && !_timer) {
_timer = setInterval(tick, timerInterval * 1000);
}
}
/**
* Reset reminder to initial state.
*/
exports.reset = function() {
Ti.App.Properties.setBool(OFF_KEY, false);
Ti.App.Properties.setBool(CRASHED_KEY, false);
this.resetCounters();
};
/**
* Reset counters only to initial state
*/
exports.resetCounters = function() {
if(_.isUndefined(timeUntilPrompt)) {
Ti.App.Properties.removeProperty(USAGE_TIMEOUT_KEY);
} else {
Ti.App.Properties.setInt(USAGE_TIMEOUT_KEY, timeUntilPrompt);
}
if(_.isUndefined(launchesUntilPrompt)) {
Ti.App.Properties.removeProperty(LAUNCH_COUNTER_KEY);
} else {
Ti.App.Properties.setInt(LAUNCH_COUNTER_KEY, launchesUntilPrompt);
}
};
/**
* Is user already done rate or rejected it
* @return {bool} is off
*/
exports.isOff = function() {
return Ti.App.Properties.getBool(OFF_KEY, false);
};
/**
* Is app crashed
* @return {bool} is crashed
*/
exports.isCrashed = function() {
return Ti.App.Properties.getBool(CRASHED_KEY, false);
};
/**
* Set mark as rate done and stop timers
*/
exports.switchOff = function() {
clearTimer();
clearDialogTimer();
Ti.App.Properties.setBool(OFF_KEY, true);
};
/**
* Set mark as rate not done and start timer
*/
exports.switchOn = function() {
Ti.App.Properties.setBool(OFF_KEY, false);
resume();
};
/**
* Goes to application store page
*/
exports.rate = function() {
// no need more
this.switchOff();
// detect android device
if( Titanium.Platform.osname == 'android' ){
var intent, url;
try {
url = "market://details?id=" + appId;
// To count with Play market backstack, After pressing back button,
// to taken back to our application, we need to add following flags to intent.
var flags = Ti.Android.FLAG_ACTIVITY_NO_HISTORY | Ti.Android.FLAG_ACTIVITY_MULTIPLE_TASK |
(Ti.Platform.Android.API_LEVEL >= 21 ? 524288 //FLAG_ACTIVITY_NEW_DOCUMENT
:Ti.Android.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent = Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
data: url,
//packageName: "com.android.vending",
//className: "com.google.android.finsky.activities.LaunchUrlHandlerActivity",
flags: flags
});
Ti.Android.currentActivity.startActivity(intent);
} catch(e) {
url = "https://play.google.com/store/apps/details?id=" + appId;
Ti.API.debug(TAG, 'Use failback rate url:', url);
intent = Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
data: url
//packageName: "com.android.vending",
//className: "com.google.android.finsky.activities.LaunchUrlHandlerActivity"
});
try {
Ti.Android.currentActivity.startActivity(intent);
} catch(e) {
url = "market://details?id=" + appId;
Ti.API.debug(TAG, 'Use failback rate url:', url);
Ti.Platform.openURL(url);
}
}
// detect iphone and ipad devices
} else {
if (Ti.Platform.version < 7) {
Ti.Platform.openURL("itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=" + appId);
} else {
Ti.Platform.openURL("itms-apps://itunes.apple.com/app/id" + appId);
}
}
};
/**
* Get remained launches until prompt
* @return {number} remained launches
*/
exports.getLaunchesUntilPrompt = function() {
return Ti.App.Properties.getInt(LAUNCH_COUNTER_KEY, 0);
};
/**
* Reminder remained time
* @return {number} remained time in seconds
*/
exports.getRemainedTimeout = function() {
return Ti.App.Properties.getInt(USAGE_TIMEOUT_KEY, 0);
};
function clearTimer() {
if(_timer) {
clearInterval(_timer);
_timer = null;
}
}
function clearDialogTimer() {
if(_dialogTimeout) {
clearTimeout(_dialogTimeout);
_dialogTimeout = null;
}
}
function tick() {
if(exports.isOff()) return;
var timeoutValue = exports.getRemainedTimeout();
if(timeoutValue === 0) return;
timeoutValue = Math.max(0, timeoutValue - timerInterval);
Ti.App.Properties.setInt(USAGE_TIMEOUT_KEY, timeoutValue);
if(timeoutValue === 0) {
Ti.API.debug(TAG, 'by timer');
showReminderPrompt();
}
}
function showReminderPrompt() {
if(exports.isOff() || (!showIfAppHasCrashed && exports.isCrashed())) return;
clearDialogTimer();
clearTimer();
exports.resetCounters();
if(promptDelay) {
_dialogTimeout = setTimeout(function() {
exports.showRateDialog();
_dialogTimeout = null;
}, promptDelay * 1000);
} else {
exports.showRateDialog();
}
}
/**
* Show rate me dialog
*/
exports.showRateDialog = function() {
if(_.isFunction(this.onRate)) {
this.onRate();
return;
}
var dialog = Ti.UI.createAlertDialog({
title: L('rateme_title', 'Feedback'),
message: String.format(
L('rateme_message', 'Thank you for using %s! It would mean a lot for us if you take a minute to rate us at the %s!'),
Ti.App.name, this.storeName),
buttonNames: [
L('rateme_option_ratenow', 'Rate now'),
L('rateme_option_off', "Don't remind me again"),
L('rateme_option_later', 'Not now')
],
cancel: 2
});
dialog.addEventListener('click', function(evt){
switch(evt.index) {
case 0 : // rate
exports.rate();
break;
case 1: // don't remind
exports.switchOff();
break;
case 2: // not now
exports.switchOn();
break;
}
});
dialog.show();
};
function decrementLauchCounter() {
var counter = Ti.App.Properties.getInt(LAUNCH_COUNTER_KEY);
if(counter) Ti.App.Properties.setInt(LAUNCH_COUNTER_KEY, --counter);
if(!exports.isOff() && counter === 0) {
// delay for window open
Ti.API.debug(TAG, 'by launch counter');
_.delay(showReminderPrompt, 2000);
}
}
decrementLauchCounter();