Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Clearer – A Gesture-Driven Windows 8 To-Do Application

, 9 Oct 2012
This article describes my experiences of porting a novel gesture-driven Windows Phone app to Windows 8.
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using SystemColor = Windows.UI.Color;

namespace ClearStyle.ViewModel
{
  /// <summary>
  /// A collection of todo items
  /// </summary>
  public class ToDoListViewModel : INotifyPropertyChanged
  {
    private ObservableCollectionEx<ToDoItem> _todoItems = new ObservableCollectionEx<ToDoItem>();

    private string _searchText = "";

    private ToDoItem _selectedItem;

    public ToDoListViewModel()
    {
      _todoItems.CollectionChanged += (s, e) =>
        {
          UpdateToDoColors();
          UpdateTileStatus();
        };

      _todoItems.RelayedPropertyChanged += (s, e) =>
        {
          UpdateTileStatus();
        };
    }

    public string SearchText
    {
      get
      {
        return _searchText;
      }
      set
      {
        _searchText = value;
        PropertyChanged(this, new PropertyChangedEventArgs("Items"));
      }
    }

    public ToDoItem SelectedItem
    {
      get
      {
        return _selectedItem;
      }
      set
      {
        _selectedItem = value;
        PropertyChanged(this, new PropertyChangedEventArgs("SelectedItem"));
      }
    }


    public ObservableCollectionEx<ToDoItem> Items
    {
      get
      {
        if (string.IsNullOrEmpty(_searchText))
        {
          return _todoItems;
        }
        else
        {
          return new ObservableCollectionEx<ToDoItem>(
            _todoItems.Where(i => i.Text.ToLowerInvariant().Contains(_searchText.ToLowerInvariant())));
        }
      }
    }
        
    private void UpdateToDoColors()
    {
      double itemCount = _todoItems.Count;
      double index = 0;
      foreach (var todoItem in _todoItems)
      {
        double val = (index / itemCount) * 155.0;
        index++;

        if (!todoItem.Completed)
        {
          todoItem.Color = SystemColor.FromArgb(255, 255, (byte)val, 0);
        }
      };
    }

    private void UpdateTileStatus()
    {
      XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideBlockAndText01);

      int index = 0;
      foreach(ToDoItem item in _todoItems.Take(4))
      {
        SetElementText(tileXml, index++, item.Text);
      }

      SetElementText(tileXml, 4, _todoItems.Where(t => !t.Completed).Count().ToString());
      SetElementText(tileXml, 5, "Left to-do");

      TileUpdateManager.CreateTileUpdaterForApplication().Update(new TileNotification(tileXml));
    }

    private void SetElementText(XmlDocument doc, int index, string text)
    {
      var element = (XmlElement)doc.GetElementsByTagName("text")[index];
      element.AppendChild(doc.CreateTextNode(text));
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public ToDoItem AddNewItem()
    {
      var item = new ToDoItem("New item");
      _todoItems.Insert(0, item);
      SearchText = "";
      return item;
    }
  }
}

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)

Share

About the Author

Colin Eberhardt
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
 
-
Follow on   Twitter   Google+

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 10 Oct 2012
Article Copyright 2012 by Colin Eberhardt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid