allow to select a date & fix issue

This commit is contained in:
2022-03-08 14:19:50 +08:00
parent 91db3caa15
commit 63ee572e8b
27 changed files with 368 additions and 158 deletions

View File

@ -25,7 +25,7 @@
TextColor="{DynamicResource TextColor}"
FontFamily="{TemplateBinding BillingDay.FontFamily}"
Opacity="{TemplateBinding BillingDay.TextOpacity}"/>
<StackLayout Grid.Row="1"
<StackLayout Grid.Row="1" Margin="8, 0"
IsVisible="{TemplateBinding BillingDay.IsSelected}"
BackgroundColor="{DynamicResource PrimaryColor}"
Opacity="{TemplateBinding BillingDay.Opacity}"/>
@ -40,7 +40,7 @@
TextColor="{DynamicResource RedColor}"
FontFamily="{TemplateBinding BillingDay.FontFamily}"
Opacity="{TemplateBinding BillingDay.TextOpacity}"/>
<StackLayout Grid.Row="1"
<StackLayout Grid.Row="1" Margin="8, 0"
IsVisible="{TemplateBinding BillingDay.IsSelected}"
BackgroundColor="{DynamicResource PrimaryColor}"
Opacity="{TemplateBinding BillingDay.Opacity}"/>

View File

@ -7,13 +7,13 @@ namespace Billing.UI
{
#region UI Properties
public static readonly BindableProperty SundayProperty = BindableProperty.Create(nameof(Sunday), typeof(BillingDay), typeof(BillingDate));
public static readonly BindableProperty MondayProperty = BindableProperty.Create(nameof(Monday), typeof(BillingDay), typeof(BillingDate));
public static readonly BindableProperty TuesdayProperty = BindableProperty.Create(nameof(Tuesday), typeof(BillingDay), typeof(BillingDate));
public static readonly BindableProperty WednesdayProperty = BindableProperty.Create(nameof(Wednesday), typeof(BillingDay), typeof(BillingDate));
public static readonly BindableProperty ThursdayProperty = BindableProperty.Create(nameof(Thursday), typeof(BillingDay), typeof(BillingDate));
public static readonly BindableProperty FridayProperty = BindableProperty.Create(nameof(Friday), typeof(BillingDay), typeof(BillingDate));
public static readonly BindableProperty SaturdayProperty = BindableProperty.Create(nameof(Saturday), typeof(BillingDay), typeof(BillingDate));
public static readonly BindableProperty SundayProperty = Helper.Create<BillingDay, BillingDate>(nameof(Sunday));
public static readonly BindableProperty MondayProperty = Helper.Create<BillingDay, BillingDate>(nameof(Monday));
public static readonly BindableProperty TuesdayProperty = Helper.Create<BillingDay, BillingDate>(nameof(Tuesday));
public static readonly BindableProperty WednesdayProperty = Helper.Create<BillingDay, BillingDate>(nameof(Wednesday));
public static readonly BindableProperty ThursdayProperty = Helper.Create<BillingDay, BillingDate>(nameof(Thursday));
public static readonly BindableProperty FridayProperty = Helper.Create<BillingDay, BillingDate>(nameof(Friday));
public static readonly BindableProperty SaturdayProperty = Helper.Create<BillingDay, BillingDate>(nameof(Saturday));
public BillingDay Sunday => (BillingDay)GetValue(SundayProperty);
public BillingDay Monday => (BillingDay)GetValue(MondayProperty);
@ -39,38 +39,36 @@ namespace Billing.UI
};
}
public static readonly BindableProperty LocatedDateProperty = BindableProperty.Create(nameof(LocatedDate), typeof(DateTime), typeof(BillingDate), propertyChanged: OnLocatedDatePropertyChanged);
public static readonly BindableProperty SelectedDateProperty = BindableProperty.Create(nameof(SelectedDate), typeof(DateTime), typeof(BillingDate), propertyChanged: OnSelectedDatePropertyChanged);
public static readonly BindableProperty LocatedDateProperty = Helper.Create<DateTime, BillingDate>(nameof(LocatedDate), propertyChanged: OnLocatedDatePropertyChanged);
public static readonly BindableProperty SelectedDateProperty = Helper.Create<DateTime, BillingDate>(nameof(SelectedDate), propertyChanged: OnSelectedDatePropertyChanged);
private static void OnLocatedDatePropertyChanged(BindableObject obj, object old, object @new)
private static void OnLocatedDatePropertyChanged(BillingDate billingDate, DateTime old, DateTime date)
{
if (obj is BillingDate billingDate && @new is DateTime date)
var week = (int)date.DayOfWeek;
var tmpDate = date.AddDays(-week);
for (var i = 0; i < 7; i++)
{
var week = (int)date.DayOfWeek;
var tmpDate = date.AddDays(-week);
for (var i = 0; i < 7; i++)
{
var prop = GetWeekProperty(i);
var day = new BillingDay(tmpDate);
billingDate.SetValue(prop, day);
tmpDate = tmpDate.AddDays(1);
}
var day = new BillingDay(tmpDate);
billingDate[i] = day;
tmpDate = tmpDate.AddDays(1);
}
}
private static void OnSelectedDatePropertyChanged(BindableObject obj, object old, object @new)
private static void OnSelectedDatePropertyChanged(BillingDate billingDate, DateTime old, DateTime selected)
{
if (obj is BillingDate billingDate && @new is DateTime selected)
for (var i = 0; i < 7; i++)
{
for (var i = 0; i < 7; i++)
{
var prop = GetWeekProperty(i);
var day = (BillingDay)billingDate.GetValue(prop);
day.Refresh(selected);
}
var day = billingDate[i];
day.Refresh(selected);
}
}
public BillingDay this[int index]
{
get => (BillingDay)GetValue(GetWeekProperty(index));
set => SetValue(GetWeekProperty(index), value);
}
public DateTime LocatedDate
{
get => (DateTime)GetValue(LocatedDateProperty);
@ -99,13 +97,26 @@ namespace Billing.UI
public void SetDateTime(DateTime selectedDate, DateTime? locatedDate = null)
{
if (Helper.IsSameDay(selectedDate, SelectedDate))
{
return;
}
DateTime located;
if (locatedDate != null)
{
LocatedDate = locatedDate.Value;
located = Helper.FirstDay(locatedDate.Value);
}
else
{
LocatedDate = selectedDate;
located = Helper.FirstDay(selectedDate);
}
if (LocatedDate != located)
{
LocatedDate = located;
}
else
{
RestoreState(this[(int)selectedDate.DayOfWeek]);
}
SelectedDate = selectedDate;
DateSelected?.Invoke(this, new DateEventArgs(selectedDate));
@ -114,43 +125,48 @@ namespace Billing.UI
private void OnDayChanged(object o)
{
var selected = SelectedDate;
if (o is BillingDay day && (selected.Year != day.Date.Year || selected.DayOfYear != day.Date.DayOfYear))
if (o is BillingDay day && !Helper.IsSameDay(selected, day.Date))
{
for (var i = 0; i < 7; i++)
{
var d = (BillingDay)GetValue(GetWeekProperty(i));
if (d.IsSelected)
{
this.AbortAnimation("unselected");
this.Animate("unselected", v =>
{
d.Opacity = v;
},
start: 1, end: 0,
easing: Easing.CubicOut,
finished: (v, b) =>
{
d.Opacity = 0;
d.IsSelected = false;
});
}
}
RestoreState(day);
SelectedDate = day.Date;
this.AbortAnimation("selected");
this.Animate("selected", v =>
{
day.Opacity = v;
},
start: 0, end: 1,
easing: Easing.CubicOut,
finished: (v, b) =>
{
day.Opacity = 1;
});
DateSelected?.Invoke(this, new DateEventArgs(day.Date));
}
}
private void RestoreState(BillingDay day)
{
for (var i = 0; i < 7; i++)
{
var d = (BillingDay)GetValue(GetWeekProperty(i));
if (d.IsSelected)
{
this.AbortAnimation("unselected");
this.Animate("unselected", v =>
{
d.Opacity = v;
},
start: 1, end: 0,
easing: Easing.CubicOut,
finished: (v, b) =>
{
d.Opacity = 0;
d.IsSelected = false;
});
}
}
this.AbortAnimation("selected");
this.Animate("selected", v =>
{
day.Opacity = v;
},
start: 0, end: 1,
easing: Easing.CubicOut,
finished: (v, b) =>
{
day.Opacity = 1;
});
}
private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
if (e.StatusType == GestureStatus.Started)
@ -176,7 +192,6 @@ namespace Billing.UI
x1 = null;
var ms = (DateTime.Now - lastDate).TotalMilliseconds;
var speed = totalX / ms;
Helper.Debug($"completed, speed: {speed}");
if (speed < -0.7)
{
LocatedDate = LocatedDate.AddDays(7);
@ -185,7 +200,7 @@ namespace Billing.UI
{
LocatedDate = LocatedDate.AddDays(-7);
}
OnSelectedDatePropertyChanged(this, null, SelectedDate);
OnSelectedDatePropertyChanged(this, default, SelectedDate);
}
}
}
@ -202,50 +217,37 @@ namespace Billing.UI
public class BillingDayView : ContentView
{
public static readonly BindableProperty BillingDayProperty = BindableProperty.Create(nameof(BillingDay), typeof(BillingDay), typeof(BillingDayView));
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(Command), typeof(BillingDayView));
public static readonly BindableProperty BillingDayProperty = Helper.Create<BillingDay, BillingDayView>(nameof(BillingDay));
public BillingDay BillingDay
{
get => (BillingDay)GetValue(BillingDayProperty);
set => SetValue(BillingDayProperty, value);
}
public Command Command
{
get => (Command)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
}
public class BillingDay : BindableObject
{
public static readonly BindableProperty DateProperty = BindableProperty.Create(nameof(Date), typeof(DateTime), typeof(BillingDay), propertyChanged: OnDatePropertyChanged);
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(BillingDay));
public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create(nameof(FontFamily), typeof(string), typeof(BillingDay), defaultValue: Definition.GetRegularFontFamily());
public static readonly BindableProperty IsSelectedProperty = BindableProperty.Create(nameof(IsSelected), typeof(bool), typeof(BillingDay));
public static readonly BindableProperty OpacityProperty = BindableProperty.Create(nameof(Opacity), typeof(double), typeof(BillingDay), defaultValue: 1.0);
public static readonly BindableProperty TextOpacityProperty = BindableProperty.Create(nameof(TextOpacity), typeof(double), typeof(BillingDay), defaultValue: 1.0);
public static readonly BindableProperty DateProperty = Helper.Create<DateTime, BillingDay>(nameof(Date), propertyChanged: OnDatePropertyChanged);
public static readonly BindableProperty TextProperty = Helper.Create<string, BillingDay>(nameof(Text));
public static readonly BindableProperty FontFamilyProperty = Helper.Create<string, BillingDay>(nameof(FontFamily), defaultValue: Definition.GetRegularFontFamily());
public static readonly BindableProperty IsSelectedProperty = Helper.Create<bool, BillingDay>(nameof(IsSelected), defaultValue: false);
public static readonly BindableProperty OpacityProperty = Helper.Create<double, BillingDay>(nameof(Opacity), defaultValue: 1.0);
public static readonly BindableProperty TextOpacityProperty = Helper.Create<double, BillingDay>(nameof(TextOpacity), defaultValue: 1.0);
private static void OnDatePropertyChanged(BindableObject obj, object old, object @new)
private static void OnDatePropertyChanged(BillingDay day, DateTime old, DateTime date)
{
if (obj is BillingDay day && @new is DateTime date)
if (date.Day == 1)
{
if (date.Day == 1)
{
day.SetValue(TextProperty, date.ToString("MMM"));
}
else
{
day.SetValue(TextProperty, date.Day.ToString());
}
day.SetValue(TextProperty, date.ToString("MMM"));
}
else
{
day.SetValue(TextProperty, date.Day.ToString());
}
}
public DateTime Date
{
get => (DateTime)GetValue(DateProperty);
set => SetValue(DateProperty, value);
}
public DateTime Date => (DateTime)GetValue(DateProperty);
public string Text => (string)GetValue(TextProperty);
public string FontFamily => (string)GetValue(FontFamilyProperty);
public bool IsSelected
@ -262,15 +264,15 @@ namespace Billing.UI
public BillingDay(DateTime date)
{
Date = date;
SetValue(DateProperty, date);
}
public void Refresh(DateTime selected)
{
var date = Date;
if (date.Year == selected.Year && date.DayOfYear == selected.DayOfYear)
if (Helper.IsSameDay(date, selected))
{
SetValue(IsSelectedProperty, true);
IsSelected = true;
SetValue(FontFamilyProperty, Definition.GetBoldFontFamily());
}
else

View File

@ -3,17 +3,19 @@ using Xamarin.Forms;
namespace Billing.UI
{
public class TintImage : Image
public class TintHelper
{
public static readonly BindableProperty PrimaryColorProperty = BindableProperty.Create(nameof(PrimaryColor), typeof(Color?), typeof(TintImage));
public const string TintColor = nameof(TintColor);
public static readonly BindableProperty TintColorProperty = BindableProperty.CreateAttached(TintColor, typeof(Color?), typeof(TintHelper), null);
public Color? PrimaryColor
{
get => (Color?)GetValue(PrimaryColorProperty);
set => SetValue(PrimaryColorProperty, value);
}
public static void SetTintColor(BindableObject obj, Color? color) => obj.SetValue(TintColorProperty, color);
public static Color? GetTintColor(BindableObject obj) => (Color?)obj.GetValue(TintColorProperty);
}
public class TintImage : Image { }
public class TintImageButton : ImageButton { }
public class LongPressButton : Button
{
public event EventHandler LongPressed;

View File

@ -14,6 +14,7 @@ namespace Billing.UI
public static string PrimaryColorKey = "PrimaryColor";
public static partial (string main, long build) GetVersion();
public static partial string GetRegularFontFamily();
public static partial string GetSemiBoldFontFamily();
public static partial string GetBoldFontFamily();
public static partial string GetBrandsFontFamily();
}

View File

@ -1,6 +1,5 @@
using Billing.Themes;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Billing.UI
@ -44,8 +43,8 @@ namespace Billing.UI
ColumnDefinitions =
{
new ColumnDefinition { Width = GridLength.Auto },
new ColumnDefinition { Width = new GridLength(.3, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength(.7, GridUnitType.Star) }
new ColumnDefinition { Width = new GridLength(.35, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength(.65, GridUnitType.Star) }
},
Children =
{
@ -220,7 +219,7 @@ namespace Billing.UI
Margin = new Thickness(6, 0)
}
.Binding(Image.SourceProperty, nameof(ImageSource))
.Binding(TintImage.PrimaryColorProperty, nameof(TintColor)),
.Binding(TintHelper.TintColorProperty, nameof(TintColor)),
new TintImage
{
@ -270,7 +269,7 @@ namespace Billing.UI
protected override View Content => new OptionDatePicker
{
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center
VerticalOptions = LayoutOptions.Fill
}
.Binding(DatePicker.DateProperty, nameof(Date), mode: BindingMode.TwoWay)
.DynamicResource(DatePicker.TextColorProperty, BaseTheme.TextColor)
@ -290,7 +289,7 @@ namespace Billing.UI
protected override View Content => new OptionTimePicker
{
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center
VerticalOptions = LayoutOptions.Fill
}
.Binding(TimePicker.TimeProperty, nameof(Time), mode: BindingMode.TwoWay)
.DynamicResource(TimePicker.TextColorProperty, BaseTheme.TextColor)
@ -329,7 +328,8 @@ namespace Billing.UI
{
HorizontalOptions = LayoutOptions.Fill,
HorizontalTextAlignment = TextAlignment.End,
VerticalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Fill,
VerticalTextAlignment = TextAlignment.Center,
ReturnType = ReturnType.Next
}
.Binding(Entry.TextProperty, nameof(Text), mode: BindingMode.TwoWay)