129 lines
3.4 KiB
Go
129 lines
3.4 KiB
Go
package ui
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.patanix.de/git/kettlebell-app/internal/data"
|
|
"git.patanix.de/git/kettlebell-app/internal/services"
|
|
"git.patanix.de/git/kettlebell-app/internal/ui/theme"
|
|
"git.patanix.de/git/kettlebell-app/internal/ui/utils"
|
|
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/canvas"
|
|
"fyne.io/fyne/v2/container"
|
|
"fyne.io/fyne/v2/widget"
|
|
)
|
|
|
|
func MakeTrainingScreen(ts *services.TrainingService, ss *services.SettingsService, parent fyne.Window) (fyne.CanvasObject, func()) {
|
|
timerLabel := canvas.NewText("00:00", theme.OneDarkText)
|
|
timerLabel.TextSize = 60
|
|
timerLabel.TextStyle.Bold = true
|
|
timerLabel.Alignment = fyne.TextAlignCenter
|
|
|
|
setsLabel := canvas.NewText("0 / 0", theme.OneDarkGreen)
|
|
setsLabel.TextSize = 48
|
|
setsLabel.TextStyle.Bold = true
|
|
setsLabel.Alignment = fyne.TextAlignCenter
|
|
|
|
repsLabel := canvas.NewText("0 Wiederholungen", theme.OneDarkSubtleText)
|
|
repsLabel.TextSize = 20
|
|
repsLabel.Alignment = fyne.TextAlignCenter
|
|
|
|
var finishButton *widget.Button
|
|
|
|
var mainTimer *time.Ticker
|
|
|
|
updateUI := func() {
|
|
state := ts.State
|
|
timerLabel.Text = utils.FormatDuration(int64(state.RemainingSeconds))
|
|
setsLabel.Text = fmt.Sprintf("%d / %d", state.SetsDone, state.GoalSets)
|
|
repsLabel.Text = fmt.Sprintf("%d Wiederholungen", state.RepsPerSet)
|
|
|
|
if finishButton != nil {
|
|
if state.RemainingSeconds <= 0 && state.IsTrainingRunning {
|
|
finishButton.Show()
|
|
} else {
|
|
finishButton.Hide()
|
|
}
|
|
}
|
|
|
|
timerLabel.Refresh()
|
|
setsLabel.Refresh()
|
|
repsLabel.Refresh()
|
|
}
|
|
|
|
finishAction := func() {
|
|
if ts.State.RemainingSeconds > 0 {
|
|
return
|
|
}
|
|
if mainTimer != nil {
|
|
mainTimer.Stop()
|
|
mainTimer = nil
|
|
}
|
|
if !ts.State.IsTrainingRunning {
|
|
return
|
|
}
|
|
session := &data.TrainingSession{
|
|
Date: time.Now(),
|
|
Sets: int64(ts.State.SetsDone),
|
|
WeightLeft: ss.LoadSettings().WeightLeft,
|
|
WeightRight: ss.LoadSettings().WeightRight,
|
|
RepsPerSet: int64(ts.State.RepsPerSet),
|
|
Duration: int64(ts.State.InitialDurationSeconds - ts.State.RemainingSeconds),
|
|
}
|
|
ts.FinishTraining(session)
|
|
fyne.CurrentApp().SendNotification(&fyne.Notification{Title: "Training gespeichert!", Content: "Gut gemacht!"})
|
|
updateUI()
|
|
}
|
|
|
|
startAction := func() {
|
|
if ts.State.IsTrainingRunning {
|
|
return
|
|
}
|
|
settings := ss.LoadSettings()
|
|
ts.StartTraining(settings.TrainingTimeMinutes, settings.GoalSets)
|
|
updateUI()
|
|
|
|
mainTimer = time.NewTicker(time.Second)
|
|
go func() {
|
|
for mainTimer != nil {
|
|
<-mainTimer.C
|
|
if ts.State.RemainingSeconds <= 0 {
|
|
finishAction()
|
|
return
|
|
}
|
|
ts.Tick()
|
|
updateUI()
|
|
}
|
|
}()
|
|
}
|
|
|
|
setAction := func() {
|
|
if !ts.State.IsTrainingRunning {
|
|
return
|
|
}
|
|
ts.CompleteSet()
|
|
updateUI()
|
|
}
|
|
|
|
topPart := container.NewVBox(widget.NewLabelWithStyle("Verbleibende Zeit", fyne.TextAlignCenter, fyne.TextStyle{}), timerLabel)
|
|
middlePart := container.NewVBox(widget.NewLabelWithStyle("Sätze", fyne.TextAlignCenter, fyne.TextStyle{}), setsLabel, repsLabel)
|
|
finishButton = widget.NewButton("Training beenden", finishAction)
|
|
finishButton.Hide() // oder: finishButton.Hide() // finishButton.Disable()
|
|
setButton := widget.NewButton("Satz abschließen", setAction)
|
|
setButton.Importance = widget.HighImportance
|
|
setButton.Resize(fyne.NewSize(120, 60))
|
|
|
|
bottomPart := container.NewVBox(
|
|
setButton,
|
|
finishButton,
|
|
)
|
|
|
|
layout := container.NewBorder(topPart, bottomPart, nil, nil, container.NewCenter(middlePart))
|
|
if layout.Visible() {
|
|
updateUI()
|
|
}
|
|
|
|
return layout, startAction
|
|
}
|