|
var _properties = {}, _platform; |
|
|
|
function plus(_points, _show) { |
|
|
|
if (typeof _points !== 'number') { |
|
_points = 1; |
|
} |
|
|
|
return _trigger(_points, _show); |
|
} |
|
|
|
function test() { |
|
return _trigger(); |
|
} |
|
|
|
function ask() { |
|
|
|
if (typeof _platform !== 'string') { |
|
_platform = Ti.Platform.name; |
|
} |
|
|
|
if (_platform === 'iPhone OS' && !exports.iTunesTrackId) { |
|
|
|
_lookup(function (result) { |
|
|
|
if (!result) { |
|
Ti.API.debug('[RATE] Lookup failed.'); |
|
return; |
|
} |
|
|
|
exports.iTunesTrackId = result.trackId; |
|
_ask(); |
|
|
|
return; |
|
}); |
|
|
|
return; |
|
} |
|
|
|
var buttonNames = [exports.yes, exports.later, exports.never]; |
|
var cancel = buttonNames.length - 1; |
|
|
|
var alertDialog = Titanium.UI.createAlertDialog({ |
|
title: exports.title, |
|
message: exports.message, |
|
buttonNames: buttonNames, |
|
cancel: cancel |
|
}); |
|
|
|
alertDialog.addEventListener('click', function(e) { |
|
|
|
if (buttonNames[e.index] === exports.yes) { |
|
Ti.App.Properties.setBool('rate_done', true); |
|
|
|
show(); |
|
|
|
} else if (buttonNames[e.index] === exports.never) { |
|
Ti.App.Properties.setBool('rate_done', true); |
|
} |
|
|
|
return; |
|
}); |
|
|
|
alertDialog.show(); |
|
|
|
return; |
|
} |
|
|
|
function open() { |
|
|
|
if (Ti.Platform.name === 'android') { |
|
Ti.Platform.openURL('market://details?id=' + Ti.App.id); |
|
|
|
} else if (Ti.Platform.name === 'iPhone OS') { |
|
|
|
if (!exports.iTunesTrackId) { |
|
_lookup(function (result) { |
|
|
|
if (!result) { |
|
Ti.API.debug('[RATE] Lookup failed.'); |
|
return; |
|
} |
|
|
|
exports.iTunesTrackId = result.trackId; |
|
return open(); |
|
}); |
|
|
|
return; |
|
} |
|
|
|
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'); |
|
} |
|
|
|
return; |
|
} |
|
|
|
function reset() { |
|
Ti.API.debug('[RATE] Reset.'); |
|
|
|
Ti.App.Properties.removeProperty('rate_done'); |
|
Ti.App.Properties.removeProperty('rate_points'); |
|
Ti.App.Properties.removeProperty('rate_asked'); |
|
|
|
return; |
|
} |
|
|
|
function _trigger(_points, _show) { |
|
|
|
if (Ti.App.Properties.getBool('rate_done', false) === true) { |
|
Ti.API.debug('[RATE] Rating already done or rejected.'); |
|
return; |
|
} |
|
|
|
var points = Ti.App.Properties.getInt('rate_points', 0); |
|
|
|
if (_points) { |
|
points = points + (_points || 1); |
|
Ti.API.debug('[RATE] Rating points changed to: ' + points); |
|
} |
|
|
|
var now = (new Date() / 1000), |
|
checked = Ti.App.Properties.getInt('rate_asked', 0); |
|
|
|
if (_show !== false && points >= exports.pointsBetween && (now - checked) >= (exports.daysBetween * 86400)) { |
|
Ti.API.debug('[RATE] Rating triggered!'); |
|
|
|
Ti.App.Properties.setInt('rate_points', 0); |
|
Ti.App.Properties.setInt('rate_asked', now); |
|
|
|
ask(); |
|
|
|
} else { |
|
Ti.App.Properties.setInt('rate_points', points); |
|
} |
|
|
|
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 { |
|
Ti.API.error('[XHR:response] ' + this.responseText); |
|
} |
|
|
|
} catch (err) { |
|
Ti.API.error('[XHR:json] ' + JSON.stringify(err)); |
|
} |
|
} |
|
|
|
_callback(); |
|
return; |
|
}, |
|
onerror: function (e) { |
|
Ti.API.error('[XHR:error] ' + JSON.stringify(e.error)); |
|
_callback(); |
|
return; |
|
} |
|
}); |
|
|
|
var url = 'http://itunes.apple.com/lookup?'; |
|
|
|
if (exports.iTunesTrackId) { |
|
url = url + 'id=' + exports.iTunesTrackId; |
|
} else { |
|
url = url + 'bundleId=' + Ti.App.id; |
|
} |
|
|
|
xhr.open('GET', url); |
|
xhr.send(); |
|
|
|
return; |
|
} |
|
|
|
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.daysBetween = 3; |
|
exports.pointsBetween = 3; |
|
|
|
exports.plus = plus; |
|
exports.test = test; |
|
exports.ask = ask; |
|
exports.open = open; |
|
exports.reset = reset; |
One advice to those using this module ( not a technical issue):
Take GOOD CARE what you are putting there in the title and message, Apple is very picky related to this, don't ask the user to give a good rating - they consider that you are braking the point 3.10 in the guidelines.
I know this as we got a rejection with ArtistBox because of this:)