Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

Commit

Permalink
Update gui and add support for all the data fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 10, 2020
1 parent 37fa395 commit dbfaf0c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 66 deletions.
46 changes: 40 additions & 6 deletions src/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type Exercise struct {
Activity string `xml:"activity"`
Distance float64 `xml:"distance"`
Time float64 `xml:"time"`
//Reps int `xml:"reps"`
//Sets int `xml:"sets"`
//Comment string `xml:"comment"`
Reps int `xml:"reps"`
Sets int `xml:"sets"`
Comment string `xml:"comment"`
}

// Config returns the config directory and handles the error accordingly.
Expand Down Expand Up @@ -140,6 +140,10 @@ func (d *Data) Write(key *[32]byte) {

// ParseFloat is a wrapper around strconv.ParseFloat that handles the error to make the function usable inline.
func ParseFloat(input string) float64 {
if input == "" {
return 0
}

output, err := strconv.ParseFloat(input, 32)
if err != nil {
fmt.Print(err)
Expand All @@ -148,9 +152,39 @@ func ParseFloat(input string) float64 {
return output
}

// ParseInt is just a wrapper around strconv.Atoi().
func ParseInt(input string) int {
if input == "" {
return 0
}

output, err := strconv.Atoi(input)
if err != nil {
fmt.Print(err)
}

return output
}

// Format formats the latest updated data in the Data struct to display information.
func (d *Data) Format(i int) string {
return fmt.Sprintf("\nAt %s on %s, you trained %s. The distance was %v kilometers and the exercise lasted for %v minutes.\nThis resulted in an average speed of %.3f km/min.\n",
d.Exercise[i].Clock, d.Exercise[i].Date, d.Exercise[i].Activity, d.Exercise[i].Distance,
d.Exercise[i].Time, d.Exercise[i].Distance/d.Exercise[i].Time)
if d.Exercise[i].Reps == 0 && d.Exercise[i].Sets == 0 && d.Exercise[i].Comment == "" {
return fmt.Sprintf("\nAt %s on %s, you trained %s. The distance was %v kilometers and the exercise lasted for %v minutes.\nThis resulted in an average speed of %.3f km/min.\n",
d.Exercise[i].Clock, d.Exercise[i].Date, d.Exercise[i].Activity, d.Exercise[i].Distance,
d.Exercise[i].Time, d.Exercise[i].Distance/d.Exercise[i].Time)
} else if d.Exercise[i].Reps == 0 && d.Exercise[i].Sets == 0 {
return fmt.Sprintf("\nAt %s on %s, you trained %s. The distance was %v kilometers and the exercise lasted for %v minutes.\nThis resulted in an average speed of %.3f km/min.\nComment: %s\n",
d.Exercise[i].Clock, d.Exercise[i].Date, d.Exercise[i].Activity, d.Exercise[i].Distance,
d.Exercise[i].Time, d.Exercise[i].Distance/d.Exercise[i].Time, d.Exercise[i].Comment)
} else if d.Exercise[i].Distance == 0 && d.Exercise[i].Comment == "" {
return fmt.Sprintf("\nAt %s on %s, you trained %s for %v minutes. You did %v sets with %v reps each.\n",
d.Exercise[i].Clock, d.Exercise[i].Date, d.Exercise[i].Activity, d.Exercise[i].Time, d.Exercise[i].Sets, d.Exercise[i].Reps)
} else if d.Exercise[i].Distance == 0 {
return fmt.Sprintf("\nAt %s on %s, you trained %s for %v minutes. You did %v sets with %v reps each.\nComment: %s\n",
d.Exercise[i].Clock, d.Exercise[i].Date, d.Exercise[i].Activity, d.Exercise[i].Time, d.Exercise[i].Sets, d.Exercise[i].Reps, d.Exercise[i].Comment)
}

// If none of the above cases are true, we return all data even if some might be empty.
return fmt.Sprintf("\nAt %s on %s, you trained %s for %v minutes. The distance was %v kilometers and you did %v sets with %v reps each.\nComment: %s\n",
d.Exercise[i].Clock, d.Exercise[i].Date, d.Exercise[i].Activity, d.Exercise[i].Time, d.Exercise[i].Distance, d.Exercise[i].Sets, d.Exercise[i].Reps, d.Exercise[i].Comment)
}
50 changes: 50 additions & 0 deletions src/gui/entry-widgets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package gui

import (
"fyne.io/fyne"
"fyne.io/fyne/widget"
)

// ExtendedEntry is used to make an entry that reacts to key presses.
type ExtendedEntry struct {
widget.Entry
*Action
}

// Action handles the Button press action.
type Action struct {
widget.Button
}

// TypedKey handles the key presses inside our UsernameEntry and uses Action to press the linked button.
func (e *ExtendedEntry) TypedKey(ev *fyne.KeyEvent) {
switch ev.Name {
case fyne.KeyReturn:
e.Action.Button.OnTapped()
default:
e.Entry.TypedKey(ev)
}
}

// NewExtendedEntry creates an ExtendedEntry button.
func NewExtendedEntry(password bool) *ExtendedEntry {
entry := &ExtendedEntry{}

// Extend the base widget.
entry.ExtendBaseWidget(entry)

// Check if we are creating a password entry.
if password {
entry.Password = true
}

return entry
}

// NewEntryWithPlaceholder makes it easy to create entry widgets with placeholders.
func NewEntryWithPlaceholder(text string) *widget.Entry {
entry := widget.NewEntry()
entry.SetPlaceHolder(text)

return entry
}
81 changes: 21 additions & 60 deletions src/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,6 @@ import (
"fyne.io/fyne/widget"
)

// ExtendedEntry is used to make an entry that reacts to key presses.
type ExtendedEntry struct {
widget.Entry
*Action
}

// Action handles the Button press action.
type Action struct {
widget.Button
}

// TypedKey handles the key presses inside our UsernameEntry and uses Action to press the linked button.
func (e *ExtendedEntry) TypedKey(ev *fyne.KeyEvent) {
switch ev.Name {
case fyne.KeyReturn:
e.Action.Button.OnTapped()
default:
e.Entry.TypedKey(ev)
}
}

// NewExtendedEntry creates an ExtendedEntry button.
func NewExtendedEntry(password bool) *ExtendedEntry {
entry := &ExtendedEntry{}

// Extend the base widget.
entry.ExtendBaseWidget(entry)

// Check if we are creating a password entry.
if password {
entry.Password = true
}

return entry
}

// PasswordKey contains the key taken from the username and password.
var PasswordKey [32]byte

Expand Down Expand Up @@ -85,8 +49,9 @@ func Init() {
return
}

// Adapt the window to a good size.
window.Resize(fyne.NewSize(600, 400))
// Adapt the window to a good size and make it resizable again.
window.SetFixedSize(false)
window.Resize(fyne.NewSize(800, 500))

// Calculate the sha256 hash of the username and password.
PasswordKey = encrypt.EncryptionKey(userName.Text, userPassword.Text)
Expand All @@ -99,31 +64,23 @@ func Init() {

// The button for creating a new exercise.
newExercise := widget.NewButtonWithIcon("Add new exercise", theme.ContentAddIcon(), func() {
// Form information for the first row.
dateEntry := widget.NewEntry()
dateEntry.SetPlaceHolder("YYYY-MM-DD")

// Second row of form information.
clockEntry := widget.NewEntry()
clockEntry.SetPlaceHolder("HH:MM")

// Third row of the form.
activityEntry := widget.NewEntry()

// Forth row of form data.
distanceEntry := widget.NewEntry()
distanceEntry.SetPlaceHolder("kilometers")

// Fifth row in the form.
timeEntry := widget.NewEntry()
timeEntry.SetPlaceHolder("minutes")
// Variables for the entry variables used in the form.
dateEntry := NewEntryWithPlaceholder("YYYY-MM-DD")
clockEntry := NewEntryWithPlaceholder("HH:MM")
activityEntry := NewEntryWithPlaceholder("Name of activity")
distanceEntry := NewEntryWithPlaceholder("Kilometers")
timeEntry := NewEntryWithPlaceholder("Minutes")
repsEntry := NewEntryWithPlaceholder("Number of reps")
setsEntry := NewEntryWithPlaceholder("Number of sets")
commentEntry := widget.NewMultiLineEntry()

// Create the form for displaying.
form := &widget.Form{
OnSubmit: func() {
go func() {
// Append new values to a new index.
XMLData.Exercise = append(XMLData.Exercise, file.Exercise{Date: dateEntry.Text, Clock: clockEntry.Text, Activity: activityEntry.Text, Distance: file.ParseFloat(distanceEntry.Text), Time: file.ParseFloat(timeEntry.Text)})
XMLData.Exercise = append(XMLData.Exercise, file.Exercise{Date: dateEntry.Text, Clock: clockEntry.Text, Activity: activityEntry.Text, Distance: file.ParseFloat(distanceEntry.Text), Time: file.ParseFloat(timeEntry.Text), Reps: file.ParseInt(repsEntry.Text), Sets: file.ParseInt(setsEntry.Text), Comment: commentEntry.Text})

// Encrypt and write the data to the configuration file. Do so on another goroutine.
go XMLData.Write(&PasswordKey)
Expand All @@ -140,6 +97,9 @@ func Init() {
form.Append("Activity", activityEntry)
form.Append("Distance", distanceEntry)
form.Append("Time", timeEntry)
form.Append("Reps", repsEntry)
form.Append("Sets", setsEntry)
form.Append("Comment", commentEntry)

// Show the popup dialog.
dialog.ShowCustom("Add activity", "Done", form, window)
Expand Down Expand Up @@ -174,14 +134,15 @@ func Init() {
window.SetContent(widget.NewScrollContainer(fyne.NewContainerWithLayout(layout.NewVBoxLayout(), newExercise, label)))
})

// Add the Action component to make actions work inside the struct. Thhis is used to press the loginButton on pressing enter/return ton the keyboard.
// Add the Action component to make actions work inside the struct. This is used to press the loginButton on pressing enter/return ton the keyboard.
userName.Action, userPassword.Action = &Action{*loginButton}, &Action{*loginButton}

// Set the content to be displayed. It is the userName, userPassword fields and the login button inside a layout.
window.SetContent(fyne.NewContainerWithLayout(layout.NewGridLayout(1), userName, userPassword, loginButton))
window.SetContent(fyne.NewContainerWithLayout(layout.NewVBoxLayout(), userName, userPassword, loginButton))

// Set a sane default for the window size on login.
window.Resize(fyne.NewSize(450, 150))
// Set a sane default for the window size on login and make sure that it isn't resizable.
window.Resize(fyne.NewSize(400, 100))
window.SetFixedSize(true)

// Show all of our set content and run the gui.
window.ShowAndRun()
Expand Down

0 comments on commit dbfaf0c

Please sign in to comment.