Skip to content

Instantly share code, notes, and snippets.

@Redth
Created May 27, 2010 18:14
Show Gist options
  • Save Redth/416141 to your computer and use it in GitHub Desktop.
Save Redth/416141 to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using MonoTouch.Dialog;
using MonoTouch.Foundation;
using MonoTouch.CoreFoundation;
namespace FbGroups
{
public class FeedElement : Element, IElementSizing
{
static string reuseIdentifier = "feedElement";
public FbFeed Feed
{
get;set;
}
public bool ShowComments
{
get;set;
}
public bool ShowLikes
{
get;set;
}
public Action<FbFeed> Tapped
{
get;set;
}
public FeedElement(FbFeed feed, bool showComments, bool showLikes, Action<FbFeed> tapped) : base(null)
{
this.Feed = feed;
this.ShowComments = showComments;
this.ShowLikes = showLikes;
this.Tapped = tapped;
if (this.Feed.Likes <= 0)
this.ShowLikes = false;
if (this.Feed.Comments == null || this.Feed.Comments.Items == null || this.Feed.Comments.Items.Count <= 0)
this.ShowComments = false;
}
public override UITableViewCell GetCell (UITableView tv)
{
var cell = tv.DequeueReusableCell(reuseIdentifier) as FeedCell;
if (cell == null)
{
cell = new FeedCell(UITableViewCellStyle.Default, reuseIdentifier, this.Feed, this.ShowComments, this.ShowLikes);
SetAccessory(cell);
}
else
{
SetAccessory(cell);
cell.UpdateCell(this.Feed, this.ShowComments, this.ShowLikes);
}
return cell;
}
public float GetHeight (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
return FeedCell.FeedCellView.HeightOfCell(tableView.Bounds, this.Feed, this.ShowComments, this.ShowLikes);
}
public override void Selected (DialogViewController dvc, UITableView tableView, MonoTouch.Foundation.NSIndexPath path)
{
if (this.Tapped != null)
this.Tapped(this.Feed);
}
void SetAccessory(FeedCell cell)
{
if (this.Tapped != null)
{
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
}
else
{
cell.Accessory = UITableViewCellAccessory.None;
}
}
}
public class FeedCell : UITableViewCell
{
const float userImageSize = 38;
const float paddingX = 5;
const float paddingY = 5;
const float textLeftStart = paddingX * 2 + userImageSize;
FeedCellView cellView;
public FeedCell (UITableViewCellStyle style, string reuseIdentifier, FbFeed FeedCell, bool showComments, bool showLikes) : base(style, reuseIdentifier)
{
cellView = new FeedCellView(FeedCell, showComments, showLikes);
ContentView.Add(cellView);
}
public void UpdateCell(FbFeed feed, bool showComments, bool showLikes)
{
cellView.Update(feed, showComments, showLikes);
SetNeedsDisplay();
}
public override void LayoutSubviews ()
{
base.LayoutSubviews ();
cellView.Frame = ContentView.Bounds;
}
public class FeedCellView : UIView, MonoTouch.Dialog.UrlImageStore.IUrlImageUpdated<string>
{
static UIImage imgBgNormal = UIImage.FromFile("Images/fbCommentsLikesBgNormal.png").StretchableImage(30, 0);
FbFeed feed;
UIImage userImage;
bool showComments = false;
bool showLikes = false;
public FeedCellView(FbFeed feed, bool showComments, bool showLikes) : base()
{
Update(feed, showComments, showLikes);
}
public void Update(FbFeed feed, bool showComments, bool showLikes)
{
this.feed = feed;
this.showComments = showComments;
this.showLikes = showLikes;
userImage = FbApi.UserImages.RequestImage(feed.From.Id, FbApi.GetPictureUrl(feed.From.Id), this);
SetNeedsDisplay();
}
public override void Draw (RectangleF rect)
{
var context = UIGraphics.GetCurrentContext();
bool highlighted = (Superview.Superview as UITableViewCell).Highlighted;
if (!highlighted)
{
UIColor.White.SetColor ();
context.FillRect (Bounds);
}
float yLocation = 0;
FbApi.Blue.SetColor();
DrawString(this.feed.From.Name, new RectangleF(textLeftStart, paddingY, Bounds.Width - textLeftStart - paddingX, 15), UIFont.BoldSystemFontOfSize(13.0f), UILineBreakMode.TailTruncation);
yLocation += paddingY + 15;
UIColor.Black.SetColor();
float heightOfMessage = HeightOfMessage(this.Bounds, this.feed.Message);
if (!string.IsNullOrEmpty(this.feed.Message))
{
DrawString(this.feed.Message, new RectangleF(textLeftStart, paddingY + yLocation, Bounds.Width - textLeftStart - paddingX, heightOfMessage), UIFont.SystemFontOfSize(13.0f), UILineBreakMode.WordWrap);
yLocation += paddingY + heightOfMessage;
}
UIColor.Gray.SetColor();
DrawString(FriendlyTime(this.feed.CreatedTime), new RectangleF(textLeftStart, paddingY + yLocation, Bounds.Width - textLeftStart - paddingX, 15), UIFont.SystemFontOfSize(12.0f), UILineBreakMode.TailTruncation);
yLocation += paddingY + 15;
if (showComments || showLikes)
{
//Get string
string display = "";
if (this.showComments)
{
display += this.feed.Comments.Items.Count.ToString() + " comment";
if (this.feed.Comments.Items.Count > 1)
display += "s";
display += " ";
}
if (this.showLikes)
{
display += this.feed.Likes.ToString() + " like";
if (this.feed.Likes > 1)
display += "s";
}
//Get width
float displayWidth = this.StringSize(display, UIFont.BoldSystemFontOfSize(11.0f), new SizeF(Bounds.Width - textLeftStart - paddingX - 30, 15), UILineBreakMode.TailTruncation).Width;
imgBgNormal.Draw(new RectangleF(textLeftStart, yLocation + 2, displayWidth + 20, 35.0f));
FbApi.Blue.SetColor();
DrawString(display, new RectangleF(textLeftStart + 10, yLocation + 17, displayWidth, 15), UIFont.BoldSystemFontOfSize(11.0f), UILineBreakMode.TailTruncation);
}
if (userImage != null)
userImage.Draw(new RectangleF(paddingX, paddingY, userImageSize, userImageSize));
}
public void UrlImageUpdated (string id)
{
if (this.feed != null && this.feed.From.Id.Equals(id))
{
this.userImage = FbApi.UserImages.GetImage(id);
SetNeedsDisplay();
}
}
public static float HeightOfMessage(RectangleF bounds, string message)
{
if (string.IsNullOrEmpty(message))
return 0;
else
return new NSString(message).StringSize(UIFont.SystemFontOfSize(13.0f), new SizeF(bounds.Width - textLeftStart - paddingX, 10000.0f), UILineBreakMode.WordWrap).Height;
}
public static float HeightOfCell(RectangleF bounds, FbFeed feed, bool showComments, bool showLikes)
{
float h = paddingY + 15 + paddingY + HeightOfMessage(bounds, feed.Message) + paddingY + 15 + paddingY;
if (showComments || showLikes)
h += 35 + paddingY;
if (h < 48)
h = 48;
return h;
}
static string FriendlyTime(DateTime date)
{
TimeSpan span = DateTime.Now - date.ToLocalTime();
if (span.TotalDays > 1)
return span.TotalDays.ToString("#") + " days ago";
else if (span.TotalDays > 0)
return "a day and " + span.Hours.ToString("#") + " hours ago";
else if (span.TotalHours > 1)
return span.TotalHours.ToString("#") + " hours ago";
else if (span.TotalHours > 0)
return "an hour and " + span.Minutes.ToString("#") + " min ago";
else if (span.TotalMinutes > 1)
return span.TotalMinutes.ToString("#") + " minutes ago";
else if (span.TotalMinutes > 0)
return "about a minute ago";
else
return "a moment ago";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment