Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 2) - TodoService, TodoItemViewModel

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
9/10/2014 4:22:48 AM
TodoService

A custom service provides for all CRUD operations for the TodoItem objects. It serves as an intermediary between the LINQ to SQL DataContext for TodoItems and viewmodel logic.

When the TodoService is instantiated, a TodoDataContext is created. TodoItem objects can then be retrieved using the GetTodoItem method, shown in the following excerpt:

public TodoItem GetTodoItem(int itemId)
{
    TodoItem item = dataContext.TodoItems.Where(
                    todoItem => todoItem.Id == itemId).FirstOrDefault();

    if (item == null)
    {
        throw new KeyNotFoundException(
            string.Format("Item with key '{0}' was not found.", itemId));
    }

    return item;
}

Conversely, inserting a new TodoItem into the database is done using the service’s AddOrUpdateItem method.

If the TodoItem has an Id that is less than 1, it indicates that the TodoItem does not already exist in the database. If greater than 0, it is indicative of an update. See the following excerpt:

public void AddOrUpdateItem(TodoItem todoItem)
{
    ArgumentValidator.AssertNotNull(todoItem, "todoItem");

    if (todoItem.Id <= 0)
    {
        dataContext.TodoItems.InsertOnSubmit(todoItem);
    }
    dataContext.SubmitChanges();
}

The third method of the TodoService worth noting is GetTodoItems, which retrieves all TodoItem objects via the TodoItems property of the data context, like so:

public IEnumerable<TodoItem> GetTodoItems()
{
    return dataContext.TodoItems;
}

As you see in a moment, GetTodoItems is used by the TodoListViewModel.

With the TodoService in place we now look at how the service is used by the app’s viewmodels to display all to-do items and to create new to-do items.

TodoItemViewModel

The TodoItemView page retrieves all TodoItem objects for the user and displays them in a list. Its viewmodel relies on the to-do service being passed as a constructor argument.

Retrieving the TodoItem objects is performed in the viewmodel’s PopulateItems method. The to-do service is used to retrieve the items, and then, using a LINQ expression, the items are grouped by the TodoItem.DueDate property. This allows the list to be bound to a LongListSelector in the view. See the following excerpt:

void PopulateItems()
{
    try
    {
        IEnumerable<TodoItem> items = todoService.GetTodoItems();

        IEnumerable<CustomGrouping<TodoItem>> groups
                      = from todoItem in items
                        orderby todoItem.DueDate
                        group todoItem by todoItem.DueDate.Date
                        into grouping
                        select new CustomGrouping<TodoItem>(
                                    new DateGroupingKey(grouping.Key),
                                    grouping.AsEnumerable());

        GroupedTodoItems = groups.ToList();
    }
    catch (Exception ex)
    {
        Message = "Unable to retrieve items.";
        Console.WriteLine(ex);
    }
}

TodoItems are grouped using a custom class called DateGroupingKey, which allows us to provide some additional logic to the item groupings, namely an Overdue property (see Listing 3). This allows you to change the background for the grouping header to red if the to-do items in the group have due dates occurring in the past.

Alternatively, you may choose to use a value converter for determining whether the date is overdue. The example uses a custom key class, however, because it better encapsulates the date value and the logic for determining whether it is overdue.

LISTING 3. DateGroupingKey Class


public class DateGroupingKey
{
    public DateTime DateTime { get; private set; }

    public DateGroupingKey(DateTime dateTime)
    {
        DateTime = dateTime;
        Overdue = DateTime.Now > dateTime;
    }

    public bool Overdue { get; private set; }
}


The viewmodel’s Load method commences population of the to-do items asynchronously, using a thread from the thread pool. This prevents blocking the UI thread during a potentially long-running operation.

A PeriodicTask is registered with the ScheduledActionService. If the PeriodicTask has already been registered, it is first removed. This resets the task’s expiration date. See the following excerpt:

public void Load()
{
    ThreadPool.QueueUserWorkItem(delegate { PopulateItems(); });

    PeriodicTask periodicTask = new PeriodicTask(agentName)
    {
        Description = "Updates a tile.",
        ExpirationTime = DateTime.Now.AddDays(14)
    };

    if (ScheduledActionService.Find(agentName) != null)
    {
        ScheduledActionService.Remove(agentName);
    }

    /* This can only be called when the app
     * is running in the foreground. */
    ScheduledActionService.Add(periodicTask);
}

The TodoListViewModel constructor calls its Load method after initializing various ICommands (see Listing 4). NewItemCommand uses the ViewModelBase class’s Navigate method to open the TodoItemView page. EditItemCommand also navigates to the TodoItemView page, but passes the Id of the TodoItem, provided as a command argument.

LISTING 4. TodoListViewModel Constructor


public TodoListViewModel(
    ITodoService todoService, IDeviceProperties deviceProperties)
{
    this.todoService = ArgumentValidator.AssertNotNull(
                                          todoService, "todoService");
    this.deviceProperties = ArgumentValidator.AssertNotNull(
                   deviceProperties, "deviceProperties");

    editItemCommand = new DelegateCommand<int>(
        todoItemId => Navigate(todoItemViewUrl + "?TodoItemId=" + todoItemId));

    testAgentCommand = new DelegateCommand(
                            obj => ScheduledActionService.LaunchForTest(
                                agentName, TimeSpan.Zero));

    backupDatabaseCommand = new DelegateCommand(obj => BackupDatabase());

    restoreDatabaseCommand = new DelegateCommand(obj => RestoreDatabase());
    Load();
}

Other -----------------
- Windows Phone 8 : Scheduled Tasks - Using Scheduled Tasks
- Windows Phone 8 : Scheduled Tasks - Background Agent Types
- Windows Phone 8 : Windows Phone Toolkit Animated Page Transitions - Reusing the Transition Attached Properties
- Windows Phone 8 : Windows Phone Toolkit Animated Page Transitions - Using Windows Phone Toolkit Transitions
- Windows Phone 8 : Messaging - Composing a New Message (part 8) - Checking for New Mail
- Windows Phone 8 : Messaging - Composing a New Message (part 7) - Adding Emoticons and Clip Art
- Windows Phone 8 : Messaging - Composing a New Message (part 6) - Adding Recipients Through CC and Blind CC
- Windows Phone 8 : Messaging - Composing a New Message (part 5) - Setting Message Priority
- Windows Phone 8 : Messaging - Composing a New Message (part 4) - Removing a Message Attachment
- Windows Phone 8 : Messaging - Composing a New Message (part 3) - Sending a Picture from the Camera
 
 
25 Inspiring Game of Thrones Quotes
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
programming4us
Natural Miscarriage
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Game Trailer