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

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

@ -43,15 +43,15 @@
></Label> ></Label>
</StackLayout> </StackLayout>
</Frame> </Frame>
<views:RatingView Rating="{Binding Rating}"/> <views:RatingView Rating="{Binding Rating, Mode=TwoWay}" x:Name="ratingView"/>
</StackLayout> </StackLayout>
</lc:GesturesContentView> </lc:GesturesContentView>
<Button Text="{Binding PerformerStatus}" Clicked="OnViewPerformerStatus" /> <Button Text="{Binding PerformerStatus}" Clicked="OnViewPerformerStatus" />
<Button Text="{Binding UserQueries}" Clicked="OnViewUserQueries" /> <Button Text="{Binding UserQueries}" Clicked="OnViewUserQueries" VisualElement.IsVisible="{Binding UserIsPro}"/>
<StackLayout Orientation="Horizontal" > <StackLayout Orientation="Horizontal" VisualElement.IsVisible="{Binding HaveAnUser}">
<Label Text="Recevoir les notifications push" StyleClass="Header" /> <Label Text="Recevoir les notifications push" StyleClass="Header" />
<Switch IsToggled="{Binding ReceivePushNotifications, Mode=TwoWay}" <Switch IsToggled="{Binding ReceivePushNotifications, Mode=TwoWay}"
HorizontalOptions="End" /> HorizontalOptions="End" />
@ -60,7 +60,7 @@
<Label Text="Utiliser ma position" StyleClass="Header" /> <Label Text="Utiliser ma position" StyleClass="Header" />
<Switch HorizontalOptions="End" IsToggled="{Binding AllowUseMyPosition, Mode=TwoWay}"/> <Switch HorizontalOptions="End" IsToggled="{Binding AllowUseMyPosition, Mode=TwoWay}"/>
</StackLayout> </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"/> <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}"/> <Switch HorizontalOptions="End" IsToggled="{Binding AllowProBookingOnly, Mode=TwoWay}"/>
</StackLayout> </StackLayout>

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

@ -11,66 +11,25 @@ namespace BookAStar.Views
{ {
public partial class RatingView : ContentView public partial class RatingView : ContentView
{ {
protected int rating; public static BindableProperty RatingProperty = BindableProperty.Create(
"Rating", typeof(int), typeof(RatingView), 0, BindingMode.TwoWay,
public int Rating { propertyChanged: OnRatingChanged);
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);
}
}
}
private bool isStarred; private static void OnRatingChanged(BindableObject bindable, object oldValue, object newValue)
public bool IsStarred
{ {
get var view = (RatingView) bindable;
{ int newValueInt = (int)newValue;
return isStarred; if (oldValue!=newValue)
}
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; 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() public RatingView()
{ {
@ -79,7 +38,13 @@ namespace BookAStar.Views
{ {
starOne, starTwo, starThree, starFour, starFive 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…