Skip to content

Commit

Permalink
true ReadOnlyBindableReactiveProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Aug 15, 2024
1 parent a299bf9 commit f7be1bf
Showing 1 changed file with 101 additions and 3 deletions.
104 changes: 101 additions & 3 deletions src/R3/ReactivePropertyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
namespace R3;
using System.Collections;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

namespace R3;

public static class ReactivePropertyExtensions
{
Expand Down Expand Up @@ -32,12 +37,12 @@ public static BindableReactiveProperty<T> ToBindableReactiveProperty<T>(this Obs

public static IReadOnlyBindableReactiveProperty<T> ToReadOnlyBindableReactiveProperty<T>(this Observable<T> source, T initialValue = default!)
{
return new BindableReactiveProperty<T>(source, initialValue, EqualityComparer<T>.Default);
return new ReadOnlyBindableReactiveProperty<T>(new BindableReactiveProperty<T>(source, initialValue, EqualityComparer<T>.Default));
}

public static IReadOnlyBindableReactiveProperty<T> ToReadOnlyBindableReactiveProperty<T>(this Observable<T> source, IEqualityComparer<T>? equalityComparer, T initialValue = default!)
{
return new BindableReactiveProperty<T>(source, initialValue, equalityComparer);
return new ReadOnlyBindableReactiveProperty<T>(new BindableReactiveProperty<T>(source, initialValue, equalityComparer));
}
}

Expand Down Expand Up @@ -75,3 +80,96 @@ protected override void OnCompletedCore(Result result)
}
}

internal sealed class ReadOnlyBindableReactiveProperty<T>(BindableReactiveProperty<T> property) : IReadOnlyBindableReactiveProperty<T>
{
public T Value => ((IReadOnlyBindableReactiveProperty<T>)property).Value;

public bool HasErrors => ((INotifyDataErrorInfo)property).HasErrors;

object? IReadOnlyBindableReactiveProperty.Value => ((IReadOnlyBindableReactiveProperty)property).Value;

PropertyChangedEventHandler? propertyChangedCore;

public event PropertyChangedEventHandler? PropertyChanged
{
add
{
propertyChangedCore += value;
((INotifyPropertyChanged)property).PropertyChanged += PropertyChangedEventHandler;
}

remove
{
propertyChangedCore -= value;
((INotifyPropertyChanged)property).PropertyChanged -= PropertyChangedEventHandler;
}
}

EventHandler<DataErrorsChangedEventArgs>? errorsChangedCore;

public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged
{
add
{
errorsChangedCore += value;
((INotifyDataErrorInfo)property).ErrorsChanged += ErrorsChangedEventHandler;
}

remove
{
errorsChangedCore -= value;
((INotifyDataErrorInfo)property).ErrorsChanged -= ErrorsChangedEventHandler;
}
}

void PropertyChangedEventHandler(object? sender, PropertyChangedEventArgs e)
{
// parent sender is BindableReactiveProperty<T>, change to self.
propertyChangedCore?.Invoke(this, e);
}

void ErrorsChangedEventHandler(object? sender, DataErrorsChangedEventArgs e)
{
errorsChangedCore?.Invoke(this, e);
}

public Observable<T> AsObservable()
{
return ((IReadOnlyBindableReactiveProperty<T>)property).AsObservable();
}

public void Dispose()
{
((IDisposable)property).Dispose();
}

public IReadOnlyBindableReactiveProperty<T> EnableValidation()
{
return ((IReadOnlyBindableReactiveProperty<T>)property).EnableValidation();
}

public IReadOnlyBindableReactiveProperty<T> EnableValidation(Func<T, Exception?> validator)
{
return ((IReadOnlyBindableReactiveProperty<T>)property).EnableValidation(validator);
}

public IReadOnlyBindableReactiveProperty<T> EnableValidation<TClass>([CallerMemberName] string? propertyName = null)
{
return ((IReadOnlyBindableReactiveProperty<T>)property).EnableValidation<TClass>(propertyName);
}

public IReadOnlyBindableReactiveProperty<T> EnableValidation(Expression<Func<IReadOnlyBindableReactiveProperty<T>?>> selfSelector)
{
return ((IReadOnlyBindableReactiveProperty<T>)property).EnableValidation(selfSelector);
}

public IEnumerable GetErrors(string? propertyName)
{
return ((INotifyDataErrorInfo)property).GetErrors(propertyName);
}

public override string? ToString()
{
return property.ToString();
}
}

0 comments on commit f7be1bf

Please sign in to comment.