| | | 1 | | using Microsoft.AspNetCore.Components; |
| | | 2 | | using Pomodoro.Web.Services; |
| | | 3 | | |
| | | 4 | | namespace Pomodoro.Web.Components.History; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Code-behind for DateNavigator component |
| | | 8 | | /// Provides date navigation controls |
| | | 9 | | /// </summary> |
| | | 10 | | public class DateNavigatorBase : ComponentBase |
| | | 11 | | { |
| | | 12 | | #region Lifecycle Methods |
| | | 13 | | |
| | | 14 | | protected override async Task OnInitializedAsync() |
| | 102 | 15 | | { |
| | 102 | 16 | | await UpdateFormattedDate(); |
| | 102 | 17 | | await UpdateIsTodayAsync(); |
| | 102 | 18 | | await base.OnInitializedAsync(); |
| | 102 | 19 | | } |
| | | 20 | | |
| | | 21 | | protected override async Task OnParametersSetAsync() |
| | 114 | 22 | | { |
| | 114 | 23 | | await UpdateFormattedDate(); |
| | 114 | 24 | | await UpdateIsTodayAsync(); |
| | 114 | 25 | | await base.OnParametersSetAsync(); |
| | 114 | 26 | | } |
| | | 27 | | |
| | | 28 | | #endregion |
| | | 29 | | |
| | | 30 | | #region Services |
| | | 31 | | |
| | | 32 | | [Inject] |
| | 637 | 33 | | protected ILocalDateTimeService LocalDateTimeService { get; set; } = default!; |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region Parameters |
| | | 38 | | |
| | | 39 | | [Parameter] |
| | 553 | 40 | | public DateTime SelectedDate { get; set; } |
| | | 41 | | |
| | | 42 | | [Parameter] |
| | 105 | 43 | | public EventCallback<DateTime> OnDateChanged { get; set; } |
| | | 44 | | |
| | | 45 | | #endregion |
| | | 46 | | |
| | | 47 | | #region Properties |
| | | 48 | | |
| | 438 | 49 | | protected string FormattedDate { get; set; } = string.Empty; |
| | 456 | 50 | | protected bool IsTodayAsyncValue { get; set; } |
| | | 51 | | |
| | | 52 | | protected async Task<bool> IsTodayAsync() |
| | 216 | 53 | | { |
| | 216 | 54 | | var localDate = await LocalDateTimeService.GetLocalDateAsync(); |
| | 216 | 55 | | return SelectedDate.Date == localDate; |
| | 216 | 56 | | } |
| | | 57 | | |
| | 2 | 58 | | protected bool IsToday => SelectedDate.Date == DateTime.Now.Date; |
| | | 59 | | |
| | | 60 | | #endregion |
| | | 61 | | |
| | | 62 | | #region Actions |
| | | 63 | | |
| | | 64 | | protected async Task GoToPrevious() |
| | 4 | 65 | | { |
| | 4 | 66 | | var newDate = SelectedDate.AddDays(Constants.TimeConversion.DayOffsetPrevious); |
| | 4 | 67 | | await OnDateChanged.InvokeAsync(newDate); |
| | 4 | 68 | | } |
| | | 69 | | |
| | | 70 | | protected async Task GoToNext() |
| | 1 | 71 | | { |
| | 1 | 72 | | var newDate = SelectedDate.AddDays(Constants.TimeConversion.DayOffsetNext); |
| | 1 | 73 | | await OnDateChanged.InvokeAsync(newDate); |
| | 1 | 74 | | } |
| | | 75 | | |
| | | 76 | | protected async Task GoToToday() |
| | 1 | 77 | | { |
| | 1 | 78 | | var localDate = await LocalDateTimeService.GetLocalDateAsync(); |
| | 1 | 79 | | await OnDateChanged.InvokeAsync(localDate); |
| | 1 | 80 | | } |
| | | 81 | | |
| | | 82 | | #endregion |
| | | 83 | | |
| | | 84 | | #region Helper Methods |
| | | 85 | | |
| | | 86 | | protected async Task UpdateFormattedDate() |
| | 216 | 87 | | { |
| | 216 | 88 | | FormattedDate = await FormatDateAsync(SelectedDate); |
| | 216 | 89 | | } |
| | | 90 | | |
| | | 91 | | protected async Task UpdateIsTodayAsync() |
| | 216 | 92 | | { |
| | 216 | 93 | | IsTodayAsyncValue = await IsTodayAsync(); |
| | 216 | 94 | | } |
| | | 95 | | |
| | | 96 | | protected async Task<string> FormatDateAsync(DateTime date) |
| | 216 | 97 | | { |
| | 216 | 98 | | var today = await LocalDateTimeService.GetLocalDateAsync(); |
| | 216 | 99 | | var yesterday = today.AddDays(Constants.TimeConversion.DayOffsetPrevious); |
| | | 100 | | |
| | 216 | 101 | | if (date.Date == today) |
| | 168 | 102 | | return Constants.TimeFormats.TodayText; |
| | 48 | 103 | | if (date.Date == yesterday) |
| | 3 | 104 | | return Constants.TimeFormats.YesterdayText; |
| | | 105 | | |
| | 45 | 106 | | return date.ToString(Constants.TimeFormats.DateFormat); |
| | 216 | 107 | | } |
| | | 108 | | |
| | | 109 | | protected string FormatDate(DateTime date) |
| | 3 | 110 | | { |
| | 3 | 111 | | var today = DateTime.Now.Date; |
| | 3 | 112 | | var yesterday = today.AddDays(Constants.TimeConversion.DayOffsetPrevious); |
| | | 113 | | |
| | 3 | 114 | | if (date.Date == today) |
| | 1 | 115 | | return Constants.TimeFormats.TodayText; |
| | 2 | 116 | | if (date.Date == yesterday) |
| | 1 | 117 | | return Constants.TimeFormats.YesterdayText; |
| | | 118 | | |
| | 1 | 119 | | return date.ToString(Constants.TimeFormats.DateFormat); |
| | 3 | 120 | | } |
| | | 121 | | |
| | | 122 | | #endregion |
| | | 123 | | } |