1+ using JetBrains . Annotations ;
12using System ;
23using System . Collections ;
34using System . Collections . Generic ;
78
89namespace RedMoon . ReactiveKit
910{
10- public static class BindingExtensions
11+ public static partial class BindingExtensions
1112 {
1213 #region Visual Element
13- public static IDisposable BindEnabled ( this VisualElement element , IReadOnlyReactiveProperty < bool > property )
14+ /// <summary>
15+ /// Binds Element's Enabled State to Boolean Observable
16+ /// </summary>
17+ /// <param name="element">Element to Bind</param>
18+ /// <param name="observable">Observable Stream to Subscribe to</param>
19+ /// <returns>Disposable for Disposing Subscription</returns>
20+ [ MustUseReturnValue ]
21+ public static IDisposable BindEnabled ( this VisualElement element , IObservable < bool > observable )
22+ {
23+ return observable . ObserveOnMainThread ( ) . DistinctUntilChanged ( ) . Subscribe ( element . SetEnabled ) ;
24+ }
25+ /// <summary>
26+ /// Binds Element's Visibility State to Visibility Observable
27+ /// </summary>
28+ /// <param name="element">Element to Bind</param>
29+ /// <param name="observable">Observable Stream to Subscribe to</param>
30+ /// <returns>Disposable for Disposing Subscription</returns>
31+ [ MustUseReturnValue ]
32+ public static IDisposable BindVisibility ( this VisualElement element , IObservable < Visibility > observable )
1433 {
15- return property . SubscribeWithState ( element , ( __property , __element ) =>
16- {
17- element . SetEnabled ( __property ) ;
18- } ) ;
34+ return observable . ObserveOnMainThread ( ) . DistinctUntilChanged ( ) . Subscribe ( value => element . style . visibility = value ) ;
1935 }
36+ /// <summary>
37+ /// Bind Element's Click Callback to Command
38+ /// </summary>
39+ /// <param name="element">Element to Bind</param>
40+ /// <param name="command">Command to Execute</param>
41+ /// <returns>Disposable for Disposing Subscription</returns>
42+ [ MustUseReturnValue ]
2043 public static IDisposable BindClick ( this VisualElement element , IReactiveCommand < ClickEvent > command )
2144 {
22- var d1 = element . BindEnabled ( command . CanExecute ) ;
23- var d2 = element . BindCallback ( command ) ;
45+ IDisposable d1 = element . BindEnabled ( command . CanExecute ) ;
46+ IDisposable d2 = element . BindCallback ( command ) ;
2447 return StableCompositeDisposable . Create ( d1 , d2 ) ;
2548 }
49+ /// <summary>
50+ /// Bind Element's Click Callback to Command
51+ /// </summary>
52+ /// <param name="element">Element to Bind</param>
53+ /// <param name="command">Command to Execute</param>
54+ /// <param name="dataForCallback">Callback as a part of Command</param>
55+ /// <returns>Disposable for Disposing Subscription</returns>
56+ [ MustUseReturnValue ]
2657 public static IDisposable BindClick < TArgs > ( this VisualElement element , IReactiveCommand < ( ClickEvent , TArgs ) > command , TArgs dataForCallback )
2758 {
2859 var d1 = element . BindEnabled ( command . CanExecute ) ;
@@ -32,97 +63,77 @@ public static IDisposable BindClick<TArgs>(this VisualElement element, IReactive
3263 #endregion
3364
3465 #region Callback Event Handler
66+ [ MustUseReturnValue ]
3567 public static IDisposable BindCallback < TEventType > ( this CallbackEventHandler element , EventCallback < TEventType > callback , TrickleDown trickleDown = TrickleDown . NoTrickleDown ) where TEventType : EventBase < TEventType > , new ( )
3668 {
3769 element . RegisterCallback ( callback , trickleDown ) ;
3870 return Disposable . Create ( ( ) => { element . UnregisterCallback ( callback , trickleDown ) ; } ) ;
3971 }
72+ [ MustUseReturnValue ]
4073 public static IDisposable BindCallback < TEventType , TUserArgsType > ( this CallbackEventHandler element , EventCallback < TEventType , TUserArgsType > callback , TUserArgsType dataForCallback , TrickleDown trickleDown = TrickleDown . NoTrickleDown ) where TEventType : EventBase < TEventType > , new ( )
4174 {
4275 element . RegisterCallback ( callback , dataForCallback , trickleDown ) ;
4376 return Disposable . Create ( ( ) => { element . UnregisterCallback ( callback , trickleDown ) ; } ) ;
4477 }
78+ [ MustUseReturnValue ]
4579 public static IDisposable BindCallback < TEventType > ( this CallbackEventHandler element , IReactiveCommand < TEventType > command , TrickleDown trickleDown = TrickleDown . NoTrickleDown ) where TEventType : EventBase < TEventType > , new ( )
4680 {
47- var callback = new EventCallback < TEventType > ( ( ev ) => command . Execute ( ev ) ) ;
81+ EventCallback < TEventType > callback = new EventCallback < TEventType > ( ( ev ) => command . Execute ( ev ) ) ;
4882 element . RegisterCallback ( callback , trickleDown ) ;
4983 return Disposable . Create ( ( ) => { element . UnregisterCallback ( callback , trickleDown ) ; } ) ;
5084 }
85+ [ MustUseReturnValue ]
5186 public static IDisposable BindCallback < TEventType , TUserArgsType > ( this CallbackEventHandler element , IReactiveCommand < ( TEventType , TUserArgsType ) > command , TUserArgsType dataForCallback , TrickleDown trickleDown = TrickleDown . NoTrickleDown ) where TEventType : EventBase < TEventType > , new ( )
5287 {
53- var callback = new EventCallback < TEventType , TUserArgsType > ( ( ev , args ) => command . Execute ( ( ev , args ) ) ) ;
88+ EventCallback < TEventType , TUserArgsType > callback = new EventCallback < TEventType , TUserArgsType > ( ( ev , args ) => command . Execute ( ( ev , args ) ) ) ;
5489 element . RegisterCallback ( callback , dataForCallback , trickleDown ) ;
5590 return Disposable . Create ( ( ) => { element . UnregisterCallback ( callback , trickleDown ) ; } ) ;
5691 }
5792 #endregion
5893
5994 #region Notifications
95+ [ MustUseReturnValue ]
6096 public static IDisposable BindValueChanged < T > ( this INotifyValueChanged < T > element , IReactiveCommand < ChangeEvent < T > > command )
6197 {
62- var callback = new EventCallback < ChangeEvent < T > > ( ( ev ) => command . Execute ( ev ) ) ;
98+ EventCallback < ChangeEvent < T > > callback = new EventCallback < ChangeEvent < T > > ( ( ev ) => command . Execute ( ev ) ) ;
6399 element . RegisterValueChangedCallback ( callback ) ;
64100 return Disposable . Create ( ( ) => { element . UnregisterValueChangedCallback ( callback ) ; } ) ;
65101 }
102+ /// <summary>
103+ /// Binds a Property Value to Element Changing
104+ /// </summary>
105+ /// <typeparam name="T">Type for Change Event</typeparam>
106+ /// <param name="element">Element that Notifies Change</param>
107+ /// <param name="property">Property to Change on Notification</param>
108+ /// <returns>Disposable for Disposing Subscription</returns>
109+ [ MustUseReturnValue ]
66110 public static IDisposable BindValueChanged < T > ( this INotifyValueChanged < T > element , IReactiveProperty < T > property )
67111 {
68- var callback = new EventCallback < ChangeEvent < T > > ( ( ev ) => property . Value = ev . newValue ) ;
112+ EventCallback < ChangeEvent < T > > callback = new EventCallback < ChangeEvent < T > > ( ( ev ) => property . Value = ev . newValue ) ;
69113 element . RegisterValueChangedCallback ( callback ) ;
70114 return Disposable . Create ( ( ) => { element . UnregisterValueChangedCallback ( callback ) ; } ) ;
71115 }
72116 /// <summary>
73- /// Binds Element changes to Update Property Values.
74- /// Then Initializes Values.
117+ /// Binds an Element to a Property Changing
118+ /// Does not Trigger Element Notifications
75119 /// </summary>
76- /// <typeparam name="T"></typeparam>
77- /// <param name="element">Element with Value Changed Callbacks</param>
78- /// <param name="property">Reactive Property that value should change on a Value Changed Callback</param>
79- /// <param name="stateIsProperty">Whether Default Value should be Default Property Value</param>
80- /// <returns></returns>
81- public static IDisposable BindValueChangedWithState < T > ( this INotifyValueChanged < T > element , IReactiveProperty < T > property , bool stateIsProperty = true )
82- {
83- var d1 = BindValueChanged ( element , property ) ;
84- if ( stateIsProperty )
85- {
86- element . value = property . Value ;
87- }
88- else
89- {
90- property . Value = element . value ;
91- }
92- return d1 ;
93- }
94- public static IDisposable BindToValueChanged < T > ( this INotifyValueChanged < T > element , IReactiveProperty < T > property )
95- {
96- return property . SubscribeWithState ( element , ( __property , __element ) =>
97- {
98- __element . SetValueWithoutNotify ( __property ) ;
99- } ) ;
100- }
101- public static IDisposable BindToValueChangedWithState < T > ( this INotifyValueChanged < T > element , IReactiveProperty < T > property , bool stateIsProperty = true )
120+ /// <typeparam name="T">Type for Fields</typeparam>
121+ /// <param name="element">Element that has value updated</param>
122+ /// <param name="observable">Observable to update value</param>
123+ /// <returns>Disposable for Disposing Subscription</returns>
124+ [ MustUseReturnValue ]
125+ public static IDisposable BindToValueChanged < T > ( this INotifyValueChanged < T > element , IObservable < T > observable )
102126 {
103- var d1 = BindToValueChanged ( element , property ) ;
104- if ( stateIsProperty )
105- {
106- element . value = property . Value ;
107- }
108- else
109- {
110- property . Value = element . value ;
111- }
112- return d1 ;
127+ return observable . ObserveOnMainThread ( ) . DistinctUntilChanged ( ) . Subscribe ( ( ev ) => element . SetValueWithoutNotify ( ev ) ) ;
113128 }
129+ [ MustUseReturnValue ]
114130 public static IDisposable BindTwoWayValueChanged < T > ( this INotifyValueChanged < T > element , IReactiveProperty < T > property )
115131 {
116- var d1 = BindValueChanged ( element , property ) ;
117- var d2 = BindToValueChanged ( element , property ) ;
118- return StableCompositeDisposable . Create ( d1 , d2 ) ;
119- }
120- public static IDisposable BindTwoWayValueChangedWithState < T > ( this INotifyValueChanged < T > element , IReactiveProperty < T > property , bool stateIsProperty = true )
121- {
122- var d1 = BindValueChangedWithState ( element , property , stateIsProperty ) ;
123- var d2 = BindToValueChangedWithState ( element , property , stateIsProperty ) ;
132+ IDisposable d1 = BindValueChanged ( element , property ) ;
133+ IDisposable d2 = BindToValueChanged ( element , property ) ;
124134 return StableCompositeDisposable . Create ( d1 , d2 ) ;
125135 }
126136 #endregion
137+
127138 }
128139}
0 commit comments