1
+ using JetBrains . Annotations ;
1
2
using System ;
2
3
using System . Collections ;
3
4
using System . Collections . Generic ;
7
8
8
9
namespace RedMoon . ReactiveKit
9
10
{
10
- public static class BindingExtensions
11
+ public static partial class BindingExtensions
11
12
{
12
13
#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 )
14
33
{
15
- return property . SubscribeWithState ( element , ( __property , __element ) =>
16
- {
17
- element . SetEnabled ( __property ) ;
18
- } ) ;
34
+ return observable . ObserveOnMainThread ( ) . DistinctUntilChanged ( ) . Subscribe ( value => element . style . visibility = value ) ;
19
35
}
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 ]
20
43
public static IDisposable BindClick ( this VisualElement element , IReactiveCommand < ClickEvent > command )
21
44
{
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 ) ;
24
47
return StableCompositeDisposable . Create ( d1 , d2 ) ;
25
48
}
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 ]
26
57
public static IDisposable BindClick < TArgs > ( this VisualElement element , IReactiveCommand < ( ClickEvent , TArgs ) > command , TArgs dataForCallback )
27
58
{
28
59
var d1 = element . BindEnabled ( command . CanExecute ) ;
@@ -32,97 +63,77 @@ public static IDisposable BindClick<TArgs>(this VisualElement element, IReactive
32
63
#endregion
33
64
34
65
#region Callback Event Handler
66
+ [ MustUseReturnValue ]
35
67
public static IDisposable BindCallback < TEventType > ( this CallbackEventHandler element , EventCallback < TEventType > callback , TrickleDown trickleDown = TrickleDown . NoTrickleDown ) where TEventType : EventBase < TEventType > , new ( )
36
68
{
37
69
element . RegisterCallback ( callback , trickleDown ) ;
38
70
return Disposable . Create ( ( ) => { element . UnregisterCallback ( callback , trickleDown ) ; } ) ;
39
71
}
72
+ [ MustUseReturnValue ]
40
73
public static IDisposable BindCallback < TEventType , TUserArgsType > ( this CallbackEventHandler element , EventCallback < TEventType , TUserArgsType > callback , TUserArgsType dataForCallback , TrickleDown trickleDown = TrickleDown . NoTrickleDown ) where TEventType : EventBase < TEventType > , new ( )
41
74
{
42
75
element . RegisterCallback ( callback , dataForCallback , trickleDown ) ;
43
76
return Disposable . Create ( ( ) => { element . UnregisterCallback ( callback , trickleDown ) ; } ) ;
44
77
}
78
+ [ MustUseReturnValue ]
45
79
public static IDisposable BindCallback < TEventType > ( this CallbackEventHandler element , IReactiveCommand < TEventType > command , TrickleDown trickleDown = TrickleDown . NoTrickleDown ) where TEventType : EventBase < TEventType > , new ( )
46
80
{
47
- var callback = new EventCallback < TEventType > ( ( ev ) => command . Execute ( ev ) ) ;
81
+ EventCallback < TEventType > callback = new EventCallback < TEventType > ( ( ev ) => command . Execute ( ev ) ) ;
48
82
element . RegisterCallback ( callback , trickleDown ) ;
49
83
return Disposable . Create ( ( ) => { element . UnregisterCallback ( callback , trickleDown ) ; } ) ;
50
84
}
85
+ [ MustUseReturnValue ]
51
86
public static IDisposable BindCallback < TEventType , TUserArgsType > ( this CallbackEventHandler element , IReactiveCommand < ( TEventType , TUserArgsType ) > command , TUserArgsType dataForCallback , TrickleDown trickleDown = TrickleDown . NoTrickleDown ) where TEventType : EventBase < TEventType > , new ( )
52
87
{
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 ) ) ) ;
54
89
element . RegisterCallback ( callback , dataForCallback , trickleDown ) ;
55
90
return Disposable . Create ( ( ) => { element . UnregisterCallback ( callback , trickleDown ) ; } ) ;
56
91
}
57
92
#endregion
58
93
59
94
#region Notifications
95
+ [ MustUseReturnValue ]
60
96
public static IDisposable BindValueChanged < T > ( this INotifyValueChanged < T > element , IReactiveCommand < ChangeEvent < T > > command )
61
97
{
62
- var callback = new EventCallback < ChangeEvent < T > > ( ( ev ) => command . Execute ( ev ) ) ;
98
+ EventCallback < ChangeEvent < T > > callback = new EventCallback < ChangeEvent < T > > ( ( ev ) => command . Execute ( ev ) ) ;
63
99
element . RegisterValueChangedCallback ( callback ) ;
64
100
return Disposable . Create ( ( ) => { element . UnregisterValueChangedCallback ( callback ) ; } ) ;
65
101
}
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 ]
66
110
public static IDisposable BindValueChanged < T > ( this INotifyValueChanged < T > element , IReactiveProperty < T > property )
67
111
{
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 ) ;
69
113
element . RegisterValueChangedCallback ( callback ) ;
70
114
return Disposable . Create ( ( ) => { element . UnregisterValueChangedCallback ( callback ) ; } ) ;
71
115
}
72
116
/// <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
75
119
/// </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 )
102
126
{
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 ) ) ;
113
128
}
129
+ [ MustUseReturnValue ]
114
130
public static IDisposable BindTwoWayValueChanged < T > ( this INotifyValueChanged < T > element , IReactiveProperty < T > property )
115
131
{
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 ) ;
124
134
return StableCompositeDisposable . Create ( d1 , d2 ) ;
125
135
}
126
136
#endregion
137
+
127
138
}
128
139
}
0 commit comments