Fixes the RatingView Binding property `Rating`

main
Paul Schneider 9 years ago
parent 8abb26a362
commit 77aa610c3a
4 changed files with 82 additions and 74 deletions

@ -31,6 +31,9 @@ namespace BookAStar.Behaviors
typeof(int),
typeof(StarBehavior), default(int),
BindingMode.TwoWay);
public static event EventHandler<EventArgs> StarTapped;
public int Rating
{
@ -87,7 +90,8 @@ namespace BookAStar.Behaviors
BindableProperty.Create("IsStarred",
typeof(bool),
typeof(StarBehavior),
false);
false,
propertyChanged: OnIsStarredChanged);
public bool IsStarred
{
@ -113,11 +117,12 @@ namespace BookAStar.Behaviors
}
bool itemReached = false;
int count = 1, position = 0;
int count = 0, position = 0;
// all positions to left IsStarred = true and all position to the right is false
foreach (var item in behaviors)
{
if (item != behavior && !itemReached)
{
count++;
if (item != behavior && !itemReached)
{
item.IsStarred = true;
}
@ -131,16 +136,14 @@ namespace BookAStar.Behaviors
if ((bool)newValue)
position = count;
else position = count - 1;
}
if (item != behavior && itemReached)
item.IsStarred = false;
item.Rating = position;
count++;
}
// Assert count > 0
behaviors[count - 1].Rating = position;
}
@ -164,9 +167,11 @@ namespace BookAStar.Behaviors
void OnTapRecognizerTapped(object sender, EventArgs args)
{
// Assert sender is mine
bool currentIsStarred = (bool) GetValue(IsStarredProperty);
SetValue(IsStarredProperty, !currentIsStarred);
OnIsStarredChanged(this,currentIsStarred,!currentIsStarred);
IsStarred = !currentIsStarred;
if (StarTapped!=null)
StarTapped.Invoke(this, new EventArgs());
}
}
}

@ -43,15 +43,15 @@
></Label>
</StackLayout>
</Frame>
<views:RatingView Rating="{Binding Rating}"/>
<views:RatingView Rating="{Binding Rating, Mode=TwoWay}" x:Name="ratingView"/>
</StackLayout>
</lc:GesturesContentView>
<Button Text="{Binding PerformerStatus}" Clicked="OnViewPerformerStatus" />
<Button Text="{Binding UserQueries}" Clicked="OnViewUserQueries" />
<StackLayout Orientation="Horizontal" >
<Button Text="{Binding UserQueries}" Clicked="OnViewUserQueries" VisualElement.IsVisible="{Binding UserIsPro}"/>
<StackLayout Orientation="Horizontal" VisualElement.IsVisible="{Binding HaveAnUser}">
<Label Text="Recevoir les notifications push" StyleClass="Header" />
<Switch IsToggled="{Binding ReceivePushNotifications, Mode=TwoWay}"
HorizontalOptions="End" />
@ -60,7 +60,7 @@
<Label Text="Utiliser ma position" StyleClass="Header" />
<Switch HorizontalOptions="End" IsToggled="{Binding AllowUseMyPosition, Mode=TwoWay}"/>
</StackLayout>
<StackLayout Orientation="Horizontal" VerticalOptions="Start">
<StackLayout Orientation="Horizontal" VerticalOptions="Start" VisualElement.IsVisible="{Binding UserIsPro}">
<Label Text="Ne recevoir de demande de devis que de la part de professionnels uniquement" StyleClass="Header"/>
<Switch HorizontalOptions="End" IsToggled="{Binding AllowProBookingOnly, Mode=TwoWay}"/>
</StackLayout>

@ -15,7 +15,18 @@ namespace BookAStar.ViewModels
internal class DashboardViewModel : ViewModel
{
public int Rating { get; set; }
int rating;
public int Rating
{
get
{
return rating;
}
set
{
SetProperty<int>(ref rating, value, "Rating");
}
}
public string UserId
{
@ -101,7 +112,14 @@ namespace BookAStar.ViewModels
return performerStatus;
}
}
string userQueries;
public string UserQueries
{
get
{
return userQueries;
}
}
public string UserName
{
get
@ -117,7 +135,7 @@ namespace BookAStar.ViewModels
Accounts = MainSettings.AccountList;
User = MainSettings.CurrentUser;
UpdateUserMeta();
Rating = 2;
UserNameGesture = new RelayGesture((g, x) =>
{
if (g.GestureType == GestureType.LongPress)
@ -135,31 +153,51 @@ namespace BookAStar.ViewModels
UpdateUserMeta();
}
bool haveAnUser;
public bool HaveAnUser
{
get { return User!=null; }
}
public bool UserIsPro
{
get { return User?.Roles?.Contains("Performer") ?? false ; }
}
private void UpdateUserMeta ()
{
string newStatusString;
long newQueryCount;
bool newUserIsPro;
ImageSource newAvatar;
if (user==null)
{
string newQueriesButtonText;
bool newHaveAnUser = user == null;
if (newHaveAnUser) {
newQueryCount = 0;
newUserIsPro = false;
newStatusString = null;
newAvatar = null;
newQueriesButtonText = null;
}
else
{
newUserIsPro = User.Roles?.Contains("Performer") ?? false;
newUserIsPro = UserIsPro;
newQueryCount = newUserIsPro ? DataManager.Current.BookQueries.Count : 0;
newStatusString = newUserIsPro ?
$"Profile professionel renseigné,\n{newQueryCount} demandes valides en cours" :
$"Profile professionel renseigné" :
"Profile professionel non renseigné";
newQueriesButtonText = newUserIsPro ?
$"{newQueryCount} demandes valides en cours" :
"Profile professionel non renseigné";
newAvatar = UserHelpers.Avatar(user.Avatar);
}
SetProperty<bool>(ref haveAnUser, newHaveAnUser, "UserIsPro");
SetProperty<bool>(ref userIsPro, newUserIsPro, "UserIsPro");
SetProperty<string>(ref performerStatus, newStatusString, "PerformerStatus");
SetProperty<string>(ref userQueries, newQueriesButtonText, "PerformerStatus");
SetProperty<long>(ref queryCount, newQueryCount, "QueryCount");
SetProperty<ImageSource>(ref avatar, newAvatar, "Avatar");
}

@ -11,66 +11,25 @@ namespace BookAStar.Views
{
public partial class RatingView : ContentView
{
protected int rating;
public int Rating {
get
{
return rating;
}
set
{
if (value != rating)
{
if (value < 0 || value > 5)
{
SetValue(RatingProperty, 0);
rating = 0;
}
else
{
SetValue(RatingProperty, value);
rating = value;
}
starBehaviors[4].SetValue(StarBehavior.RatingProperty, rating);
for (int i = 0; i < rating && i < 5; i++)
starBehaviors[i].SetValue(StarBehavior.IsStarredProperty, true);
for (int i = rating; i < 5 && i >= 0; i++)
starBehaviors[i].SetValue(StarBehavior.IsStarredProperty, false);
}
}
}
public static BindableProperty RatingProperty = BindableProperty.Create(
"Rating", typeof(int), typeof(RatingView), 0, BindingMode.TwoWay,
propertyChanged: OnRatingChanged);
private bool isStarred;
public bool IsStarred
private static void OnRatingChanged(BindableObject bindable, object oldValue, object newValue)
{
get
var view = (RatingView) bindable;
int newValueInt = (int)newValue;
if (oldValue!=newValue)
{
return isStarred;
}
set
{
SetValue(IsStarredProperty, value);
// This will set starBehaviors[4].Rating, five times
for (int i = 0; i < 5 && i < newValueInt; i++)
view.starBehaviors[i].IsStarred = true;
for (int i = newValueInt; i < 5; i++)
view.starBehaviors[i].IsStarred = false;
}
}
StarBehavior[] starBehaviors;
public static BindableProperty IsStarredProperty = BindableProperty.Create(
propertyName: "IsStarred",
returnType: typeof(bool),
declaringType: typeof(RatingView),
defaultValue: false,
defaultBindingMode: BindingMode.TwoWay
);
public static BindableProperty RatingProperty = BindableProperty.Create(
propertyName: "Rating",
returnType: typeof(int),
declaringType: typeof(RatingView),
defaultValue: 0,
defaultBindingMode: BindingMode.TwoWay
);
public RatingView()
{
@ -79,7 +38,13 @@ namespace BookAStar.Views
{
starOne, starTwo, starThree, starFour, starFive
};
StarBehavior.StarTapped += StarBehavior_StarTapped;
}
private void StarBehavior_StarTapped(object sender, EventArgs e)
{
if (starBehaviors.Contains(sender))
SetValue(RatingProperty, starFive.Rating);
}
}
}

Loading…