Skip to content

Instantly share code, notes, and snippets.

@nicwise
Created November 30, 2011 22:17
Show Gist options
  • Save nicwise/1411331 to your computer and use it in GitHub Desktop.
Save nicwise/1411331 to your computer and use it in GitHub Desktop.
using System;
using MonoTouch.Dialog;
using MonoTouch.UIKit;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
namespace TravelBudget
{
//BaseDialogViewController just inherits off DialogViewController
// Sets things like header colour, background gradient etc.
// just common stuff.
public class SummaryDialogViewController : BaseDialogViewController
{
public SummaryDialogViewController () : base(null, false)
{
}
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
LoadView ();
}
LineChart chart = null;
public override void LoadView ()
{
base.LoadView ();
//base method - just sets up the gradient background
SetTableViewBackground();
//if (TotalBudget == null) return;
DataSource.Instance.CalculateSummaryData();
//go talk to my DAL to get some data from the DB.
Summary sum = DataSource.Instance.SummaryInstance;
Settings settings = DataSource.Instance.SettingsInstance;
//Make a new root - I tend to build it all, then assign it to Root
// I could just do Root.Add etc all the way, but I think this is cleaner.
var root = new RootElement("Summary");
// make a new section inside the root
var section = new Section();
//I set the headerview and footerview, as this has a custom header/footer, see the image I attached.
//you dont need to do this if you just want text - just assign .Header = "foo";
// but you CAN put ANY view in here!
section.HeaderView = new UIImageView(Resources.HeaderTearoff);
section.FooterView = new UIImageView(Resources.FooterTearoff);
// a BackgroundStringElement is my one - mostly transparent.
var element = new BackgroundStringElement("Budget", FormatNativeCurrency(settings.TotalBudget));
section.Add (element);
element = new BackgroundStringElement("Spent", FormatNativeCurrency(sum.ActualTotalSpent));
section.Add (element);
if (settings.TotalBudget > sum.ActualTotalSpent)
{
element = new BackgroundStringElement("Remaining", FormatNativeCurrency(settings.TotalBudget - sum.ActualTotalSpent));
} else {
element = new BackgroundStringElement("Overspent", FormatNativeCurrency(sum.ActualTotalSpent - settings.TotalBudget));
}
section.Add (element);
element = new BackgroundStringElement("Projected Spend", FormatNativeCurrency(sum.ProjectedTotalSpent));
section.Add (element);
root.Add (section);
section = new Section();
section.HeaderView = new UIImageView(Resources.HeaderTearoff);
section.FooterView = new UIImageView(Resources.FooterTearoff);
element = new BackgroundStringElement("{0} days remaining".Fmt(sum.NumberOfDaysRemaining));
section.Add (element);
element = new BackgroundStringElement("You are spending {0} per day.".Fmt(FormatNativeCurrency(sum.ActualDailySpent)));
section.Add (element);
element = new BackgroundStringElement("Your budget is {0} per day.".Fmt(FormatNativeCurrency(sum.DailyBudget)));
section.Add (element);
root.Add (section);
//UIViewWithBackgroundElement is just a UIViewElement with a specific background set.
// Most likely going to be refactored out :)
section = new Section();
chart = BuildChart(sum, settings);
section.HeaderView = new UIImageView(Resources.HeaderTearoff);
section.FooterView = new UIImageView(Resources.FooterTearoff);
section.Add (new UIViewWithBackgroundElement("chart", chart, false));
root.Add (section);
Root = root;
}
private LineChart BuildChart(Summary sum, Settings settings)
{
// SNIP - Building the chart dataset here.
return chart;
}
public string FormatNativeCurrency(float amount)
{
return string.Format("{0:0} {1}", amount, DataSource.Instance.SettingsInstance.HomeCurrency);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment