fix date selection range issue

This commit is contained in:
gaoyuan
2022-03-11 00:07:50 +08:00
parent 74053a328e
commit 71c1a7f0f1
2 changed files with 82 additions and 19 deletions

View File

@ -141,8 +141,8 @@
<ui:BlurryPanel x:Name="panelFilter" VerticalOptions="Start" Opacity="0" <ui:BlurryPanel x:Name="panelFilter" VerticalOptions="Start" Opacity="0"
BackgroundColor="{DynamicResource WindowBackgroundColor}" BackgroundColor="{DynamicResource WindowBackgroundColor}"
HeightRequest="{Binding Height, Source={x:Reference gridFilter}}"/> HeightRequest="{Binding Height, Source={x:Reference gridFilter}}"/>
<Grid x:Name="gridFilter" VerticalOptions="Start" Opacity="0" Padding="10" RowDefinitions="Auto, Auto, Auto"> <Grid x:Name="gridFilter" VerticalOptions="Start" Opacity="0" RowDefinitions="Auto, Auto, Auto, Auto">
<ui:SegmentedControl Margin="6, 6, 6, 3" VerticalOptions="Center" <ui:SegmentedControl Margin="16, 16, 16, 3" VerticalOptions="Center"
SelectedSegmentIndex="{Binding SegmentType, Mode=TwoWay}" SelectedSegmentIndex="{Binding SegmentType, Mode=TwoWay}"
SelectedTextColor="{DynamicResource TextColor}" SelectedTextColor="{DynamicResource TextColor}"
TintColor="{DynamicResource PromptBackgroundColor}"> TintColor="{DynamicResource PromptBackgroundColor}">
@ -151,7 +151,7 @@
<ui:SegmentedControlOption Text="{r:Text Income}"/> <ui:SegmentedControlOption Text="{r:Text Income}"/>
</ui:SegmentedControl.Children> </ui:SegmentedControl.Children>
</ui:SegmentedControl> </ui:SegmentedControl>
<ScrollView Grid.Row="1" Margin="6, 6, 6, 3"> <ScrollView Grid.Row="1" Margin="16, 6, 16, 3">
<ui:SegmentedControl SelectedSegmentIndex="{Binding SegmentDate, Mode=TwoWay}" <ui:SegmentedControl SelectedSegmentIndex="{Binding SegmentDate, Mode=TwoWay}"
SelectedTextColor="{DynamicResource TextColor}" SelectedTextColor="{DynamicResource TextColor}"
TintColor="{DynamicResource PromptBackgroundColor}"> TintColor="{DynamicResource PromptBackgroundColor}">
@ -166,7 +166,7 @@
</ui:SegmentedControl.Children> </ui:SegmentedControl.Children>
</ui:SegmentedControl> </ui:SegmentedControl>
</ScrollView> </ScrollView>
<Grid Grid.Row="2" ColumnDefinitions="*, Auto, *" Margin="0, 10"> <Grid Grid.Row="2" ColumnDefinitions="*, Auto, *" Margin="10">
<ui:OptionDatePicker Date="{Binding StartPickerDate, Mode=TwoWay}" <ui:OptionDatePicker Date="{Binding StartPickerDate, Mode=TwoWay}"
FontSize="16" TextColor="{DynamicResource TextColor}" FontSize="16" TextColor="{DynamicResource TextColor}"
VerticalOptions="Center" VerticalOptions="Center"
@ -178,6 +178,8 @@
VerticalOptions="Center" VerticalOptions="Center"
ios:DatePicker.UpdateMode="WhenFinished"/> ios:DatePicker.UpdateMode="WhenFinished"/>
</Grid> </Grid>
<Grid Grid.Row="3" HeightRequest="1" BackgroundColor="{DynamicResource PromptBackgroundColor}"
IsVisible="{OnPlatform iOS=False}"/>
</Grid> </Grid>
</Grid> </Grid>
</ui:BillingPage> </ui:BillingPage>

View File

@ -14,6 +14,17 @@ using Resource = Billing.Languages.Resource;
namespace Billing.Views namespace Billing.Views
{ {
public enum DateType : int
{
Monthly = 0,
Today,
PastMonth,
PastQuarter,
PastSixMonths,
PastYear,
Total
}
public partial class RankPage : BillingPage public partial class RankPage : BillingPage
{ {
private static readonly DateTime today = DateTime.Today; private static readonly DateTime today = DateTime.Today;
@ -53,13 +64,13 @@ namespace Billing.Views
} }
private static void OnSegmentDateChanged(RankPage page, int old, int @new) private static void OnSegmentDateChanged(RankPage page, int old, int @new)
{ {
page.OnDateTypeCommand(@new); page.OnDateTypeCommand((DateType)@new);
} }
private static void OnDateChanged(RankPage page, DateTime old = default, DateTime @new = default) private static void OnDateChanged(RankPage page, DateTime old = default, DateTime @new = default)
{ {
page.isLocked = true; page.isLocked = true;
page.StartPickerDate = page.StartDate; page.StartPickerDate = page.StartDate.Date;
page.EndPickerDate = page.EndDate; page.EndPickerDate = page.EndDate.Date;
page.isLocked = false; page.isLocked = false;
if (!page.isFreezed) if (!page.isFreezed)
{ {
@ -73,7 +84,7 @@ namespace Billing.Views
if (!page.isLocked) if (!page.isLocked)
{ {
page.SegmentDate = -1; page.SegmentDate = -1;
page.StartDate = @new; page.StartDate = @new.Date;
} }
} }
private static void OnPickerEndDateChanged(RankPage page, DateTime _, DateTime @new) private static void OnPickerEndDateChanged(RankPage page, DateTime _, DateTime @new)
@ -81,7 +92,7 @@ namespace Billing.Views
if (!page.isLocked) if (!page.isLocked)
{ {
page.SegmentDate = -1; page.SegmentDate = -1;
page.EndDate = @new; page.EndDate = @new.Date.LastMoment();
} }
} }
@ -186,7 +197,7 @@ namespace Billing.Views
OnDateChanged(this); OnDateChanged(this);
} }
private void OnDateTypeCommand(int index) private void OnDateTypeCommand(DateType index)
{ {
if (index < 0) if (index < 0)
{ {
@ -200,31 +211,31 @@ namespace Billing.Views
var today = DateTime.Today; var today = DateTime.Today;
switch (index) switch (index)
{ {
case 0: // monthly case DateType.Monthly:
StartDate = today.AddDays(1 - today.Day); StartDate = today.AddDays(1 - today.Day);
EndDate = today.AddDays(DateTime.DaysInMonth(today.Year, today.Month) - today.Day).LastMoment(); EndDate = today.AddDays(DateTime.DaysInMonth(today.Year, today.Month) - today.Day).LastMoment();
break; break;
case 1: // today case DateType.Today:
StartDate = today; StartDate = today;
EndDate = today.LastMoment(); EndDate = today.LastMoment();
break; break;
case 2: // past month case DateType.PastMonth:
StartDate = today.AddMonths(-1).AddDays(1); StartDate = today.AddMonths(-1).AddDays(1);
EndDate = today.LastMoment(); EndDate = today.LastMoment();
break; break;
case 3: // past quarter case DateType.PastQuarter:
StartDate = today.AddMonths(-3).AddDays(1); StartDate = today.AddMonths(-3).AddDays(1);
EndDate = today.LastMoment(); EndDate = today.LastMoment();
break; break;
case 4: // past six months case DateType.PastSixMonths:
StartDate = today.AddMonths(-6).AddDays(1); StartDate = today.AddMonths(-6).AddDays(1);
EndDate = today.LastMoment(); EndDate = today.LastMoment();
break; break;
case 5: // past year case DateType.PastYear:
StartDate = today.AddYears(-1).AddDays(1); StartDate = today.AddYears(-1).AddDays(1);
EndDate = today.LastMoment(); EndDate = today.LastMoment();
break; break;
case 6: // total case DateType.Total:
//StartDate = App.Bills.Min(b => b.CreateTime).Date; //StartDate = App.Bills.Min(b => b.CreateTime).Date;
//EndDate = App.Bills.Max(b => b.CreateTime).Date.LastMoment(); //EndDate = App.Bills.Max(b => b.CreateTime).Date.LastMoment();
DateTime min = DateTime.MaxValue; DateTime min = DateTime.MaxValue;
@ -257,6 +268,11 @@ namespace Billing.Views
private void OnLeftCommand() private void OnLeftCommand()
{ {
var type = (DateType)SegmentDate;
if (type < DateType.Monthly || type >= DateType.Total)
{
return;
}
if (scroller.ScrollY > 0) if (scroller.ScrollY > 0)
{ {
scroller.ScrollToAsync(0, 0, true); scroller.ScrollToAsync(0, 0, true);
@ -264,11 +280,31 @@ namespace Billing.Views
isFreezed = true; isFreezed = true;
var start = StartDate; var start = StartDate;
var end = EndDate; var end = EndDate;
if (IsPreset(start, end)) if (type == DateType.Monthly || IsPreset(start, end))
{ {
start = start.AddMonths(-1); start = start.AddMonths(-1);
end = start.AddDays(DateTime.DaysInMonth(start.Year, start.Month) - 1).LastMoment(); end = start.AddDays(DateTime.DaysInMonth(start.Year, start.Month) - 1).LastMoment();
} }
else if (type == DateType.PastMonth)
{
start = start.AddMonths(-1);
end = end.AddMonths(-1);
}
else if (type == DateType.PastQuarter)
{
start = start.AddMonths(-3);
end = end.AddMonths(-3);
}
else if (type == DateType.PastSixMonths)
{
start = start.AddMonths(-6);
end = end.AddMonths(-6);
}
else if (type == DateType.PastYear)
{
start = start.AddYears(-1);
end = end.AddYears(-1);
}
else else
{ {
var days = (end.Date - start.Date).TotalDays + 1; var days = (end.Date - start.Date).TotalDays + 1;
@ -288,6 +324,11 @@ namespace Billing.Views
private void OnRightCommand() private void OnRightCommand()
{ {
var type = (DateType)SegmentDate;
if (type < DateType.Monthly || type >= DateType.Total)
{
return;
}
if (scroller.ScrollY > 0) if (scroller.ScrollY > 0)
{ {
scroller.ScrollToAsync(0, 0, true); scroller.ScrollToAsync(0, 0, true);
@ -295,11 +336,31 @@ namespace Billing.Views
isFreezed = true; isFreezed = true;
var start = StartDate; var start = StartDate;
var end = EndDate; var end = EndDate;
if (IsPreset(start, end)) if (type == DateType.Monthly || IsPreset(start, end))
{ {
start = start.AddMonths(1); start = start.AddMonths(1);
end = start.AddDays(DateTime.DaysInMonth(start.Year, start.Month) - 1).LastMoment(); end = start.AddDays(DateTime.DaysInMonth(start.Year, start.Month) - 1).LastMoment();
} }
else if (type == DateType.PastMonth)
{
start = start.AddMonths(1);
end = end.AddMonths(1);
}
else if (type == DateType.PastQuarter)
{
start = start.AddMonths(3);
end = end.AddMonths(3);
}
else if (type == DateType.PastSixMonths)
{
start = start.AddMonths(6);
end = end.AddMonths(6);
}
else if (type == DateType.PastYear)
{
start = start.AddYears(1);
end = end.AddYears(1);
}
else else
{ {
var days = (end.Date - start.Date).TotalDays + 1; var days = (end.Date - start.Date).TotalDays + 1;