コード例 #1
0
        protected override void UpdateTrigger()
        {
            var eventTrigger = EventTrigger;

            if (eventTrigger == null)
            {
                return;
            }

            MatchEventsFromTriggerIfNecessary();
            RemovePendingEventsFromTrigger();

            foreach (var kvp in targetValueMap)
            {
                var characteristic = kvp.Key;
                var triggerValue   = kvp.Value;

                var newEvent = new HMCharacteristicEvent(characteristic, triggerValue);

                SaveTriggerGroup.Enter();
                eventTrigger.AddEvent(newEvent, error => {
                    if (error != null)
                    {
                        Errors.Add(error);
                    }
                    SaveTriggerGroup.Leave();
                });
            }
            SavePredicate();
        }
コード例 #2
0
        // Loops through the characteristic events in the trigger.
        // If any characteristics in our map table are also in the event,
        // replace the value with the one we have stored and remove that entry from our map table.
        void MatchEventsFromTriggerIfNecessary()
        {
            var eventTrigger = EventTrigger;

            if (eventTrigger == null)
            {
                return;
            }

            // Find events who's characteristic is in our map table.
            foreach (var e in eventTrigger.CharacteristicEvents())
            {
                INSCopying triggerValue;
                if (targetValueMap.TryGetValue(e.Characteristic, out triggerValue))
                {
                    SaveTriggerGroup.Enter();
                    e.UpdateTriggerValue(triggerValue, error => {
                        if (error != null)
                        {
                            Errors.Add(error);
                        }
                        SaveTriggerGroup.Leave();
                    });
                }
            }
        }
コード例 #3
0
        // Completes one of two actions based on the current status of the `trigger` object:
        // 1. Updates the existing trigger.
        // 2. Creates a new trigger.
        public void SaveTriggerWithName(string name, IEnumerable <HMActionSet> actionSets, Action <HMTrigger, IEnumerable <NSError> > completion)
        {
            Name = name;
            if (Trigger != null)
            {
                // Let the subclass update the trigger.
                UpdateTrigger();
                UpdateNameIfNecessary();
                ConfigureWithActionSets(actionSets);
            }
            else
            {
                Trigger = NewTrigger();
                SaveTriggerGroup.Enter();
                Home.AddTrigger(Trigger, error => {
                    if (error != null)
                    {
                        errors.Add(error);
                    }
                    else
                    {
                        ConfigureWithActionSets(actionSets);
                    }
                    SaveTriggerGroup.Leave();
                });
            }

            // Call the completion block with our event trigger and any accumulated errors from the saving process.
            SaveTriggerGroup.Notify(DispatchQueue.MainQueue, () => {
                CleanUp();
                completion(Trigger, errors);
            });
        }
コード例 #4
0
        // Updates the predicates and saves the new, generated predicate to the event trigger.
        protected void SavePredicate()
        {
            UpdatePredicates();
            SaveTriggerGroup.Enter();

            var eventTrigger = EventTrigger;

            if (eventTrigger != null)
            {
                eventTrigger.UpdatePredicate(NewPredicate(), error => {
                    if (error != null)
                    {
                        Errors.Add(error);
                    }
                    SaveTriggerGroup.Leave();
                });
            }
        }
コード例 #5
0
        // Updates the trigger's recurrence components, entering and leaving the dispatch group if necessary.
        // If the trigger's components are already equal to the passed-in components, this method does nothing.
        void UpdateRecurrenceIfNecessary()
        {
            var timerTrigger = TimerTrigger;

            if (timerTrigger == null || timerTrigger.Recurrence == RecurrenceComponents)
            {
                return;
            }

            SaveTriggerGroup.Enter();
            timerTrigger.UpdateRecurrence(RecurrenceComponents, error => {
                if (error != null)
                {
                    Errors.Add(error);
                }
                SaveTriggerGroup.Leave();
            });
        }
コード例 #6
0
        // Updates the trigger's fire date, entering and leaving the dispatch group if necessary.
        // If the trigger's fire date is already equal to the passed-in fire date, this method does nothing.
        void UpdateFireDateIfNecessary()
        {
            var timerTrigger = TimerTrigger;

            if (timerTrigger == null || timerTrigger.FireDate == FireDate)
            {
                return;
            }

            SaveTriggerGroup.Enter();
            timerTrigger.UpdateFireDate(FireDate, error => {
                if (error != null)
                {
                    Errors.Add(error);
                }
                SaveTriggerGroup.Leave();
            });
        }
コード例 #7
0
        // Updates the trigger's name from the stored name, entering and leaving the dispatch group if necessary.
        void UpdateNameIfNecessary()
        {
            var trigger = Trigger;

            if (trigger == null && trigger.Name == Name)
            {
                return;
            }

            SaveTriggerGroup.Enter();

            trigger.UpdateName(Name, error => {
                if (error != null)
                {
                    errors.Add(error);
                }
                SaveTriggerGroup.Leave();
            });
        }
コード例 #8
0
        // Syncs the trigger's action sets with the specified array of action sets.
        void ConfigureWithActionSets(IEnumerable <HMActionSet> actionSets)
        {
            var trigger = Trigger;

            if (trigger == null)
            {
                return;
            }

            // Save a standard completion handler to use when we either add or remove an action set.
            Action <NSError> defaultCompletion = error => {
                // Leave the dispatch group, to notify that we've finished this task.
                if (error != null)
                {
                    errors.Add(error);
                }
                SaveTriggerGroup.Leave();
            };

            // First pass, remove the action sets that have been deselected.
            foreach (var actionSet in trigger.ActionSets)
            {
                if (actionSets.Contains(actionSet))
                {
                    continue;
                }
                SaveTriggerGroup.Enter();
                trigger.RemoveActionSet(actionSet, defaultCompletion);
            }

            // Second pass, add the new action sets that were just selected.
            foreach (var actionSet in actionSets)
            {
                if (trigger.ActionSets.Contains(actionSet))
                {
                    continue;
                }
                SaveTriggerGroup.Enter();
                trigger.AddActionSet(actionSet, defaultCompletion);
            }
        }
コード例 #9
0
        // Removes all `HMCharacteristicEvent`s from the `removalCharacteristicEvents`
        // array and stores any errors that accumulate.
        void RemovePendingEventsFromTrigger()
        {
            var eventTrigger = EventTrigger;

            if (eventTrigger == null)
            {
                return;
            }

            foreach (var e in removalCharacteristicEvents)
            {
                SaveTriggerGroup.Enter();
                eventTrigger.RemoveEvent(e, error => {
                    if (error != null)
                    {
                        Errors.Add(error);
                    }
                    SaveTriggerGroup.Leave();
                });
            }

            removalCharacteristicEvents.Clear();
        }
コード例 #10
0
        protected override void UpdateTrigger()
        {
            var region = TargetRegion;

            if (region != null)
            {
                PrepareRegion();
                var locationEvent = LocationEvent;
                if (locationEvent != null)
                {
                    SaveTriggerGroup.Enter();
                    locationEvent.UpdateRegion(region, error => {
                        if (error != null)
                        {
                            Errors.Add(error);
                        }
                        SaveTriggerGroup.Leave();
                    });
                }
            }

            SavePredicate();
        }