Skip to content

Commit

Permalink
implement the rating view and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
Eel2000 committed Jul 8, 2023
1 parent de0372c commit 791034b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
7 changes: 6 additions & 1 deletion RatingView.Sample/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:rv="clr-namespace:RatingView.Views;assembly=RatingView"
x:Class="RatingView.Sample.MainPage">

<ScrollView>
Expand All @@ -16,7 +17,7 @@
HorizontalOptions="Center" />

<Label
Text="Hello, World!"
Text="Hello, World! this is a RatingView Sample"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />
Expand All @@ -28,6 +29,10 @@
FontSize="18"
HorizontalOptions="Center" />

<rv:RatingView Maximum="5" Value="3" StarSize="40" AllowClickRating="True"/>

<rv:RatingView Maximum="5" Value="2.5" StarSize="40" StarColor="BlueViolet"/>

<Button
x:Name="CounterBtn"
Text="Click me"
Expand Down
2 changes: 2 additions & 0 deletions RatingView.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using RatingView.Extensions;

namespace RatingView.Sample
{
Expand All @@ -9,6 +10,7 @@ public static MauiApp CreateMauiApp()
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureRatingView()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
Expand Down
4 changes: 4 additions & 0 deletions RatingView.Sample/RatingView.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RatingView\RatingView.csproj" />
</ItemGroup>

</Project>
43 changes: 29 additions & 14 deletions RatingView/Views/RatingView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@ public class RatingView : BaseTemplateView<Grid>
defaultValue: null,
propertyChanged: OnCommandChanged);

public static readonly BindableProperty AnimateProperty =
BindableProperty.Create(
nameof(Animate),
typeof(bool),
typeof(RatingView),
defaultValue: true,
propertyChanged: OnAnimateChanged);

#endregion

#region Private Properties
//The collection of stars images
private Image[] stars;

//The flag that specifies if the use is interacting with the view
private bool isTouching;
#endregion

#region Public Properties
Expand Down Expand Up @@ -114,9 +117,6 @@ public double StarSpacing
set => SetValue(StarSpacingProperty, value);
}

/// <summary>
/// Activate the rating event on clicked
/// </summary>
public bool AllowClickRating
{
get => (bool)GetValue(AllowClickRatingProperty);
Expand All @@ -128,6 +128,12 @@ public ICommand Command
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}

public bool Animate
{
get => (bool)GetValue(AnimateProperty);
set => SetValue(AnimateProperty, value);
}
#endregion


Expand Down Expand Up @@ -179,9 +185,6 @@ private void UpdateStars()
}
}

/// <summary>
/// Create the stars images to fill with the grid
/// </summary>
private void GenerateStars()
{
for (int i = 0; i < Maximum; i++)
Expand Down Expand Up @@ -230,7 +233,10 @@ private void GenerateStars()
this.Control.SetColumn(image, i);


stars[i] = ApplyCustomStyle(image);
if (Animate)
stars[i] = ApplyCustomStyle(image);
else
stars[i] = image;
}

UpdateStars();
Expand Down Expand Up @@ -290,7 +296,10 @@ private void RegenerateStars()
Control.Children.Add(image);
this.Control.SetColumn(image, i);

stars[i] = image;
if (Animate)
stars[i] = ApplyCustomStyle(image);
else
stars[i] = image;
}

UpdateStars();
Expand All @@ -308,7 +317,7 @@ private Image ApplyCustomStyle(Image image)
Setter pointerOverSetter = new()
{
Property = Image.ScaleProperty,
Value = 1.02,
Value = 1.05,
};
pointerOverState.Setters.Add(pointerOverSetter);

Expand All @@ -325,7 +334,6 @@ private Image ApplyCustomStyle(Image image)
commonStatesGroup.States.Add(pointerOverState);

VisualStateManager.GetVisualStateGroups(image).Add(commonStatesGroup);
//VisualStateManager.GetVisualStateGroups(imageStyle).Add(commonStatesGroup);

image.Style = imageStyle;

Expand Down Expand Up @@ -403,6 +411,13 @@ private static void OnCommandChanged(BindableObject bindable, object oldValue, o
ratingView.RegenerateStars();
}

private static void OnAnimateChanged(BindableObject bindable, object oldValue, object newValue)
{
var ratingView = bindable as RatingView;

ratingView.RegenerateStars();
}

private void OnStarTapped(object sender, TappedEventArgs e)
{
var tappedImage = sender as Image;
Expand Down

0 comments on commit 791034b

Please sign in to comment.