Skip to content

Instantly share code, notes, and snippets.

@nicwise
Created October 26, 2011 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicwise/1315899 to your computer and use it in GitHub Desktop.
Save nicwise/1315899 to your computer and use it in GitHub Desktop.
MT.D code
using System;
using MonoTouch.Dialog;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Drawing;
using MonoTouch.CoreGraphics;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
namespace DeadDrop
{
public class InvoicesDialogViewController : BaseDialogViewController
{
public InvoicesDialogViewController () : base(null, true)
{
Title = "Invoices";
this.Style = MonoTouch.UIKit.UITableViewStyle.Plain;
//BuildRootElement();
}
public override void RefreshFromDatastore ()
{
BuildRootElement ();
}
public void BuildRootElement ()
{
Util.Log("Building invoices");
var newRoot = new RootElement ("Invoices");
Section section = new Section();
foreach(var invoice in DataSource.Instance.Invoices)
{
if (invoice.Status != "Paid")
{
var localContact = DataSource.Instance.ContactById(invoice.ContactId);
var localInvoice = invoice;
section.Add (new InvoiceElement (localInvoice, localContact, delegate {
NavigationController.PushViewController (new InvoiceDetailDialogViewController (localContact, localInvoice), true);
}));
}
}
newRoot.Add(section);
Root = newRoot;
}
}
public class InvoiceElement : StyledStringElement, IElementSizing
{
private Contact contact;
private Invoice invoice;
bool myAccount = false;
static NSString skey = new NSString("InvoiceElement");
public InvoiceElement(Invoice invoice, Contact contact, NSAction ontapped) : base("", ontapped)
{
this.contact = contact;
this.invoice = invoice;
myAccount = DataSource.Instance.IsMyAccount;
}
readonly float cellHeight = 44f;
public float GetHeight (UITableView tableView, NSIndexPath indexPath)
{
if (myAccount) return cellHeight + 15f;
return cellHeight;
}
public override UITableViewCell GetCell (UITableView tv)
{
var cell = tv.DequeueReusableCell (skey);
UILabel contactNameLabel = null;
UILabel invoiceAmountLabel = null;
UILabel dueOnLabel = null;
UILabel invoiceNumberLabel = null;
UILabel extraInfoLabel = null;
if (cell == null){
cell = new UITableViewCell (UITableViewCellStyle.Default, skey);
cell.SelectionStyle = UITableViewCellSelectionStyle.Blue;
cell.BackgroundColor = UIColor.Clear;
cell.BackgroundView = new UIView { Frame = new RectangleF(0,0,0,0) };
//cell.ContentView.BackgroundColor = UIColor.Red;
var frame = cell.ContentView.Frame;
frame.Height = cellHeight;
cell.ContentView.Frame = frame;
float width = 0;
invoiceAmountLabel = new UILabel();
invoiceAmountLabel.Frame = new RectangleF(220,5, 100,21);
invoiceAmountLabel.TextAlignment = UITextAlignment.Right;
invoiceAmountLabel.Font = UIFont.BoldSystemFontOfSize(14f);
invoiceAmountLabel.TextColor = StockImages.DetailColor;
invoiceAmountLabel.BackgroundColor = UIColor.Clear;
invoiceAmountLabel.AdjustsFontSizeToFitWidth = false;
invoiceAmountLabel.Tag = 12;
invoiceAmountLabel.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin;
cell.ContentView.AddSubview(invoiceAmountLabel);
contactNameLabel = new UILabel();
contactNameLabel.Frame = new RectangleF(10,5, 300,21);
contactNameLabel.Font = UIFont.BoldSystemFontOfSize(18f);
contactNameLabel.Tag = 10;
contactNameLabel.BackgroundColor = UIColor.Clear;
contactNameLabel.AdjustsFontSizeToFitWidth = true;
contactNameLabel.MinimumFontSize = 14f;
contactNameLabel.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin ;
cell.ContentView.AddSubview(contactNameLabel);
dueOnLabel = new UILabel();
dueOnLabel.Frame = new RectangleF(10,25, 300,21);
dueOnLabel.Font = UIFont.SystemFontOfSize(12f);
dueOnLabel.TextColor = StockImages.DetailColor;
dueOnLabel.BackgroundColor = UIColor.Clear;
dueOnLabel.AdjustsFontSizeToFitWidth = true;
dueOnLabel.MinimumFontSize = 12f;
dueOnLabel.TextAlignment = UITextAlignment.Left;
dueOnLabel.Tag = 11;
dueOnLabel.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin;
cell.ContentView.AddSubview(dueOnLabel);
invoiceNumberLabel = new UILabel();
invoiceNumberLabel.Frame = new RectangleF(10,25, 300,21);
invoiceNumberLabel.Font = UIFont.SystemFontOfSize(12f);
invoiceNumberLabel.TextColor = StockImages.DetailColor;
invoiceNumberLabel.BackgroundColor = UIColor.Clear;
invoiceNumberLabel.AdjustsFontSizeToFitWidth = true;
invoiceNumberLabel.MinimumFontSize = 5f;
invoiceNumberLabel.TextAlignment = UITextAlignment.Right;
invoiceNumberLabel.Tag = 13;
invoiceNumberLabel.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin;
cell.ContentView.AddSubview(invoiceNumberLabel);
if (myAccount)
{
extraInfoLabel = new UILabel();
extraInfoLabel.Frame = new RectangleF(10,37, 300,21);
extraInfoLabel.Font = UIFont.SystemFontOfSize(12f);
extraInfoLabel.TextColor = StockImages.DetailColor;
extraInfoLabel.BackgroundColor = UIColor.Clear;
extraInfoLabel.AdjustsFontSizeToFitWidth = true;
extraInfoLabel.MinimumFontSize = 12f;
extraInfoLabel.TextAlignment = UITextAlignment.Left;
extraInfoLabel.Tag = 14;
extraInfoLabel.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin ;
cell.ContentView.AddSubview(extraInfoLabel);
}
} else {
contactNameLabel = cell.ViewWithTag(10) as UILabel;
dueOnLabel = cell.ViewWithTag(11) as UILabel;
invoiceAmountLabel = cell.ViewWithTag(12) as UILabel;
invoiceNumberLabel = cell.ViewWithTag(13) as UILabel;
extraInfoLabel = cell.ViewWithTag(14) as UILabel;
}
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
contactNameLabel.Text = contact.DisplayName;
dueOnLabel.TextColor = StockImages.DetailColor;
switch (invoice.Status)
{
case "Overdue":
dueOnLabel.TextColor = UIColor.Red;
dueOnLabel.Text = "Overdue. Due {0:0} days ago".Fmt((DateTime.Now - invoice.DueOn).TotalDays);
break;
case "Paid":
dueOnLabel.Text = "Paid";
break;
case "Draft":
dueOnLabel.Text = "Draft.";
break;
default:
dueOnLabel.Text = "Due in {0:0} days".Fmt((invoice.DueOn - DateTime.Now).TotalDays);
break;
}
invoiceAmountLabel.Text = Util.FormatValueAsCurrency(invoice.NetValue + invoice.SalesTaxValue, invoice.Currency);
invoiceNumberLabel.Text = invoice.Reference;
float amountWidth = new NSString(invoiceAmountLabel.Text).StringSize(invoiceAmountLabel.Font).Width;
float contactHeight = new NSString(contactNameLabel.Text).StringSize(contactNameLabel.Font).Height+10;
float invoiceNumberWidth = new NSString(invoiceNumberLabel.Text).StringSize(invoiceNumberLabel.Font).Width;
float frameWidth = cell.ContentView.Bounds.Width-20;
invoiceAmountLabel.Frame = new RectangleF(frameWidth-amountWidth, 0, amountWidth+ 10, contactHeight);
contactNameLabel.Frame = new RectangleF(10, 0, frameWidth-10-amountWidth, contactHeight);
invoiceNumberLabel.Frame = new RectangleF(frameWidth - invoiceNumberWidth, contactHeight - 10, invoiceNumberWidth+10, 21);
dueOnLabel.Frame = new RectangleF(10,contactHeight-10,frameWidth - invoiceNumberWidth,21);
if (myAccount)
{
extraInfoLabel.Frame = new RectangleF(10, contactHeight-10+15,frameWidth,21);
extraInfoLabel.Text = "Total:{3:0.00}/T:{0:0.00}/V:{1:0.00}/S:{2:0.00}".Fmt(
invoice.NetValue * 0.2,
invoice.SalesTaxValue,
invoice.NetValue * 0.1,
(invoice.NetValue * 0.2)+
(invoice.SalesTaxValue)+
(invoice.NetValue * 0.1)
);
}
return cell;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment