Skip to content

Commit

Permalink
Add Expression tree based factory for OAPH
Browse files Browse the repository at this point in the history
  • Loading branch information
anaisbetts committed Sep 26, 2010
1 parent 1c22a7d commit 6bc0d04
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
5 changes: 3 additions & 2 deletions ReactiveXaml.Sample/AddPersonDialog.xaml.cs
Expand Up @@ -185,6 +185,7 @@ protected void setupCommands()
results.Subscribe(s => Person.Image = new BitmapImage(new Uri(s)),
ex => this.Log().Error("Oh no!", ex));


/* COOLSTUFF: The Reactive Extensions
*
* Reactive Extensions (Rx) allow us to take existing IObservables
Expand All @@ -198,9 +199,9 @@ protected void setupCommands()
* that we change a property since it's a ReactiveObservableObject.
*/

_SpinnerVisibility = new ObservableAsPropertyHelper<Visibility>(
_SpinnerVisibility = this.ObservableToProperty(
SetImageViaFlickr.CanExecuteObservable.Select(x => x ? Visibility.Collapsed : Visibility.Visible),
_ => RaisePropertyChanged("SpinnerVisibility"), Visibility.Collapsed);
x => x.SpinnerVisibility);

OkCommand = new ReactiveCommand(
SetImageViaFlickr.CanExecuteObservable.CombineLatest(Person.Select(x => Person.IsValid()),
Expand Down
16 changes: 4 additions & 12 deletions ReactiveXaml.Sample/App.xaml.cs
Expand Up @@ -88,11 +88,6 @@ public AppViewModel()
setupCommands();
}


/*
* Dependency Properties
*/

PersonEntry _SelectedPerson;
public PersonEntry SelectedPerson {
get { return _SelectedPerson; }
Expand Down Expand Up @@ -120,14 +115,11 @@ public AppViewModel()
* with the host application.
*/

// FIXME: This syntax sucks, is there any way to hide it?
ReactiveCommand _AddPerson;
[Export("AddPerson", typeof(IReactiveCommand))]
public ReactiveCommand AddPerson { get { return _AddPerson; } }
public ReactiveCommand AddPerson { get; protected set; }

ReactiveCommand _RemovePerson;
[Export("RemovePerson", typeof(IReactiveCommand))]
public ReactiveCommand RemovePerson { get { return _RemovePerson; } }
public ReactiveCommand RemovePerson { get; protected set; }


/* COOLSTUFF: Setting up Commands
Expand All @@ -150,7 +142,7 @@ public AppViewModel()

protected void setupCommands()
{
_AddPerson = new ReactiveCommand(item => {
AddPerson = new ReactiveCommand(item => {
var to_add = (item as PersonEntry) ?? addPersonDialog.Prompt(this, null);
if (to_add == null)
Expand All @@ -162,7 +154,7 @@ protected void setupCommands()
People.Add(to_add);
});

_RemovePerson = new ReactiveCommand(param => (param ?? SelectedPerson) != null && People.Count > 0, item => {
RemovePerson = new ReactiveCommand(param => (param ?? SelectedPerson) != null && People.Count > 0, item => {
var to_remove = (PersonEntry)item ?? SelectedPerson;
this.Log().DebugFormat("Removing '{0}'", to_remove.Name);
People.Remove(to_remove);
Expand Down
24 changes: 22 additions & 2 deletions ReactiveXaml/ObservableAsPropertyHelper.cs
Expand Up @@ -5,6 +5,7 @@
using System.Concurrency;
using System.Windows.Threading;
using System.Diagnostics.Contracts;
using System.Linq.Expressions;

namespace ReactiveXaml
{
Expand All @@ -21,8 +22,8 @@ public ObservableAsPropertyHelper(IObservable<T> observable, Action<T> on_change
scheduler = scheduler ?? RxApp.DeferredScheduler;
lastValue = initial_value;

observable.ObserveOn(scheduler)
.DistinctUntilChanged()
observable.DistinctUntilChanged()
.ObserveOn(scheduler)
.Subscribe(x => {
this.Log().DebugFormat("Property helper {0:X} changed", this.GetHashCode());
lastValue = x;
Expand All @@ -40,4 +41,23 @@ public ObservableAsPropertyHelper(IObservable<T> observable, Action<T> on_change
}
}
}

public static class OAPHCreationHelperMixin
{
public static ObservableAsPropertyHelper<TRet> ObservableToProperty<TObj, TRet>(
this TObj This,
IObservable<TRet> Observable,
Expression<Func<TObj, TRet>> Property,
TRet InitialValue = default(TRet),
IScheduler Scheduler = null)
where TObj : ReactiveObject
{
Contract.Requires(This != null);
Contract.Requires(Observable != null);
Contract.Requires(Property != null);

string prop_name = RxApp.expressionToPropertyName(Property);
return new ObservableAsPropertyHelper<TRet>(Observable, _ => This.RaisePropertyChanged(prop_name), InitialValue, Scheduler);
}
}
}

0 comments on commit 6bc0d04

Please sign in to comment.