Use the same type to track docs to load and docs loaded

This commit is contained in:
Christian Rocha 2020-10-17 23:13:49 -04:00 committed by Christian Rocha
parent 54a7394448
commit 32f0be2688
3 changed files with 22 additions and 35 deletions

View file

@ -265,9 +265,9 @@ func runTUI(stashedOnly bool) error {
cfg.ShowAllFiles = showAllFiles cfg.ShowAllFiles = showAllFiles
if stashedOnly { if stashedOnly {
cfg.DocumentTypes = ui.StashedDocument | ui.NewsDocument cfg.DocumentTypes = ui.StashedDocuments | ui.NewsDocuments
} else if localOnly { } else if localOnly {
cfg.DocumentTypes = ui.LocalDocument cfg.DocumentTypes = ui.LocalDocuments
} }
// Run Bubble Tea program // Run Bubble Tea program

View file

@ -47,17 +47,9 @@ type deletedStashedItemMsg int
type DocumentType byte type DocumentType byte
const ( const (
LocalDocument DocumentType = 1 << iota LocalDocuments DocumentType = 1 << iota
StashedDocument StashedDocuments
NewsDocument NewsDocuments
)
type loadedState byte
const (
loadedStash loadedState = 1 << iota
loadedNews
loadedLocalFiles
) )
type stashState int type stashState int
@ -81,9 +73,9 @@ type stashModel struct {
noteInput textinput.Model noteInput textinput.Model
terminalWidth int terminalWidth int
terminalHeight int terminalHeight int
stashFullyLoaded bool // have we loaded everything from the server? stashFullyLoaded bool // have we loaded all available stashed documents from the server?
loadingFromNetwork bool // are we currently loading something from the network? loadingFromNetwork bool // are we currently loading something from the network?
loaded loadedState // tracks news, stash and local files loading; we find out with bitmasking loaded DocumentType // load status for news, stash and local files loading; we find out exactly with bitmasking
// Paths to files being stashed. We treat this like a set, ignoring the // Paths to files being stashed. We treat this like a set, ignoring the
// value portion with an empty struct. // value portion with an empty struct.
@ -110,21 +102,16 @@ type stashModel struct {
} }
func (m stashModel) localOnly() bool { func (m stashModel) localOnly() bool {
return m.cfg.DocumentTypes == LocalDocument return m.cfg.DocumentTypes == LocalDocuments
} }
func (m stashModel) stashedOnly() bool { func (m stashModel) stashedOnly() bool {
return m.cfg.DocumentTypes&LocalDocument == 0 return m.cfg.DocumentTypes&LocalDocuments == 0
} }
func (m stashModel) loadingDone() bool { func (m stashModel) loadingDone() bool {
state := m.loaded&loadedStash != 0 && m.loaded&loadedNews != 0 // Do the types loaded match the types we want to have?
if m.stashedOnly() { return m.loaded == m.cfg.DocumentTypes
return state
} else if m.localOnly() {
return m.loaded&loadedLocalFiles != 0
}
return state && m.loaded&loadedLocalFiles != 0
} }
func (m *stashModel) setSize(width, height int) { func (m *stashModel) setSize(width, height int) {
@ -260,24 +247,24 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) {
case stashLoadErrMsg: case stashLoadErrMsg:
m.err = msg.err m.err = msg.err
m.loaded |= loadedStash // still done, albeit unsuccessfully m.loaded |= StashedDocuments // still done, albeit unsuccessfully
m.stashFullyLoaded = true m.stashFullyLoaded = true
m.loadingFromNetwork = false m.loadingFromNetwork = false
case newsLoadErrMsg: case newsLoadErrMsg:
m.err = msg.err m.err = msg.err
m.loaded |= loadedNews // still done, albeit unsuccessfully m.loaded |= NewsDocuments // still done, albeit unsuccessfully
// We're finished searching for local files // We're finished searching for local files
case localFileSearchFinished: case localFileSearchFinished:
m.loaded |= loadedLocalFiles m.loaded |= LocalDocuments
// Stash results have come in from the server // Stash results have come in from the server
case gotStashMsg: case gotStashMsg:
// This doesn't mean the whole stash listing is loaded, but some we've // This doesn't mean the whole stash listing is loaded, but some we've
// finished checking for the stash, at least, so mark the stash as // finished checking for the stash, at least, so mark the stash as
// loaded here. // loaded here.
m.loaded |= loadedStash m.loaded |= StashedDocuments
m.loadingFromNetwork = false m.loadingFromNetwork = false
@ -291,7 +278,7 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) {
// News has come in from the server // News has come in from the server
case gotNewsMsg: case gotNewsMsg:
m.loaded |= loadedNews m.loaded |= NewsDocuments
if len(msg) > 0 { if len(msg) > 0 {
docs := wrapMarkdowns(newsMarkdown, msg) docs := wrapMarkdowns(newsMarkdown, msg)
m.addMarkdowns(docs...) m.addMarkdowns(docs...)

View file

@ -201,19 +201,19 @@ func initialize(cfg Config, style string) func() (tea.Model, tea.Cmd) {
m.stash = newStashModel(&cfg, m.authStatus) m.stash = newStashModel(&cfg, m.authStatus)
if cfg.DocumentTypes == 0 { if cfg.DocumentTypes == 0 {
cfg.DocumentTypes = LocalDocument | StashedDocument | NewsDocument cfg.DocumentTypes = LocalDocuments | StashedDocuments | NewsDocuments
} }
var cmds []tea.Cmd var cmds []tea.Cmd
if cfg.DocumentTypes&StashedDocument != 0 || cfg.DocumentTypes&NewsDocument != 0 { if cfg.DocumentTypes&StashedDocuments != 0 || cfg.DocumentTypes&NewsDocuments != 0 {
cmds = append(cmds, cmds = append(cmds,
newCharmClient, newCharmClient,
spinner.Tick(m.stash.spinner), spinner.Tick(m.stash.spinner),
) )
} }
if cfg.DocumentTypes&LocalDocument != 0 { if cfg.DocumentTypes&LocalDocuments != 0 {
cmds = append(cmds, findLocalFiles(m)) cmds = append(cmds, findLocalFiles(m))
} }
@ -332,7 +332,7 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
} }
// Even though it failed, news/stash loading is finished // Even though it failed, news/stash loading is finished
m.stash.loaded |= loadedStash | loadedNews m.stash.loaded |= StashedDocuments | NewsDocuments
m.stash.loadingFromNetwork = false m.stash.loadingFromNetwork = false
} }
@ -347,7 +347,7 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
m.keygenState = keygenFinished m.keygenState = keygenFinished
// Even though it failed, news/stash loading is finished // Even though it failed, news/stash loading is finished
m.stash.loaded |= loadedStash | loadedNews m.stash.loaded |= StashedDocuments | NewsDocuments
m.stash.loadingFromNetwork = false m.stash.loadingFromNetwork = false
case keygenSuccessMsg: case keygenSuccessMsg: