Exemple #1
0
        public void Observes <U>(IReadOnlyValue <U> other)
        {
            // REVIEW: recursive?
            void Handler(bool isUserAction)
            {
                var res = _computation();

                Logger.Debug(GetType(), "DerivedValue computation[1] result={0}", res);

                var old = Value;

                Errors = !res.Success ? new[] { res.ErrorMessage } : new string[] { };
                Value  = res.Success ? res.Result : _errorValue;

                Logger.Debug(GetType(), "DerivedValue computation[1] oldValue={0} newValue={1} errors={2}", old, Value, Errors.PrettyToString());

                Changed?.Invoke(this, old, Value, Errors, isUserAction);
            }

            other.Changed += (sender, value, newValue, errors, change) => Handler(change);

            Handler(false); //trigger validation / initialization

            Logger.Debug(GetType(), "DerivedValue subscribed to value");
        }
 public static MenuItemUserModel CreateRemoteLeaf(
     IReadOnlyValue <string> label, Func <Task <Unit> > command, bool isExitItem = false,
     string descriptionOrNull = null)
 {
     return(new MenuItemUserModel(
                label, new RemoteActionModel <Unit>(false, command), null, isExitItem, descriptionOrNull));
 }
        public void Observes <U>(IReadOnlyValue <U> other)
        {
            _dependencies.Add(() => other.Errors);

            void Handler(bool isUserAction)
            {
                var old = Value;

                var valueAndErrors = _computeValueAndErrors();

                Value  = valueAndErrors.Item1;
                Errors = valueAndErrors.Item2;

                var errCopy = Errors.ToList();

                Logger.Debug(GetType(), "ComputedValueOnDependancyChanged oldValue={0} newValue={1} errors={2}", old, Value, errCopy.PrettyToString());

                Changed?.Invoke(this, old, Value, errCopy, isUserAction);
            }

            // REVIEW: this looks like recursive call, because handler calls event inside it's body... or am I not getting somethiung ...
            other.Changed += (sender, value, newValue, errors, change) => Handler(change);

            Handler(false); //trigger validation / initialization

            Logger.Debug(GetType(), "ComputedValueOnDependancyChanged subscribed to {0} values", _dependencies.Count);
        }
 private MenuItemModel(
     int menuItemId, IReadOnlyValue <string> label, IEnumerable <MenuItemModel> subItemsOrNull,
     string descriptionOrNull, MenuItemUserModel source)
 {
     Id = menuItemId;
     DescriptonOrNull = descriptionOrNull;
     Label            = label;
     Items            = subItemsOrNull;
     Source           = source;
 }
 private MenuItemUserModel(
     IReadOnlyValue <string> label, IActionModel <Unit> actionOrNull,
     IEnumerable <MenuItemUserModel> subItemsOrNull, bool isExitItem, string descriptionOrNull)
 {
     Action            = actionOrNull;
     DescriptionOrNull = descriptionOrNull;
     Label             = label;
     Items             = subItemsOrNull;
     IsExit            = isExitItem;
 }
        public static ValueChangedRich <DomainT> BindReadOnlyAndInitialize <WidgetT, ViewT, DomainT>(
            this IReadOnlyValueView <WidgetT, ViewT> view,
            IReadOnlyValue <DomainT> domain,
            Func <DomainT, ViewT> convertFromDomain)
        {
            var handler = view.BindRead(domain, convertFromDomain);

            handler(domain, domain.Value, domain.Value, domain.Errors, false);

            return(handler);
        }
        public void Observes <U>(IReadOnlyValue <U> other)
        {
            _dependencies.Add(() => other.Errors);

            void Handler(object sender, U oldValue, U newValue, IEnumerable <string> _, bool isUserAction)
            {
                var old    = Value;
                var errors = DependencyErrors();

                Value = _computation(this);

                Logger.Debug(GetType(), "AggregatedErrorsValue oldValue={0} newValue={1} errors={2}", old, Value, errors.PrettyToString());

                Changed?.Invoke(this, old, Value, errors, isUserAction);
            }

            other.Changed += Handler;

            Handler(this, other.Value, other.Value, other.Errors, false); //trigger validation / initialization

            Logger.Debug(GetType(), "AggregatedErrorsValue subscribed to {0} values", _dependencies.Count);
        }
 public static MenuItemModel CreateSubTree(
     IReadOnlyValue <string> label, MenuItemUserModel source,
     IEnumerable <MenuItemModel> subItems)
 {
     return(new MenuItemModel(-1, label, subItems, null, source));
 }
 public static MenuItemModel CreateLeaf(
     int menuItemId, IReadOnlyValue <string> label, MenuItemUserModel source,
     string descriptionOrNull = null)
 {
     return(new MenuItemModel(menuItemId, label, null, descriptionOrNull, source));
 }
        public static ValueChangedRich <bool> BindEnableAndInitialize <T>(this IActionModel <T> model, IReadOnlyValue <bool> validator)
        {
            void Handler(object sender, bool oldValue, bool newValue, IEnumerable <string> errors, bool isUserAction)
            {
                model.ChangeEnabled(newValue, errors, isUserAction);
                Logger.Debug(typeof(ActionModelExtensions), "IActionModelExtensions->BindEnableAndInitialize set enable to {0}", newValue);
            }

            validator.Changed += Handler;
            Handler(validator, validator.Value, validator.Value, validator.Errors, false);
            return(Handler);
        }
 public static ValueChangedRich <T> BindReadOnlyAndInitialize <WidgetT, T>(this IReadOnlyValueView <WidgetT, T> view, IReadOnlyValue <T> domain)
 {
     return(view.BindReadOnlyAndInitialize(domain, x => x));
 }
        public static ValueChangedRich <DomainT> BindRead <WidgetT, ViewT, DomainT>(this IReadOnlyValueView <WidgetT, ViewT> view, IReadOnlyValue <DomainT> domain, Func <DomainT, ViewT> convertFromDomain)
        {
            ValueChangedRich <DomainT> handler = (sender, oldValue, newValue, rawErrors, isUserAction) => {
                var errors           = rawErrors.ToList();
                var allErrors        = new HashSet <string>(errors);
                var value            = RunConvertingExceptionToFailure(convertFromDomain, newValue);
                var selfInvalidation = sender == view && errors.Any();

                if (!value.Success)
                {
                    allErrors.Add(value.ErrorMessage);
                }

                Logger.Debug(typeof(ValueViewExtensions), "BindRead handler called with value after conversion={0} errors={1} senderIsSelf={2} selfInvalidation={3}", value, sender == view, allErrors.PrettyToString(), selfInvalidation);

                if (!selfInvalidation)
                {
                    //don't overwrite errorneus entry if entry origins from self so that user sees wrong value
                    view.Value = value.Result;
                }

                view.SetErrors(allErrors, isUserAction);
            };

            domain.Changed += handler;
            return(handler);
        }
 public static MenuItemUserModel CreateSubTree(
     IReadOnlyValue <string> label, params MenuItemUserModel[] subItems)
 {
     return(new MenuItemUserModel(label, null, subItems, false, null));
 }
 public static MenuItemUserModel CreateSubTree(
     IReadOnlyValue <string> label, IEnumerable <MenuItemUserModel> subItems)
 {
     return(new MenuItemUserModel(label, null, subItems, false, null));
 }
 public static MenuItemUserModel CreateLocalLeaf(
     IReadOnlyValue <string> label, Action command, string descriptionOrNull = null)
 {
     return(new MenuItemUserModel(label, new LocalActionModel(command), null, false, descriptionOrNull));
 }
 public static MenuItemUserModel CreateLogoutLeaf(
     IReadOnlyValue <string> label, string descriptionOrNull = null)
 {
     return(new MenuItemUserModel(
                label, new LocalActionModel(LambdaUtil.DoNothingAction), null, true, descriptionOrNull));
 }