/// <summary> /// Creates and adds a time track objects into the Items collection. /// </summary> public async void LoadData() { var fileList = await ApplicationData.Current.LocalFolder.GetFilesAsync(); var file = fileList.FirstOrDefault(f => f.Name == localFileName); if (file != null) { using (var stream = await file.OpenReadAsync()) { using (var dr = new Windows.Storage.Streams.DataReader(stream)) { try { await dr.LoadAsync(unchecked((uint)stream.Size)); var numberOfItems = dr.ReadInt32(); ObservableCollection<TrackItem> loadingItems = new ObservableCollection<TrackItem>(); for (int x = 0; x < numberOfItems; x++) { TrackItem item = new TrackItem(); item.Start = dr.ReadDateTime(); item.End = dr.ReadDateTime(); int topicLength = dr.ReadInt32(); item.Topic = dr.ReadString((uint)topicLength); loadingItems.Add(item); } bool currentItemExists = dr.ReadBoolean(); TrackItem loadingCurrentItem = null; if (currentItemExists) { loadingCurrentItem = new TrackItem(); loadingCurrentItem.Start = dr.ReadDateTime(); int topicLength = dr.ReadInt32(); loadingCurrentItem.Topic = dr.ReadString((uint)topicLength); } dr.DetachStream(); Items = loadingItems; OnPropertyChanged(nameof(Items)); CurrentItem = loadingCurrentItem; IsDataLoaded = true; } catch { try { await file.DeleteAsync(); } catch { } CurrentItem = null; IsDataLoaded = true; } } } } else { // Sample data; replace with real data Items.Add(new TrackItem() { Topic = "First Tracked Item", Start = new DateTimeOffset(new DateTime(2015, 11, 5, 9, 0, 0), new TimeSpan(-6, 0, 0)), End = new DateTimeOffset(new DateTime(2015, 11, 5, 11, 30, 0), new TimeSpan(-6, 0, 0)), }); Items.Add(new TrackItem() { Topic = "Second Tracked Item", Start = new DateTimeOffset(new DateTime(2015, 11, 5, 12, 30, 0), new TimeSpan(-6, 0, 0)), End = new DateTimeOffset(new DateTime(2015, 11, 5, 17, 0, 0), new TimeSpan(-6, 0, 0)), }); CurrentItem = null; this.IsDataLoaded = true; } }
public void CreateItem(string topic) { if (CurrentItem != null) { CurrentItem.End = DateTimeOffset.Now; Items.Add(CurrentItem); } CurrentItem = new TrackItem() { Topic = topic, Start = DateTimeOffset.Now, End = null, }; }