161 lines
4.4 KiB
Go
161 lines
4.4 KiB
Go
package ui
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"git.patanix.de/git/kettlebell-app/internal/data"
|
|
"git.patanix.de/git/kettlebell-app/internal/services"
|
|
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/container"
|
|
"fyne.io/fyne/v2/dialog"
|
|
"fyne.io/fyne/v2/theme"
|
|
"fyne.io/fyne/v2/widget"
|
|
)
|
|
|
|
func MakeTrainingScreen(ts *services.TrainingService, ss *services.SettingsService, parent fyne.Window) fyne.CanvasObject {
|
|
programLabel := widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{Bold: true})
|
|
blockDayLabel := widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{})
|
|
repsLabel := widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{Bold: true})
|
|
|
|
timerLabel := widget.NewLabelWithStyle("00:00", fyne.TextAlignCenter, fyne.TextStyle{Bold: true})
|
|
|
|
progressBar := widget.NewProgressBar()
|
|
progressLabel := widget.NewLabelWithStyle("Fortschritt: 0%", fyne.TextAlignCenter, fyne.TextStyle{})
|
|
|
|
var startButton, setButton, finishButton *widget.Button
|
|
|
|
setHistoryList := widget.NewList(
|
|
func() int {
|
|
return len(ts.State.SetTimes)
|
|
},
|
|
func() fyne.CanvasObject {
|
|
return widget.NewLabel("")
|
|
},
|
|
func(id widget.ListItemID, obj fyne.CanvasObject) {
|
|
t := ts.State.SetTimes[id]
|
|
obj.(*widget.Label).SetText(fmt.Sprintf("#%d um %s (%d Reps)", id+1, t.Format("15:04:05"), ts.State.CurrentReps))
|
|
},
|
|
)
|
|
|
|
var mainTimer, lastSetTimer *time.Ticker
|
|
|
|
updateUI := func() {
|
|
state := ts.State
|
|
programLabel.SetText(state.CurrentProgram)
|
|
blockDayLabel.SetText(fmt.Sprintf("Block Tag: %d", state.CurrentBlockDay))
|
|
repsLabel.SetText(fmt.Sprintf("Reps pro Satz: %d", state.CurrentReps))
|
|
|
|
timerLabel.SetText(formatDuration(int64(state.RemainingSeconds)))
|
|
progressBar.SetValue(state.Progress)
|
|
progressLabel.SetText(fmt.Sprintf("Fortschritt: %.0f%%", state.Progress*100))
|
|
|
|
if state.IsTrainingRunning {
|
|
startButton.Disable()
|
|
setButton.Enable()
|
|
finishButton.Enable()
|
|
} else {
|
|
startButton.Enable()
|
|
setButton.Disable()
|
|
finishButton.Disable()
|
|
}
|
|
setHistoryList.Refresh()
|
|
}
|
|
|
|
stopTimers := func() {
|
|
if mainTimer != nil {
|
|
mainTimer.Stop()
|
|
mainTimer = nil
|
|
}
|
|
if lastSetTimer != nil {
|
|
lastSetTimer.Stop()
|
|
lastSetTimer = nil
|
|
}
|
|
}
|
|
|
|
startAction := func() {
|
|
settings := ss.LoadSettings()
|
|
ts.StartTraining(settings.TrainingTimeMinutes, settings.GoalSets)
|
|
updateUI()
|
|
|
|
mainTimer = time.NewTicker(time.Second)
|
|
go func() {
|
|
for range mainTimer.C {
|
|
if ts.State.RemainingSeconds <= 0 {
|
|
stopTimers()
|
|
fyne.CurrentApp().SendNotification(&fyne.Notification{
|
|
Title: "Zeit abgelaufen!",
|
|
Content: "Training wird automatisch gespeichert.",
|
|
})
|
|
finishButton.OnTapped()
|
|
return
|
|
}
|
|
ts.Tick()
|
|
updateUI()
|
|
}
|
|
}()
|
|
|
|
lastSetTimer = time.NewTicker(time.Second)
|
|
go func() {
|
|
for range lastSetTimer.C {
|
|
ts.TickLastSetTimer()
|
|
}
|
|
}()
|
|
}
|
|
|
|
setAction := func() {
|
|
ts.CompleteSet()
|
|
fyne.CurrentApp().SendNotification(&fyne.Notification{Title: "Satz gespeichert!", Content: ""})
|
|
updateUI()
|
|
}
|
|
|
|
finishAction := func() {
|
|
stopTimers()
|
|
settings := ss.LoadSettings()
|
|
state := ts.State
|
|
|
|
session := &data.TrainingSession{
|
|
Date: time.Now(),
|
|
Sets: int64(state.SetsDone),
|
|
WeightLeft: settings.WeightLeft,
|
|
WeightRight: settings.WeightRight,
|
|
RepsPerSet: int64(state.RepsPerSet),
|
|
Duration: int64(state.InitialDurationSeconds - state.RemainingSeconds),
|
|
}
|
|
|
|
if err := ts.FinishTraining(session); err != nil {
|
|
dialog.ShowError(err, parent)
|
|
log.Printf("Fehler beim Speichern des Trainings: %v", err)
|
|
} else {
|
|
fyne.CurrentApp().SendNotification(&fyne.Notification{Title: "Training gespeichert!", Content: "Gut gemacht!"})
|
|
}
|
|
|
|
updateUI()
|
|
}
|
|
|
|
startButton = widget.NewButtonWithIcon("Start", theme.MediaPlayIcon(), startAction)
|
|
setButton = widget.NewButtonWithIcon("Satz", theme.ConfirmIcon(), setAction)
|
|
finishButton = widget.NewButtonWithIcon("Beenden", theme.MediaStopIcon(), finishAction)
|
|
|
|
updateUI()
|
|
|
|
headerCard := widget.NewCard("", "", container.NewVBox(programLabel, blockDayLabel, repsLabel))
|
|
timerCard := widget.NewCard("", "", container.NewVBox(
|
|
widget.NewLabelWithStyle("Verbleibende Zeit", fyne.TextAlignCenter, fyne.TextStyle{}),
|
|
timerLabel,
|
|
progressBar,
|
|
progressLabel,
|
|
))
|
|
|
|
actionButtons := container.NewGridWithColumns(3, startButton, setButton, finishButton)
|
|
historyCard := widget.NewCard("Satz-Historie", "", setHistoryList)
|
|
|
|
return container.NewVBox(
|
|
headerCard,
|
|
timerCard,
|
|
actionButtons,
|
|
historyCard,
|
|
)
|
|
}
|