Click here to Skip to main content
15,892,298 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.1K   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.Collections.Generic;
using PropertyFinder.Model;
using System.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace PropertyFinder.Presenter
{
  /// <summary>
  /// This class hold all the application state that should be persisted
  /// between sessions. When the state changes, this class persists itself via
  /// the supplied <see cref="IStatePersistenceService"/> instance.
  /// </summary>
  [XmlInclude(typeof(PlainTextSearchItem))]
  [XmlInclude(typeof(GeoLocationSearchItem))]
  public class PropertyFinderPersistentState
  {
    private IStatePersistenceService _persistenceService;

    public IStatePersistenceService PersistenceService
    {
      set { _persistenceService = value; }
    }

    public PropertyFinderPersistentState()
    {
      Favourites = new List<Property>();
      RecentSearches = new List<RecentSearch>();
    }

    public PropertyFinderPersistentState(IStatePersistenceService persistenceService)
      : this()
    {
      _persistenceService = persistenceService;
    }

    /// <summary>
    /// Gets or sets the properties the user has marked as favourites
    /// </summary>
    public List<Property> Favourites { get; set; }

    /// <summary>
    /// Gets or sets a few recent searches
    /// </summary>
    public List<RecentSearch> RecentSearches { get; set; }

    /// <summary>
    /// Adds the given search to the RecentSearches list. 
    /// </summary>
    public void AddSearchToRecent (RecentSearch search)
    {
      // if we already have this search saved, move it to the top
      if (RecentSearches.Any (r => r.Search.DisplayText == search.Search.DisplayText))
      {
        var matchingSearch = RecentSearches.Single (r => r.Search.DisplayText == search.Search.DisplayText);
        RecentSearches.Remove (matchingSearch);
        RecentSearches.Insert (0, matchingSearch);
      }
      else
      {
        // otherwise, add it
        RecentSearches.Insert (0, search);
        if (RecentSearches.Count > 4)
        {
          RecentSearches.RemoveAt (RecentSearches.Count - 1);
        }
      }
      _persistenceService.SaveState(this);
    }

    /// <summary>
    /// Gets whether the given property is favourited
    /// </summary>
    public bool IsPropertyFavourited(Property property)
    {
      return Favourites.Any(p => p.Guid == property.Guid);
    }

    /// <summary>
    /// Toggles the favourited state of the given property.
    /// </summary>
    public void ToggleFavourite(Property property)
    {
      if (IsPropertyFavourited(property))
      {
        var matchingProperty = Favourites.Single(p => p.Guid == property.Guid);
        Favourites.Remove(matchingProperty);
      }
      else
      {
        Favourites.Add(property);
      }

      PersistState();
    }

    /// <summary>
    /// Persists the current state.
    /// </summary>
    private void PersistState()
    {
      _persistenceService.SaveState(this);
    }
  }
}

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