/// <summary>
/// Invoked when 'Get History' button is clicked.
/// Depending on the user selection, this handler uses one of the overloaded
/// 'GetSystemHistoryAsync' APIs to retrieve the pedometer history of the user
/// </summary>
/// <param name="sender">unused</param>
/// <param name="e">unused</param>
async private void GetHistory_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
IReadOnlyList<PedometerReading> historyReadings = null;
DateTimeFormatter timestampFormatter = new DateTimeFormatter("shortdate longtime");
// Disable subsequent history retrieval while the async operation is in progress
GetHistory.IsEnabled = false;
// clear previous content being displayed
historyRecords.Clear();
try
{
if (getAllHistory)
{
DateTime dt = DateTime.FromFileTimeUtc(0);
DateTimeOffset fromBeginning = new DateTimeOffset(dt);
rootPage.NotifyUser("Retrieving all available History", NotifyType.StatusMessage);
historyReadings = await Pedometer.GetSystemHistoryAsync(fromBeginning);
}
else
{
String notificationString = "Retrieving history from: ";
Calendar calendar = new Calendar();
calendar.ChangeClock("24HourClock");
// DateTime picker will also include hour, minute and seconds from the the system time.
// Decrement the same to be able to correctly add TimePicker values.
calendar.SetDateTime(FromDate.Date);
calendar.AddNanoseconds(-calendar.Nanosecond);
calendar.AddSeconds(-calendar.Second);
calendar.AddMinutes(-calendar.Minute);
calendar.AddHours(-calendar.Hour);
calendar.AddSeconds(Convert.ToInt32(FromTime.Time.TotalSeconds));
DateTimeOffset fromTime = calendar.GetDateTime();
calendar.SetDateTime(ToDate.Date);
calendar.AddNanoseconds(-calendar.Nanosecond);
calendar.AddSeconds(-calendar.Second);
calendar.AddMinutes(-calendar.Minute);
calendar.AddHours(-calendar.Hour);
calendar.AddSeconds(Convert.ToInt32(ToTime.Time.TotalSeconds));
DateTimeOffset toTime = calendar.GetDateTime();
notificationString += timestampFormatter.Format(fromTime);
notificationString += " To ";
notificationString += timestampFormatter.Format(toTime);
if (toTime.ToFileTime() < fromTime.ToFileTime())
{
rootPage.NotifyUser("Invalid time span. 'To Time' must be equal or more than 'From Time'", NotifyType.ErrorMessage);
// Enable subsequent history retrieval while the async operation is in progress
GetHistory.IsEnabled = true;
}
else
{
TimeSpan span;
span = TimeSpan.FromTicks(toTime.Ticks - fromTime.Ticks);
rootPage.NotifyUser(notificationString, NotifyType.StatusMessage);
historyReadings = await Pedometer.GetSystemHistoryAsync(fromTime, span);
}
}
if (historyReadings != null)
{
foreach(PedometerReading reading in historyReadings)
{
HistoryRecord record = new HistoryRecord(reading);
historyRecords.Add(record);
}
rootPage.NotifyUser("History retrieval completed", NotifyType.StatusMessage);
}
}
catch (UnauthorizedAccessException)
{
rootPage.NotifyUser("User has denied access to activity history", NotifyType.ErrorMessage);
}
// Finally, re-enable history retrieval
GetHistory.IsEnabled = true;
}