2015-09-21 18:05:33 -07:00
|
|
|
|
(function () {
|
|
|
|
|
|
2015-09-24 12:30:25 -07:00
|
|
|
|
var lastStart = 0;
|
|
|
|
|
|
2015-09-21 18:05:33 -07:00
|
|
|
|
function onDeviceReady() {
|
|
|
|
|
|
|
|
|
|
var fetcher = window.BackgroundFetch;
|
|
|
|
|
|
|
|
|
|
fetcher.configure(onBackgroundFetch, onBackgroundFetchFailed, {
|
|
|
|
|
stopOnTerminate: false // <-- false is default
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onSyncFinish() {
|
|
|
|
|
|
|
|
|
|
Logger.log('BackgroundFetch completed');
|
|
|
|
|
|
|
|
|
|
var fetcher = window.BackgroundFetch;
|
|
|
|
|
fetcher.finish(); // <-- N.B. You MUST called #finish so that native-side can signal completion of the background-thread to the os.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onSyncFail() {
|
|
|
|
|
|
|
|
|
|
Logger.log('BackgroundFetch completed - sync failed');
|
|
|
|
|
|
|
|
|
|
var fetcher = window.BackgroundFetch;
|
|
|
|
|
fetcher.finish(); // <-- N.B. You MUST called #finish so that native-side can signal completion of the background-thread to the os.
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-24 12:30:25 -07:00
|
|
|
|
function startSync(uploadPhotos) {
|
|
|
|
|
lastStart = new Date().getTime();
|
2015-09-21 18:05:33 -07:00
|
|
|
|
|
|
|
|
|
require(['localsync'], function () {
|
|
|
|
|
|
|
|
|
|
if (LocalSync.getSyncStatus() == 'Syncing') {
|
|
|
|
|
onSyncFinish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var syncOptions = {
|
2015-09-24 12:30:25 -07:00
|
|
|
|
uploadPhotos: uploadPhotos
|
2015-09-21 18:05:33 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LocalSync.sync(syncOptions).done(onSyncFinish).fail(onSyncFail);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-24 12:30:25 -07:00
|
|
|
|
function onBackgroundFetch() {
|
|
|
|
|
|
|
|
|
|
Logger.log('BackgroundFetch initiated');
|
2015-09-25 05:53:38 -07:00
|
|
|
|
startSync(true);
|
2015-09-24 12:30:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-21 18:05:33 -07:00
|
|
|
|
function onBackgroundFetchFailed() {
|
|
|
|
|
Logger.log('- BackgroundFetch failed');
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-24 12:30:25 -07:00
|
|
|
|
var syncInterval = 1800000;
|
|
|
|
|
|
|
|
|
|
function restartInterval() {
|
|
|
|
|
|
2015-09-25 05:53:38 -07:00
|
|
|
|
setInterval(function () {
|
2015-09-24 12:30:25 -07:00
|
|
|
|
|
2015-09-25 05:53:38 -07:00
|
|
|
|
//startSync(true);
|
2015-09-24 12:30:25 -07:00
|
|
|
|
|
2015-09-25 05:53:38 -07:00
|
|
|
|
}, syncInterval);
|
2015-09-24 22:15:29 -07:00
|
|
|
|
|
2015-09-25 05:53:38 -07:00
|
|
|
|
if (lastStart > 0 && (now - lastStart) >= syncInterval) {
|
|
|
|
|
//startSync(true);
|
|
|
|
|
}
|
2015-09-24 12:30:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dashboard.ready(restartInterval);
|
|
|
|
|
document.addEventListener("resume", restartInterval, false);
|
|
|
|
|
|
2015-09-21 18:05:33 -07:00
|
|
|
|
onDeviceReady();
|
|
|
|
|
})();
|