public MedicationDosageViewModel() { this.DosageHours = new RxUI.ReactiveList <TimeSpan>(); var canSave = this.WhenAny( vm => vm.MedicationName, vm => vm.MedicationDosage, vm => vm.Monday, vm => vm.Tuesday, vm => vm.Wednesday, vm => vm.Thursday, vm => vm.Friday, vm => vm.Saturday, vm => vm.Sunday, vm => vm.DosageHours.Count, (n, d, m, t, w, th, f, sa, su, h) => !String.IsNullOrWhiteSpace(n.Value) && !String.IsNullOrWhiteSpace(d.Value) && (m.Value | t.Value | w.Value | th.Value | f.Value | sa.Value | su.Value) && h.Value > 0); this.TakePhotoCommand = ReactiveCommand.CreateFromTask(() => PictureChooser.TakePicture(100, 90)); this.TakePhotoCommand.Subscribe(x => { if (x != null) { this.OnPicture(x); } }); this.Save = RxUI.ReactiveCommand.CreateFromTask <Unit, bool>(async _ => { var dataRecord = new MedicationDosage { Id = this.Id, Name = this.MedicationName, Dosage = this.MedicationDosage, Days = (this.Monday ? DaysOfWeek.Monday : DaysOfWeek.None) | (this.Tuesday ? DaysOfWeek.Tuesday : DaysOfWeek.None) | (this.Wednesday ? DaysOfWeek.Wednesday : DaysOfWeek.None) | (this.Thursday ? DaysOfWeek.Thursday : DaysOfWeek.None) | (this.Friday ? DaysOfWeek.Friday : DaysOfWeek.None) | (this.Saturday ? DaysOfWeek.Saturday : DaysOfWeek.None) | (this.Sunday ? DaysOfWeek.Sunday : DaysOfWeek.None), DosageHours = this.DosageHours }; if (this.Bytes != null) { dataRecord.ImageName = $"image_{medicationName}"; dataRecord.ThumbnailName = $"thumbnail_{medicationName}"; imageLoader.SaveImage(this.Bytes, dataRecord.ImageName); imageLoader.SaveImage(this.Bytes, dataRecord.ThumbnailName, 30); } await this.storage.SaveAsync <MedicationDosage>(dataRecord); //var notification = new CoreNotification(dataRecord.Id.Value, dataRecord.Name, "Rano i wieczorem", new RepeatPattern() { DayOfWeek = dataRecord.Days, Interval = RepetitionInterval.None, RepetitionFrequency = 1 }); await this.notifications.ScheduleNotification(dataRecord); return(true); }, canSave); var canDelete = this.WhenAny(x => x.Id, id => id.Value.HasValue); this.Delete = RxUI.ReactiveCommand.CreateFromTask <Data.MedicationDosage, bool>(async _ => { if (this.Id.HasValue) { await this.storage.DeleteByKeyAsync <MedicationDosage>(this.Id.Value); return(true); } return(false); }, canDelete); this.SelectAllDays = ReactiveCommand <Unit, Unit> .Create(() => { Monday = true; Tuesday = true; Wednesday = true; Thursday = true; Friday = true; Saturday = true; Sunday = true; }); //save sie udal, albo nie - tu dosatniemy rezultat komendy. Jak sie udal, to zamykamy ViewModel this.Save .Subscribe(result => { if (result) { Mvx.Resolve <IMvxMessenger>().Publish(new DataChangedMessage(this)); this.Close(this); } }); this.Save.ThrownExceptions.Subscribe(ex => { UserDialogs.Instance.ShowError(AppResources.MedicationDosageView_SaveError); // show nice message to the user }); this.Delete .Subscribe(result => { if (result) { Mvx.Resolve <IMvxMessenger>().Publish(new DataChangedMessage(this)); this.Close(this); } }); }
public MedicationDosageViewModel() { ShowDialog = ReactiveCommand.Create(() => this.ShowViewModel <BottomDialogViewModel>()); this.DosageHours = new List <TimeSpan>(); var canSave = this.WhenAny( vm => vm.MedicationName, vm => vm.MedicationDosage, vm => vm.Monday, vm => vm.Tuesday, vm => vm.Wednesday, vm => vm.Thursday, vm => vm.Friday, vm => vm.Saturday, vm => vm.Sunday, vm => vm.DosageHours, (n, d, m, t, w, th, f, sa, su, hours) => !String.IsNullOrWhiteSpace(n.Value) && !String.IsNullOrWhiteSpace(d.Value) && (m.Value | t.Value | w.Value | th.Value | f.Value | sa.Value | su.Value) && hours.Value.Count > 0); this.TakePhotoCommand = ReactiveCommand.CreateFromTask(() => PictureChooser.TakePicture(1920, 75)); this.TakePhotoCommand .Where(x => x != null) .Select(x => this.OnPicture(x)) .Subscribe(); this.Save = RxUI.ReactiveCommand.CreateFromTask <Unit, bool>(async _ => { var dataRecord = new MedicationDosage { Id = this.Id, Name = this.MedicationName, From = this.StartDate, To = this.EndDate, Dosage = this.MedicationDosage, Days = (this.Monday ? DaysOfWeek.Monday : DaysOfWeek.None) | (this.Tuesday ? DaysOfWeek.Tuesday : DaysOfWeek.None) | (this.Wednesday ? DaysOfWeek.Wednesday : DaysOfWeek.None) | (this.Thursday ? DaysOfWeek.Thursday : DaysOfWeek.None) | (this.Friday ? DaysOfWeek.Friday : DaysOfWeek.None) | (this.Saturday ? DaysOfWeek.Saturday : DaysOfWeek.None) | (this.Sunday ? DaysOfWeek.Sunday : DaysOfWeek.None), DosageHours = this.DosageHours, Hours = this.HoursLabel, RingUri = this.RingUri }; if (!string.IsNullOrEmpty(this.StartDate) && !string.IsNullOrEmpty(this.EndDate)) { DateTime start = DateTime.Parse(this.StartDate); DateTime end = DateTime.Parse(this.EndDate); if (start > end) { UserDialogs.Instance.Toast("Ustaw prawidłowo zakres dat."); return(false); } } if (this.Bytes != null) { dataRecord.ImageName = $"image_{medicationName}"; dataRecord.ThumbnailName = $"thumbnail_{medicationName}"; imageLoader.SaveImage(this.Bytes, dataRecord.ImageName); imageLoader.SaveImage(this.Bytes, dataRecord.ThumbnailName, 120); } await this.storage.SaveAsync <MedicationDosage>(dataRecord); // usuwam poprzednie notyfikacje await this.notifications.CancelAllNotificationsForMedication(dataRecord); // dodaję najbliższe wystąpienia do tabeli NotificationOccurrence await DbHelper.AddNotificationOccurrences(dataRecord); // dodaję notyfikacje - w środku czytam NotificationOccurrence await this.notifications.ScheduleNotifications(dataRecord); Mvx.Resolve <IMvxMessenger>().Publish(new NotificationsChangedMessage(this)); return(true); }, canSave); var canDelete = this.WhenAny(x => x.Id, id => id.Value.HasValue); this.Delete = RxUI.ReactiveCommand.CreateFromTask <Data.MedicationDosage, bool>(async _ => { if (this.Id.HasValue) { await this.storage.DeleteByKeyAsync <MedicationDosage>(this.Id.Value); await this.notifications.CancelAndRemove(this.Id.Value); return(true); } return(false); }, canDelete); this.SelectAllDays = ReactiveCommand.Create(() => { selectAllDays(); }); //save sie udal, albo nie - tu dosatniemy rezultat komendy. Jak sie udal, to zamykamy ViewModel this.Save .Subscribe(result => { if (result) { Mvx.Resolve <IMvxMessenger>().Publish(new DataChangedMessage(this)); this.Close(this); } }); this.Save.ThrownExceptions.Subscribe(ex => { UserDialogs.Instance.ShowError(AppResources.MedicationDosageView_SaveError); // show nice message to the user }); this.Delete .Subscribe(result => { if (result) { Mvx.Resolve <IMvxMessenger>().Publish(new DataChangedMessage(this)); this.Close(this); } }); GoSettings = ReactiveCommand.Create(() => this.ShowViewModel <SettingsViewModel>()); this.WhenAnyValue(x => x.TimeItems) .Where(x => x != null) .Select(ti => ti.ItemChanged) .Switch() .Subscribe(_ => { CheckedHours = this.TimeItems.Where(x => x.Checked).ToList(); setHours(); }); //Observe days and Humaziner this.WhenAnyValue(x => x.Monday, x => x.Tuesday, x => x.Wednesday, x => x.Thursday, x => x.Friday, x => x.Saturday, x => x.Sunday) .Subscribe(days => DaysLabel = HumanizeOrdinationScheme(new[] { days.Item1, days.Item2, days.Item3, days.Item4, days.Item5, days.Item6, days.Item7 })); }