-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappRating.js
156 lines (138 loc) · 4.71 KB
/
appRating.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
/**
* @author Chris Young
*/
var appRating = {};
(function() {
appRating.daysUntilPrompt= 5;
appRating.timeBeforeReminder= 1;
appRating.usesUntilPrompt= 3;
appRating.appId = 'Your App ID';
/*Private */
var messageTitle= String.format('Rate %@', Ti.App.getName());
var templateReviewURL= 'itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@';
var rateButton= String.format('Rate %@', Ti.App.getName());
var ratingMessage= String.format('If you enjoy using %@, would you mind taking a moment to rate it? It won\'t take more than a minute. Thanks for your support!', Ti.App.getName());
var cancelButton= 'No, Thanks';
var remindMeLater= 'Remind Me later';
incrementUseCount = function incrementUseCount () {
var version = Ti.App.getVersion();
var trackingVersion = Ti.App.Properties.getString('CurrentVersion');
if(trackingVersion == null) {
trackingVersion = version;
Ti.App.Properties.setString('CurrentVersion', trackingVersion);
}
Ti.API.info('Current Tracking Version: ' + trackingVersion);
if(trackingVersion == version) {
var timeInterval = Ti.App.Properties.getDouble('FirstUseDate', 0);
if(timeInterval == 0) {
timeInterval = new Date().getTime();
Ti.App.Properties.setDouble('FirstUseDate', timeInterval);
}
var useCount = Ti.App.Properties.getInt('UseCount', 0);
useCount++;
Ti.App.Properties.setInt('UseCount', useCount);
Ti.API.info('Use Count: ' + useCount);
} else {
//new version
Ti.App.Properties.setString('CurrentVersion', version);
Ti.App.Properties.setDouble('FirstUseDate', new Date().getTime());
Ti.App.Properties.setInt('UseCount', 1);
Ti.App.Properties.setBool('RatedCurrentVersion', false);
Ti.App.Properties.setBool('DeclinedToRate', false);
Ti.App.Properties.setDouble('ReminderRequestDate', 0);
}
}
appRating.ratingConditionsHaveBeenMet = function ratingConditionsHaveBeenMet() {
var dateOfFirstLaunch = Ti.App.Properties.getDouble('FirstUseDate');
var timeSinceFirstLaunch = new Date().getTime() - dateOfFirstLaunch;
var timeUntilRate = 60 * 60 * 24 * appRating.daysUntilPrompt;
Ti.API.info('First Launch: ' + dateOfFirstLaunch);
Ti.API.info('Since First Launch: ' + timeSinceFirstLaunch);
Ti.API.info('Time Until Rate: ' + timeUntilRate);
if(timeSinceFirstLaunch < timeUntilRate) {
return false;
}
var useCount = Ti.App.Properties.getInt('UseCount');
Ti.API.info('Used: ' + useCount);
if(useCount <= appRating.usesUntilPrompt) {
return false;
}
if(Ti.App.Properties.getBool('DeclinedToRate')) {
return false;
}
if(Ti.App.Properties.getBool('RatedCurrentVersion')) {
return false;
}
var reminderRequestDate = Ti.App.Properties.getDouble('ReminderRequestDate');
var timeSinceReminderRequest = new Date().getTime() - reminderRequestDate;
var timeUntilReminder = 60 * 60 * 24 * appRating.timeBeforeReminder;
if(timeSinceReminderRequest < timeUntilReminder) {
return false;
}
return true;
}
appRating.incrementAndRate= function incrementAndRate() {
incrementUseCount();
if(appRating.ratingConditionsHaveBeenMet()) {
appRating.showRatingAlert();
}
}
appRating.showRatingAlert = function showRatingAlert() {
var a = Titanium.UI.createAlertDialog({
title: messageTitle,
message: ratingMessage,
buttonNames: [rateButton, remindMeLater, cancelButton],
cancelButton: 2
});
a.addEventListener('click', function(e) {
switch(e.index) {
case 0:
Ti.API.info('Rate ' + Ti.Platform.model);
if(Ti.Platform.model == 'simulator') {
Ti.API.info('Launching rating url');
} else {
var reviewUrl = String.format(templateReviewURL, appRating.appId);
Ti.App.Properties.setBool('RatedCurrentVersion', true);
Ti.API.info('Launching rating url: ' + reviewUrl);
Ti.Platform.openURL(reviewUrl);
}
break;
case 1:
Ti.API.info('Later');
Ti.App.Properties.setDouble('ReminderRequestDate', new Date().getTime());
break;
case 2:
Ti.API.info('Declined to rate');
Ti.App.Properties.setBool('DeclinedToRate', true);
break;
default:
break;
}
});
a.show();
}
})();
(function() {
appRating.incrementAndRate();
if (isiOS4Plus()) {
// fired when an app resumes for suspension
Ti.App.addEventListener('resume', function(e) {
appRating.incrementAndRate();
});
Ti.App.addEventListener('resumed', function(e) {
appRating.incrementAndRate();
});
}
})();
function isiOS4Plus() {
// add iphone specific tests
if (Titanium.Platform.name == 'iPhone OS') {
var version = Titanium.Platform.version.split(".");
var major = parseInt(version[0]);
// can only test this support on a 3.2+ device
if (major >= 4) {
return true;
}
}
return false;
}