Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep menus in memory #25

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 27 additions & 54 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
"golang.org/x/text/language"
)

var currentMensa *openmensa.Canteen
// errs serves as a delegation for errors
var errs = make(chan error, 1)

// availMenus stores the currently available dates and meals of a canteen
var availMenus []openmensa.Menu

// Generic title case
var titler = cases.Title(language.Und)

Expand Down Expand Up @@ -66,61 +69,43 @@ func startApp(selected string) {
errs <- err
}

currentMensa = &c[0]
mensa := c[0]

// Set calendar data
days, err := currentMensa.Days()
// Fetch the upcoming Speisepläne
menus, err := mensa.AllMenus()
if err != nil {
errs <- err
} else {
availMenus = menus
}

calendar.Clear()
menuList.Clear()
priceTable.Clear()
notesView.Clear()

today := time.Now().Truncate(24 * time.Hour)
for _, d := range days {
if !d.Closed {

// Giga big brain hack: instead of saving the selected date as a global variable,
// set the hidden secondary text to a format that can easily be parsed back :)
dstr := d.Date.String()

// Add a nice date
date := time.Time(d.Date)
var desc string
if today.Equal(date) {
desc = "Today"
} else {
desc = date.Format("Monday, January 2")
}

calendar.AddItem(desc, dstr, 0, nil)
for _, menu := range menus {
date := time.Time(menu.Day.Date)
var desc string
if today.Equal(date) {
desc = "Today"
} else {
desc = date.Format("Monday, January 2")
}

calendar.AddItem(desc, "", 0, nil)
}

// If there are no open dates, remove meal data
if calendar.GetItemCount() == 0 {
menuList.Clear()
priceTable.Clear()
notesView.Clear()
} else {
config.SaveLastCanteen(currentMensa.Name)
if len(menus) > 0 {
config.SaveLastCanteen(mensa.Name)
}
})

// If the selected date has changed, load the meals for that date
calendar.SetChangedFunc(func(index int, mainText, secondaryText string, shortcut rune) {
// Load meals for the changed date
date, err := time.Parse("2006-01-02", secondaryText)
if err != nil {
errs <- err
}

meals, err := currentMensa.MealsOn(date)
if err != nil {
errs <- err
}

menuList.Clear()
for i, m := range meals {
for i, m := range availMenus[index].Meals {
shortcut := rune(0)
if i < 9 {
shortcut = rune('1' + i)
Expand All @@ -134,21 +119,9 @@ func startApp(selected string) {

// If the selected menu has changed, load details for that menu
menuList.SetChangedFunc(func(index int, mainText, secondaryText string, shortcut rune) {
// Load meals for the selected date
i := calendar.GetCurrentItem()
_, dstr := calendar.GetItemText(i)
date, err := time.Parse("2006-01-02", dstr)
if err != nil {
errs <- err
}

meals, err := currentMensa.MealsOn(date)
if err != nil {
errs <- err
}

// Set details for the selected meal
meal := meals[index]
menuIndex := calendar.GetCurrentItem()
meal := availMenus[menuIndex].Meals[index]

notesView.SetText(strings.Join(meal.Notes, ", "))

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ go 1.18

require (
github.com/gdamore/tcell/v2 v2.6.0
github.com/j0hax/go-openmensa v0.3.1
github.com/j0hax/go-openmensa v0.4.0
github.com/rivo/tview v0.0.0-20220801133142-711ef394f9b3
golang.org/x/text v0.7.0
)

require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
)
14 changes: 2 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
github.com/j0hax/go-openmensa v0.2.3 h1:waVdZvXGE2TSjg3wzsXaN8+vYnj8XohCOBX9+APyoo0=
github.com/j0hax/go-openmensa v0.2.3/go.mod h1:8XUwUDsrf+pz4+S03NWjd2CqbYe13yInbjVqZyBWkbs=
github.com/j0hax/go-openmensa v0.3.0 h1:d2Ozk1a4+pWcqqrfpftu/TisRaBjjTOB4L5x7CKJe+U=
github.com/j0hax/go-openmensa v0.3.0/go.mod h1:eySI6ja8Z3tBePbWsUTIrDZ1WeDoycaQnc1nYfHep4I=
github.com/j0hax/go-openmensa v0.3.1-0.20230307145825-fda7b16e39b9 h1:xmRNmtaTkfcBcCpf1V+pgnvI0GKk21Ja2GumrhAEjvc=
github.com/j0hax/go-openmensa v0.3.1-0.20230307145825-fda7b16e39b9/go.mod h1:eySI6ja8Z3tBePbWsUTIrDZ1WeDoycaQnc1nYfHep4I=
github.com/j0hax/go-openmensa v0.3.1 h1:+0MQCyoogsCiOLRVxIiT3Ni+O71z+XCVakK5TLUp2kU=
github.com/j0hax/go-openmensa v0.3.1/go.mod h1:eySI6ja8Z3tBePbWsUTIrDZ1WeDoycaQnc1nYfHep4I=
github.com/j0hax/go-openmensa v0.4.0 h1:KGfnX40amKBg3h5wVu/AurCAiLS70cK2Cr7cr87w+cQ=
github.com/j0hax/go-openmensa v0.4.0/go.mod h1:eySI6ja8Z3tBePbWsUTIrDZ1WeDoycaQnc1nYfHep4I=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
Expand All @@ -22,10 +16,6 @@ github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI=
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Expand Down
2 changes: 2 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func loadCanteens(app *tview.Application, list *tview.List) {
}
}

//func mensaSelect(app *tview.Application, mensaSelected <-chan string,)

// errWatcher waits for an error on ec.
// These errors can be dismissed "ignored," so they should not be used in situations
// where the program can not continue.
Expand Down