Skip to content

Commit 1f845be

Browse files
committed
Merge branch 'streamline_boilerplate' into rxui6-master
2 parents d909466 + 93dbf26 commit 1f845be

12 files changed

+430
-1558
lines changed

ReactiveUI.Platforms/Cocoa/ReactiveCollectionViewCell.cs

Lines changed: 21 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -18,64 +18,53 @@
1818

1919
namespace ReactiveUI.Cocoa
2020
{
21-
public abstract class ReactiveCollectionViewCell : UICollectionViewCell, IReactiveNotifyPropertyChanged, IHandleObservableErrors
21+
public abstract class ReactiveCollectionViewCell : UICollectionViewCell, IReactiveNotifyPropertyChanged, IHandleObservableErrors, IReactiveObjectExtension
2222
{
2323
public ReactiveCollectionViewCell(IntPtr handle) : base (handle) { setupRxObj(); }
2424
public ReactiveCollectionViewCell(NSObjectFlag t) : base (t) { setupRxObj(); }
2525
public ReactiveCollectionViewCell(NSCoder coder) : base (NSObjectFlag.Empty) { setupRxObj(); }
2626
public ReactiveCollectionViewCell() : base() { setupRxObj(); }
2727
public ReactiveCollectionViewCell(RectangleF frame) : base (frame) { setupRxObj(); }
2828

29-
[field:IgnoreDataMember]
3029
public event PropertyChangingEventHandler PropertyChanging;
3130

32-
[field:IgnoreDataMember]
31+
void IReactiveObjectExtension.RaisePropertyChanging(PropertyChangingEventArgs args)
32+
{
33+
var handler = PropertyChanging;
34+
if (handler != null) {
35+
handler(this, args);
36+
}
37+
}
38+
3339
public event PropertyChangedEventHandler PropertyChanged;
3440

41+
void IReactiveObjectExtension.RaisePropertyChanged(PropertyChangedEventArgs args) {
42+
var handler = PropertyChanged;
43+
if (handler != null) {
44+
handler(this, args);
45+
}
46+
}
47+
3548
/// <summary>
3649
/// Represents an Observable that fires *before* a property is about to
3750
/// be changed.
3851
/// </summary>
39-
[IgnoreDataMember]
4052
public IObservable<IObservedChange<object, object>> Changing {
41-
get { return changingSubject; }
53+
get { return this.getChangingObservable(); }
4254
}
4355

4456
/// <summary>
4557
/// Represents an Observable that fires *after* a property has changed.
4658
/// </summary>
47-
[IgnoreDataMember]
4859
public IObservable<IObservedChange<object, object>> Changed {
49-
get { return changedSubject; }
60+
get { return this.getChangedObservable(); }
5061
}
5162

52-
[IgnoreDataMember]
53-
protected Lazy<PropertyInfo[]> allPublicProperties;
54-
55-
[IgnoreDataMember]
56-
Subject<IObservedChange<object, object>> changingSubject;
57-
58-
[IgnoreDataMember]
59-
Subject<IObservedChange<object, object>> changedSubject;
60-
61-
[IgnoreDataMember]
62-
long changeNotificationsSuppressed = 0;
63-
64-
[IgnoreDataMember]
65-
readonly ScheduledSubject<Exception> thrownExceptions = new ScheduledSubject<Exception>(Scheduler.Immediate, RxApp.DefaultExceptionHandler);
66-
67-
[IgnoreDataMember]
68-
public IObservable<Exception> ThrownExceptions { get { return thrownExceptions; } }
69-
70-
[OnDeserialized]
71-
void setupRxObj(StreamingContext sc) { setupRxObj(); }
63+
public IObservable<Exception> ThrownExceptions { get { return this.getThrownExceptionsObservable(); } }
7264

7365
void setupRxObj()
7466
{
75-
changingSubject = new Subject<IObservedChange<object, object>>();
76-
changedSubject = new Subject<IObservedChange<object, object>>();
77-
78-
allPublicProperties = new Lazy<PropertyInfo[]>(() => GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToArray());
67+
this.setupReactiveExtension();
7968
}
8069

8170
/// <summary>
@@ -87,123 +76,7 @@ void setupRxObj()
8776
/// notifications.</returns>
8877
public IDisposable SuppressChangeNotifications()
8978
{
90-
Interlocked.Increment(ref changeNotificationsSuppressed);
91-
return Disposable.Create(() => Interlocked.Decrement(ref changeNotificationsSuppressed));
92-
}
93-
94-
protected internal void raisePropertyChanging(string propertyName)
95-
{
96-
Contract.Requires(propertyName != null);
97-
98-
if (!areChangeNotificationsEnabled || changingSubject == null)
99-
return;
100-
101-
var handler = this.PropertyChanging;
102-
if (handler != null) {
103-
var e = new PropertyChangingEventArgs(propertyName);
104-
handler(this, e);
105-
}
106-
107-
notifyObservable(new ObservedChange<object, object>() {
108-
PropertyName = propertyName, Sender = this, Value = null
109-
}, changingSubject);
110-
}
111-
112-
protected internal void raisePropertyChanged(string propertyName)
113-
{
114-
Contract.Requires(propertyName != null);
115-
116-
this.Log().Debug("{0:X}.{1} changed", this.GetHashCode(), propertyName);
117-
118-
if (!areChangeNotificationsEnabled || changedSubject == null) {
119-
this.Log().Debug("Suppressed change");
120-
return;
121-
}
122-
123-
var handler = this.PropertyChanged;
124-
if (handler != null) {
125-
var e = new PropertyChangedEventArgs(propertyName);
126-
handler(this, e);
127-
}
128-
129-
notifyObservable(new ObservedChange<object, object>() {
130-
PropertyName = propertyName, Sender = this, Value = null
131-
}, changedSubject);
132-
}
133-
134-
protected bool areChangeNotificationsEnabled {
135-
get {
136-
return (Interlocked.Read(ref changeNotificationsSuppressed) == 0);
137-
}
138-
}
139-
140-
internal void notifyObservable<T>(T item, Subject<T> subject)
141-
{
142-
try {
143-
subject.OnNext(item);
144-
} catch (Exception ex) {
145-
this.Log().ErrorException("ReactiveObject Subscriber threw exception", ex);
146-
thrownExceptions.OnNext(ex);
147-
}
148-
}
149-
150-
/// <summary>
151-
/// RaiseAndSetIfChanged fully implements a Setter for a read-write
152-
/// property on a ReactiveObject, using CallerMemberName to raise the notification
153-
/// and the ref to the backing field to set the property.
154-
/// </summary>
155-
/// <typeparam name="TObj">The type of the This.</typeparam>
156-
/// <typeparam name="TRet">The type of the return value.</typeparam>
157-
/// <param name="This">The <see cref="ReactiveObject"/> raising the notification.</param>
158-
/// <param name="backingField">A Reference to the backing field for this
159-
/// property.</param>
160-
/// <param name="newValue">The new value.</param>
161-
/// <param name="propertyName">The name of the property, usually
162-
/// automatically provided through the CallerMemberName attribute.</param>
163-
/// <returns>The newly set value, normally discarded.</returns>
164-
public TRet RaiseAndSetIfChanged<TRet>(
165-
ref TRet backingField,
166-
TRet newValue,
167-
[CallerMemberName] string propertyName = null)
168-
{
169-
Contract.Requires(propertyName != null);
170-
171-
if (EqualityComparer<TRet>.Default.Equals(backingField, newValue)) {
172-
return newValue;
173-
}
174-
175-
raisePropertyChanging(propertyName);
176-
backingField = newValue;
177-
raisePropertyChanged(propertyName);
178-
return newValue;
179-
}
180-
181-
/// <summary>
182-
/// Use this method in your ReactiveObject classes when creating custom
183-
/// properties where raiseAndSetIfChanged doesn't suffice.
184-
/// </summary>
185-
/// <param name="This">The instance of ReactiveObject on which the property has changed.</param>
186-
/// <param name="propertyName">
187-
/// A string representing the name of the property that has been changed.
188-
/// Leave <c>null</c> to let the runtime set to caller member name.
189-
/// </param>
190-
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
191-
{
192-
raisePropertyChanged(propertyName);
193-
}
194-
195-
/// <summary>
196-
/// Use this method in your ReactiveObject classes when creating custom
197-
/// properties where raiseAndSetIfChanged doesn't suffice.
198-
/// </summary>
199-
/// <param name="This">The instance of ReactiveObject on which the property has changed.</param>
200-
/// <param name="propertyName">
201-
/// A string representing the name of the property that has been changed.
202-
/// Leave <c>null</c> to let the runtime set to caller member name.
203-
/// </param>
204-
public void RaisePropertyChanging([CallerMemberName] string propertyName = null)
205-
{
206-
raisePropertyChanging(propertyName);
79+
return this.suppressChangeNotifications();
20780
}
20881
}
20982
}

0 commit comments

Comments
 (0)