Skip to content

Commit a5a35b1

Browse files
committed
feature: Added non type specific ReactivePopupPage
1 parent 7e903c4 commit a5a35b1

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

src/ReactiveUI.Plugins.Popup/ReactivePopupPage.cs

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace RxUI.Plugins.Popup
1313
/// Base Popup page for that implements <see cref="IViewFor"/>.
1414
/// </summary>
1515
/// <typeparam name="TViewModel">The view model type.</typeparam>
16-
public abstract class ReactivePopupPage<TViewModel> : PopupPage, IViewFor<TViewModel>
16+
public abstract class ReactivePopupPage<TViewModel> : ReactivePopupPage, IViewFor<TViewModel>
1717
where TViewModel : class
1818
{
1919
/// <summary>
@@ -31,29 +31,6 @@ public abstract class ReactivePopupPage<TViewModel> : PopupPage, IViewFor<TViewM
3131
(BindableProperty.CoerceValueDelegate)null,
3232
(BindableProperty.CreateDefaultValueDelegate)null);
3333

34-
/// <summary>
35-
/// Initializes a new instance of the <see cref="ReactivePopupPage{TViewModel}"/> class.
36-
/// </summary>
37-
protected ReactivePopupPage()
38-
{
39-
BackgroundClick =
40-
Observable.FromEvent<EventHandler, Unit>(
41-
handler =>
42-
{
43-
void EventHandler(object sender, EventArgs args) => handler(Unit.Default);
44-
return EventHandler;
45-
},
46-
x => BackgroundClicked += x,
47-
x => BackgroundClicked -= x)
48-
.Select(_ => Unit.Default);
49-
}
50-
51-
/// <summary>
52-
/// Gets or sets the background click observable signal.
53-
/// </summary>
54-
/// <value>The background click.</value>
55-
public IObservable<Unit> BackgroundClick { get; protected set; }
56-
5734
/// <summary>
5835
/// Gets or sets the ViewModel corresponding to this specific View.
5936
/// This should be a BindableProperty if you're using XAML.
@@ -70,7 +47,7 @@ object IViewFor.ViewModel
7047
public TViewModel ViewModel
7148
{
7249
get => (TViewModel)GetValue(ViewModelProperty);
73-
set => SetValue(ViewModelProperty, (object)value);
50+
set => SetValue(ViewModelProperty, value);
7451
}
7552

7653
/// <summary>
@@ -90,4 +67,74 @@ private static void OnViewModelChanged(BindableObject bindableObject, object old
9067
bindableObject.BindingContext = newValue;
9168
}
9269
}
70+
71+
/// <summary>
72+
/// Base Popup page for that implements <see cref="IViewFor"/>.
73+
/// </summary>
74+
public abstract class ReactivePopupPage : PopupPage, IViewFor
75+
{
76+
/// <summary>
77+
/// The view model property.
78+
/// </summary>
79+
public static readonly BindableProperty ViewModelProperty = BindableProperty.Create(
80+
nameof(ViewModel),
81+
typeof(object),
82+
typeof(IViewFor<object>),
83+
(object)null,
84+
BindingMode.OneWay,
85+
(BindableProperty.ValidateValueDelegate)null,
86+
new BindableProperty.BindingPropertyChangedDelegate(OnViewModelChanged),
87+
(BindableProperty.BindingPropertyChangingDelegate)null,
88+
(BindableProperty.CoerceValueDelegate)null,
89+
(BindableProperty.CreateDefaultValueDelegate)null);
90+
91+
/// <summary>
92+
/// Gets or sets the background click observable signal.
93+
/// </summary>
94+
/// <value>The background click.</value>
95+
public IObservable<Unit> BackgroundClick { get; protected set; }
96+
97+
/// <summary>
98+
/// Gets or sets the ViewModel to display.
99+
/// </summary>
100+
public object ViewModel
101+
{
102+
get => GetValue(ViewModelProperty);
103+
set => SetValue(ViewModelProperty, value);
104+
}
105+
106+
/// <summary>
107+
/// Gets the control binding disposable.
108+
/// </summary>
109+
protected CompositeDisposable ControlBindings { get; } = new CompositeDisposable();
110+
111+
/// <summary>
112+
/// Initializes a new instance of the <see cref="ReactivePopupPage{TViewModel}"/> class.
113+
/// </summary>
114+
protected ReactivePopupPage()
115+
{
116+
BackgroundClick =
117+
Observable.FromEvent<EventHandler, Unit>(
118+
handler =>
119+
{
120+
void EventHandler(object sender, EventArgs args) => handler(Unit.Default);
121+
return EventHandler;
122+
},
123+
x => BackgroundClicked += x,
124+
x => BackgroundClicked -= x)
125+
.Select(_ => Unit.Default);
126+
}
127+
128+
/// <inheritdoc/>
129+
protected override void OnBindingContextChanged()
130+
{
131+
base.OnBindingContextChanged();
132+
ViewModel = BindingContext;
133+
}
134+
135+
private static void OnViewModelChanged(BindableObject bindableObject, object oldValue, object newValue)
136+
{
137+
bindableObject.BindingContext = newValue;
138+
}
139+
}
93140
}

0 commit comments

Comments
 (0)