예제 #1
0
파일: DbHelper.cs 프로젝트: dejvids/Piller
        public static async Task AddNotificationOccurrences(MedicationDosage medDosage)
        {
            if (medDosage.Days.AllSelected())
            {
                // schedule for every occurrence of hour for every 24 hours
                foreach (var hour in medDosage.HoursEncoded.Split(';'))
                {
                    var occurrence = new NotificationOccurrence()
                    {
                        Name               = medDosage.Name,
                        Dosage             = medDosage.Dosage,
                        MedicationDosageId = medDosage.Id.Value,
                        ThumbnailImage     = medDosage.ThumbnailName,
                        OccurrenceDateTime = NextOccurrenceFromHour(TimeSpan.Parse(hour))
                    };

                    await storage.SaveAsync(occurrence);
                }
            }
            else
            {
                // todo calculate all dates upon selected days of week
                // NumberOfDaysUntil returns non-negative values so the only thing that you should be careful of
                // is the nearest day/hour combination which may occur in the past and should be scheduled for week later
                var todayDay = DateTime.Now.DayOfWeek.ToDaysOfWeek();
                // schedule in a weekly manner for each day of week
                foreach (var hour in medDosage.HoursEncoded.Split(';'))
                {
                    foreach (var day in medDosage.Days.GetSelected())
                    {
                        day.TestNumberOfDaysUntil();
                        var occurrenceDate     = DateTime.Now.AddDays(todayDay.NumberOfDaysUntil(day)).Date;
                        var occurrenceTime     = TimeSpan.Parse(hour);
                        var occurrenceDateTime = new DateTime(occurrenceDate.Year, occurrenceDate.Month, occurrenceDate.Day, occurrenceTime.Hours, occurrenceTime.Minutes, 0);

                        var occurrence = new NotificationOccurrence()
                        {
                            Name               = medDosage.Name,
                            Dosage             = medDosage.Dosage,
                            MedicationDosageId = medDosage.Id.Value,
                            ThumbnailImage     = medDosage.ThumbnailName,
                            OccurrenceDateTime = occurrenceDateTime
                        };

                        await storage.SaveAsync(occurrence);
                    }
                }
            }
        }
예제 #2
0
        public async Task reloadDataBase()
        {
            IPermanentStorageService storage       = Mvx.Resolve <IPermanentStorageService>();
            INotificationService     notifications = Mvx.Resolve <INotificationService>();
            var items = await storage.List <MedicationDosage>();

            if (items == null)
            {
                return;
            }
            var medicationList = new List <MedicationDosage>(items);

            foreach (var item in medicationList)
            {
                var dosageHours = new List <TimeSpan>();
                var hours       = item.Hours.Split(new string[] { ", " }, StringSplitOptions.None);

                dosageHours = HoursList
                              .Where(h => hours.Contains(h.Name))
                              .Select(i => i.Hour)
                              .ToList();

                if (dosageHours.Count > 0)
                {
                    item.DosageHours = dosageHours;
                    item.RingUri     = ringUri;

                    // szud bi in som separet entity
                    await notifications.CancelAllNotificationsForMedication(item);

                    await storage.SaveAsync <MedicationDosage>(item);

                    await DbHelper.AddNotificationOccurrences(item);

                    await notifications.ScheduleNotifications(item);
                }
            }
        }