forked from raulriera/Rater-Module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrater.js
149 lines (119 loc) · 3.74 KB
/
rater.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
/*
Rater Module for Appcelerator Titanium
About:
Created by Greg Pierce, http://agiletortoise.com
Modified by Raul Riera, http://raulriera.com
*/
var Rater = {
appName: '',
appId: '',
appLaunches: 20,
appUsageInSeconds: 0 // 0 if you don't want to use this
};
Rater.data = {
launchCount: 0,
timeUsed: 0,
neverRemind: false
};
Rater.startUsageTimer = function(){
Rater.usageTimerInterval = setInterval(function() {
Rater.data.timeUsed++;
// Debugging
// Ti.API.info(Rater.data.timeUsed + " seconds of app usage");
// Check if the desired usage time has been reached
if(Rater.data.timeUsed === Rater.appUsageInSeconds) {
// Pause the timer
Rater.pauseUsageTimer();
// Open the rating dialog
Rater.openRateDialog();
}
}, 1000);
};
Rater.pauseUsageTimer = function(){
clearInterval(Rater.usageTimerInterval);
};
Rater.initUsageCounter = function(){
// Check if the user wants to use this feature
if (Rater.appUsageInSeconds > 0) {
Rater.startUsageTimer();
}
};
Rater.load = function(){
// Read the data
Rater.read();
// Increase the launch count
Rater.data.launchCount++;
// Init the usage counter
Rater.initUsageCounter();
// Save the data
Rater.save();
};
Rater.read = function(){
var prop = Ti.App.Properties.getString("RaterData", null);
if(prop) {
Rater.data = JSON.parse(prop);
}
};
Rater.save = function(){
Ti.App.Properties.setString("RaterData", JSON.stringify(Rater.data));
};
Rater.run = function(){
if(Rater.data.neverRemind || Rater.data.launchCount % Rater.appLaunches != 0) { return; }
Rater.openRateDialog();
};
Rater.openRateDialog = function(){
var a = Ti.UI.createAlertDialog({
title: L("rating_title"),
message: String.format(L('rating_message'), Rater.appName),
buttonNames: [L("rating_option_1"), L("rating_option_2"), L("rating_option_3")],
cancel: 2
});
a.addEventListener('click',function(e){
switch(e.index) {
case 0 : // rate
Rater.data.neverRemind = true;
Rater.save();
// detect android device
if( Titanium.Platform.osname == 'android' ){
Ti.Platform.openURL("market://details?id=" + Ti.App.id);
}
else {
Ti.Platform.openURL("itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=" + Rater.appId);
}
break;
case 1 : // don't remind
Rater.data.neverRemind = true;
Rater.save();
break;
}
});
a.show();
};
exports.gotoRating = function(_appId) {
if (!_appId) _appId = Rater.appId;
// detect android device
if( Titanium.Platform.osname == 'android' ){
Ti.API.info('Opening rater for android with id: ' + Ti.App.id);
Ti.Platform.openURL("market://details?id=" + Ti.App.id);
}
else{
// detect iphone and ipad device
Ti.API.info('Opening rater for iOS with id: ' + _appId);
Ti.Platform.openURL("itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=" + _appId);
}
};
exports.init = function(_appName, _appId) {
Rater.load();
// Set the default values
Rater.appName = _appName;
Rater.appId = _appId;
Rater.run();
};
Ti.App.addEventListener('resumed', function(e){
// Read the data again
Rater.read();
});
Ti.App.addEventListener('pause', function(e){
// Save the data
Rater.save();
});