Skip to content

Commit 8051299

Browse files
Refactor Layouts to use [BindableProperty] partial properties (#3016)
* Update Bindable Properties for Layouts * Add default values and `EnsureDefault()` tests * Use `PropertyChangingMethodName` for validation, Fix Method Names * Update XML Comments * `dotnet format` --------- Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com>
1 parent 025ecd4 commit 8051299

File tree

11 files changed

+78
-83
lines changed

11 files changed

+78
-83
lines changed

src/CommunityToolkit.Maui.Core/Essentials/FileSaver/FileSaverImplementation.macios.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Task<string> InternalSaveAsync(string fileName, Stream stream, IProgress<double>
1212
{
1313
return InternalSaveAsync("/", fileName, stream, progress, cancellationToken);
1414
}
15-
15+
1616
async Task<string> InternalSaveAsync(
1717
string initialPath,
1818
string fileName,
@@ -23,8 +23,8 @@ async Task<string> InternalSaveAsync(
2323
cancellationToken.ThrowIfCancellationRequested();
2424

2525
var currentViewController = Platform.GetCurrentUIViewController()
26-
?? throw new FileSaveException(
27-
"Cannot present file picker: No active view controller found. Ensure the app is active with a visible window.");
26+
?? throw new FileSaveException(
27+
"Cannot present file picker: No active view controller found. Ensure the app is active with a visible window.");
2828

2929
var fileManager = NSFileManager.DefaultManager;
3030

@@ -71,22 +71,22 @@ await WriteStream(
7171
picker.DidPickDocumentAtUrls -= OnPicked;
7272
picker.WasCancelled -= OnCancelled;
7373
}
74-
74+
7575
void OnPicked(object? sender, UIDocumentPickedAtUrlsEventArgs e)
7676
{
7777
if (e.Urls.Length is 0)
7878
{
7979
tcs.TrySetException(new FileSaveException("No file was selected."));
8080
return;
8181
}
82-
82+
8383
var path = e.Urls[0].Path;
8484
if (path is null)
8585
{
8686
tcs.TrySetException(new FileSaveException("File path cannot be null."));
8787
return;
8888
}
89-
89+
9090
tcs.TrySetResult(path);
9191
}
9292

src/CommunityToolkit.Maui.Core/Essentials/FileSaver/FileSaverResult.shared.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public record FileSaverResult(string? FilePath, Exception? Exception)
1616
[MemberNotNullWhen(true, nameof(FilePath))]
1717
[MemberNotNullWhen(false, nameof(Exception))]
1818
public bool IsSuccessful => Exception is null;
19-
19+
2020
/// <summary>
2121
/// Check if the operation was cancelled.
2222
/// </summary>

src/CommunityToolkit.Maui.Core/Essentials/FolderPicker/FolderPickerImplementation.macios.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Task<Folder> InternalPickAsync(CancellationToken cancellationToken)
1414
{
1515
return InternalPickAsync("/", cancellationToken);
1616
}
17-
17+
1818
async Task<Folder> InternalPickAsync(string initialPath, CancellationToken cancellationToken)
1919
{
2020
cancellationToken.ThrowIfCancellationRequested();
2121

2222
var currentViewController = Platform.GetCurrentUIViewController()
23-
?? throw new FolderPickerException("Unable to get a window where to present the folder picker UI.");
23+
?? throw new FolderPickerException("Unable to get a window where to present the folder picker UI.");
2424

2525
var tcs = new TaskCompletionSource<Folder>(
2626
TaskCreationOptions.RunContinuationsAsynchronously);
@@ -44,7 +44,7 @@ async Task<Folder> InternalPickAsync(string initialPath, CancellationToken cance
4444
picker.DidPickDocumentAtUrls -= OnPicked;
4545
picker.WasCancelled -= OnCancelled;
4646
}
47-
47+
4848
void OnPicked(object? sender, UIDocumentPickedAtUrlsEventArgs e)
4949
{
5050
if (e.Urls.Length is 0)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CommunityToolkit.Maui.Core;
2+
3+
static class DockLayoutDefaults
4+
{
5+
public const bool ShouldExpandLastChild = true;
6+
public const double HorizontalSpacing = 0.0d;
7+
public const double VerticalSpacing = 0.0d;
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace CommunityToolkit.Maui.Core;
2+
3+
static class UniformItemLayoutDefaults
4+
{
5+
public const int MaxRows = int.MaxValue;
6+
public const int MaxColumns = int.MaxValue;
7+
}

src/CommunityToolkit.Maui.UnitTests/Behaviors/NumericValidationBehaviorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public async Task IsNull()
8585
Text = text
8686
};
8787
entry.Behaviors.Add(behavior);
88-
88+
8989
//Act
9090
await behavior.ForceValidate(TestContext.Current.CancellationToken);
9191

src/CommunityToolkit.Maui.UnitTests/ImageSources/GravatarImageSource/GravatarImageSourceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ public void DefaultUri()
111111
var gravatarImageSource = new GravatarImageSource();
112112
Assert.Equal(GravatarImageSourceDefaults.Uri, gravatarImageSource.Uri);
113113
}
114-
114+
115115
[Fact]
116116
public void DefaultParentHeight()
117117
{
118118
var gravatarImageSource = new GravatarImageSource();
119119
Assert.Equal(GravatarImageSourceDefaults.ParentHeight, gravatarImageSource.ParentHeight);
120120
}
121-
121+
122122
[Fact]
123123
public void DefaultParentWidth()
124124
{

src/CommunityToolkit.Maui.UnitTests/Layouts/DockLayoutTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public DockLayoutTests()
4141
DockLayout.SetDockPosition(childBottomView, DockPosition.Bottom);
4242
}
4343

44+
[Fact]
45+
public void EnsureDefaults()
46+
{
47+
var layout = new DockLayout();
48+
Assert.Equal(DockLayoutDefaults.ShouldExpandLastChild, layout.ShouldExpandLastChild);
49+
Assert.Equal(DockLayoutDefaults.HorizontalSpacing, layout.HorizontalSpacing);
50+
Assert.Equal(DockLayoutDefaults.VerticalSpacing, layout.VerticalSpacing);
51+
}
52+
4453
#region Measure
4554

4655
[Fact]

src/CommunityToolkit.Maui.UnitTests/Layouts/UniformItemsLayoutTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CommunityToolkit.Maui.Layouts;
1+
using CommunityToolkit.Maui.Core;
2+
using CommunityToolkit.Maui.Layouts;
23
using FluentAssertions;
34
using Xunit;
45

@@ -109,6 +110,14 @@ public void ArrangeChildrenUniformItemsLayout()
109110
Assert.Equal(childSize * childCount, actualSize);
110111
}
111112

113+
[Fact]
114+
public void EnsureDefaults()
115+
{
116+
var layout = new UniformItemsLayout();
117+
Assert.Equal(UniformItemLayoutDefaults.MaxRows, layout.MaxRows);
118+
Assert.Equal(UniformItemLayoutDefaults.MaxColumns, layout.MaxColumns);
119+
}
120+
112121
class TestView : View
113122
{
114123
readonly Size size;

src/CommunityToolkit.Maui/Layouts/DockLayout.shared.cs

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,26 @@ public partial class DockLayout : Layout, IDockLayout
1616
typeof(DockPosition),
1717
typeof(DockLayout),
1818
DockPosition.None,
19-
propertyChanged: OnDockPositionPropertyChanged);
19+
propertyChanged: OnDockPositionPropertyChanged);
2020

2121
/// <summary>
2222
/// If true, the last child is expanded to fill the remaining space (default: true).
2323
/// </summary>
24-
public static readonly BindableProperty ShouldExpandLastChildProperty = BindableProperty.Create(nameof(ShouldExpandLastChild),
25-
typeof(bool),
26-
typeof(DockLayout),
27-
true,
28-
propertyChanged: OnShouldExpandLastChildPropertyChanged);
24+
[BindableProperty(PropertyChangedMethodName = nameof(OnShouldExpandLastChildPropertyChanged))]
25+
public partial bool ShouldExpandLastChild { get; set; } = DockLayoutDefaults.ShouldExpandLastChild;
2926

3027
/// <summary>
31-
/// Horizontal spacing between docked views.
28+
/// Gets or sets the horizontal spacing between docked views.
3229
/// </summary>
33-
public static readonly BindableProperty HorizontalSpacingProperty = BindableProperty.Create(nameof(HorizontalSpacing),
34-
typeof(double),
35-
typeof(DockLayout),
36-
0.0d,
37-
propertyChanged: OnHorizontalSpacingPropertyChanged);
30+
[BindableProperty(PropertyChangedMethodName = nameof(OnHorizontalSpacingPropertyChanged))]
31+
public partial double HorizontalSpacing { get; set; } = DockLayoutDefaults.HorizontalSpacing;
3832

3933
/// <summary>
40-
/// Vertical spacing between docked views.
34+
/// Gets or sets the vertical spacing between docked views.
4135
/// </summary>
42-
public static readonly BindableProperty VerticalSpacingProperty = BindableProperty.Create(nameof(VerticalSpacing),
43-
typeof(double),
44-
typeof(DockLayout),
45-
0.0d,
46-
propertyChanged: OnVerticalSpacingPropertyChanged);
47-
48-
/// <inheritdoc/>
49-
public bool ShouldExpandLastChild
50-
{
51-
get => (bool)GetValue(ShouldExpandLastChildProperty);
52-
set => SetValue(ShouldExpandLastChildProperty, value);
53-
}
54-
55-
/// <inheritdoc/>
56-
public double HorizontalSpacing
57-
{
58-
get => (double)GetValue(HorizontalSpacingProperty);
59-
set => SetValue(HorizontalSpacingProperty, value);
60-
}
36+
[BindableProperty(PropertyChangedMethodName = nameof(OnVerticalSpacingPropertyChanged))]
37+
public partial double VerticalSpacing { get; set; } = DockLayoutDefaults.VerticalSpacing;
6138

62-
/// <inheritdoc/>
63-
public double VerticalSpacing
64-
{
65-
get => (double)GetValue(VerticalSpacingProperty);
66-
set => SetValue(VerticalSpacingProperty, value);
67-
}
6839

6940
/// <summary>
7041
/// Gets the docking position for a view.

0 commit comments

Comments
 (0)