Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Changed MessageElement to allow injection of the cell rendering code via a constructor FactoryMethod. #125

Closed
wants to merge 2 commits into from

2 participants

@felixcollins

This means that people can reuse MessageElement and easily change the way it displays without starting from scratch (copying the code). Maybe this pattern could be useful for other elements too. This is not a breaking change - it maintains compatibility with existing code as the MessageElement siimply defaults to the existing MessageSummaryView. I'm happy to provide documentation and/or example code if this pull request is accepted.

@migueldeicaza

Elements are in general very small bits of code, so I wonder if this is really needed. I need to sleep on this.

The other problem is that this patch contains style changes, and those in general should not be done when submitting patches.

@felixcollins

Sorry about the style changes (I assume you are referring to the brace position). Maybe you are right about making elements flexible... I ended up having to inherit MessageElement to add extra data members (indentlevel and userimage) anyway. I guess as long as the source is available it can be copied and modified to suit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 30, 2012
  1. Changed MessageElement to allow injection of cell rendering code via …

    Felix Collins authored
    …constructor FactoryMethod
This page is out of date. Refresh to see the latest.
Showing with 50 additions and 12 deletions.
  1. +50 −12 MonoTouch.Dialog/Elements/MessageElement.cs
View
62 MonoTouch.Dialog/Elements/MessageElement.cs
@@ -4,9 +4,21 @@
using MonoTouch.Foundation;
using MonoTouch.CoreGraphics;
-namespace MonoTouch.Dialog {
+namespace MonoTouch.Dialog
+{
+ /// <summary>
+ /// Interface for a view used to render a message cell
+ /// </summary>
+ public abstract class BaseMessageSummaryView : UIView
+ {
+ public abstract void Update (string sender, string body, string subject, DateTime date, bool newFlag, int messageCount);
+ }
+
+ public delegate BaseMessageSummaryView MessageSummaryViewFactory();
- public class MessageSummaryView : UIView {
+
+ public class MessageSummaryView : BaseMessageSummaryView
+ {
static UIFont SenderFont = UIFont.BoldSystemFontOfSize (19);
static UIFont SubjectFont = UIFont.SystemFontOfSize (14);
static UIFont TextFont = UIFont.SystemFontOfSize (13);
@@ -22,7 +34,8 @@ public class MessageSummaryView : UIView {
static MessageSummaryView ()
{
- using (var colorspace = CGColorSpace.CreateDeviceRGB ()){
+ using (var colorspace = CGColorSpace.CreateDeviceRGB ())
+ {
gradient = new CGGradient (colorspace, new float [] { /* first */ .52f, .69f, .96f, 1, /* second */ .12f, .31f, .67f, 1 }, null); //new float [] { 0, 1 });
}
}
@@ -32,7 +45,7 @@ public MessageSummaryView ()
BackgroundColor = UIColor.White;
}
- public void Update (string sender, string body, string subject, DateTime date, bool newFlag, int messageCount)
+ public override void Update (string sender, string body, string subject, DateTime date, bool newFlag, int messageCount)
{
Sender = sender;
Body = body;
@@ -72,7 +85,7 @@ public override void Draw (RectangleF rect)
if (DateTime.Now.Day == Date.Day)
label = Date.ToShortTimeString ();
else if (diff <= TimeSpan.FromHours (24))
- label = "Yesterday".GetText ();
+ label = "Yesterday";
else if (diff < TimeSpan.FromDays (6))
label = Date.ToString ("dddd");
else
@@ -111,16 +124,19 @@ public override void Draw (RectangleF rect)
}
}
- public class MessageElement : Element, IElementSizing {
- static NSString mKey = new NSString ("MessageElement");
+ public class MessageElement : Element, IElementSizing
+ {
+ protected static NSString mKey = new NSString ("MessageElement");
public string Sender, Body, Subject;
public DateTime Date;
public bool NewFlag;
public int MessageCount;
+
+ protected MessageSummaryViewFactory m_ViewFactory;
- class MessageCell : UITableViewCell {
- MessageSummaryView view;
+ protected class MessageCell : UITableViewCell {
+ protected BaseMessageSummaryView view;
public MessageCell () : base (UITableViewCellStyle.Default, mKey)
{
@@ -128,8 +144,16 @@ public MessageCell () : base (UITableViewCellStyle.Default, mKey)
ContentView.Add (view);
Accessory = UITableViewCellAccessory.DisclosureIndicator;
}
+
+ public MessageCell (MessageSummaryViewFactory viewFactory)
+ : base (UITableViewCellStyle.Default, mKey)
+ {
+ view = viewFactory();
+ ContentView.Add (view);
+ Accessory = UITableViewCellAccessory.DisclosureIndicator;
+ }
- public void Update (MessageElement me)
+ public virtual void Update (MessageElement me)
{
view.Update (me.Sender, me.Body, me.Subject, me.Date, me.NewFlag, me.MessageCount);
}
@@ -150,12 +174,26 @@ public MessageElement (Action<DialogViewController,UITableView,NSIndexPath> tapp
{
Tapped += tapped;
}
-
+ public MessageElement (MessageSummaryViewFactory viewFactory)
+ :base("")
+ {
+ m_ViewFactory = viewFactory;
+ }
+
public override UITableViewCell GetCell (UITableView tv)
{
var cell = tv.DequeueReusableCell (mKey) as MessageCell;
if (cell == null)
- cell = new MessageCell ();
+ {
+ if(m_ViewFactory != null)
+ {
+ cell = new MessageCell(m_ViewFactory);
+ }
+ else
+ {
+ cell = new MessageCell ();
+ }
+ }
cell.Update (this);
return cell;
}
Something went wrong with that request. Please try again.