Esempio n. 1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            NetworkStateChangeEventDetails details = taskInstance.TriggerDetails as NetworkStateChangeEventDetails;

            if (!details.HasNewNetworkConnectivityLevel)
            {
                return;
            }

            var connectionProfile = NetworkInformation.GetInternetConnectionProfile();

            if (connectionProfile.GetNetworkConnectivityLevel() != NetworkConnectivityLevel.InternetAccess)
            {
                return;
            }

            var deferral = taskInstance.GetDeferral();

            var settings   = new BackgroundSettingsViewModel();
            var dataSource = new MobileServicesDataSource(settings.MobileServicesUserId,
                                                          new AccessTokenRetriever(),
                                                          new BugSenseLogger());
            bool couldLogIn = await dataSource.Login(false, true);

            if (couldLogIn)
            {
                await dataSource.PushLocalChangesToServer();
            }

            deferral.Complete();
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            var deferral = taskInstance.GetDeferral();

            var tileUpdater = TileUpdateManager.CreateTileUpdaterForApplication();

            tileUpdater.EnableNotificationQueue(true);
            tileUpdater.Clear();

            var settings   = new BackgroundSettingsViewModel();
            var dataSource = new MobileServicesDataSource(settings.MobileServicesUserId,
                                                          new AccessTokenRetriever(),
                                                          new BugSenseLogger());
            bool couldLogIn = await dataSource.Login(false, true);

            if (couldLogIn)
            {
                var babies = await dataSource.GetBabies(settings.FamilyId);

                var babyActiviyScheduleTasks = babies.Select(async baby =>
                {
                    var activitySchedule = await dataSource.GetActivitySchedule(baby.Id);
                    ScheduleToastNotifications(activitySchedule, baby, settings);
                    return(Tuple.Create(baby, activitySchedule));
                });
                var babyActivitySchedules = await Task.WhenAll(babyActiviyScheduleTasks.ToArray());

                UpdateTiles(babyActivitySchedules, tileUpdater, settings);
            }

            deferral.Complete();
        }
        private static void ScheduleToastNotifications(ActivitySchedule schedule, Baby baby, BackgroundSettingsViewModel settings)
        {
            var    toastNotifier = ToastNotificationManager.CreateToastNotifier();
            string notificationId;

            // Delete & recreate Feed notification
            DeleteExistingNotification(toastNotifier, settings.NextFeedNotificationId);
            settings.NextFeedNotificationId = null;
            if (!schedule.CurrentlyFeeding)
            {
                notificationId = ScheduleToastNotificationForActivity(schedule.NextFeedDueAt,
                                                                      "feed",
                                                                      baby.GivenName,
                                                                      toastNotifier);
                settings.NextFeedNotificationId = notificationId;
            }

            // Delete & recreate Sleep notification
            DeleteExistingNotification(toastNotifier, settings.NextSleepNotificationId);
            settings.NextSleepNotificationId = null;
            if (!schedule.CurrentlySleeping)
            {
                notificationId = ScheduleToastNotificationForActivity(schedule.NextSleepDueAt,
                                                                      "sleep",
                                                                      baby.GivenName,
                                                                      toastNotifier);
                settings.NextSleepNotificationId = notificationId;
            }

            // Delete & recreate Change notification
            DeleteExistingNotification(toastNotifier, settings.NextChangeNotificationId);
            settings.NextChangeNotificationId = null;
            notificationId = ScheduleToastNotificationForActivity(schedule.NextChangeDueAt,
                                                                  "change",
                                                                  baby.GivenName,
                                                                  toastNotifier);
            settings.NextChangeNotificationId = notificationId;
        }
        private void UpdateTiles(IEnumerable <Tuple <Baby, ActivitySchedule> > schedules, TileUpdater tileUpdater, BackgroundSettingsViewModel settings)
        {
            string feedActivityName   = resourceLoader.GetString("ActivityName_Feed"),
                   sleepActivityName  = resourceLoader.GetString("ActivityName_Sleep"),
                   changeActivityName = resourceLoader.GetString("ActivityName_Change");

            var feedSchedules = schedules.Select(s => new BabyActivityStatus
            {
                ActivityName          = feedActivityName,
                BabyGivenName         = s.Item1.GivenName,
                ActivityIsRunning     = s.Item2.CurrentlyFeeding,
                LastActivityStartTime = s.Item2.LastFeed != null ? s.Item2.LastFeed.StartTime : (DateTimeOffset?)null,
                NextActivityStartTime = s.Item2.NextFeedDueAt
            });

            var sleepSchedules = schedules.Select(s => new BabyActivityStatus
            {
                ActivityName          = sleepActivityName,
                BabyGivenName         = s.Item1.GivenName,
                ActivityIsRunning     = s.Item2.CurrentlySleeping,
                LastActivityStartTime = s.Item2.LastSleep != null ? s.Item2.LastSleep.StartTime : (DateTimeOffset?)null,
                NextActivityStartTime = s.Item2.NextSleepDueAt
            });

            var changeSchedules = schedules.Select(s => new BabyActivityStatus
            {
                ActivityName          = changeActivityName,
                BabyGivenName         = s.Item1.GivenName,
                ActivityIsRunning     = false,
                LastActivityStartTime = s.Item2.LastChange != null ? s.Item2.LastChange.StartTime : (DateTimeOffset?)null,
                NextActivityStartTime = s.Item2.NextChangeDueAt
            });

            tileUpdater.CreateActivityTile(feedActivityName, feedSchedules);
            tileUpdater.CreateActivityTile(sleepActivityName, sleepSchedules);
            tileUpdater.CreateActivityTile(changeActivityName, changeSchedules);
        }