Skip to content

Instantly share code, notes, and snippets.

@migueldeicaza
Created February 24, 2010 00:09
Show Gist options
  • Save migueldeicaza/312891 to your computer and use it in GitHub Desktop.
Save migueldeicaza/312891 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Collections.Generic;
using MonoTouch.Dialog;
using MonoTouch.UIKit;
using System.Text;
using System.Drawing;
namespace MIX10
{
public partial class HomeViewController : DialogViewController {
const int days = 4;
static DateTime [] DayStarts;
static HomeViewController ()
{
DayStarts = new DateTime [days+1];
for (int i = 0; i < days+1; i++)
DayStarts [i] = new DateTime (2010, 3, 14+i);
}
// Used to format a date
static string FormatDate (DateTime date, DateTime now)
{
if (date.Year == now.Year && date.Month == now.Month){
if (date.Day == now.Day)
return "Today";
if (date.AddDays (1).Day == now.Day)
return "Yesterday";
if (date.AddDays (-1).Day == now.Day)
return "Tomorrow";
}
return date.ToString ("dddd");
}
// Pretifies a caption to show a nice time relative to the current time.
public static string MakeCaption (string caption, DateTime start)
{
string date;
var now = DateTime.Now;
if (start.Year == now.Year && start.Month == now.Month){
if (start.Day == now.Day)
date = "";
else if (start.Day == now.Day+1)
date = "tomorrow at";
else
date = start.ToString ("MMM dd");
} else
date = start.ToString ("MMM dd");
return String.Format ("{0}{1} {2} {3}", caption, caption != "" ? " " : "", date, start.ToString ("H:mm"));
}
// Fills out the schedule for a given day
public Element MakeSchedule (DateTime d)
{ // Added .ToString/Convert.ToDateTime
// to avoid System.ExecutionEngineException: Attempting to JIT compile method 'System.Collections.Generic.GenericEqualityComparer`1<System.DateTime>:.ctor ()' while
running with --aot-only.
var sections = from s in AppDelegate.ConferenceData.Sessions
where s.Start.Day == d.Day
orderby s.Start ascending
group s by s.Start.ToString() into g
select new Section (MakeCaption ("", Convert.ToDateTime(g.Key))) {
from hs in g
select (Element) new SessionElement (hs)
};
var root = new RootElement (FormatDate (d, DateTime.Now));
foreach (var s in sections)
root.Add (s);
return root;
}
// Appends the favorites
void AppendFavorites (Section section, IEnumerable<MIX10Xml.Session> list)
{
var favs = AppDelegate.UserData.GetFavoriteCodes ();
var favsessions = from s in list
where favs.Contains (s.Code)
select (Element) new SessionElement (s);
section.Add (favsessions);
}
// Appends a section to the list.
public bool AppendSection (RootElement root, IEnumerable<MIX10Xml.Session> list, string title)
{
if (list == null || list.Count () == 0)
return false;
var sschedule = new Section ();
var schedule = new RootElement ("Sessions") { sschedule };
foreach (var s in list)
sschedule.Add (new SessionElement (s));
var section = new Section (title);
section.Add (schedule);
AppendFavorites (section, list);
root.Add (section);
return true;
}
// Use this to work with the .100 release of the DLL
UIBarButtonItem item, item2;
public HomeViewController () : base (null)
{
UIImage celebSpotterIcon = UIImage.FromFile("Images/eye.png");
item = new UIBarButtonItem (celebSpotterIcon, UIBarButtonItemStyle.Bordered, (o,s) => {CelebSpot ();});
NavigationItem.SetRightBarButtonItem (item, false);
var infoLight = UIButton.FromType(UIButtonType.InfoLight);
infoLight.Frame = new RectangleF(0f, 10f, 35f, 35f);
infoLight.TouchUpInside += (s, e) => { About();};
item2 = new UIBarButtonItem(infoLight);
NavigationItem.SetLeftBarButtonItem (item2, false);
}
void About ()
{
var about = new AboutViewController ();
ActivateController (about);
}
public override void ViewWillAppear (bool animated)
{
Root = GenerateRoot ();
base.ViewWillAppear (animated);
}
RootElement GenerateRoot ()
{
var now = DateTime.Now;
var nowStart = now.AddMinutes (-now.Minute);
// The shortest session appears to be 30 minutes that I could see
var nextStart = nowStart.AddMinutes (30);
var happeningNow = from s in AppDelegate.ConferenceData.Sessions
where s.Start < now && s.End > now
select s;
// Added .ToString/Convert.ToDateTime
// to avoid System.ExecutionEngineException: Attempting to JIT compile method 'System.Collections.Generic.GenericEqualityComparer`1<System.DateTime>:.ctor ()' while
running with --aot-only.
var allUpcoming = from s in AppDelegate.ConferenceData.Sessions
where s.Start >= nextStart
orderby s.Start.Ticks
group s by s.Start.Ticks into g
select new { Start = g.Key, Sessions = g };
var upcoming = allUpcoming.FirstOrDefault ();
var root = new RootElement ("MIX10 Conference");
var haveNow = AppendSection (root, happeningNow, MakeCaption ("On Now", nowStart));
AppendSection (root, upcoming.Sessions, MakeCaption ("Up Next", new DateTime (upcoming.Start)));
// Get also the next slot
if (!haveNow){
upcoming = allUpcoming.Skip (1).FirstOrDefault ();
AppendSection (root, upcoming.Sessions, "Afterwards");
}
var full = new Section ("Full Schedule");
for (int i = 1; i < DayStarts.Length; i++)
if (now < DayStarts [i])
full.Add (MakeSchedule (DayStarts [i-1]));
root.Add (full);
return root;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment