public MedicinePage() { InitializeComponent(); medicineManager = MedicineManager.DefaultManager; reminderManager = ReminderManager.DefaultManager; medicineHistoryManager = MedicineHistoryManager.DefaultManager; Init(); ReminderClicked(); currentView = 0; }
async void EndClicked(object sender, EventArgs e) { var startday = new DateTime(currentMedicine.StartTime.Year, currentMedicine.StartTime.Month, currentMedicine.StartTime.Day); var diff = DateTime.Now - startday; var difff = diff.Days + 1; if (difff < int.Parse(currentMedicine.Duration)) { var res = await DisplayAlert("Notice!", "You haven't finished this medication. Are you sure to terminate it early? ", "Yes", "No"); if (res) { //delete related reminders ReminderManager reminderManager = ReminderManager.DefaultManager; var temp = await reminderManager.GetReminderAsync(); foreach (var t in temp) { if (t.MedicineId == currentMedicine.Id) { await reminderManager.DeleteTaskAsync(t); DeleteNotification(t); } } //delete thie medicine MedicineManager medicineManager = MedicineManager.DefaultManager; await medicineManager.DeleteTaskAsync(currentMedicine); App.contentChanged = true; //set a new medicine history MedicineHistory medicineHistory = new MedicineHistory { Id = Guid.NewGuid().ToString(), UserId = App.email, MedicineName = currentMedicine.MedicineName, Directions = currentMedicine.Directions, TimeToDisplay = currentMedicine.StartTime.ToString("dd/MMM/yyyy ddd") + " - " + DateTime.Now.ToString("dd/MMM/yyyy ddd"), Description = currentMedicine.Description, Duration = currentMedicine.Duration, TimesPerDay = currentMedicine.TimesPerDay, Unit = currentMedicine.Unit, StartTime = currentMedicine.StartTime, DirectionsToDisplay = currentMedicine.TimesPerDay + " times a day, " + currentMedicine.Unit + " each time", IsDone = false, IsUnDone = true }; if (currentMedicine.TimesPerDay == 1) { medicineHistory.DirectionsToDisplay = "Once a day, " + currentMedicine.Unit + " each time"; } medicineHistory.Duration = difff.ToString(); MedicineHistoryManager medicineHistoryManager = MedicineHistoryManager.DefaultManager; await medicineHistoryManager.SaveTaskAsync(medicineHistory, true); await DisplayAlert("Notice:", "You have ended this medicine!", "OK"); } } else { //delete related reminders ReminderManager reminderManager = ReminderManager.DefaultManager; var temp = await reminderManager.GetReminderAsync(); foreach (var t in temp) { if (t.MedicineId == currentMedicine.Id) { await reminderManager.DeleteTaskAsync(t); DeleteNotification(t); } } //delete thie medicine MedicineManager medicineManager = MedicineManager.DefaultManager; await medicineManager.DeleteTaskAsync(currentMedicine); App.contentChanged = true; //set a new medicine history MedicineHistory medicineHistory = new MedicineHistory { Id = Guid.NewGuid().ToString(), UserId = App.email, MedicineName = currentMedicine.MedicineName, Directions = currentMedicine.Directions, TimeToDisplay = currentMedicine.StartTime.ToString("dd/MMM/yyyy ddd") + " - " + DateTime.Now.ToString("dd/MMM/yyyy ddd"), Description = currentMedicine.Description, Duration = currentMedicine.Duration, TimesPerDay = currentMedicine.TimesPerDay, Unit = currentMedicine.Unit, StartTime = currentMedicine.StartTime, DirectionsToDisplay = currentMedicine.TimesPerDay + " times a day, " + currentMedicine.Unit + " each time", IsDone = true, IsUnDone = false }; if (currentMedicine.TimesPerDay == 1) { medicineHistory.DirectionsToDisplay = "Once a day, " + currentMedicine.Unit + " each time"; } medicineHistory.Duration = difff.ToString(); MedicineHistoryManager medicineHistoryManager = MedicineHistoryManager.DefaultManager; await medicineHistoryManager.SaveTaskAsync(medicineHistory, true); await DisplayAlert("Notice:", "You have ended this medicine!", "OK"); } await Navigation.PopAsync(); }