Skip to content

Instantly share code, notes, and snippets.

@Corliosity
Created January 9, 2013 16:41
Show Gist options
  • Save Corliosity/cf429a90f3622e327c59 to your computer and use it in GitHub Desktop.
Save Corliosity/cf429a90f3622e327c59 to your computer and use it in GitHub Desktop.
/**
* @author Andrew Corliss
*/
function Calendar(navController)
{
var events = require('data/events');
var newEvent = new events();
Titanium.Calendar = Ti.Calendar = require("ag.calendar");
//If I use event kit with recursion that will solve event duplication
//however wait to hear back from ChrisRM (github name) if there is another solution for problem.
//it seems the fix may only effect the eventkit framework so lets test on that theory
Ti.Calendar.dataSource("coredata");
var osname = Ti.Platform.osname;
var self = Ti.UI.createWindow
({
title: 'TSTA Calendar',
backgroundColor: '#eaeaea',
backButtonTitle: 'Home',
barColor: '#b0b0b0'
//barColor: '#eaeaea'
});
var calendarView = Ti.Calendar.createView({
top : 0,
editable : true,
color : "white"
});
// Button to select todays date
var todayButton = Ti.UI.createButton({
title : "Today"
});
// Eventlistener
todayButton.addEventListener("click", function() {
calendarView.selectTodaysDate();
});
/*
calendarView.addEventListener('month:previous', function(e) {
alert("Previous month event received");
});
calendarView.addEventListener('month:following', function(e) {
alert("Following month event received");
});
Ti.Calendar.addEventListener('month:previous', function(e) {
alert("Previous month event received");
});
Ti.Calendar.addEventListener('month:following', function(e) {
alert("Following month event received");
});
*/
// Now, let's create an eventlistener to get the data we want
// from our calendar. This event will fire when a user touches
// an event in the tableview.
calendarView.addEventListener('event:clicked', function(e) {
var event = e.event;
// Log event details
Ti.API.info(JSON.stringify(event));
// Event dates is returned as strings.
// To convert the string to a Date() object, input
// the string in a Date([string]) as shown below.
var toDateObj = new Date(event.startDate);
// Now you can trigger all date functions
// http://www.w3schools.com/jsref/jsref_obj_date.asp
// This example utilizes the toUTCString to show date according to universal time
Ti.API.info("This event will start: " + toDateObj.toUTCString());
alert("The Event " + event.title + " Will Start " + toDateObj.toUTCString());
});
Ti.App.addEventListener('get.events', function(d) {
jsonArray = JSON.parse(d.responseText);
//Ti.API.info(jsonArray);
for (var i = 0; i < jsonArray.length; i++) {
Ti.API.info(jsonArray[i].title);
//To view the date format into YYYY/MM/DD
//Then Add our formated date to the new Date Object
//Add to endDate as well to set the new day
var dateStr = jsonArray[i].field_event_start_date_value;
var ourstr = dateStr.replace(/T00:00:00/g, '');
//Ti.API.info(jsonArray[i].title + ' ' + ourstr);
var totalDate = ourstr.replace(/-/g, '/');
//Ti.API.info(totalDate);
//Ti.API.info(new Date(totalDate));
//Create an endDate String to add
var ending = jsonArray[i].field_event_start_date_value2;
var endingstr = ending.replace(/T:00:00:00/g, '');
var createDate = endingstr.replace(/-/g, '/');
var endDate = new Date(createDate);
endDate.setHours(endDate.getHours()+3); // Set event to last 3 hours.
//endDate.setHours(endDate.getHours() + 3);
// Set event to last 3 hours.
// Date to end our recurring event
//var recurringEnd = new Date(totalDate);
//recurringEnd.setMonth(recurringEnd.getMonth());
//Recurring ends in 6 months
// - Failing due to Memory leaks. Need to fix start and end dates so events are only shown when occuring
Ti.Calendar.addEvent({
title : jsonArray[i].title,
startDate : new Date(totalDate),
endDate : endDate,
location : jsonArray[i].field_event_city_value,
identifier : Ti.Calendar.identifier,
type : "public",
attendees : '',
organizer : jsonArray[i].name,
/*recurrence : {
frequency : "week", // day, week, month, year
interval : 1,
end : recurringEnd
}*/
});
//Ti.API.info(Ti.Calendar.addEvent());
/*
setTimeout(function() {
Ti.Calendar.addEvent();
}, 1000);
*/
/*
var recurringEvent = {};
recurringEvent.title = jsonArray[i].title;
recurringEvent.startDate = new Date(totalDate);
recurringEvent.endDate = endDate;
recurringEvent.identifier = Ti.Calendar.identifier;
recurringEvent.organizer = jsonArray[i].name;
Ti.Calendar.addEvent(recurringEvent);
*/
/*
*
recurringEvent.identifier = Ti.Calendar.identifier;
recurringEvent.type = "private";
recurringEvent.attendees = "Bill Gates, Mark Zuckerberg";
recurringEvent.organizer = "Chris";
Ti.Calendar.addEvent(recurringEvent);
*/
// A fix so when adding more than one event they are not duplicated.
setTimeout(function() {
calendarView.selectTodaysDate();
}, 1000);
}; //End the for loop
});
self.setRightNavButton(todayButton);
self.add(calendarView);
//Temporary fix for adding more than one event
/*
setTimeout(function() {
calendarView.selectTodaysDate();
}, 1000);
*/
return self;
};
module.exports = Calendar;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment