Skip to content

Commit

Permalink
Convert the session component to a smart component
Browse files Browse the repository at this point in the history
This simplifies usage, and consolidates the various ways of working with sessions
  • Loading branch information
LiamMorrow committed Jul 4, 2024
1 parent 8498118 commit ee8c1d6
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public CurrentSessionState ToModel() =>
IsHydrated: true,
WorkoutSession: WorkoutSession?.ToModel(),
HistorySession: HistorySession?.ToModel(),
FeedSession: null,
LatestSetTimerNotificationId: LatestSetTimerNotificationId
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public CurrentSessionState ToModel() =>
IsHydrated: true,
WorkoutSession?.ToModel(),
HistorySession?.ToModel(),
FeedSession: null,
LatestSetTimerNotificationId
);
}
86 changes: 19 additions & 67 deletions LiftLog.Ui/Pages/Feed/ViewFeedSessionPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,26 @@

@inject IState<FeedState> FeedState
@inject IState<ProgramState> ProgramState
@inject IState<CurrentSessionState> CurrentSessionState
@inject NavigationManager NavigationManager
@inject IDispatcher Dispatcher

@inherits Fluxor.Blazor.Web.Components.FluxorComponent

@if (session is not null)
@if (CurrentSessionState.Value.FeedSession is not null)
{
<div class="pb-2">
<SessionComponent
CycleRepcountForExercise=@((_, _) => { })
SetRepCountForExercise="@((_, _, _) => { })"
UpdateWeightForExercise=@((_, _) => { })
Session=session
SaveSession=@(() => { })
CloseSession=@(() => { })
PreviouslyCompleted=@(ImmutableDictionary<KeyedExerciseBlueprint, ImmutableListValue<DatedRecordedExercise>>.Empty)
RemoveExercise=@((_) => { })
EditExercise="@((_, _) => { })"
AddExercise="@((_) => { })"
UpdateNotesForExercise="@((_, _) => { })"
ShowRestTimer="false"
ShowBodyweight="session.Bodyweight is not null"
ToggleExercisePerSetWeight=@((_) => { })
UpdateWeightForSet=@((_, _, _) => { })
SessionInPlan=@(ProgramState.Value.GetActivePlanSessionBlueprints().Contains(session.Blueprint))
SessionWithSameNameInPlan=@(ProgramState.Value.GetActivePlanSessionBlueprints().Any(x => x.Name == session.Blueprint.Name))
AddSessionToPlan="AddSessionToPlan"
UpdateSessionInPlan="UpdateSessionInPlan"
UpdateBodyweight="@(_ => { })"
IsReadonly=true
Username="@username"/>
</div>
<SessionComponent
SessionTarget="SessionTarget.FeedSession"
NavigateOnClose="/feed"
PreviouslyCompleted=@(ImmutableDictionary<KeyedExerciseBlueprint, ImmutableListValue<DatedRecordedExercise>>.Empty)
ShowRestTimer="false"
ShowBodyweight="CurrentSessionState.Value.FeedSession.Bodyweight is not null"
IsReadonly=true
Username="@username"/>
}

@code
{

Session? session;

[Parameter] public string EventId { get; set; } = "";

string? username;
Expand All @@ -52,59 +33,30 @@
Dispatcher.Dispatch(new SetPageTitleAction("Feed Session"));
Dispatcher.Dispatch(new SetBackNavigationUrlAction("/feed"));

var hasSession = false;

if (Guid.TryParse(EventId, out var eventId))
{
var feedItem = FeedState
.Value.Feed.OfType<SessionFeedItem>()
.FirstOrDefault(x => x.EventId == eventId);
if(feedItem is not null)
{
session = feedItem.Session;
hasSession = true;
Dispatcher.Dispatch(new SetCurrentSessionAction(SessionTarget.FeedSession, feedItem.Session));
username = FeedState.Value.FollowedUsers.TryGetValue(feedItem.UserId, out var user) ? user.DisplayName : null;
}
if (session is not null)
Dispatcher.Dispatch(new SetPageTitleAction(session.Blueprint.Name));
if (CurrentSessionState.Value.FeedSession is not null)
Dispatcher.Dispatch(new SetPageTitleAction(CurrentSessionState.Value.FeedSession.Blueprint.Name));
}

if (session is null)
Console.WriteLine("{0} {1}", hasSession, CurrentSessionState.Value.FeedSession);

if (!hasSession)
{
NavigationManager.NavigateTo("/feed");
}

base.OnInitialized();
}


private void AddSessionToPlan()
{
if (session?.Blueprint is SessionBlueprint blueprint)
{
var activePlanId = ProgramState.Value.ActivePlanId;
var existingSessionWithSameName = ProgramState.Value.GetSessionBlueprints(activePlanId).Any(x => x.Name == blueprint.Name);
if (existingSessionWithSameName)
{
var blueprintNameWithoutNumber = System.Text.RegularExpressions.Regex.Replace(blueprint.Name, @" \(\d+\)$", "");
var newName = blueprint.Name;
var i = 1;
while (ProgramState.Value.GetSessionBlueprints(activePlanId).Any(x => x.Name == newName))
{
newName = $"{blueprintNameWithoutNumber} ({i})";
i++;
}

blueprint = blueprint with { Name = newName };
}

Dispatcher.Dispatch(new AddProgramSessionAction(activePlanId, blueprint));
}
}

private void UpdateSessionInPlan()
{
if (session?.Blueprint is SessionBlueprint blueprint)
{
var blueprintIndex = ProgramState.Value.GetSessionBlueprints(ProgramState.Value.ActivePlanId).IndexedTuples().First(x => x.Item.Name == blueprint.Name).Index;
Dispatcher.Dispatch(new SetProgramSessionAction(ProgramState.Value.ActivePlanId, blueprintIndex, blueprint));
}
}
}
103 changes: 3 additions & 100 deletions LiftLog.Ui/Pages/History/HistoryEditPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
@inject IState<CurrentSessionState> CurrentSessionState
@inject IState<ProgramState> ProgramState
@inject ProgressRepository ProgressRepository
@inject IState<SettingsState> SettingsState
@inject IDispatcher Dispatcher
@inject IState<SettingsState> SettingsState;
@inject NavigationManager NavigationManager

@if (CurrentSessionState.Value.HistorySession is not null)
Expand All @@ -26,25 +26,10 @@
</div>
<SessionComponent
ShowRestTimer=false
CycleRepcountForExercise=CycleRepcountForExercise
SetRepCountForExercise="SetRepCountForExercise"
UpdateWeightForExercise=UpdateWeightForExercise
Session=@(CurrentSessionState.Value.HistorySession)
SaveSession=SaveSession
CloseSession=CloseSession
PreviouslyCompleted=previouslyCompleted
RemoveExercise=RemoveExercise
EditExercise="EditExercise"
AddExercise="AddExercise"
UpdateNotesForExercise="UpdateNotesForExercise"
ShowBodyweight="SettingsState.Value.ShowBodyweight"
ToggleExercisePerSetWeight=ToggleExercisePerSetWeight
UpdateWeightForSet=UpdateWeightForSet
SessionInPlan=@(ProgramState.Value.GetActivePlanSessionBlueprints().Contains(CurrentSessionState.Value.HistorySession.Blueprint))
SessionWithSameNameInPlan=@(ProgramState.Value.GetActivePlanSessionBlueprints().Any(x => x.Name == CurrentSessionState.Value.HistorySession.Blueprint.Name))
AddSessionToPlan="AddSessionToPlan"
UpdateSessionInPlan="UpdateSessionInPlan"
UpdateBodyweight="UpdateBodyweight"/>
SessionTarget=@SessionTarget.HistorySession
NavigateOnClose="/history" />
}

@code
Expand All @@ -58,88 +43,6 @@
);
}

private void SetRepCountForExercise(int exerciseIndex, int setIndex, int? repCount)
=> Dispatcher.Dispatch(new SetExerciseRepsAction(SessionTarget.HistorySession, exerciseIndex, setIndex, repCount));

private void CycleRepcountForExercise(int exerciseIndex, int setIndex)
=> Dispatcher.Dispatch(new CycleExerciseRepsAction(SessionTarget.HistorySession, exerciseIndex, setIndex));

private void UpdateWeightForExercise(int exerciseIndex, decimal weight)
=> Dispatcher.Dispatch(new UpdateExerciseWeightAction(SessionTarget.HistorySession, exerciseIndex, weight));

private void EditExercise(int exerciseIndex, ExerciseBlueprint exercise)
=> Dispatcher.Dispatch(new EditExerciseInActiveSessionAction(SessionTarget.HistorySession, exerciseIndex, exercise));

private void AddExercise(ExerciseBlueprint exercise)
=> Dispatcher.Dispatch(new AddExerciseToActiveSessionAction(SessionTarget.HistorySession, exercise));

private void RemoveExercise(int exerciseIndex)
=> Dispatcher.Dispatch(new RemoveExerciseFromActiveSessionAction(SessionTarget.HistorySession, exerciseIndex));

private void UpdateNotesForExercise(int exerciseIndex, string? notes)
=> Dispatcher.Dispatch(new UpdateNotesForExerciseAction(SessionTarget.HistorySession, exerciseIndex, notes));

private void ToggleExercisePerSetWeight(int exerciseIndex)
=> Dispatcher.Dispatch(new ToggleExercisePerSetWeightAction(SessionTarget.HistorySession, exerciseIndex));

private void UpdateWeightForSet(int exerciseIndex, int setIndex, decimal weight)
=> Dispatcher.Dispatch(new UpdateWeightForSetAction(SessionTarget.HistorySession, exerciseIndex, setIndex, weight));

private void UpdateBodyweight(decimal? bodyweight)
=> Dispatcher.Dispatch(new UpdateBodyweightAction(SessionTarget.HistorySession, bodyweight));

private void AddSessionToPlan()
{
if (CurrentSessionState.Value.HistorySession?.Blueprint is SessionBlueprint blueprint)
{
var existingSessionWithSameName = ProgramState.Value.GetActivePlanSessionBlueprints().Any(x => x.Name == blueprint.Name);
if (existingSessionWithSameName)
{
var blueprintNameWithoutNumber = System.Text.RegularExpressions.Regex.Replace(blueprint.Name, @" \(\d+\)$", "");
var newName = blueprint.Name;
var i = 1;
while (ProgramState.Value.GetActivePlanSessionBlueprints().Any(x => x.Name == newName))
{
newName = $"{blueprintNameWithoutNumber} ({i})";
i++;
}

blueprint = blueprint with { Name = newName };
}

Dispatcher.Dispatch(new AddProgramSessionAction(ProgramState.Value.ActivePlanId, blueprint));
Dispatcher.Dispatch(new SetCurrentSessionAction(SessionTarget.HistorySession, CurrentSessionState.Value.HistorySession with { Blueprint = blueprint }));
Dispatcher.Dispatch(new SetPageTitleAction(blueprint.Name));
}
}

private void UpdateSessionInPlan()
{
if (CurrentSessionState.Value.HistorySession?.Blueprint is SessionBlueprint blueprint)
{
var blueprintIndex = ProgramState.Value.GetActivePlanSessionBlueprints().IndexedTuples().First(x => x.Item.Name == blueprint.Name).Index;
Dispatcher.Dispatch(new SetProgramSessionAction(ProgramState.Value.ActivePlanId, blueprintIndex, blueprint));
}
}

private void SaveSession()
{
Dispatcher.Dispatch(new PersistCurrentSessionAction(SessionTarget.HistorySession));
if (CurrentSessionState.Value.HistorySession is not null)
{
Dispatcher.Dispatch(new AddUnpublishedSessionIdAction(CurrentSessionState.Value.HistorySession.Id));
}
Dispatcher.Dispatch(new SetCurrentSessionAction(SessionTarget.HistorySession, null));
Dispatcher.Dispatch(new SetStatsIsDirtyAction(true));

NavigationManager.NavigateTo("/history");
}

private void CloseSession()
{
NavigationManager.NavigateTo("/history");
}

protected override void OnInitialized()
{
Dispatcher.Dispatch(new SetPageTitleAction("Session"));
Expand Down
Loading

0 comments on commit ee8c1d6

Please sign in to comment.