-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ReactiveRecyclerViewViewHolder.cs
211 lines (174 loc) · 8.82 KB
/
ReactiveRecyclerViewViewHolder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) 2023 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reflection;
using System.Runtime.Serialization;
using Android.Views;
using AndroidX.RecyclerView.Widget;
namespace ReactiveUI.AndroidX;
/// <summary>
/// A <see cref="RecyclerView.ViewHolder"/> implementation that binds to a reactive view model.
/// </summary>
/// <typeparam name="TViewModel">The type of the view model.</typeparam>
public class ReactiveRecyclerViewViewHolder<TViewModel> : RecyclerView.ViewHolder, ILayoutViewHost, IViewFor<TViewModel>, IReactiveNotifyPropertyChanged<ReactiveRecyclerViewViewHolder<TViewModel>>, IReactiveObject, ICanActivate
where TViewModel : class, IReactiveObject
{
/// <summary>
/// Gets all public accessible properties.
/// </summary>
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401: Field should be private", Justification = "Legacy reasons")]
[SuppressMessage("Design", "CA1051: Do not declare visible instance fields", Justification = "Legacy reasons")]
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1306: Field should start with a lower case letter", Justification = "Legacy reasons")]
[IgnoreDataMember]
protected Lazy<PropertyInfo[]>? AllPublicProperties;
private readonly Subject<Unit> _activated = new();
private readonly Subject<Unit> _deactivated = new();
private TViewModel? _viewModel;
/// <summary>
/// Initializes a new instance of the <see cref="ReactiveRecyclerViewViewHolder{TViewModel}"/> class.
/// </summary>
/// <param name="view">The view.</param>
[Obsolete("This method was deprecated in API level 23.", false)]
protected ReactiveRecyclerViewViewHolder(View view)
: base(view)
{
SetupRxObj();
if (view is null)
{
throw new ArgumentNullException(nameof(view));
}
view.ViewAttachedToWindow += OnViewAttachedToWindow;
view.ViewDetachedFromWindow += OnViewDetachedFromWindow;
Selected = Observable.FromEvent<EventHandler, int>(
eventHandler =>
{
void Handler(object sender, EventArgs e) => eventHandler(AdapterPosition);
return Handler;
},
h => view.Click += h,
h => view.Click -= h);
LongClicked = Observable.FromEvent<EventHandler<View.LongClickEventArgs>, int>(
eventHandler =>
{
void Handler(object sender, View.LongClickEventArgs e) => eventHandler(AdapterPosition);
return Handler;
},
h => view.LongClick += h,
h => view.LongClick -= h);
SelectedWithViewModel = Observable.FromEvent<EventHandler, TViewModel?>(
eventHandler =>
{
void Handler(object sender, EventArgs e) => eventHandler(ViewModel);
return Handler;
},
h => view.Click += h,
h => view.Click -= h);
LongClickedWithViewModel = Observable.FromEvent<EventHandler<View.LongClickEventArgs>, TViewModel?>(
eventHandler =>
{
void Handler(object sender, View.LongClickEventArgs e) => eventHandler(ViewModel);
return Handler;
},
h => view.LongClick += h,
h => view.LongClick -= h);
}
/// <inheritdoc/>
public event PropertyChangingEventHandler? PropertyChanging;
/// <inheritdoc/>
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// Gets an observable that signals that this ViewHolder has been selected.
///
/// The <see cref="int"/> is the position of this ViewHolder in the <see cref="RecyclerView"/>
/// and corresponds to the <see cref="RecyclerView.ViewHolder.AdapterPosition"/> property.
/// </summary>
public IObservable<int> Selected { get; }
/// <summary>
/// Gets an observable that signals that this ViewHolder has been selected.
///
/// The <see cref="IObservable{TViewModel}"/> is the ViewModel of this ViewHolder in the <see cref="RecyclerView"/>.
/// </summary>
public IObservable<TViewModel?> SelectedWithViewModel { get; }
/// <summary>
/// Gets an observable that signals that this ViewHolder has been long-clicked.
///
/// The <see cref="int"/> is the position of this ViewHolder in the <see cref="RecyclerView"/>
/// and corresponds to the <see cref="RecyclerView.ViewHolder.AdapterPosition"/> property.
/// </summary>
public IObservable<int> LongClicked { get; }
/// <summary>
/// Gets an observable that signals that this ViewHolder has been long-clicked.
///
/// The <see cref="IObservable{TViewModel}"/> is the ViewModel of this ViewHolder in the <see cref="RecyclerView"/>.
/// </summary>
public IObservable<TViewModel?> LongClickedWithViewModel { get; }
/// <inheritdoc/>
public IObservable<Unit> Activated => _activated.AsObservable();
/// <inheritdoc/>
public IObservable<Unit> Deactivated => _deactivated.AsObservable();
/// <summary>
/// Gets the current view being shown.
/// </summary>
public View View => ItemView;
/// <inheritdoc/>
public TViewModel? ViewModel
{
get => _viewModel;
set => this.RaiseAndSetIfChanged(ref _viewModel, value);
}
/// <summary>
/// Gets an observable which signals when exceptions are thrown.
/// </summary>
[IgnoreDataMember]
public IObservable<Exception> ThrownExceptions => this.GetThrownExceptionsObservable();
/// <inheritdoc/>
object? IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (TViewModel?)value;
}
/// <inheritdoc/>
[IgnoreDataMember]
public IObservable<IReactivePropertyChangedEventArgs<ReactiveRecyclerViewViewHolder<TViewModel>>> Changing => this.GetChangingObservable();
/// <inheritdoc/>
[IgnoreDataMember]
public IObservable<IReactivePropertyChangedEventArgs<ReactiveRecyclerViewViewHolder<TViewModel>>> Changed => this.GetChangedObservable();
/// <inheritdoc/>
public IDisposable SuppressChangeNotifications() => IReactiveObjectExtensions.SuppressChangeNotifications(this);
/// <summary>
/// Gets if change notifications via the INotifyPropertyChanged interface are being sent.
/// </summary>
/// <returns>A value indicating whether change notifications are enabled or not.</returns>
public bool AreChangeNotificationsEnabled() => IReactiveObjectExtensions.AreChangeNotificationsEnabled(this);
/// <inheritdoc/>
void IReactiveObject.RaisePropertyChanging(PropertyChangingEventArgs args) => PropertyChanging?.Invoke(this, args);
/// <inheritdoc/>
void IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args) => PropertyChanged?.Invoke(this, args);
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
{
View.ViewAttachedToWindow -= OnViewAttachedToWindow;
View.ViewDetachedFromWindow -= OnViewDetachedFromWindow;
_activated.Dispose();
_deactivated.Dispose();
}
base.Dispose(disposing);
}
[OnDeserialized]
private void SetupRxObj(StreamingContext sc) => SetupRxObj();
private void SetupRxObj() =>
AllPublicProperties = new Lazy<PropertyInfo[]>(() =>
GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToArray());
private void OnViewAttachedToWindow(object sender, View.ViewAttachedToWindowEventArgs args) => _activated.OnNext(Unit.Default);
private void OnViewDetachedFromWindow(object sender, View.ViewDetachedFromWindowEventArgs args) => _deactivated.OnNext(Unit.Default);
}