Skip to content

Commit

Permalink
Ensure that carry over weight comes from an exercise with the same nu…
Browse files Browse the repository at this point in the history
…mber of reps
  • Loading branch information
LiamMorrow committed Oct 3, 2024
1 parent cc47b87 commit 4a61021
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
44 changes: 23 additions & 21 deletions LiftLog.Lib/Models/BlueprintModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,36 @@ public record ExerciseBlueprint(
string Notes
);

public sealed record KeyedExerciseBlueprint : IEquatable<KeyedExerciseBlueprint>
public record KeyedExerciseBlueprint(string Name, int Sets, int RepsPerSet)
{
private readonly string normalizedName = string.Empty;
public string Name { get; }
public static implicit operator KeyedExerciseBlueprint(ExerciseBlueprint e) =>
new(e.Name, e.Sets, e.RepsPerSet);

public KeyedExerciseBlueprint(string name)
public class NormalizedNameOnlyEqualityComparer : IEqualityComparer<KeyedExerciseBlueprint>
{
Name = name;
normalizedName = NormalizeName(name);
}

public static implicit operator KeyedExerciseBlueprint(ExerciseBlueprint e) => new(e.Name);
public static readonly NormalizedNameOnlyEqualityComparer Instance = new();

public bool Equals(KeyedExerciseBlueprint? other) => other?.normalizedName == normalizedName;
public bool Equals(KeyedExerciseBlueprint? x, KeyedExerciseBlueprint? y) =>
NormalizeName(x?.Name) == NormalizeName(y?.Name);

public override int GetHashCode() => normalizedName.GetHashCode();
public int GetHashCode(KeyedExerciseBlueprint obj) => NormalizeName(obj.Name).GetHashCode();

private static string NormalizeName(string name)
{
var lowerName = name.ToLower().Trim().Replace("flies", "flys").Replace("flyes", "flys");
var withoutPlural = lowerName switch
private static string NormalizeName(string? name)
{
string when lowerName.EndsWith("es") => lowerName[..^2],
string when lowerName.EndsWith('s') => lowerName[..^1],
_ => lowerName,
};

return withoutPlural;
if (name is null)
{
return string.Empty;
}
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;
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion LiftLog.Ui/Services/ProgressRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ public ValueTask<
))
.ToAsyncEnumerable()
)
.GroupBy(x => (KeyedExerciseBlueprint)x.RecordedExercise.Blueprint)
.GroupBy(
x => (KeyedExerciseBlueprint)x.RecordedExercise.Blueprint,
KeyedExerciseBlueprint.NormalizedNameOnlyEqualityComparer.Instance
)
.ToImmutableDictionaryAwaitAsync(
x => ValueTask.FromResult(x.Key),
async x => await x.Take(maxRecordsPerExercise).ToImmutableListValueAsync()
Expand Down
10 changes: 8 additions & 2 deletions LiftLog.Ui/Store/Stats/StatsEffects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ state.Value.OverallViewSessionName is null
ex
))
)
.GroupBy(x => new KeyedExerciseBlueprint(x.RecordedExercise.Blueprint.Name))
.GroupBy(
x => (KeyedExerciseBlueprint)x.RecordedExercise.Blueprint,
KeyedExerciseBlueprint.NormalizedNameOnlyEqualityComparer.Instance
)
.Select(CreateExerciseStatistic)
.ToImmutableList();

Expand Down Expand Up @@ -103,7 +106,10 @@ state.Value.OverallViewSessionName is null
var exerciseMostTimeSpent = sessions
.SelectMany(x => x.RecordedExercises)
.Where(x => x.LastRecordedSet?.Set is not null)
.GroupBy(x => new KeyedExerciseBlueprint(x.Blueprint.Name))
.GroupBy(
x => (KeyedExerciseBlueprint)x.Blueprint,
KeyedExerciseBlueprint.NormalizedNameOnlyEqualityComparer.Instance
)
.Select(x => new TimeSpentExercise(
x.First().Blueprint.Name,
x.Select(x => x.TimeSpent).Aggregate((a, b) => a + b)
Expand Down

0 comments on commit 4a61021

Please sign in to comment.