Skip to content

Commit

Permalink
Dispatch all data functions used in the views
Browse files Browse the repository at this point in the history
This prevents issues when the data is nil.

Signed-off-by: Jo Vandeginste <[email protected]>
  • Loading branch information
jovandeginste committed Nov 8, 2024
1 parent 2c37c70 commit 40dd98f
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 33 deletions.
112 changes: 112 additions & 0 deletions pkg/database/workouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ func (w *Workout) Weight() float64 {
return w.Data.TotalWeight
}

func (w *Workout) AverageSpeed() float64 {
if w.Data == nil {
return 0
}

return w.Data.AverageSpeed()
}

func (w *Workout) TotalDuration() time.Duration {
if w.Data == nil {
return 0
}

return w.Data.TotalDuration
}

func (w *Workout) TotalDistance() float64 {
if w.Data == nil {
return 0
}

return w.Data.TotalDistance
}

func (w *Workout) Repetitions() int {
if w.Data == nil {
return 0
Expand Down Expand Up @@ -126,6 +150,94 @@ func (w *Workout) FullAddress() string {
return w.Data.AddressString
}

func (w *Workout) Center() *MapCenter {
if w.Data == nil {
return nil
}

return &w.Data.Center
}

func (w *Workout) Details() *MapDataDetails {
if w.Data == nil {
return nil
}

return w.Data.Details
}

func (w *Workout) TotalDown() float64 {
if w.Data == nil {
return 0
}

return w.Data.TotalDown
}

func (w *Workout) TotalUp() float64 {
if w.Data == nil {
return 0
}

return w.Data.TotalUp
}

func (w *Workout) MaxElevation() float64 {
if w.Data == nil {
return 0
}

return w.Data.MaxElevation
}

func (w *Workout) MinElevation() float64 {
if w.Data == nil {
return 0
}

return w.Data.MinElevation
}

func (w *Workout) MaxSpeed() float64 {
if w.Data == nil {
return 0
}

return w.Data.MaxSpeed
}

func (w *Workout) AverageSpeedNoPause() float64 {
if w.Data == nil {
return 0
}

return w.Data.AverageSpeedNoPause()
}

func (w *Workout) PauseDuration() time.Duration {
if w.Data == nil {
return 0
}

return w.Data.PauseDuration
}

func (w *Workout) Creator() string {
if w.Data == nil {
return ""
}

return w.Data.Creator
}

func (w *Workout) City() string {
if w.Data == nil || w.Data.Address == nil {
return ""
}

return w.Data.Address.City
}

func (w *Workout) Address() string {
if w.Data == nil {
return ""
Expand Down
34 changes: 16 additions & 18 deletions views/partials/workout_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<tr>
<td>{{ IconFor `source` }}</td>
<th>{{ i18n "Source" }}</th>
<td>{{ .Data.Creator }}</td>
<td>{{ .Creator }}</td>
</tr>
<tr>
<td>{{ IconFor `workout` }}</td>
Expand All @@ -38,7 +38,7 @@
<tr>
<td>{{ IconFor `repetitions` }}</td>
<th>{{ i18n "Repetitions" }}</th>
<td class="whitespace-nowrap font-mono">{{ .Data.TotalRepetitions }}</td>
<td class="whitespace-nowrap font-mono">{{ .TotalRepetitions }}</td>
</tr>
<tr>
<td>{{ IconFor `tempo` }}</td>
Expand All @@ -51,29 +51,29 @@
<tr>
<td>{{ IconFor `weight` }}</td>
<th>{{ i18n "Weight" }}</th>
<td class="whitespace-nowrap font-mono">{{ .Data.TotalWeight }}</td>
<td class="whitespace-nowrap font-mono">{{ .Weight }}</td>
</tr>
{{ end }} {{ if .Type.IsDuration }}
<tr>
<td>{{ IconFor `duration` }}</td>
<th>{{ i18n "Total duration" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.TotalDuration | HumanDuration }}
{{ .TotalDuration | HumanDuration }}
</td>
</tr>
<tr>
<td>{{ IconFor `pause` }}</td>
<th>{{ i18n "Time paused" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.PauseDuration | HumanDuration }}
{{ .PauseDuration | HumanDuration }}
</td>
</tr>
{{ end }} {{ if .Type.IsDistance }}
<tr>
<td>{{ IconFor `distance` }}</td>
<th>{{ i18n "Total distance" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.TotalDistance | HumanDistance }} {{
{{ .TotalDistance | HumanDistance }} {{
CurrentUser.PreferredUnits.Distance }}
</td>
</tr>
Expand All @@ -82,71 +82,69 @@
<td>{{ IconFor `speed` }}</td>
<th>{{ i18n "Average speed" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.AverageSpeed | HumanSpeed }} {{
CurrentUser.PreferredUnits.Speed }}
{{ .AverageSpeed | HumanSpeed }} {{ CurrentUser.PreferredUnits.Speed }}
</td>
</tr>
<tr>
<td>{{ IconFor `speed` }}</td>
<th>{{ i18n "Average speed (no pause)" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.AverageSpeedNoPause | HumanSpeed }} {{
{{ .AverageSpeedNoPause | HumanSpeed }} {{
CurrentUser.PreferredUnits.Speed }}
</td>
</tr>
<tr>
<td>{{ IconFor `tempo` }}</td>
<th>{{ i18n "Average tempo" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.AverageSpeed | HumanTempo }} {{
CurrentUser.PreferredUnits.Tempo }}
{{ .AverageSpeed | HumanTempo }} {{ CurrentUser.PreferredUnits.Tempo }}
</td>
</tr>
<tr>
<td>{{ IconFor `tempo` }}</td>
<th>{{ i18n "Average tempo (no pause)" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.AverageSpeedNoPause | HumanTempo }} {{
{{ .AverageSpeedNoPause | HumanTempo }} {{
CurrentUser.PreferredUnits.Tempo }}
</td>
</tr>
<tr>
<td>{{ IconFor `max-speed` }}</td>
<th>{{ i18n "Max speed" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.MaxSpeed | HumanSpeed }} {{ CurrentUser.PreferredUnits.Speed }}
{{ .MaxSpeed | HumanSpeed }} {{ CurrentUser.PreferredUnits.Speed }}
</td>
</tr>
{{ end }} {{ if .Type.IsLocation }}
<tr>
<td>{{ IconFor `elevation` }}</td>
<th>{{ i18n "Min elevation" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.MinElevation | HumanElevation }} {{
{{ .MinElevation | HumanElevation }} {{
CurrentUser.PreferredUnits.Elevation }}
</td>
</tr>
<tr>
<td>{{ IconFor `elevation` }}</td>
<th>{{ i18n "Max elevation" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.MaxElevation | HumanElevation }} {{
{{ .MaxElevation | HumanElevation }} {{
CurrentUser.PreferredUnits.Elevation }}
</td>
</tr>
<tr>
<td>{{ IconFor `up` }}</td>
<th>{{ i18n "Total up" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.TotalUp | HumanElevation }} {{
CurrentUser.PreferredUnits.Elevation }}
{{ .TotalUp | HumanElevation }} {{ CurrentUser.PreferredUnits.Elevation
}}
</td>
</tr>
<tr>
<td>{{ IconFor `down` }}</td>
<th>{{ i18n "Total down" }}</th>
<td class="whitespace-nowrap font-mono">
{{ .Data.TotalDown | HumanElevation }} {{
{{ .TotalDown | HumanElevation }} {{
CurrentUser.PreferredUnits.Elevation }}
</td>
</tr>
Expand Down
10 changes: 5 additions & 5 deletions views/partials/workout_map.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
<script>
makeMap({
elementID: "map",
center: [{{ .Data.Center.Lat }}, {{ .Data.Center.Lng }}],
minElevation: {{ .Data.MinElevation }},
maxElevation: {{ .Data.MaxElevation }},
maxSpeed: {{ .Data.MaxSpeed }},
center: [{{ .Center.Lat }}, {{ .Center.Lng }}],
minElevation: {{ .MinElevation }},
maxElevation: {{ .MaxElevation }},
maxSpeed: {{ .MaxSpeed }},
speedName: "{{ i18n "Average speed" }}",
elevationName: "{{ i18n "Elevation" }}",
streetsName: "{{ i18n "Streets" }}",
aerialName: "{{ i18n "Aerial" }}",

points: [
{{ with .Data.Details }}
{{ with .Details }}
{{ range .Points -}}
{ "lat": {{ .Lat }}, "lng": {{ .Lng }}, "speed": {{ .AverageSpeed }}, "elevation": {{ .ExtraMetrics.Get "elevation" }}, "title": "{{ template `workout_point_title` . }}", },
{{ end }}
Expand Down
4 changes: 2 additions & 2 deletions views/partials/workout_social.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<div
class="shareon print:hidden"
data-url=" "
data-title="{{ i18n `I completed a workout: %s.` (i18n .Type.String) }} {{ i18n `It took me %s to go %s. I averaged %s.` (.Data.TotalDuration | HumanDuration) (.Data.TotalDistance | HumanDistance) (.Data.AverageSpeed | HumanSpeed) }}"
data-hashtags="workout,{{ i18n .Type.String }}{{ if .Data.Address }},{{ .Data.Address.City }}{{ end }}"
data-title="{{ i18n `I completed a workout: %s.` (i18n .Type.String) }} {{ i18n `It took me %s to go %s. I averaged %s.` (.TotalDuration | HumanDuration) (.TotalDistance | HumanDistance) (.AverageSpeed | HumanSpeed) }}"
data-hashtags="workout,{{ i18n .Type.String }}{{ if .City }},{{ .City }}{{ end }}"
>
<a class="twitter"></a>
<a class="mastodon"></a>
Expand Down
4 changes: 2 additions & 2 deletions views/partials/workouts_list_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
</div>
{{ end }} {{ if .Type.IsDistance }}
<div class="font-mono" title="{{ i18n `Distance` }}">
{{ IconFor `distance` }} {{ .Data.TotalDistance | HumanDistance }} {{
{{ IconFor `distance` }} {{ .TotalDistance | HumanDistance }} {{
CurrentUser.PreferredUnits.Distance }}
</div>
{{ end }} {{ if .Type.IsDuration }}
<div class="hidden sm:table-cell font-mono" title="{{ i18n `Duration` }}">
{{ IconFor `duration` }} {{ .Data.TotalDuration | HumanDuration }}
{{ IconFor `duration` }} {{ .TotalDuration | HumanDuration }}
</div>
{{ end }} {{ if and .Type.IsDistance .Type.IsDuration }}
<div
Expand Down
8 changes: 4 additions & 4 deletions views/workouts/workouts_route_segment.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h2>{{ IconFor .Type.String }}{{ .Name }}</h2>
<script src="{{ RouteFor `assets` }}/map.js"></script>
<script>
points = [
{{ with .Data.Details }}
{{ with .Details }}
{{ range .Points -}}
{ "lat": {{ .Lat }}, "lng": {{ .Lng }}, "distance": {{ .TotalDistance }} },
{{ end }}
Expand Down Expand Up @@ -77,7 +77,7 @@ <h2>{{ IconFor .Type.String }}{{ .Name }}</h2>
id="start"
name="start"
min="1"
max="{{ .Data.Details.Points | len }}"
max="{{ .Details.Points | len }}"
value="1"
oninput="updateStart()"
/>
Expand All @@ -93,8 +93,8 @@ <h2>{{ IconFor .Type.String }}{{ .Name }}</h2>
id="end"
name="end"
min="1"
max="{{ .Data.Details.Points | len }}"
value="{{ .Data.Details.Points | len }}"
max="{{ .Details.Points | len }}"
value="{{ .Details.Points | len }}"
oninput="updateEnd()"
/>
<span id="end-show"></span>
Expand Down
4 changes: 2 additions & 2 deletions views/workouts/workouts_show.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ <h2>{{ IconFor .Type.String }}{{ .Name }}</h2>
<div class="inner-form">{{ template "workout_details" . }}</div>
</div>
<div class="basis-1/2 2xl:basis-1/3">
{{ if and .Type.IsDistance .Type.IsDuration .Data.Details }}
{{ if and .Type.IsDistance .Type.IsDuration .Details }}
<div class="inner-form">
<div class="print:w-full overflow-y-auto">
{{ template "workout_breakdown" (.StatisticsPer 1
Expand Down Expand Up @@ -71,7 +71,7 @@ <h3>{{ IconFor `note` }}{{ i18n "Notes" }}</h3>
</div>
{{ end }}
</div>
{{ if .Data.Details }}
{{ if .Details }}
<div class="inner-form h-[300px] md:h-[500px] print:hidden">
<h3>
<span>{{ IconFor `speed` }}{{ i18n "Average speed" }}</span>
Expand Down

0 comments on commit 40dd98f

Please sign in to comment.