|
var _properties = {}, _platform; |
|
|
|
function plus(_points, _show) { |
|
|
|
if (typeof _points !== 'number') { |
|
_points = 1; |
|
} |
|
|
|
return _trigger(_points, _show); |
|
} |
|
|
|
function test() { |
|
return _trigger(); |
|
} |
|
|
|
function show() { |
|
return _ask(true); |
|
} |
|
|
|
function reset() { |
|
Ti.API.debug('[RATE] Reset.'); |
|
|
|
_property('done', 0); |
|
_property('repeats', 0); |
|
_property('points', 0); |
|
|
|
return; |
|
} |
|
|
|
function _trigger(_points, _show) { |
|
|
|
if (_property('done')) { |
|
Ti.API.debug('[RATE] Rating already done or rejected.'); |
|
return; |
|
} |
|
|
|
if (_property('repeats') >= exports.maxRepeats) { |
|
Ti.API.debug('[RATE] Rating repeat reached maximum: ' + exports.maxRepeats); |
|
return; |
|
} |
|
|
|
if (_points) { |
|
_property('points', _property('points') + (_points || 1)); |
|
Ti.API.debug('[RATE] Rating points changed to: ' + _property('points')); |
|
} |
|
|
|
if (_show === false) { |
|
return; |
|
} |
|
|
|
if (_property('points') >= exports.minimumPoints) { |
|
Ti.API.debug('[RATE] Rating triggered!'); |
|
|
|
_property('repeats', _property('repeats') + 1); |
|
_property('points', 0); |
|
|
|
_ask(); |
|
} |
|
|
|
return; |
|
} |
|
|
|
function _ask(forced) { |
|
|
|
if (typeof _platform !== 'string') { |
|
_platform = Ti.Platform.name; |
|
} |
|
|
|
if (_platform === 'iPhone OS' && !exports.iTunesTrackId) { |
|
_lookup(forced); |
|
return; |
|
} |
|
|
|
var buttonNames; |
|
|
|
if (forced) { |
|
buttonNames = [exports.yes, exports.later]; |
|
} else if (_property('repeats') >= exports.maxRepeats) { |
|
buttonNames = [exports.yes, exports.never]; |
|
} else { |
|
buttonNames = [exports.yes, exports.later, exports.never]; |
|
} |
|
|
|
if (_platform === 'iPhone OS' && buttonNames.length === 2) { |
|
buttonNames.reverse(); |
|
} |
|
|
|
var alertDialog = Titanium.UI.createAlertDialog({ |
|
title: exports.title, |
|
message: exports.message, |
|
buttonNames: buttonNames, |
|
cancel: buttonNames.length - 1 |
|
}); |
|
|
|
alertDialog.addEventListener('click', function(e) { |
|
|
|
if (buttonNames[e.index] === exports.yes) { |
|
_property('done', 1); |
|
|
|
if (Ti.Platform.name === 'android') { |
|
Ti.Platform.openURL('market://details?id=' + Ti.App.id); |
|
|
|
} else if (Ti.Platform.name === 'iPhone OS' && exports.iTunesTrackId) { |
|
Ti.Platform.openURL('itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=' + exports.iTunesTrackId + '&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software'); |
|
} |
|
|
|
} else if (buttonNames[e.index] === exports.never) { |
|
_property('done', 1); |
|
} |
|
|
|
return; |
|
}); |
|
|
|
alertDialog.show(); |
|
|
|
return; |
|
} |
|
|
|
function _lookup(forced) { |
|
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 && json.results[0].trackId) { |
|
exports.iTunesTrackId = json.results[0].trackId; |
|
_ask(forced); |
|
|
|
} else { |
|
Ti.API.error('[RATE:lookup] ' + this.responseText); |
|
} |
|
|
|
} catch (err) { |
|
Ti.API.error('[RATE:xhr] ' + JSON.stringify(err)); |
|
} |
|
} |
|
return; |
|
}, |
|
onerror: function (e) { |
|
Ti.API.error('[RATE:xhr] ' + JSON.stringify(e.error)); |
|
return; |
|
} |
|
}); |
|
|
|
xhr.open('GET', 'http://itunes.apple.com/lookup?bundleId=' + Ti.App.id); |
|
xhr.send(); |
|
|
|
return; |
|
} |
|
|
|
function _property(key, value) { |
|
|
|
if (typeof value === 'number') { |
|
_properties[key] = value; |
|
Ti.App.Properties.setInt('rate_' + key, value); |
|
|
|
} else if (typeof _properties[key] !== 'number') { |
|
_properties[key] = Ti.App.Properties.getInt('rate_' + key, 0); |
|
} |
|
|
|
return _properties[key]; |
|
} |
|
|
|
exports.title = 'Like our app?'; |
|
exports.message = 'Please take a minute to rate it:'; |
|
exports.yes = 'Yes, of course'; |
|
exports.later = 'Ask me later'; |
|
exports.never = 'No, thank you'; |
|
|
|
exports.iTunesTrackId = null; |
|
exports.minimumPoints = 3; |
|
exports.maxRepeats = 3; |
|
|
|
exports.plus = plus; |
|
exports.test = test; |
|
exports.show = show; |
|
exports.reset = reset; |