public static void UpdateAlarm(AlarmRecord record) { var obj = record; // Update native alarm DependencyService.Get <IAlarm>().UpdateAlarm(ref obj); record = obj; string alarmUID = record.GetUniqueIdentifier(); // Update list for (int i = 0; i < ObservableAlarmList.Count; i++) { AlarmRecord item = ObservableAlarmList[i]; //AlarmModel.Compare(record, item); if (item.GetUniqueIdentifier() == alarmUID) { DependencyService.Get <ILog>().Debug(" Found AlarmRecord(UID: " + item.GetUniqueIdentifier() + ") in ObservableAlarmList."); AlarmRecordDictionary.Remove(alarmUID); item.DeepCopy(record); AlarmRecordDictionary.Add(alarmUID, item); SaveDictionary(); break; } } }
/// <summary> /// Determines whether ObservableAlarmList contains an alarm which is scheduled at the same time and has the same alarm name. /// </summary> /// <param name="duplicate">AlarmRecord</param> /// <returns> true if a same alarm is found in the list; otherwise, false.</returns> private bool CheckAlarmExist(ref AlarmRecord duplicate) { foreach (AlarmRecord item in AlarmModel.ObservableAlarmList) { // Check scheduled time and alarm name if (item.ScheduledDateTime.Equals(AlarmModel.BindableAlarmRecord.ScheduledDateTime) && item.AlarmName.Equals(AlarmModel.BindableAlarmRecord.AlarmName)) { // a same alarm is found. duplicate.DeepCopy(item); return(true); } } // there's no same alarm. duplicate = null; return(false); }
/// <summary> /// Create native alarm /// </summary> public static void CreateAlarmAndSave() { // create a native alarm using AlarmModel.BindableAlarmRecord // After then, update Native alarm ID. BindableAlarmRecord.NativeAlarmID = DependencyService.Get <IAlarm>().CreateAlarm(BindableAlarmRecord); DependencyService.Get <ILog>().Debug("====== AlarmModel.CreateAlarm >> BindableAlarmRecord.NativeAlarmID : " + BindableAlarmRecord.NativeAlarmID); // ObservableAlarmList is ItemsSource for AlarmList view. This is bindable so this model // should be changed to reflect record change (adding a new alarm) AlarmRecord record = new AlarmRecord(); record.DeepCopy(BindableAlarmRecord); //AlarmModel.Compare(BindableAlarmRecord, record); ObservableAlarmList.Add(record); AlarmRecordDictionary.Add(record.GetUniqueIdentifier(), record); SaveDictionary(); }
// When button clicked, need to do followings: // 1. check time set by users // 2. convert TimeSpan to DateTime (since epoc time) // 3. get alarm name // 4. get alarm key (creation date which identify alarm uniquely) // 5. save a new record or update existing alarm. /// <summary> /// Invoked when "DONE" button is clicked /// </summary> /// <param name="sender">Titlebar's right button(DONE button)</param> /// <param name="e">EventArgs</param> private void DONE_Clicked(object sender, EventArgs e) { // Get time from TimePicker TimeSpan ts = editTimePickerCell.Time; DateTime now = System.DateTime.Now; AlarmModel.BindableAlarmRecord.ScheduledDateTime = new System.DateTime(now.Year, now.Month, now.Day, ts.Hours, ts.Minutes, 0); // Get name from Entry AlarmModel.BindableAlarmRecord.AlarmName = ((AlarmEditName)nameCell.View).mainEntry.Text; AlarmModel.BindableAlarmRecord.WeekdayRepeatText = AlarmModel.BindableAlarmRecord.GetFormatted(AlarmModel.BindableAlarmRecord.WeekFlag, AlarmModel.BindableAlarmRecord.AlarmState < AlarmStates.Inactive ? true : false); DependencyService.Get <ILog>().Debug("*** [START] [Clicked_SaveAlarm] BindableAlarmRecord : " + AlarmModel.BindableAlarmRecord); AlarmModel.BindableAlarmRecord.PrintProperty(); AlarmRecord duplicate = new AlarmRecord(); bool existSameAlarm = CheckAlarmExist(ref duplicate); if (existSameAlarm) { DependencyService.Get <ILog>().Debug("[Clicked_SaveAlarm] SAME ALARM EXISTS!!!! duplicate : " + duplicate); duplicate.PrintProperty(); // Need to show information Toast.DisplayText("Alarm already set for " + AlarmModel.BindableAlarmRecord.ScheduledDateTime + " " + AlarmModel.BindableAlarmRecord.AlarmName + ".\r\n Existing alarm updated."); // Use alarm created date for unique identifier for an alarm record string alarmUID = AlarmModel.BindableAlarmRecord.GetUniqueIdentifier(); if (!AlarmModel.BindableAlarmRecord.IsSerialized) { // in case that AlarmEditPage is shown by clicking a FloatingButton // when trying to create a system alarm and save it, find the same alarm // expected behavior : update the previous one and do not create a new alarm DependencyService.Get <ILog>().Debug(" case 1: A new alarm will be not created. The existing alarm(duplicate) will be updated with new info."); AlarmModel.BindableAlarmRecord.IsSerialized = true; // Copy modified data to the existing alarm record except alarm UID & native UID duplicate.DeepCopy(AlarmModel.BindableAlarmRecord, false); // Update the existing alarm(duplicate) AlarmModel.UpdateAlarm(duplicate); } else if (alarmUID.Equals(duplicate.GetUniqueIdentifier())) { // in case that AlarmEditPage is shown by selecting an item of ListView in AlarmListPage // At saving time, just update itself. (It doesn't affect other alarms) DependencyService.Get <ILog>().Debug(" case 2: duplicate == AlarmModel.BindableAlarmRecord. So, just update BindableAlarmRecord."); AlarmModel.UpdateAlarm(AlarmModel.BindableAlarmRecord); } else { // in case that AlarmEditPage is shown by selecting an item of ListView in AlarmListPage // At saving time, the same alarm is found. // In case that this alarm is not new, the existing alarm(duplicate) will be deleted and it will be updated. DependencyService.Get <ILog>().Debug(" case 3: delete duplicate and then update BindableAlarmRecord."); // 1. delete duplicate alarm AlarmModel.DeleteAlarm(duplicate); // 2. update bindableAlarmRecord AlarmModel.UpdateAlarm(AlarmModel.BindableAlarmRecord); } } else { DependencyService.Get <ILog>().Debug("NO SAME ALARM EXISTS!!!!"); if (!AlarmModel.BindableAlarmRecord.IsSerialized) { // In case that AlarmEditPage is shown by clicking FloatingButton // There's no same alarm. So, just create a new alarm and add to list and dictionary. DependencyService.Get <ILog>().Debug(" case 4: just create an alarm"); AlarmModel.BindableAlarmRecord.IsSerialized = true; AlarmModel.CreateAlarmAndSave(); } else { // in case that AlarmEditPage is shown by selecting an item of ListView in AlarmListPage // There's no same alarm. So, just update itself. DependencyService.Get <ILog>().Debug(" case 5: just update itself"); AlarmModel.UpdateAlarm(AlarmModel.BindableAlarmRecord); } } AlarmModel.PrintAll("Move from AlarmEditPage to AlarmListPage"); ((App)Application.Current).floatingButton.Show(); Navigation.PopAsync(); }