Skip to content

Instantly share code, notes, and snippets.

@AppWerft
Created June 2, 2015 07:53
Show Gist options
  • Save AppWerft/26fa43705fb95b78bc3c to your computer and use it in GitHub Desktop.
Save AppWerft/26fa43705fb95b78bc3c to your computer and use it in GitHub Desktop.
loadEventsofDays : function() {
var events = [];
var that = this;
var ok = this.getEventsofDay(_date);
if (Ti.Network.online) {
var xhr = Ti.Network.createHTTPClient({
timeout : 30000,
onload : function() {
var end = new Date().getTime();
if (this.status == 200) {
Ti.Android && require('bencoding.alarmmanager').createAlarmManager().addAlarmNotification({
requestCode : Math.random(),
second : 1,
contentTitle : 'Kulturlotsendatenabgleich',
contentText : 'Veranstaltungen ' + Moment(_date).format('DD.MM.YYYY') + ' erneuert',
playSound : true,
icon : Ti.App.Android.R.drawable.notification,
});
var json = JSON.parse(this.responseText);
var link = Ti.Database.open(DB);
link.execute("BEGIN");
json.eventsOfTheDay.forEach(function(json) {
link.execute("INSERT OR REPLACE INTO events VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", //
_date, json.eventid, '' + json.locationId, json.title, json.text, json.imageToken, json.locationName, json.locationStreet, json.locationPlz, json.locationCity, json.categoryId, json.displayDate, json.featured);
});
link.execute("COMMIT");
link.execute("INSERT OR REPLACE INTO days VALUES (?,?,?,?)", _date, xhr.getResponseHeader('ETag'), this.responseText.length, Moment().unix());
link.close();
}
that.getEventsofDay(_date);
},
onerror : function() {
if (_date == Moment().format('YYYY-MM-DD'))
if (ok) {
Ti.UI.createNotification({
message : 'Der Kulturlotsenserver hat keine gültigen Daten, allerdings gibt es alte Daten, die jetzt angezeigt werden.'
}).show();
} else {
var dialog = Ti.UI.createAlertDialog({
message : 'Leider ist zur Zeit der Kulturlotsen-Server nicht erreichbar.\nEin Problem, dass nicht in Verantwortung der AppWerft liegt.',
ok : 'Okay',
title : 'Datenserver ohne Antwort'
});
dialog.show();
}
}
});
var url = Ti.App.Properties.getString("ENDPOINT") + "multidaily";
xhr.open("POST", url);
xhr.setRequestHeader('Accept', 'application/json; charset=utf-8');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Accept-Encoding', 'gzip, deflate');
var link = Ti.Database.open(DB);
var res = link.execute("SELECT * FROM days");
var body = [];
while (res.isValidRow()) {
body.push({
day : res.getFieldByName('day'),
hash : res.getFieldByName('hash')
});
res.next();
}
res.close();
link.close();
xhr.send(JSON.stringify(body));
var start = new Date().getTime();
} else {
this.fireEvent("eventsready", {
error : true,
reason : 'offline'
});
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment