Skip to content

Commit

Permalink
Remember calendar position + add history cards
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamMorrow committed Aug 8, 2024
1 parent 8824be1 commit 5327f98
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 59 deletions.
37 changes: 23 additions & 14 deletions LiftLog.Ui/Pages/History/HistoryPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@inject SessionService SessionService
@inject NavigationManager NavigationManager
@inject IState<CurrentSessionState> CurrentSessionState
@inject IState<AppState> AppState
@inject IState<ProgramState> ProgramState
@inject IDispatcher Dispatcher
@inject ILogger<HistoryPage> Logger
Expand All @@ -25,26 +26,25 @@ else
{
<Card class="mx-2" Type=Card.CardType.Filled>
<HistoryCalendar
CurrentMonth="@currentMonth.Month"
CurrentYear="@currentMonth.Year"
Sessions="_latestSessions"
OnSessionClick="HandleSessionClick"
OnMonthChange="HandleMonthChange"
OnDateSelect=CreateSessionAtDate
OnSessionLongPress=@((session)=>{_selectedSession = session; _deleteDialog?.Open();}) />
</Card>
<ItemList CardType=Card.CardType.Filled Items="filteredToMonthSessions" TItem="Session" Dividers=false >
<button class="p-6 relative w-full" @onclick=@(()=>HandleSessionClick(context)) @oncontextmenu=@(()=>{_selectedSession = context; _deleteDialog?.Open();}) @oncontextmenu:preventDefault=true >
<md-ripple></md-ripple>
<SplitCardControl>
<TitleContent>
<SessionSummaryTitle IsFilled="true" Session="context" ></SessionSummaryTitle>
</TitleContent>
<MainContent>
<SessionSummary Session="context" ShowSets="true"></SessionSummary>
</MainContent>
<CardList CardType=Card.CardType.Outlined Items="filteredToMonthSessions" TItem="Session" OnClick="HandleSessionClick" OnContextMenu=@((item)=>{_selectedSession = item; _deleteDialog?.Open();}) >
<SplitCardControl>
<TitleContent>
<SessionSummaryTitle IsFilled="true" Session="context" ></SessionSummaryTitle>
</TitleContent>
<MainContent>
<SessionSummary Session="context" ShowSets="true"></SessionSummary>
</MainContent>

</SplitCardControl>
</button>
</ItemList>
</SplitCardControl>
</CardList>
}

<ConfirmationDialog @ref="_deleteDialog" OkText="Delete" OnOk=DeleteSession>
Expand Down Expand Up @@ -87,6 +87,7 @@ else
{
currentMonth = date;
filteredToMonthSessions = _latestSessions.Where(s => s.Date.Month == date.Month && s.Date.Year == date.Year).ToList();
Dispatcher.Dispatch(new SetHistoryYearMonthAction(date.Year, date.Month));
}

private void CreateSessionAtDate(DateOnly date)
Expand All @@ -105,7 +106,15 @@ else
this._latestSessions = await SessionService
.GetLatestSessionsAsync()
.ToListAsync();
HandleMonthChange(DateOnly.FromDateTime(DateTime.Now));

if (AppState.Value.HistoryYearMonth is (int year, int month))
{
HandleMonthChange(new DateOnly(year, month, 1));
}
else
{
HandleMonthChange(DateOnly.FromDateTime(DateTime.Now));
}
await base.OnInitializedAsync();
sw.Stop();
Logger.LogInformation($"History page initialized in {sw.ElapsedMilliseconds} ms");
Expand Down
7 changes: 6 additions & 1 deletion LiftLog.Ui/Shared/Presentation/Card.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

<div
class="@(AdditionalAttributes?.GetValueOrDefault("class")) @(HasPadding ?"p-5":"") relative @Style rounded-card text-on-surface card"
@onclick="HandleOnClick">
@onclick="HandleOnClick"
@oncontextmenu=OnContextMenu @oncontextmenu:preventDefault=@(OnContextMenu.HasDelegate)
>

@if (OnClick != null)
{
<md-ripple></md-ripple>
Expand All @@ -20,6 +23,8 @@
[Parameter] public Action<MouseEventArgs>? OnClick { get; set; }
[Parameter] public EventCallback OnContextMenu { get; set; }
[Parameter] public bool IsHighlighted { get; set; }
[Parameter] public CardType Type { get; set; } = CardType.Outlined;
Expand Down
9 changes: 8 additions & 1 deletion LiftLog.Ui/Shared/Presentation/CardList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<div @attributes="AdditionalAttributes" class=" @(AdditionalAttributes?.GetValueOrDefault("class")) flex flex-col gap-2 p-2 cardlist">
@foreach (var item in Items)
{
<Card class="@CardClass" Type="CardType" IsHighlighted="ShouldHighlight?.Invoke(item) ?? false" OnClick=@(OnClick != null ? _ => OnClick(item) : null)>
<Card
class="@CardClass"
Type="CardType"
OnContextMenu=@(()=>OnContextMenu.InvokeAsync(item))
IsHighlighted="ShouldHighlight?.Invoke(item) ?? false"
OnClick=@(OnClick != null ? _ => OnClick(item) : null)>
@ChildContent?.Invoke(item)
</Card>
}
Expand All @@ -16,6 +21,8 @@

[Parameter] public Action<TItem>? OnClick { get; set; }

[Parameter] public EventCallback<TItem> OnContextMenu { get; set; }

[Parameter] public Func<TItem, bool>? ShouldHighlight { get; set; }

[Parameter] public string? CardClass { get; set; }
Expand Down
47 changes: 27 additions & 20 deletions LiftLog.Ui/Shared/Presentation/HistoryCalendar.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


@{
var firstDayOfMonth = new DateOnly(_currentYear, _currentMonth, 1);
var firstDayOfMonth = new DateOnly(CurrentYear, CurrentMonth, 1);
var firstDayOfWeek = (int)firstDayOfMonth.DayOfWeek;
var lastDayOfPreviousMonth = firstDayOfMonth.AddDays(-1);
var index = 0;
Expand All @@ -12,7 +12,7 @@
<IconButton data-cy="calendar-nav-previous-month" Type=IconButtonType.Standard @onclick="PreviousMonth" Icon="chevron_left" />
</div>
<div class="col-span-5 flex my-2 justify-center items-center">
<h2 data-cy="calendar-month">@DateTimeFormatInfo.CurrentInfo.GetMonthName(_currentMonth) @_currentYear</h2>
<h2 data-cy="calendar-month">@DateTimeFormatInfo.CurrentInfo.GetMonthName(CurrentMonth) @CurrentYear</h2>
</div>
<div class="col-span-1 my-2 text-right">
<IconButton Type=IconButtonType.Standard @onclick="NextMonth" disabled=@DisableNext Icon="chevron_right" />
Expand All @@ -28,24 +28,31 @@
@for (int i = 0; i < firstDayOfWeek; i++)
{
var date = lastDayOfPreviousMonth.AddDays(-i);
<HistoryCalendarDay @key=@(date.ToString()+index) Index="index++" Day=date Sessions=_sessionsByDate[date] OnDayClick=@(()=>HandleDayClick(date)) OnDayLongPress=@(()=>HandleDayLongPress(date)) />
<HistoryCalendarDay ForOtherMonth=true @key=@(date.ToString()+index) Index="index++" Day=date Sessions=_sessionsByDate[date] OnDayClick=@(()=>HandleDayClick(date)) OnDayLongPress=@(()=>HandleDayLongPress(date)) />
}
@for (int i = 1; i <= DateTime.DaysInMonth(_currentYear, _currentMonth); i++)
@for (int i = 1; i <= DateTime.DaysInMonth(CurrentYear, CurrentMonth); i++)
{
var date = new DateOnly(_currentYear, _currentMonth, i);
var date = new DateOnly(CurrentYear, CurrentMonth, i);

<HistoryCalendarDay @key=@(date.ToString()+index) Index="index++" Day=date Sessions=_sessionsByDate[date] OnDayClick=@(()=>HandleDayClick(date)) OnDayLongPress=@(()=>HandleDayLongPress(date)) />
}

@{var extraDay =0;}
@while(index % 7 != 0)
{
var date =new DateOnly(CurrentYear, CurrentMonth, DateTime.DaysInMonth(CurrentYear, CurrentMonth)).AddDays(++extraDay);
<HistoryCalendarDay ForOtherMonth=true @key=@(date.ToString()+index) Index="index++" Day=date Sessions=_sessionsByDate[date] OnDayClick=@(()=>HandleDayClick(date)) OnDayLongPress=@(()=>HandleDayLongPress(date)) />
}

</div>

@code {
private int _currentMonth = DateTime.Now.Month;
private int _currentYear = DateTime.Now.Year;
[Parameter][EditorRequired] public int CurrentMonth {get;set;}
[Parameter][EditorRequired] public int CurrentYear {get;set;}
private ILookup<DateOnly, Session> _sessionsByDate = null!;
private bool DisableNext => _currentYear == DateTime.Now.Year && _currentMonth == DateTime.Now.Month;
private bool DisableNext => CurrentYear == DateTime.Now.Year && CurrentMonth == DateTime.Now.Month;

[Parameter]
[EditorRequired]
Expand Down Expand Up @@ -76,37 +83,37 @@
if(DisableNext){
return;
}
if (_currentMonth == 12)
if (CurrentMonth == 12)
{
_currentMonth = 1;
_currentYear++;
CurrentMonth = 1;
CurrentYear++;
}
else
{
_currentMonth++;
CurrentMonth++;
}
OnMonthChange.InvokeAsync(new DateOnly(_currentYear, _currentMonth, 1));
OnMonthChange.InvokeAsync(new DateOnly(CurrentYear, CurrentMonth, 1));
}

private void PreviousMonth()
{
if (_currentMonth == 1)
if (CurrentMonth == 1)
{
_currentMonth = 12;
_currentYear--;
CurrentMonth = 12;
CurrentYear--;
}
else
{
_currentMonth--;
CurrentMonth--;
}

OnMonthChange.InvokeAsync(new DateOnly(_currentYear, _currentMonth, 1));
OnMonthChange.InvokeAsync(new DateOnly(CurrentYear, CurrentMonth, 1));
}

private void GoToToday()
{
_currentMonth = DateTime.Now.Month;
_currentYear = DateTime.Now.Year;
CurrentMonth = DateTime.Now.Month;
CurrentYear = DateTime.Now.Year;
}

private async Task HandleDayLongPress(DateOnly date)
Expand Down
3 changes: 3 additions & 0 deletions LiftLog.Ui/Shared/Presentation/HistoryCalendarDay.razor
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
[EditorRequired]
public EventCallback OnDayLongPress {get;set;}

[Parameter]
public bool ForOtherMonth { get;set; }

string AnimationClass = "scale-0 animate-zoom-in";

bool isHolding = false;
Expand Down
45 changes: 24 additions & 21 deletions LiftLog.Ui/Shared/Presentation/SessionSummary.razor
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@

<div class="flex flex-col gap-y-1 text-start" data-cy="session-summary">
<div class="flex flex-col gap-y-1.5 text-start" data-cy="session-summary">
@foreach (var exercise in Session.RecordedExercises)
{
var splitWeights = exercise.PerSetWeight && !exercise.PotentialSets.All(s => s.Weight == exercise.Weight);
<span class="flex flex-wrap items-center">
<span>@exercise.Blueprint.Name</span>
@if(ShowSets || ShowWeight)
{
<span class="flex justify-around ml-auto items-center gap-0.5 bg-surface-container-highest text-on-surface-variant rounded-md py-1 px-2">

@if (ShowSets && !splitWeights)
<span class="flex items-center">
<span>@exercise.Blueprint.Name</span>
@if(ShowSets || ShowWeight)
{
@if(!splitWeights)
{
<span>
@(exercise.Blueprint.Sets)x@(exercise.Blueprint.RepsPerSet)
</span>
@if(ShowWeight)
<span class="flex justify-around ml-auto items-center gap-0.5 bg-surface-container-highest text-on-surface-variant rounded-md py-1 px-2">

@if (ShowSets)
{
<span class="text-2xs">@@</span>
<span>
@(exercise.Blueprint.Sets)x@(exercise.Blueprint.RepsPerSet)
</span>
@if(ShowWeight)
{
<span class="text-2xs">@@</span>
}
}
}

@if (ShowWeight && !splitWeights)
{
<WeightFormat Weight="@exercise.Weight"/>
@if (ShowWeight)
{
<WeightFormat Weight="@exercise.Weight"/>
}
</span>
}

@if (ShowWeight && splitWeights)
{
<span class="flex flex-wrap gap-1 items-center ml-auto">
<span class="flex ml-auto flex-wrap justify-end gap-1">
@foreach(var set in exercise.PotentialSets)
{
<span class="flex gap-0.5 items-center">
<span class="flex items-center gap-0.5 bg-surface-container-highest text-on-surface-variant rounded-md py-1 px-2">
@if (ShowSets){
<span>@(set.Set?.RepsCompleted ?? exercise.Blueprint.RepsPerSet)</span><span class="text-2xs">@@</span>
}
<WeightFormat Weight="@set.Weight"/>
</span>
<span class="divider [&:last-child]:hidden bg-outline-variant self-stretch my-1 w-[1px]"></span>
}
</span>
}
</span>
}
}
</span>
}
</div>
Expand Down
2 changes: 2 additions & 0 deletions LiftLog.Ui/Store/App/AppActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public record ToastAction(string Message);

public record SetBackNavigationUrlAction(string? BackNavigationUrl);

public record SetHistoryYearMonthAction(int Year, int Month);

public record NavigateAction(
string Path,
bool ClearPageStack = true,
Expand Down
3 changes: 2 additions & 1 deletion LiftLog.Ui/Store/App/AppFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected override AppState GetInitialState() =>
HasRequestedNotificationPermission: false,
ColorScheme: new AppColorScheme<uint>(),
AppLaunchCount: 0,
AppRatingResult: AppRatingResult.NotRated
AppRatingResult: AppRatingResult.NotRated,
HistoryYearMonth: null
);
}
7 changes: 7 additions & 0 deletions LiftLog.Ui/Store/App/AppReducers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ state with
ProState = new(action.ProToken)
};

[ReducerMethod]
public static AppState SetHistoryYearMonth(AppState state, SetHistoryYearMonthAction action) =>
state with
{
HistoryYearMonth = (action.Year, action.Month)
};

[ReducerMethod]
public static AppState SetThemeColor(AppState state, ThemeColorUpdatedAction action) =>
state with
Expand Down
3 changes: 2 additions & 1 deletion LiftLog.Ui/Store/App/AppState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public record AppState(
bool HasRequestedNotificationPermission,
AppColorScheme<uint> ColorScheme,
int AppLaunchCount,
AppRatingResult AppRatingResult
AppRatingResult AppRatingResult,
(int Year, int Month)? HistoryYearMonth
);

public enum AppRatingResult
Expand Down

0 comments on commit 5327f98

Please sign in to comment.