~edwargix/tallyard

2313b34a2f062ccda1f668b4fc2c7006bd46f604 — David Florness 4 years ago c65b2b2
TUI: differentiate between waiting for results and showing them now
2 files changed, 31 insertions(+), 25 deletions(-)

M election/result.go
M ui/tui.go
M election/result.go => election/result.go +5 -2
@@ 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

M ui/tui.go => ui/tui.go +26 -23
@@ 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" {