From 2313b34a2f062ccda1f668b4fc2c7006bd46f604 Mon Sep 17 00:00:00 2001 From: David Florness Date: Sun, 14 Feb 2021 21:59:51 -0500 Subject: [PATCH] TUI: differentiate between waiting for results and showing them now --- election/result.go | 7 +++++-- ui/tui.go | 49 ++++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/election/result.go b/election/result.go index e5717ad..d85cfc0 100644 --- a/election/result.go +++ b/election/result.go @@ -27,8 +27,11 @@ func (el *Election) CalculateResult() { } func (el *Election) PrintResults() { - for el.Result == nil { - time.Sleep(time.Millisecond * 200) + if el.Result == nil { + fmt.Println("waiting for results...") + for el.Result == nil { + time.Sleep(time.Millisecond * 200) + } } result := *el.Result candidates := el.Candidates diff --git a/ui/tui.go b/ui/tui.go index 9dcc080..f531e99 100644 --- a/ui/tui.go +++ b/ui/tui.go @@ -144,26 +144,28 @@ func RoomTUI(client *mautrix.Client, roomID id.RoomID, elections *election.Elect elections.RUnlock() if i > 0 { - // user wants to join election + // user selected election el = L[i - 1] - // don't need to lock because this goroutine controls LocalVoter - if el.LocalVoter == nil { - // ask user if s/he wants to join election (or - // display results if it's already started) - if !electionConfirmation(el) { - // user needs to select a different - // election - el, err = RoomTUI(client, roomID, elections) - } else if el.StartID == nil { - // user selected yes on joining the - // election - err = el.JoinElection(client, elections.EventStore) - } + el.RLock() + userJoined := el.LocalVoter != nil + el.RUnlock() + if userJoined { + return // good to go + } + // ask user if s/he wants to join election (or display + // results if it's already started) + if !electionConfirmation(el) { + // user needs to select a different election + el, err = RoomTUI(client, roomID, elections) + } else if el.StartID == nil { + // user wants to join the election + err = el.JoinElection(client, elections.EventStore) } + // otherwise, user wants to wait for results return } - // user wants to create election (i == 0) + // user opted to create election (i == 0) title, candidates := CreateElectionTUI() if candidates == nil { return @@ -180,23 +182,24 @@ func RoomTUI(client *mautrix.Client, roomID id.RoomID, elections *election.Elect } }) app.SetRoot(list, true) - err = app.Run() + err2 := app.Run() + if err == nil { + err = err2 + } return } func electionConfirmation(el *election.Election) (shouldJoin bool) { app := newTallyardApplication() - - var buttons []string var text string el.RLock() // TODO: handle when election starts while in modal - if el.StartID != nil { - buttons = []string{"Yes", "No"} - text = "Election has already started; display results?" + if el.Result != nil { + text = "Election has already concluded; show results?" + } else if el.StartID != nil { + text = "Election has already started; wait for results?" } else { - buttons = []string{"Yes", "No"} text = "Join election?" } el.RUnlock() @@ -208,7 +211,7 @@ func electionConfirmation(el *election.Election) (shouldJoin bool) { modal := tview.NewModal(). SetText(text). - AddButtons(buttons). + AddButtons([]string{"Yes", "No"}). SetDoneFunc(func(_ int, buttonLabel string) { app.Stop() if buttonLabel == "Yes" { -- 2.38.4