Beispiel #1
0
        public MainViewModel()
        {
            this.calculator1 = new CalculatorViewModel(false);
            this.calculator2 = new CalculatorViewModel(false);

            this.currentCalculator = ObservablePropertyFactory.Instance.CreateProperty(this.calculator1);
        }
        protected override async ValueTask <bool> TryInvokeAsync(ObservablePropertyStateChangedArgs e)
        {
            if (consumer == null)
            {
                return(false);
            }

            if (!consumer.IsAlive())
            {
                return(true);
            }

            IObservableProperty observableProperty = e.ObservableProperty;
            string propertyName = e.PropertyInfo.Name;

            if (observableContainers.TryGetValue(observableProperty, out IObservableContainer container))
            {
                if (container.IsSubscribed(propertyName))
                {
                    await consumer.ForceUpdate();

                    return(true);
                }
            }

            return(false);
        }
        protected virtual void OnKeyChanged()
        {
            if (this.value != null)
            {
                this.value.ValueChanged -= OnValueChanged;
            }

            if (!this.enabled || this.target == null || string.IsNullOrEmpty(key))
            {
                return;
            }

            Localization localization = Localization.Current;

            this.value = localization.GetValue(key);

            //if (this.value == null)
            //{
            //    if (Application.isPlaying && log.IsErrorEnabled)
            //        log.ErrorFormat("There is an invalid localization key \"{0}\" on the {1} object named \"{2}\".", key, typeof(T).Name, this.name);
            //    return;
            //}

            this.value.ValueChanged += OnValueChanged;
            this.OnValueChanged(this.value, EventArgs.Empty);
        }
Beispiel #4
0
 private void ProfileManager_OnPropertyUpdatedEvent(short key, IObservableProperty property)
 {
     if (key == (short)ObservablePropertyCodes.DisplayName)
     {
         DisplayName = $"{ property.Serialize()}";
     }
 }
        protected virtual void OnKeyChanged()
        {
            if (this.value != null)
            {
                this.value.ValueChanged -= OnValueChanged;
            }

            if (!this.enabled || this.target == null || string.IsNullOrEmpty(key))
            {
                return;
            }

            Localization localization = Localization.Current;

            this.value = localization.Get <IObservableProperty>(key);
            if (this.value == null)
            {
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat("There is an invalid localization key \"{0}\" on the {1} object named \"{2}\".", key, typeof(T).Name, this.name);
                }
                return;
            }

            this.value.ValueChanged += OnValueChanged;
            this.OnValueChanged(this.value, EventArgs.Empty);
        }
 protected virtual void OnDisable()
 {
     if (this.value != null)
     {
         this.value.ValueChanged -= OnValueChanged;
         this.value = null;
     }
 }
Beispiel #7
0
 public static IObservableProperty <T> InterceptSetterBefore <T>(this IObservableProperty <T> observableProperty, Action <T, T> interceptor)
 {
     if (interceptor == null)
     {
         throw new ArgumentNullException("interceptor");
     }
     return(new ObservablePropertyBeforeSetterInterceptor <T>(observableProperty, interceptor));
 }
Beispiel #8
0
 public ViewModelPropertyInfo(IObservableProperty property, bool isElementProperty, bool isCollectionProperty,
                              bool isEnum, bool isComputed = false)
 {
     Property             = property;
     IsElementProperty    = isElementProperty;
     IsCollectionProperty = isCollectionProperty;
     IsEnum     = isEnum;
     IsComputed = isComputed;
 }
        public static IObservableGetProperty <TSource> ToGetPropertyMask <TSource>(this IObservableProperty <TSource> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            return(new ObservableGetPropertyMask <TSource>(source));
        }
Beispiel #10
0
        protected override void OnDirtyProperty(IObservableProperty property)
        {
            base.OnDirtyProperty(property);

            if (OnModifiedInServerEvent != null)
            {
                OnModifiedInServerEvent.Invoke(this);
            }
        }
 public ViewModelPropertyInfo(IObservableProperty property, bool isElementProperty, bool isCollectionProperty,
     bool isEnum, bool isComputed = false)
 {
     Property = property;
     IsElementProperty = isElementProperty;
     IsCollectionProperty = isCollectionProperty;
     IsEnum = isEnum;
     IsComputed = isComputed;
 }
        private DynamicPropertyProxy(
            IObservableProperty observableProperty,
            bool readOnly)
        {
            ObservableProperty = observableProperty;
            IsReadOnly         = readOnly;
            observableFactory  = observableProperty.CreateFactory();

            Initialize();
        }
        public ObservableLiteralSourceProxy(IObservableProperty source) : base(source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            this.observableProperty = source;
            this.observableProperty.ValueChanged += OnValueChanged;
        }
        private TStore GetStore(IObservableProperty observableProperty)
        {
            if (!storeInstanceCache.TryGetValue(observableProperty, out TStore store))
            {
                IPropertyProxy propertyProxy = propertyProxyFactory.Create(observableProperty);
                store = propertyProxyWrapper.WrapPropertyObservable <TStore>(propertyProxy);
            }

            return(store);
        }
Beispiel #15
0
        public bool TryGetValue(string themeId, out IObservableProperty <T> value)
        {
            if (_values.TryGetValue(themeId, out var v))
            {
                value = v;
                return(true);
            }

            value = null;
            return(false);
        }
Beispiel #16
0
        void Awake()
        {
            Localization localization = Localization.Current;

            this.value = localization.Get <IObservableProperty>(key);
            this.text  = this.GetComponent <Text>();
            if (this.value != null)
            {
                this.value.ValueChanged += OnValueChanged;
                this.text.text           = (string)Convert.ChangeType(this.value.Value, typeof(string));
            }
        }
        public RuntimeProxyManager(
            IObservableProperty observableProperty,
            MethodInterceptions methodInterceptions,
            bool readOnly)
        {
            ObservableProperty       = observableProperty;
            this.methodInterceptions = methodInterceptions;
            IsReadOnly        = readOnly;
            observableFactory = observableProperty.CreateFactory();

            Initialize();
        }
        private object CreatePropertyObservable(IObservableProperty observableProperty)
        {
            DynamicPropertyProxy dynamicProxy = new DynamicPropertyProxy(observableProperty, IsReadOnly);
            object boxedItem = Box(dynamicProxy, observableProperty.ObservedType).Target;

            foreach (var subscriber in subscribers)
            {
                dynamicProxy.Subscribe(subscriber);
            }

            return(boxedItem);
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="innerProperty"></param>
 /// <param name="action">Action to execute before the setter. First parameter is the old value, the second one is the new value.</param>
 public ObservablePropertyBeforeSetterInterceptor(IObservableProperty <T> innerProperty, Action <T, T> action)
 {
     if (innerProperty == null)
     {
         throw new ArgumentNullException("innerProperty");
     }
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     _innerProperty = innerProperty;
     _action        = action;
 }
Beispiel #20
0
        public ObservablePropertyHolder(
            IPropertyProxyFactory propertyProxyFactory,
            IPropertyProxyWrapper propertyProxyWrapper,
            IObservableFactoryFactory observableFactoryFactory)
        {
            this.propertyProxyWrapper = propertyProxyWrapper;
            this.propertyProxyFactory = propertyProxyFactory;
            this.observableFactory    = observableFactoryFactory.CreateFactory(
                OnPropertyStateChanged,
                OnCollectionItemsChanged);

            RootObservableProperty = observableFactory.CreateObservableProperty(typeof(T));
        }
        protected virtual ISourceProxy CreateProxy(LuaTable table, IPathNode node)
        {
            var indexedNode = node as IndexedNode;

            if (indexedNode != null)
            {
                if (indexedNode.Value is int)
                {
                    return(new LuaIntTableNodeProxy(table, (int)indexedNode.Value));
                }

                if (indexedNode.Value is string)
                {
                    return(new LuaStringTableNodeProxy(table, (string)indexedNode.Value));
                }

                return(null);
            }

            var memberNode = node as MemberNode;

            if (memberNode != null)
            {
                var obj = table.Get <object>(memberNode.Name);
                if (obj != null)
                {
                    LuaFunction function = obj as LuaFunction;
                    if (function != null)
                    {
                        return(new LuaMethodNodeProxy(table, function));
                    }

                    IObservableProperty observableValue = obj as IObservableProperty;
                    if (observableValue != null)
                    {
                        return(new ObservableNodeProxy(table, observableValue));
                    }

                    IInteractionRequest request = obj as IInteractionRequest;
                    if (request != null)
                    {
                        return(new InteractionNodeProxy(table, request));
                    }
                }

                return(new LuaStringTableNodeProxy(table, memberNode.Name));
            }
            return(null);
        }
Beispiel #22
0
        protected virtual void OnEnable()
        {
            this.target = this.GetComponent <T>();
            if (this.target == null)
            {
                return;
            }

            Localization localization = Localization.Current;

            this.value = localization.Get <IObservableProperty>(key);
            if (this.value != null)
            {
                this.value.ValueChanged += OnValueChanged;
                this.OnValueChanged(this.value, EventArgs.Empty);
            }
        }
Beispiel #23
0
        public void OverwriteFrom(IObservableProperty source, bool notify)
        {
            object newValue;

            foreach (var propertyName in allPropertiesByName.Keys)
            {
                if (!source.TryGetMember(propertyName, out newValue))
                {
                    throw new Exception($"Could not copy from source. Property name: {propertyName}");
                }

                if (!TrySetMemberInternal(propertyName, newValue, notify))
                {
                    throw new Exception($"Could not copy to target. Property name: {propertyName}");
                }
            }
        }
Beispiel #24
0
        private T BoxItem(T item)
        {
            if (!(item != null && proxyWrapper.UnwrapPropertyObservable(item) is IPropertyProxy propertyProxy))
            {
                IObservableProperty observableProperty = observableFactory.CreateObservableProperty(typeof(T));
                if (item != null)
                {
                    observableProperty.OverwriteFrom(item, true);
                }
                propertyProxy = proxyFactory.Create(observableProperty);
            }

            foreach (var subscriber in subscribers)
            {
                propertyProxy.Subscribe(subscriber);
            }

            return(proxyWrapper.WrapPropertyObservable <T>(propertyProxy));
        }
        private void Initialize()
        {
            var observedProperties  = ObservableProperty.GetObservedProperties();
            var observedCollections = ObservableProperty.GetObservedCollections();

            foreach (var observedProperty in observedProperties)
            {
                IObservableProperty observableProperty = observedProperty.Value;
                runtimeProxies[observedProperty.Key] = (IRuntimeProxy)RuntimeProxyManagerHelper.CreateRuntimeManager(observableProperty, null, IsReadOnly).Implementation;
            }

            foreach (var observedCollection in observedCollections)
            {
                collectionProxies[observedCollection.Key] = CollectionProxy.Create(
                    observedCollection.Value,
                    proxyWrapper,
                    proxyFactory,
                    observableFactory);
            }
        }
Beispiel #26
0
        public ITargetProxy CreateProxy(object target, BindingDescription description)
        {
            if (target == null || !(target is ILuaExtendable))
            {
                return(null);
            }

            LuaTable metatable = (target as ILuaExtendable).GetMetatable();

            if (metatable == null || !metatable.ContainsKey(description.TargetName))
            {
                return(null);
            }

            var obj = metatable.Get <object>(description.TargetName);

            if (obj != null)
            {
                LuaFunction function = obj as LuaFunction;
                if (function != null)
                {
                    return(new LuaMethodTargetProxy(target, function));
                }

                IObservableProperty observableValue = obj as IObservableProperty;
                if (observableValue != null)
                {
                    return(new ObservableTargetProxy(target, observableValue));
                }

                IInteractionAction interactionAction = obj as IInteractionAction;
                if (interactionAction != null)
                {
                    return(new InteractionTargetProxy(target, interactionAction));
                }
            }
            return(new LuaTableTargetProxy(target, description.TargetName));
        }
Beispiel #27
0
        public MainViewModel()
        {
            this.text = ObservablePropertyFactory.Instance.CreateProperty(string.Empty);

            Random r = new Random();
            this.calculated = ObservablePropertyFactory.Instance.CreateCancellableAsyncCalculatedPropertyWithContext(
                r,
                this.text,
                TimeSpan.FromSeconds(0.1),
                async (helper, context, text) =>
                {
                    int millisecondsToWait = context.Next(5000);
                    await Task.Delay(millisecondsToWait, helper.Token).ConfigureAwait(false);
                    await helper.CheckCancellationTokenAndYield().ConfigureAwait(false);
                    return Tuple.Create(text + " [CALCULATED]", millisecondsToWait);
                });

            this.calculatedText = ObservablePropertyFactory.Instance.CreateCalculatedProperty(
                this.calculated, calculated => calculated.Switch(v => v == null ? null : v.Item1, e => "[ERROR]"));

            this.calculatedTime = ObservablePropertyFactory.Instance.CreateCalculatedProperty(
                this.calculated, calculated => calculated.Switch(v => v == null ? 0 : v.Item2, e => -1));
        }
Beispiel #28
0
 private void ProfilesManager_OnPropertyUpdatedEvent(short key, IObservableProperty property)
 {
     if (key == (short)ObservablePropertiyCodes.DisplayName)
     {
         DisplayName = $"Name: {property.Serialize()}";
     }
     else if (key == (short)ObservablePropertiyCodes.Avatar)
     {
         LoadAvatarImage(property.Serialize());
     }
     else if (key == (short)ObservablePropertiyCodes.Level)
     {
         Level = $"Level: {property.CastTo<ObservableFloat>().GetValue().ToString("F2")}";
     }
     else if (key == (short)ObservablePropertiyCodes.XP)
     {
         XP = $"XP: {property.CastTo<ObservableFloat>().GetValue().ToString("F2")}";
     }
     else if (key == (short)ObservablePropertiyCodes.Gold)
     {
         Gold = $"Gold: {property.CastTo<ObservableFloat>().GetValue().ToString("F2")}";
     }
 }
 private void ProfilesManager_OnPropertyUpdatedEvent(short key, IObservableProperty property)
 {
     if (key == (short)ObservablePropertiyCodes.DisplayName)
     {
         DisplayName = property.CastTo <ObservableString>().GetValue();
     }
     else if (key == (short)ObservablePropertiyCodes.Avatar)
     {
         LoadAvatarImage(property.Serialize());
     }
     else if (key == (short)ObservablePropertiyCodes.Bronze)
     {
         Bronze = property.CastTo <ObservableFloat>().GetValue().ToString("F2");
     }
     else if (key == (short)ObservablePropertiyCodes.Silver)
     {
         Silver = property.CastTo <ObservableFloat>().GetValue().ToString("F2");
     }
     else if (key == (short)ObservablePropertiyCodes.Gold)
     {
         Gold = property.CastTo <ObservableFloat>().GetValue().ToString("F2");
     }
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="property"></param>
 protected override void OnDirtyPropertyEventHandler(IObservableProperty property)
 {
     base.OnDirtyPropertyEventHandler(property);
     OnModifiedInServerEvent?.Invoke(this);
 }
 public SynchObservableAction(Action oxBodyAction, IObservableProperty <bool> isAllowedObservable, string commandName)
     : base(oxBodyAction, isAllowedObservable.GetValue, commandName)
 {
     _oxBodyAction = oxBodyAction;
     _Cached_IsExecutingPropertyDescription = PropertyDescription.FromExpression <ISynchAction, bool>(x => x.IsExecuting);
 }
Beispiel #32
0
 private void OnPropertyUpdatedEventHandler(short key, IObservableProperty property)
 {
     OnPropertyUpdatedEvent?.Invoke(key, property);
 }
Beispiel #33
0
 internal static void ValueAccessed(IObservableProperty observableProperty)
 {
     observableProperty.IsAccessed = true;
     if (BeingComputed.Count > 0) BeingComputed.Peek()(new PropertyAccessNotification(observableProperty));
     observableProperty.IsAccessed = false;
 }
Beispiel #34
0
        public CalculatorViewModel(bool supportsAsync)
        {
            this.updateInRealTimeItems =
                ObservablePropertyFactory.Instance.CreateReadOnlyProperty(
                    ObservableCollectionFactory.Instance.CreateObservableCollection(new[] { true, false }));
            this.updateInRealTimeSelection = ObservablePropertyFactory.Instance.CreateProperty<bool?>(true);
            this.updateInRealTime =
                ObservablePropertyFactory.Instance.CreateCalculatedProperty(
                    this.updateInRealTimeSelection,
                    updateInRealTimeSelection => updateInRealTimeSelection.HasValue && updateInRealTimeSelection.Value);
            this.supportsAsync = ObservablePropertyFactory.Instance.CreateReadOnlyProperty(supportsAsync);
            this.simulateLatencyItems =
                ObservablePropertyFactory.Instance.CreateReadOnlyProperty(
                    ObservableCollectionFactory.Instance.CreateObservableCollection(new[] { true, false }));
            this.simulateLatencySelection = ObservablePropertyFactory.Instance.CreateProperty<bool?>(true);
            this.simulateLatency =
                ObservablePropertyFactory.Instance.CreateCalculatedProperty(
                    this.simulateLatencySelection,
                    simulateLatencySelection => simulateLatencySelection.HasValue && simulateLatencySelection.Value);
            this.operators =
                ObservablePropertyFactory.Instance.CreateReadOnlyProperty(
                    ObservableCollectionFactory.Instance.CreateObservableCollection(
                        new[] { Operator.Add, Operator.Subtract, Operator.Multiply, Operator.Divide }));
            this.showMultiply =
                ObservablePropertyFactory.Instance.CreateCalculatedProperty(
                    this.operators.MergeCollectionPropertyWithChanges(this.operators),
                    c => c.Collection.Contains(Operator.Multiply));
            this.showDivide =
                ObservablePropertyFactory.Instance.CreateCalculatedProperty(
                    this.operators.MergeCollectionPropertyWithChanges(this.operators),
                    c => c.Collection.Contains(Operator.Divide));
            this.operand1 = ObservablePropertyFactory.Instance.CreateProperty<string>(null);
            this.selectedOperator = ObservablePropertyFactory.Instance.CreateProperty(Operator.Add);
            this.selectedOperatorString =
                ObservablePropertyFactory.Instance.CreateCalculatedProperty(
                    this.selectedOperator,
                    selectedOperator =>
                        {
                            switch (selectedOperator)
                            {
                                case Operator.Add:
                                    return "+";
                                case Operator.Subtract:
                                    return "-";
                                case Operator.Multiply:
                                    return "*";
                                case Operator.Divide:
                                    return "/";
                                default:
                                    throw new NotSupportedException(
                                        "Unknown enumeration value " + selectedOperator + ".");
                            }
                        });
            this.operand2 = ObservablePropertyFactory.Instance.CreateProperty<string>(null);
            this.result = ObservablePropertyFactory.Instance.CreateCalculatedProperty(
                this.Operand1,
                this.Operand2,
                this.SelectedOperator,
                (operand1, operand2, selectedOperator) =>
                    {
                        Func<double?, double?, double?> function;
                        switch (selectedOperator)
                        {
                            case Operator.Add:
                                function = (x, y) => x + y;
                                break;
                            case Operator.Subtract:
                                function = (x, y) => x - y;
                                break;
                            case Operator.Multiply:
                                function = (x, y) => x * y;
                                break;
                            case Operator.Divide:
                                function = (x, y) => x / y;
                                break;
                            default:
                                throw new NotSupportedException("Unknown enumeration value " + selectedOperator + ".");
                        }

                        double operand1Value;
                        double.TryParse(operand1, out operand1Value);
                        double operand2Value;
                        double.TryParse(operand2, out operand2Value);
                        double? r = function(operand1Value, operand2Value);
                        return r == null ? null : r.ToString();
                    });
        }
Beispiel #35
0
 /// <summary>
 /// Unbind this binding
 /// </summary>
 public virtual void Unbind()
 {
     IsBound = false;
     _modelProperty = null;
 }
Beispiel #36
0
 ///// <summary>
 ///// Constructor
 ///// </summary>
 ///// <param name="sourceView">The View that will own this binding.</param>
 ///// <param name="modelMemberName">The member of the ViewModel.</param>
 //protected Binding(ViewBase sourceView, string modelMemberName)
 //{
 //    Source = sourceView;
 //    ModelMemberName = modelMemberName;
 //}
 /// <summary>
 /// Set-up the binding. This should almost always be implemented in a deriving class.
 /// </summary>
 public virtual void Bind()
 {
     IsBound = true;
     _modelProperty = null;
 }