Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RelativeBindingSource Support for TypedBindings #201

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions samples/CommunityToolkit.Maui.Markup.Sample/App.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using CommunityToolkit.Maui.Markup.Sample.Resources;

namespace CommunityToolkit.Maui.Markup.Sample;
namespace CommunityToolkit.Maui.Markup.Sample;

class App : Application
{
Expand All @@ -10,6 +8,4 @@ public App(AppShell shell)

MainPage = shell;
}


}
4 changes: 2 additions & 2 deletions samples/CommunityToolkit.Maui.Markup.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static MauiApp CreateMauiApp()
builder.Services.AddSingleton<HackerNewsAPIService>();
builder.Services.AddRefitClient<IHackerNewsApi>()
.ConfigureHttpClient(client => client.BaseAddress = new Uri("https://hacker-news.firebaseio.com/v0"))
.AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(3, sleepDurationProvider));
.AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(3, ExponentialBackoff));

// Pages + View Models
builder.Services.AddTransient<NewsPage, NewsViewModel>();
Expand All @@ -35,6 +35,6 @@ public static MauiApp CreateMauiApp()

return builder.Build();

static TimeSpan sleepDurationProvider(int attemptNumber) => TimeSpan.FromSeconds(Math.Pow(2, attemptNumber));
static TimeSpan ExponentialBackoff(int attemptNumber) => TimeSpan.FromSeconds(Math.Pow(2, attemptNumber));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ public void BindCommandWithParameters()
Assert.AreEqual(textCell.CommandParameter, viewModel.Id);
}

[Test]
public async Task BindUsingRelativeBindingSourceSelf()
{
const int heightRequest = 200;
bool didPropertyChangeFire = false;
TaskCompletionSource<string?> propertyChangedEventArgsTCS = new();

var label = new Label().Bind(Label.TextProperty, static (Label view) => view.HeightRequest, source: RelativeBindingSource.Self);
label.PropertyChanged += HandlePropertyChanged;

Assert.AreEqual(label.HeightRequest.ToString(), label.Text);

label.HeightRequest = heightRequest;

var propertyName = await propertyChangedEventArgsTCS.Task.WaitAsync(TimeSpan.FromSeconds(3)).ConfigureAwait(false);

Assert.True(didPropertyChangeFire);
Assert.AreEqual(Label.TextProperty.PropertyName, propertyName);
Assert.AreEqual(heightRequest, label.HeightRequest);
Assert.AreEqual(label.HeightRequest.ToString(), label.Text);


void HandlePropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == Label.TextProperty.PropertyName)
{
didPropertyChangeFire = true;
propertyChangedEventArgsTCS.SetResult(e.PropertyName);
}
}
}

[Test]
public async Task ConfirmStringFormat()
{
Expand Down
18 changes: 9 additions & 9 deletions src/CommunityToolkit.Maui.Markup/TypedBindingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static TBindable BindCommand<TBindable, TCommandBindingContext>(
Expression<Func<TCommandBindingContext, ICommand>> getter,
Action<TCommandBindingContext, ICommand>? setter = null,
BindingMode mode = BindingMode.Default,
TCommandBindingContext? source = default) where TBindable : BindableObject
object? source = null) where TBindable : BindableObject
{
return BindCommand<TBindable, TCommandBindingContext, object?, object?>(
bindable,
Expand All @@ -30,12 +30,12 @@ public static TBindable BindCommand<TBindable, TCommandBindingContext, TParamete
this TBindable bindable,
Expression<Func<TCommandBindingContext, ICommand>> getter,
Action<TCommandBindingContext, ICommand>? setter = null,
TCommandBindingContext? source = default,
object? source = null,
BindingMode commandBindingMode = BindingMode.Default,
Expression<Func<TParameterBindingContext, TParameterSource>>? parameterGetter = null,
Action<TParameterBindingContext, TParameterSource>? parameterSetter = null,
BindingMode parameterBindingMode = BindingMode.Default,
TParameterBindingContext? parameterSource = default) where TBindable : BindableObject
object? parameterSource = null) where TBindable : BindableObject
{
(var commandProperty, var parameterProperty) = DefaultBindableProperties.GetCommandAndCommandParameterProperty<TBindable>();

Expand Down Expand Up @@ -68,7 +68,7 @@ public static TBindable Bind<TBindable, TBindingContext, TSource>(
Action<TBindingContext, TSource>? setter = null,
BindingMode mode = BindingMode.Default,
string? stringFormat = null,
TBindingContext? source = default) where TBindable : BindableObject
object? source = null) where TBindable : BindableObject
{
return Bind<TBindable, TBindingContext, TSource, object?, object?>(
bindable,
Expand All @@ -95,7 +95,7 @@ public static TBindable Bind<TBindable, TBindingContext, TSource, TDest>(
Func<TSource?, TDest>? convert = null,
Func<TDest?, TSource>? convertBack = null,
string? stringFormat = null,
TBindingContext? source = default,
object? source = null,
TDest? targetNullValue = default,
TDest? fallbackValue = default) where TBindable : BindableObject
{
Expand Down Expand Up @@ -123,7 +123,7 @@ public static TBindable Bind<TBindable, TBindingContext, TSource, TDest>(
BindingMode mode = BindingMode.Default,
IValueConverter? converter = null,
string? stringFormat = null,
TBindingContext? source = default,
object? source = null,
TDest? targetNullValue = default,
TDest? fallbackValue = default) where TBindable : BindableObject
{
Expand Down Expand Up @@ -151,7 +151,7 @@ public static TBindable Bind<TBindable, TBindingContext, TSource, TDest>(
BindingMode mode = BindingMode.Default,
IValueConverter? converter = null,
string? stringFormat = null,
TBindingContext? source = default,
object? source = null,
TDest? targetNullValue = default,
TDest? fallbackValue = default) where TBindable : BindableObject
{
Expand Down Expand Up @@ -181,7 +181,7 @@ public static TBindable Bind<TBindable, TBindingContext, TSource, TParam, TDest>
Func<TDest?, TParam?, TSource>? convertBack = null,
TParam? converterParameter = default,
string? stringFormat = null,
TBindingContext? source = default,
object? source = null,
TDest? targetNullValue = default,
TDest? fallbackValue = default) where TBindable : BindableObject
{
Expand Down Expand Up @@ -213,7 +213,7 @@ public static TBindable Bind<TBindable, TBindingContext, TSource, TParam, TDest>
IValueConverter? converter = null,
TParam? converterParameter = default,
string? stringFormat = null,
TBindingContext? source = default,
object? source = null,
TDest? targetNullValue = default,
TDest? fallbackValue = default) where TBindable : BindableObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static TBindable BindCommand<TBindable, TCommandBindingContext>(
(Func<TCommandBindingContext, object?>, string)[] handlers,
Action<TCommandBindingContext, ICommand>? setter = null,
BindingMode mode = BindingMode.Default,
TCommandBindingContext? source = default) where TBindable : BindableObject
object? source = null) where TBindable : BindableObject
{
return BindCommand<TBindable, TCommandBindingContext, object?, object?>(
bindable,
Expand All @@ -32,12 +32,12 @@ public static TBindable BindCommand<TBindable, TCommandBindingContext, TParamete
Func<TCommandBindingContext, ICommand> getter,
(Func<TCommandBindingContext, object?>, string)[] handlers,
Action<TCommandBindingContext, ICommand>? setter = null,
TCommandBindingContext? source = default,
object? source = null,
BindingMode commandBindingMode = BindingMode.Default,
Func<TParameterBindingContext, TParameterSource>? parameterGetter = null,
(Func<TParameterBindingContext, object?>, string)[]? parameterHandlers = null,
Action<TParameterBindingContext, TParameterSource>? parameterSetter = null,
TParameterBindingContext? parameterSource = default,
object? parameterSource = null,
BindingMode parameterBindingMode = BindingMode.Default) where TBindable : BindableObject
{
(var commandProperty, var parameterProperty) = DefaultBindableProperties.GetCommandAndCommandParameterProperty<TBindable>();
Expand Down Expand Up @@ -76,7 +76,7 @@ public static TBindable Bind<TBindable, TBindingContext, TSource>(
Action<TBindingContext, TSource>? setter = null,
BindingMode mode = BindingMode.Default,
string? stringFormat = null,
TBindingContext? source = default) where TBindable : BindableObject
object? source = null) where TBindable : BindableObject
{
return Bind<TBindable, TBindingContext, TSource, object?, object?>(
bindable,
Expand Down Expand Up @@ -105,7 +105,7 @@ public static TBindable Bind<TBindable, TBindingContext, TSource, TDest>(
Func<TSource?, TDest>? convert = null,
Func<TDest?, TSource>? convertBack = null,
string? stringFormat = null,
TBindingContext? source = default,
object? source = null,
TDest? targetNullValue = default,
TDest? fallbackValue = default) where TBindable : BindableObject
{
Expand Down Expand Up @@ -137,7 +137,7 @@ public static TBindable Bind<TBindable, TBindingContext, TSource, TParam, TDest>
Func<TDest?, TParam?, TSource>? convertBack = null,
TParam? converterParameter = default,
string? stringFormat = null,
TBindingContext? source = default,
object? source = null,
TDest? targetNullValue = default,
TDest? fallbackValue = default) where TBindable : BindableObject
{
Expand Down Expand Up @@ -172,7 +172,7 @@ public static TBindable Bind<TBindable, TBindingContext, TSource, TParam, TDest>
IValueConverter? converter = null,
TParam? converterParameter = default,
string? stringFormat = null,
TBindingContext? source = default,
object? source = null,
TDest? targetNullValue = default,
TDest? fallbackValue = default) where TBindable : BindableObject
{
Expand Down