Created
August 2, 2012 12:21
-
-
Save garchibald/3236636 to your computer and use it in GitHub Desktop.
Sample SqLiteResult called from Caliburn Micro View Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************** Module Header ******************************\ | |
Module Name: CoroutineViewModel | |
Copyright (c) Grant Archibald. | |
This source is subject to the Microsoft Public License. | |
See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. | |
All other rights reserved. | |
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | |
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | |
\***************************************************************************/ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Caliburn.Micro.WinRT.Sample.Results; | |
using SQLite; | |
namespace Caliburn.Micro.WinRT.Sample.ViewModels | |
{ | |
/// <summary> | |
/// Sample Calibrn.Micro ViewModel for WinRT that use SQLite to Search for Add/Update a <see cref="Person"/> | |
/// </summary> | |
public class CoroutineViewModel : ViewModelBase | |
{ | |
private BindableCollection<Person> _items; | |
private Person _selectedItem; | |
public CoroutineViewModel(INavigationService navigationService) | |
: base(navigationService) | |
{ | |
Items = new BindableCollection<Person>(); | |
} | |
/// <summary> | |
/// The list of <see cref="Person"/> obtained from <see cref="Search"/> | |
/// </summary> | |
public BindableCollection<Person> Items | |
{ | |
get { return _items; } | |
set { _items = value; | |
NotifyOfPropertyChange(() => Items); | |
} | |
} | |
/// <summary> | |
/// The selected person from <see cref="Items"/> | |
/// </summary> | |
public Person SelectedItem { get { return _selectedItem; } set { _selectedItem = value; NotifyOfPropertyChange(() => SelectedItem); } } | |
public IEnumerable<IResult> Execute() | |
{ | |
yield return new VisualStateResult("Loading"); | |
yield return new SQLiteResult(db => db.CreateTable<Person>()); | |
yield return new VisualStateResult("LoadingComplete"); | |
yield return new MessageDialogResult("This was executed from a custom IResult, MessageDialogResult.", "IResult Coroutines"); | |
} | |
/// <summary> | |
/// Finds all <see cref="Person"/> items in the database | |
/// </summary> | |
/// <returns></returns> | |
public IEnumerable<IResult> Search() | |
{ | |
yield return new SQLiteResult(db => | |
{ | |
Items.Clear(); | |
Items.AddRange(db.Query<Person>("SELECT * FROM person")); | |
}); | |
} | |
/// <summary> | |
/// Add or Updates the <see cref="SelectedItem"/> | |
/// </summary> | |
/// <returns></returns> | |
public IEnumerable<IResult> Save() | |
{ | |
var oldSelected = SelectedItem; | |
yield return new SQLiteResult(db => | |
{ | |
if ( SelectedItem == null) | |
return; | |
if ( SelectedItem.ID == 0) | |
{ | |
SelectedItem.Created = DateTime.Now; | |
db.Insert(SelectedItem); | |
} | |
else | |
db.Update(SelectedItem); | |
}); | |
yield return Search().AsResult(); | |
if (oldSelected != null && oldSelected.ID > 0) | |
SelectedItem = Items.FirstOrDefault(i => i.ID == oldSelected.ID); | |
else | |
{ | |
SelectedItem = Items.OrderByDescending(i => i.Created).FirstOrDefault(); | |
} | |
} | |
public void Add() | |
{ | |
var newPerson = new Person(); | |
Items.Add(newPerson); | |
SelectedItem = newPerson; | |
} | |
} | |
/// <summary></summary> | |
/// <remarks> | |
/// TODO: Refactor into seperate Framework namespace | |
/// </remarks> | |
public static class ResultHelper | |
{ | |
/// <summary> | |
/// Extension method to allow a set of results to be executed sequentially as a single result | |
/// </summary> | |
/// <param name="results"></param> | |
/// <returns></returns> | |
public static IResult AsResult(this IEnumerable<IResult> results) | |
{ | |
return new SequentialResult(results.GetEnumerator()); | |
} | |
} | |
/// <remarks> | |
/// TODO: Refactor into seperate Model namespace | |
/// </remarks> | |
public class Person | |
{ | |
[AutoIncrement, PrimaryKey] | |
public int ID { get; set; } | |
public string FullName { get; set; } | |
public string EmailAddress { get; set; } | |
public double Salary { get; set; } | |
public DateTime Created { get; set; } | |
public DateTime Updated { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment