private async void btnSubmit_Click(object sender, RoutedEventArgs e) { Record r; if (!await CheckFormInput()) { MessageDialog dialog = new MessageDialog("Please Ensure All Fields Are Filled"); await dialog.ShowAsync(); return; } r = await CreateRecord(); if (r == null) { Debug.WriteLine("An Error occurred while creating the record"); return; } if (!await ReadRecordData() || records == null) { Debug.WriteLine("No records found"); records = RecordList.GetInstance(); } records.AddRecord(r); if (!await WriteRecordData(records)) { Debug.WriteLine("An Error occurred while writing records"); } MessageDialog riskDialog = new MessageDialog("Your Risk Level is: " + r.RiskLevel.ToString()); await riskDialog.ShowAsync(); Frame.GoBack(); }
private async Task <bool> WriteRecordData(RecordList records) { StorageFile file; MemoryStream ms = new MemoryStream(); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RecordList)); serializer.WriteObject(ms, records); byte[] buffer = ms.ToArray(); IStorageItem item = await ApplicationData.Current.LocalFolder.TryGetItemAsync("records.dat"); if (item == null) { file = await ApplicationData.Current.LocalFolder.CreateFileAsync("records.dat", Windows.Storage.CreationCollisionOption.ReplaceExisting); } else { file = await ApplicationData.Current.LocalFolder.GetFileAsync("records.dat"); await file.DeleteAsync(); file = await ApplicationData.Current.LocalFolder.CreateFileAsync("records.dat", Windows.Storage.CreationCollisionOption.ReplaceExisting); } StorageFolder folder = ApplicationData.Current.LocalFolder; using (var stream = await file.OpenStreamForWriteAsync()) { await stream.WriteAsync(buffer, 0, buffer.Length); } await ms.FlushAsync(); ms.Dispose(); return(true); }
private async Task <bool> ReadRecordData() { try { StorageFolder folder = ApplicationData.Current.LocalFolder; Stream stream = await folder.OpenStreamForReadAsync("records.dat"); if (stream.Length > 0) { using (StreamReader reader = new StreamReader(stream)) { DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings(); settings.RootName = "root"; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RecordList), settings); records = (RecordList)serializer.ReadObject(stream); } } return(true); } catch (FileNotFoundException ex) { StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("records.dat"); records = RecordList.GetInstance(); return(true); } return(false); }
private async Task <bool> CreateViewModel() { if (!await ReadUserDetails()) { Debug.WriteLine("Could not load user details"); return(false); } if (!await ReadRecordData()) { Debug.WriteLine("Could not load records"); return(false); } RecordList visibleRecordList = records; if (currentUser.Equals("admin")) { visibleRecordList = records; } else { visibleRecordList.Records = records.Records.Where(x => x.Owner.Username.Equals(currentUser)).ToList(); } RecordViewModel model = new RecordViewModel(records); lstRecords.DataContext = model; lstRecords.ItemsSource = model.PopulateData(); return(true); }
public static RecordList GetInstance() { if (instance == null) { instance = new RecordList(); } return(instance); }
public RecordViewModel(RecordList r) { records = r; }