Skip to content

Instantly share code, notes, and snippets.

@mntone
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mntone/b2fcfeff091db33be611 to your computer and use it in GitHub Desktop.
Save mntone/b2fcfeff091db33be611 to your computer and use it in GitHub Desktop.
(en) ReactiveProperty factory extensions.
// This file is under The MIT License (MIT).
using Reactive.Bindings;
using System;
using System.Linq.Expressions;
using System.Reactive.Concurrency;
namespace Mntone.Reactive.Bindings
{
public static class ReactiveProperty2
{
#region Int16 section
public static ReactiveProperty<string> FromInt16<TTarget>(
TTarget target,
Expression<Func<TTarget, short>> propertySelector,
ReactivePropertyMode mode = ReactivePropertyMode.DistinctUntilChanged | ReactivePropertyMode.RaiseLatestValueOnSubscribe,
bool ignoreValidationErrorValue = false)
{
return FromInt16(target, propertySelector, UIDispatcherScheduler.Default, mode, ignoreValidationErrorValue);
}
public static ReactiveProperty<string> FromInt16<TTarget>(
TTarget target,
Expression<Func<TTarget, short>> propertySelector,
IScheduler raiseEventScheduler,
ReactivePropertyMode mode = ReactivePropertyMode.DistinctUntilChanged | ReactivePropertyMode.RaiseLatestValueOnSubscribe,
bool ignoreValidationErrorValue = false)
{
return ReactiveProperty.FromObject(
target,
propertySelector,
x => x.ToString(),
x => string.IsNullOrWhiteSpace(x) || (x.Length == 1 && x[0] == '-')
? (short)0
: Convert.ToInt16(x),
raiseEventScheduler,
mode,
ignoreValidationErrorValue);
}
#endregion
#region Int32 section
public static ReactiveProperty<string> FromInt32<TTarget>(
TTarget target,
Expression<Func<TTarget, int>> propertySelector,
ReactivePropertyMode mode = ReactivePropertyMode.DistinctUntilChanged | ReactivePropertyMode.RaiseLatestValueOnSubscribe,
bool ignoreValidationErrorValue = false)
{
return FromInt32(target, propertySelector, UIDispatcherScheduler.Default, mode, ignoreValidationErrorValue);
}
public static ReactiveProperty<string> FromInt32<TTarget>(
TTarget target,
Expression<Func<TTarget, int>> propertySelector,
IScheduler raiseEventScheduler,
ReactivePropertyMode mode = ReactivePropertyMode.DistinctUntilChanged | ReactivePropertyMode.RaiseLatestValueOnSubscribe,
bool ignoreValidationErrorValue = false)
{
return ReactiveProperty.FromObject(
target,
propertySelector,
x => x.ToString(),
x => string.IsNullOrWhiteSpace(x) || (x.Length == 1 && x[0] == '-')
? 0
: Convert.ToInt32(x),
raiseEventScheduler,
mode,
ignoreValidationErrorValue);
}
#endregion
#region Int64 section
public static ReactiveProperty<string> FromInt64<TTarget>(
TTarget target,
Expression<Func<TTarget, long>> propertySelector,
ReactivePropertyMode mode = ReactivePropertyMode.DistinctUntilChanged | ReactivePropertyMode.RaiseLatestValueOnSubscribe,
bool ignoreValidationErrorValue = false)
{
return FromInt64(target, propertySelector, UIDispatcherScheduler.Default, mode, ignoreValidationErrorValue);
}
public static ReactiveProperty<string> FromInt64<TTarget>(
TTarget target,
Expression<Func<TTarget, long>> propertySelector,
IScheduler raiseEventScheduler,
ReactivePropertyMode mode = ReactivePropertyMode.DistinctUntilChanged | ReactivePropertyMode.RaiseLatestValueOnSubscribe,
bool ignoreValidationErrorValue = false)
{
return ReactiveProperty.FromObject(
target,
propertySelector,
x => x.ToString(),
x => string.IsNullOrWhiteSpace(x) || (x.Length == 1 && x[0] == '-')
? 0
: Convert.ToInt64(x),
raiseEventScheduler,
mode,
ignoreValidationErrorValue);
}
#endregion
#region Enum (derived from Int16) for RxProp<Int32> section
public static ReactiveProperty<int> FromEnumDerivedFromInt16ToReactivePropertyOfInt32<TTarget, TEnumProperty>(
TTarget target,
Expression<Func<TTarget, TEnumProperty>> propertySelector,
ReactivePropertyMode mode = ReactivePropertyMode.DistinctUntilChanged | ReactivePropertyMode.RaiseLatestValueOnSubscribe,
bool ignoreValidationErrorValue = false)
{
return FromEnumDerivedFromInt16ToReactivePropertyOfInt32(target, propertySelector, UIDispatcherScheduler.Default, mode, ignoreValidationErrorValue);
}
public static ReactiveProperty<int> FromEnumDerivedFromInt16ToReactivePropertyOfInt32<TTarget, TEnumProperty>(
TTarget target,
Expression<Func<TTarget, TEnumProperty>> propertySelector,
IScheduler raiseEventScheduler,
ReactivePropertyMode mode = ReactivePropertyMode.DistinctUntilChanged | ReactivePropertyMode.RaiseLatestValueOnSubscribe,
bool ignoreValidationErrorValue = false)
{
return ReactiveProperty.FromObject(
target,
propertySelector,
x => Convert.ToInt32(x),
x => (TEnumProperty)((object)Convert.ToInt16((object)x)),
raiseEventScheduler,
mode,
ignoreValidationErrorValue);
}
#endregion
#region Enum (derived from Int32) for RxProp<Int32> section
public static ReactiveProperty<int> FromEnumDerivedFromInt32ToReactivePropertyOfInt32<TTarget, TEnumProperty>(
TTarget target,
Expression<Func<TTarget, TEnumProperty>> propertySelector,
ReactivePropertyMode mode = ReactivePropertyMode.DistinctUntilChanged | ReactivePropertyMode.RaiseLatestValueOnSubscribe,
bool ignoreValidationErrorValue = false)
{
return FromEnumDerivedFromInt32ToReactivePropertyOfInt32(target, propertySelector, UIDispatcherScheduler.Default, mode, ignoreValidationErrorValue);
}
public static ReactiveProperty<int> FromEnumDerivedFromInt32ToReactivePropertyOfInt32<TTarget, TEnumProperty>(
TTarget target,
Expression<Func<TTarget, TEnumProperty>> propertySelector,
IScheduler raiseEventScheduler,
ReactivePropertyMode mode = ReactivePropertyMode.DistinctUntilChanged | ReactivePropertyMode.RaiseLatestValueOnSubscribe,
bool ignoreValidationErrorValue = false)
{
return ReactiveProperty.FromObject(
target,
propertySelector,
x => Convert.ToInt32(x),
x => (TEnumProperty)((object)x),
raiseEventScheduler,
mode,
ignoreValidationErrorValue);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment