Skip to content

Commit

Permalink
Merge pull request #68 from sgrebnov/WP7-DateTimePicker
Browse files Browse the repository at this point in the history
DateTimePicker plugin for windows phone
  • Loading branch information
purplecabbage committed May 16, 2012
2 parents 017340d + ba2286b commit e1b20fe
Show file tree
Hide file tree
Showing 4 changed files with 562 additions and 0 deletions.
175 changes: 175 additions & 0 deletions WindowsPhone/DateTimePicker/DateTimePicker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2005-2011, Nitobi Software Inc.
* Copyright (c) 2011, Microsoft Corporation
*/

using System.Runtime.Serialization;
using WP7GapClassLib.PhoneGap;
using WP7GapClassLib.PhoneGap.Commands;
using WP7GapClassLib.PhoneGap.JSON;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Phone.Controls;
using System.Windows;
using Microsoft.Phone.Tasks;

namespace PhoneGap.Extension.Commands
{
/// <summary>
/// Represents command that allows the user to choose a date (day/month/year) or time (hour/minute/am/pm).
/// </summary>
public class DateTimePicker : BaseCommand
{

#region DateTimePicker Options

/// <summary>
/// Represents DateTimePicker options
/// </summary>
[DataContract]
public class DateTimePickerOptions
{
/// <summary>
/// Initial value for time or date
/// </summary>
[DataMember(IsRequired = false, Name = "value")]
public DateTime Value { get; set; }

/// <summary>
/// Creates options object with default parameters
/// </summary>
public DateTimePickerOptions()
{
this.SetDefaultValues(new StreamingContext());
}

/// <summary>
/// Initializes default values for class fields.
/// Implemented in separate method because default constructor is not invoked during deserialization.
/// </summary>
/// <param name="context"></param>
[OnDeserializing()]
public void SetDefaultValues(StreamingContext context)
{
this.Value = DateTime.Now;
}

}
#endregion

/// <summary>
/// Used to open datetime picker
/// </summary>
private DateTimePickerTask dateTimePickerTask;

/// <summary>
/// DateTimePicker options
/// </summary>
private DateTimePickerOptions dateTimePickerOptions;

/// <summary>
/// Triggers special UI to select a date (day/month/year)
/// </summary>
public void selectDate(string options)
{

try
{
try
{
this.dateTimePickerOptions = String.IsNullOrEmpty(options) ? new DateTimePickerOptions() :
WP7GapClassLib.PhoneGap.JSON.JsonHelper.Deserialize<DateTimePickerOptions>(options);

}
catch (Exception ex)
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message));
return;
}

this.dateTimePickerTask = new DateTimePickerTask();
dateTimePickerTask.Value = dateTimePickerOptions.Value;

dateTimePickerTask.Completed += this.dateTimePickerTask_Completed;
dateTimePickerTask.Show(DateTimePickerTask.DateTimePickerType.Date);
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
}

/// <summary>
/// Triggers special UI to select a time (hour/minute/am/pm).
/// </summary>
public void selectTime(string options)
{

try
{
try
{
this.dateTimePickerOptions = String.IsNullOrEmpty(options) ? new DateTimePickerOptions() :
WP7GapClassLib.PhoneGap.JSON.JsonHelper.Deserialize<DateTimePickerOptions>(options);

}
catch (Exception ex)
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message));
return;
}

this.dateTimePickerTask = new DateTimePickerTask();
dateTimePickerTask.Value = dateTimePickerOptions.Value;

dateTimePickerTask.Completed += this.dateTimePickerTask_Completed;
dateTimePickerTask.Show(DateTimePickerTask.DateTimePickerType.Time);
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
}

/// <summary>
/// Handles datetime picker result
/// </summary>
/// <param name="sender"></param>
/// <param name="e">stores information about current captured image</param>
private void dateTimePickerTask_Completed(object sender, DateTimePickerTask.DateTimeResult e)
{

if (e.Error != null)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
return;
}

switch (e.TaskResult)
{
case TaskResult.OK:
try
{
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, e.Value.Value.ToString()));
}
catch (Exception ex)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Datetime picker error. " + ex.Message));
}
break;

case TaskResult.Cancel:
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Canceled."));
break;
}

this.dateTimePickerTask = null;
}

}
}
Loading

0 comments on commit e1b20fe

Please sign in to comment.