forked from jasonkneen/UTiL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
executable file
·209 lines (158 loc) · 5.22 KB
/
update.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
function test() {
return _trigger();
}
function check() {
return _trigger(true);
}
function open() {
if (!exports.appleId) {
_lookup(function (result) {
if (!result) {
return;
}
exports.appleId = result.trackId;
return open();
});
return;
}
Ti.Platform.openURL('http://itunes.apple.com/app/id' + exports.appleId);
return;
}
function reset() {
console.debug('[UPDATE] Reset.');
Ti.App.Properties.removeProperty('update_checked');
Ti.App.Properties.removeProperty('update_never');
return;
}
function _trigger(forced) {
if (Ti.Platform.name !== 'iPhone OS') {
console.debug('[UPDATE] Platform is not iOS.');
return;
}
var now = (new Date() / 1000);
if (!forced) {
var checked = Ti.App.Properties.getInt('update_checked', 0);
if (now - checked < (exports.daysBetween * 86400)) {
console.debug('[UPDATE] Checked less than ' + exports.daysBetween + ' days ago.');
return;
}
}
_lookup(function (result) {
if (!result) {
return;
}
Ti.App.Properties.setInt('update_checked', now);
if (_cmpVersion(result.version, Ti.App.version) <= 0) {
console.debug('[UPDATE] No new version.');
return;
}
if (!forced) {
var never = Ti.App.Properties.getString('update_never');
if (never === result.version) {
console.debug('[UPDATE] Never ask again for this version.');
return;
} else {
Ti.App.Properties.removeProperty('update_never');
}
}
var buttonNames, cancel;
if (forced) {
buttonNames = [exports.later, exports.yes];
cancel = 0;
} else {
buttonNames = [exports.yes, exports.later, exports.never];
cancel = 2;
}
var title, messsage;
if (exports.title) {
title = exports.title;
if (exports.title.indexOf('%') !== -1) {
title = title.replace('%version', result.version);
}
}
if (exports.message) {
message = exports.message;
if (exports.message.indexOf('%') !== -1) {
message = exports.message
.replace('%version', result.version)
.replace('%notes', result.releaseNotes);
}
}
var alertDialog = Titanium.UI.createAlertDialog({
title: title,
message: message,
buttonNames: buttonNames,
cancel: cancel
});
alertDialog.addEventListener('click', function(e) {
if (buttonNames[e.index] === exports.yes) {
open();
} else if (buttonNames[e.index] === exports.never) {
Ti.App.Properties.setString('update_never', result.version);
}
return;
});
alertDialog.show();
return;
});
return;
}
function _lookup(_callback) {
var xhr = Ti.Network.createHTTPClient({
onload: function (e) {
if (xhr.status === 200 && this.responseText) {
try {
var json = JSON.parse(this.responseText);
if (json.resultCount === 1) {
_callback(json.results[0]);
return;
} else {
console.error('[UPDATE] LOOKUP ERROR ' + this.responseText);
}
} catch (err) {
console.error('[UPDATE] LOOKUP ERROR ' + JSON.stringify(err));
}
}
_callback();
return;
},
onerror: function (e) {
console.error('[UPDATE] LOOKUP ERROR ' + JSON.stringify(e.error));
_callback();
return;
}
});
var url = 'http://itunes.apple.com/lookup?';
if (exports.appleId) {
url = url + 'id=' + exports.appleId;
} else {
url = url + 'bundleId=' + Ti.App.id;
}
xhr.open('GET', url);
xhr.send();
return;
}
function _cmpVersion(a, b) {
var i, cmp, len, re = /(\.0)+[^\.]*$/;
a = (a + '').replace(re, '').split('.');
b = (b + '').replace(re, '').split('.');
len = Math.min(a.length, b.length);
for( i = 0; i < len; i++ ) {
cmp = parseInt(a[i], 10) - parseInt(b[i], 10);
if( cmp !== 0 ) {
return cmp;
}
}
return a.length - b.length;
}
exports.title = 'New version available';
exports.message = 'Upgrade to %version for:\n\n%notes';
exports.yes = 'Yes';
exports.later = 'Not now';
exports.never = 'No, thank you';
exports.appleId = null;
exports.daysBetween = 1;
exports.test = test;
exports.check = check;
exports.open = open;
exports.reset = reset;