From 7527026bc146075c00a758aa512aebd20aa45c1e Mon Sep 17 00:00:00 2001 From: Li Date: Wed, 2 Oct 2024 17:09:37 +1000 Subject: [PATCH] Include exercises with different reps in previous history This commit relaxes the restriction of having to have the exact same name/reps/sets to show up in the exercise history list --- LiftLog.Lib/Models/BlueprintModels.cs | 31 +++++++++++++++++++++++--- LiftLog.Ui/Store/Stats/StatsEffects.cs | 17 ++------------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/LiftLog.Lib/Models/BlueprintModels.cs b/LiftLog.Lib/Models/BlueprintModels.cs index 9db4f946..7bc9c432 100644 --- a/LiftLog.Lib/Models/BlueprintModels.cs +++ b/LiftLog.Lib/Models/BlueprintModels.cs @@ -49,10 +49,35 @@ public record ExerciseBlueprint( string Notes ); -public record KeyedExerciseBlueprint(string Name, int Sets, int RepsPerSet) +public sealed record KeyedExerciseBlueprint : IEquatable { - public static implicit operator KeyedExerciseBlueprint(ExerciseBlueprint e) => - new(e.Name, e.Sets, e.RepsPerSet); + private readonly string normalizedName = string.Empty; + public string Name { get; } + + public KeyedExerciseBlueprint(string name) + { + Name = name; + normalizedName = NormalizeName(name); + } + + public static implicit operator KeyedExerciseBlueprint(ExerciseBlueprint e) => new(e.Name); + + public bool Equals(KeyedExerciseBlueprint? other) => other?.normalizedName == normalizedName; + + public override int GetHashCode() => normalizedName.GetHashCode(); + + private static string NormalizeName(string name) + { + var lowerName = name.ToLower().Trim().Replace("flies", "flys").Replace("flyes", "flys"); + var withoutPlural = lowerName switch + { + string when lowerName.EndsWith("es") => lowerName[..^2], + string when lowerName.EndsWith('s') => lowerName[..^1], + _ => lowerName, + }; + + return withoutPlural; + } } public record Rest(TimeSpan MinRest, TimeSpan MaxRest, TimeSpan FailureRest) diff --git a/LiftLog.Ui/Store/Stats/StatsEffects.cs b/LiftLog.Ui/Store/Stats/StatsEffects.cs index 0be8c724..c8ae9835 100644 --- a/LiftLog.Ui/Store/Stats/StatsEffects.cs +++ b/LiftLog.Ui/Store/Stats/StatsEffects.cs @@ -73,7 +73,7 @@ state.Value.OverallViewSessionName is null ex )) ) - .GroupBy(x => NormalizeName(x.RecordedExercise.Blueprint.Name)) + .GroupBy(x => new KeyedExerciseBlueprint(x.RecordedExercise.Blueprint.Name)) .Select(CreateExerciseStatistic) .ToImmutableList(); @@ -103,7 +103,7 @@ state.Value.OverallViewSessionName is null var exerciseMostTimeSpent = sessions .SelectMany(x => x.RecordedExercises) .Where(x => x.LastRecordedSet?.Set is not null) - .GroupBy(x => NormalizeName(x.Blueprint.Name)) + .GroupBy(x => new KeyedExerciseBlueprint(x.Blueprint.Name)) .Select(x => new TimeSpentExercise( x.First().Blueprint.Name, x.Select(x => x.TimeSpent).Aggregate((a, b) => a + b) @@ -134,19 +134,6 @@ state.Value.OverallViewSessionName is null }); } - private static string NormalizeName(string name) - { - var lowerName = name.ToLower().Trim().Replace("flies", "flys").Replace("flyes", "flys"); - var withoutPlural = lowerName switch - { - string when lowerName.EndsWith("es") => lowerName[..^2], - string when lowerName.EndsWith('s') => lowerName[..^1], - _ => lowerName, - }; - - return withoutPlural; - } - private static StatisticOverTime CreateBodyweightStatistic(IEnumerable sessions) { return new(