Skip to content

Commit d9a5085

Browse files
Use [BindableProperty] for RatingView (#2964)
* Changed to using the BindableProperty SourceGen syntax. * Completed changes to the RatingView so that it's now using the SG bindable properties, fixed failing tests and had to remove 2 as these no longer made sense. There was a hard coded Default which was added to the Defaults as well. Only issue is that the DefaultValue use from the SG only returns an Int no matter what the default Value should be like a Bool, Double, Color etc so there are CreateDefault Methods to resolve this until the SG is fixed and these can be removed... * Update Formatting, Remove Unneeded DefaultValueCreatorMethods * Fix Validators for Rating + MaximumRating, Use `PropertyChanging` for ArgumentOutOfRange Validation * Remove Validators * Remove `[RequiresUnreferencedCode]` * `dotnet format` * Add `[BindableProperty]` to `RatingView.FillOption` * Update RatingView.shared.cs --------- Co-authored-by: Brandon Minnick <[email protected]>
1 parent e9e262d commit d9a5085

File tree

7 files changed

+177
-211
lines changed

7 files changed

+177
-211
lines changed

src/CommunityToolkit.Maui.Core/Primitives/Defaults/RatingViewDefaults.shared.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
// Ignore Spelling: color
2-
3-
using System.ComponentModel;
4-
5-
namespace CommunityToolkit.Maui.Core;
1+
namespace CommunityToolkit.Maui.Core;
62

73
/// <summary>Default Values for RatingView</summary>
84
static class RatingViewDefaults
95
{
106
/// <summary>Default rating value.</summary>
11-
public const double DefaultRating = 0.0;
7+
public const double Rating = 0.0;
128

139
/// <summary>Default view element read only.</summary>
1410
public const bool IsReadOnly = false;
@@ -28,6 +24,15 @@ static class RatingViewDefaults
2824
/// <summary>Default spacing between ratings.</summary>
2925
public const double Spacing = 10.0;
3026

27+
/// <summary>Default rating shape.</summary>
28+
public const RatingViewShape Shape = RatingViewShape.Star;
29+
30+
/// <summary>Default Fill When Tapped</summary>
31+
public const RatingViewFillOption FillWhenTapped = RatingViewFillOption.Shape;
32+
33+
/// <summary>Default Fill Option</summary>
34+
public const RatingViewFillOption FillOption = RatingViewFillOption.Shape;
35+
3136
/// <summary>Default color for an empty rating.</summary>
3237
public static Color EmptyShapeColor { get; } = Colors.Transparent;
3338

@@ -37,9 +42,6 @@ static class RatingViewDefaults
3742
/// <summary>Default rating item padding.</summary>
3843
public static Thickness ShapePadding { get; } = new(0);
3944

40-
/// <summary>Default rating shape.</summary>
41-
public static RatingViewShape Shape { get; } = RatingViewShape.Star;
42-
4345
/// <summary>Default border color for a rating shape.</summary>
4446
public static Color ShapeBorderColor { get; } = Colors.Grey;
4547
}

src/CommunityToolkit.Maui.SourceGenerators.Benchmarks/BindablePropertyAttributeSourceGeneratorBenchmarks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ public Task GenerateBindableProperty_NestedClass_GeneratesCorrectCode()
3737
[Benchmark]
3838
public Task GenerateBindableProperty_WithComplexDefaultValues_GeneratesCorrectCode()
3939
=> edgeCaseTests.GenerateBindableProperty_WithComplexDefaultValues_GeneratesCorrectCode();
40-
}
40+
}

src/CommunityToolkit.Maui.SourceGenerators.Benchmarks/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ public static void Main(string[] args)
1010
var config = DefaultConfig.Instance;
1111
BenchmarkRunner.Run<BindablePropertyAttributeSourceGeneratorBenchmarks>(config, args);
1212
}
13-
}
13+
}

src/CommunityToolkit.Maui.SourceGenerators.Internal/Helpers/AttributeExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public static string GetNamedTypeArgumentsAttributeValueByNameAsCastedString(thi
2929
return $"({data.Type}){members[(int)data.Value]}";
3030
}
3131

32-
if(data.Type?.SpecialType is SpecialType.System_String)
32+
if (data.Type?.SpecialType is SpecialType.System_String)
3333
{
34-
return data.Value is null ? $"\"{placeholder}\"": $"({data.Type})\"{data.Value}\"";
34+
return data.Value is null ? $"\"{placeholder}\"" : $"({data.Type})\"{data.Value}\"";
3535
}
3636

3737
if (data.Type?.SpecialType is SpecialType.System_Char)

src/CommunityToolkit.Maui.UnitTests/Mocks/MockCameraProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private set
2121

2222
public void Dispose()
2323
{
24-
24+
2525
}
2626

2727
public Task RefreshAvailableCameras(CancellationToken token)

src/CommunityToolkit.Maui.UnitTests/Views/RatingView/RatingViewTests.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void Defaults_ShapeDefaultsApplied()
8282
public void Defaults_ShouldHaveCorrectDefaultProperties()
8383
{
8484
RatingView ratingView = new();
85-
ratingView.Rating.Should().Be(RatingViewDefaults.DefaultRating);
85+
ratingView.Rating.Should().Be(RatingViewDefaults.Rating);
8686
ratingView.EmptyShapeColor.Should().BeOfType<Color>().And.Be(RatingViewDefaults.EmptyShapeColor);
8787
ratingView.FillColor.Should().BeOfType<Color>().And.Be(RatingViewDefaults.FillColor);
8888
ratingView.IsReadOnly.Should().BeFalse().And.Be(RatingViewDefaults.IsReadOnly);
@@ -93,7 +93,8 @@ public void Defaults_ShouldHaveCorrectDefaultProperties()
9393
ratingView.ShapeBorderColor.Should().BeOfType<Color>().And.Be(RatingViewDefaults.ShapeBorderColor);
9494
ratingView.ShapeBorderThickness.Should().Be(RatingViewDefaults.ShapeBorderThickness);
9595
ratingView.Spacing.Should().Be(RatingViewDefaults.Spacing);
96-
ratingView.FillOption.Should().BeOneOf(RatingViewFillOption.Shape).And.Be(RatingViewFillOption.Shape);
96+
ratingView.FillWhenTapped.Should().Be(RatingViewDefaults.FillWhenTapped);
97+
ratingView.FillOption.Should().BeOneOf(RatingViewDefaults.FillOption);
9798
ratingView.CustomShapePath.Should().BeNull();
9899
}
99100

@@ -170,7 +171,7 @@ public void Events_RatingChanged_ShouldBeRaised_RatingPropertyChanged_NotReadOnl
170171
{
171172
const double currentRating = 3.5;
172173
RatingView ratingView = new();
173-
ratingView.Rating.Should().Be(RatingViewDefaults.DefaultRating);
174+
ratingView.Rating.Should().Be(RatingViewDefaults.Rating);
174175
var signaled = false;
175176
ratingView.RatingChanged += (sender, e) => signaled = true;
176177
ratingView.Rating = currentRating;
@@ -621,24 +622,6 @@ public void Properties_MaximumRating_NumberOfChildrenLower()
621622
ratingView.RatingLayout.Count.Should().Be(minMaximumRating);
622623
}
623624

624-
[Fact]
625-
public void Properties_MaximumRating_Validator()
626-
{
627-
RatingView ratingView = new();
628-
RatingView.MaximumRatingProperty.ValidateValue(ratingView, 0).Should().BeFalse();
629-
RatingView.MaximumRatingProperty.ValidateValue(ratingView, RatingViewDefaults.MaximumRatingLimit + 1).Should().BeFalse();
630-
RatingView.MaximumRatingProperty.ValidateValue(ratingView, 1).Should().BeTrue();
631-
}
632-
633-
[Fact]
634-
public void Properties_Rating_Validator()
635-
{
636-
RatingView ratingView = new();
637-
RatingView.RatingProperty.ValidateValue(ratingView, -1.0).Should().BeFalse();
638-
RatingView.RatingProperty.ValidateValue(ratingView, (double)(RatingViewDefaults.MaximumRatingLimit + 1)).Should().BeFalse();
639-
RatingView.RatingProperty.ValidateValue(ratingView, 0.1).Should().BeTrue();
640-
}
641-
642625
[Fact]
643626
public void RatingViewDoesNotThrowsArgumentOutOfRangeExceptionWhenRatingSetBeforeMaximumRating()
644627
{
@@ -675,7 +658,7 @@ public void RatingViewThrowsInvalidOperationExceptionWhenChildIsNotBorder()
675658
public void RatingViewThrowsArgumentOutOfRangeExceptionWhenOutsideLowerBounds()
676659
{
677660
RatingView ratingView = new();
678-
Assert.Throws<ArgumentOutOfRangeException>(() => ratingView.Rating = 0 - double.Epsilon);
661+
Assert.Throws<ArgumentOutOfRangeException>(() => ratingView.Rating = -1);
679662
}
680663

681664
[Fact]

0 commit comments

Comments
 (0)