From 493a3367c1a2faa0fdb77d0451bfa48b61ca787a Mon Sep 17 00:00:00 2001 From: abdullathedruid Date: Mon, 3 Jun 2024 16:36:38 +0100 Subject: [PATCH] feat: group by weight --- lib/gym_live_web/live/app/view_workout.ex | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/gym_live_web/live/app/view_workout.ex b/lib/gym_live_web/live/app/view_workout.ex index 075a121..420fd9c 100644 --- a/lib/gym_live_web/live/app/view_workout.ex +++ b/lib/gym_live_web/live/app/view_workout.ex @@ -20,6 +20,22 @@ defmodule GymLiveWeb.ViewWorkout do {:ok, socket} end + defp group_by_weight(sets), do: group_by_weight(sets, []) + + defp group_by_weight([], acc), do: Enum.reverse(acc) + + defp group_by_weight([hd | tl], []) do + group_by_weight(tl, [{hd.weight, [hd]}]) + end + + defp group_by_weight([hd | tl], [{prev_weight, prev_sets} | rest] = acc) do + if hd.weight == prev_weight do + group_by_weight(tl, [{hd.weight, prev_sets ++ [hd]} | rest]) + else + group_by_weight(tl, [{hd.weight, [hd]} | acc]) + end + end + def render(assigns) do ~H"""
@@ -35,7 +51,7 @@ defmodule GymLiveWeb.ViewWorkout do <%= Exercises.get_exercise_name(exercise) %>
dbg()} class="flex flex-row gap-3 justify-center" >
@@ -87,6 +103,7 @@ defmodule GymLiveWeb.ViewWorkout do {:noreply, socket} end + # deprecated def calculate_scores(sets) do Enum.reduce(sets, %{}, fn %Set{reps: reps, weight: weight, exercise: exercise}, acc -> if exercise in [:squat, :press, :bench_press, :deadlift] do