Click here to Skip to main content
15,895,777 members
Articles / Mobile Apps / Windows Phone 7

Property Finder – a Cross-Platform Xamarin MonoTouch Mobile App

Rate me:
Please Sign up or sign in to vote.
5.00/5 (17 votes)
3 Jan 2013CPOL19 min read 80.2K   1.2K   48  
A look at how Xamarin MonoTouch allows you to create cross-platform applications, using the native C# / Silverlight for Windows Phone and C#, via Xamarin MonoTouch, for iOS.
using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;

using PropertyFinder.Model;
using PropertyFinder.Presenter;
using System.Collections.Generic;
using MonoTouch.CoreFoundation;

namespace PropertyFinder
{
  public partial class SearchResultsViewController : UIViewController, SearchResultsPresenter.View
  {
    private SearchResultsPresenter _presenter;

    private TableSource _tableSource;

    public SearchResultsViewController (SearchResultsPresenter presenter)
      : base ("SearchResultsViewController", null)
    {
      Title = "Results";

      _presenter = presenter;
    }
		
    public override void DidReceiveMemoryWarning ()
    {
      base.DidReceiveMemoryWarning ();
    }
		
    public override void ViewDidLoad ()
    {
      base.ViewDidLoad ();			

      // configure the UITableView
      _tableSource = new TableSource();
      _tableSource.PropertySelected += (s,e) => PropertySelected(this, e);
      _tableSource.LoadMore += (s, e) => LoadMoreClicked(this, e);
      searchResultsTable.Source = _tableSource;

      _presenter.SetView(this);

      // set the back button text
      NavigationItem.BackBarButtonItem = new UIBarButtonItem("Results",
                           UIBarButtonItemStyle.Bordered, BackButtonEventHandler);
    }

    private void BackButtonEventHandler (object sender, EventArgs args)
    {
      NavigationController.PopViewControllerAnimated(true);
    }

    public override void ViewDidUnload ()
    {
      base.ViewDidUnload ();
			
      ReleaseDesignerOutlets ();
    }
		
    public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
    {
      return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
    }

    #region View implementation

    public event EventHandler LoadMoreClicked  = delegate{};

    public event EventHandler<PropertyEventArgs> PropertySelected = delegate{};

    public void SetSearchResults (int totalResult, int pageNumber, int totalPages,
                                                       List<Property> properties, string searchLocation)
    {
      _tableSource.SetProperties(properties, totalResult);
      searchResultsTable.ReloadData();

      Title = string.Format("{0:d} of {1:d} matches", properties.Count, totalResult);
    }

    public void SetLoadMoreVisible (bool visible)
    {
      _tableSource.LoadMoreVisible = visible;
      searchResultsTable.ReloadData();
    }

    public bool IsLoading
    {
      set
      {
       
      }
    }

    #endregion

    /// <summary>
    /// A table source implementation that renders a list of properties
    /// and optionally a load-more indicator.
    /// </summary>
    public class TableSource : UITableViewSource
    {
      private bool _loadMoreVisible = false;
      private List<Property> _properties = new List<Property>();
      private static readonly string _cellIdentifier = "TableCell";
      private int _totalResults;

      public TableSource ()
      {
      }

      public bool LoadMoreVisible
      {
        set
        {
          _loadMoreVisible = value;
        }
      }

      public void SetProperties (List<Property> properties, int totalResults)
      {
        _properties = properties;
        _totalResults = totalResults;
      }

      public override int RowsInSection (UITableView tableview, int section)
      {
        return _properties.Count + (_loadMoreVisible ? 1 : 0);
      }

      public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
      {
        UITableViewCell cell = tableView.DequeueReusableCell (_cellIdentifier);
        if (cell == null) {
          cell = new UITableViewCell (UITableViewCellStyle.Subtitle, _cellIdentifier);
        }

        int row = indexPath.Row;
        if (row < _properties.Count)
        {
          var property = _properties [indexPath.Row];
          cell.TextLabel.Text = property.FormattedPrice;
          cell.DetailTextLabel.Text = property.Title;
          cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
          cell.ImageView.Frame = new RectangleF(cell.ImageView.Frame.Location, new SizeF(60,60));

          DispatchQueue.DefaultGlobalQueue.DispatchAsync(() => {
            UIImage image = UIImage.LoadFromData (NSData.FromUrl (new NSUrl (property.ThumbnailUrl)));
            DispatchQueue.MainQueue.DispatchAsync(() => {
              cell.ImageView.Image = image;
              cell.SetNeedsLayout();
            });
          });
        }
        else
        {
          cell.TextLabel.Text = "Load more ...";
          cell.DetailTextLabel.Text = string.Format("Showing {0:d} of {1:d} matches", _properties.Count, _totalResults);
          cell.ImageView.Image = null;
        }
        return cell;
      }

      public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
      {
        return 70;
      }

      public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
      {
        int row = indexPath.Row;
        if (row < _properties.Count)
        {
          tableView.DeselectRow(indexPath, false);
          var property = _properties [row];
          PropertySelected (this, new PropertyEventArgs (property));
        }
        else
        {
          LoadMore(this, EventArgs.Empty);
        }
      }

      public event EventHandler<PropertyEventArgs> PropertySelected = delegate{};

      public event EventHandler LoadMore = delegate{};
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.

I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.

I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.

Visit my blog - Colin Eberhardt's Adventures in .NET.

Follow me on Twitter - @ColinEberhardt

-

Comments and Discussions