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
if stashedOnly {
cfg.DocumentTypes = ui.StashedDocument | ui.NewsDocument
cfg.DocumentTypes = ui.StashedDocuments | ui.NewsDocuments
} else if localOnly {
cfg.DocumentTypes = ui.LocalDocument
cfg.DocumentTypes = ui.LocalDocuments
}
// Run Bubble Tea program

View file

@ -47,17 +47,9 @@ type deletedStashedItemMsg int
type DocumentType byte
const (
LocalDocument DocumentType = 1 << iota
StashedDocument
NewsDocument
)
type loadedState byte
const (
loadedStash loadedState = 1 << iota
loadedNews
loadedLocalFiles
LocalDocuments DocumentType = 1 << iota
StashedDocuments
NewsDocuments
)
type stashState int
@ -81,9 +73,9 @@ type stashModel struct {
noteInput textinput.Model
terminalWidth int
terminalHeight int
stashFullyLoaded bool // have we loaded everything from the server?
loadingFromNetwork bool // are we currently loading something from the network?
loaded loadedState // tracks news, stash and local files loading; we find out with bitmasking
stashFullyLoaded bool // have we loaded all available stashed documents from the server?
loadingFromNetwork bool // are we currently loading something from the network?
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
// value portion with an empty struct.
@ -110,21 +102,16 @@ type stashModel struct {
}
func (m stashModel) localOnly() bool {
return m.cfg.DocumentTypes == LocalDocument
return m.cfg.DocumentTypes == LocalDocuments
}
func (m stashModel) stashedOnly() bool {
return m.cfg.DocumentTypes&LocalDocument == 0
return m.cfg.DocumentTypes&LocalDocuments == 0
}
func (m stashModel) loadingDone() bool {
state := m.loaded&loadedStash != 0 && m.loaded&loadedNews != 0
if m.stashedOnly() {
return state
} else if m.localOnly() {
return m.loaded&loadedLocalFiles != 0
}
return state && m.loaded&loadedLocalFiles != 0
// Do the types loaded match the types we want to have?
return m.loaded == m.cfg.DocumentTypes
}
func (m *stashModel) setSize(width, height int) {
@ -260,24 +247,24 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) {
case stashLoadErrMsg:
m.err = msg.err
m.loaded |= loadedStash // still done, albeit unsuccessfully
m.loaded |= StashedDocuments // still done, albeit unsuccessfully
m.stashFullyLoaded = true
m.loadingFromNetwork = false
case newsLoadErrMsg:
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
case localFileSearchFinished:
m.loaded |= loadedLocalFiles
m.loaded |= LocalDocuments
// Stash results have come in from the server
case gotStashMsg:
// 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
// loaded here.
m.loaded |= loadedStash
m.loaded |= StashedDocuments
m.loadingFromNetwork = false
@ -291,7 +278,7 @@ func stashUpdate(msg tea.Msg, m stashModel) (stashModel, tea.Cmd) {
// News has come in from the server
case gotNewsMsg:
m.loaded |= loadedNews
m.loaded |= NewsDocuments
if len(msg) > 0 {
docs := wrapMarkdowns(newsMarkdown, msg)
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)
if cfg.DocumentTypes == 0 {
cfg.DocumentTypes = LocalDocument | StashedDocument | NewsDocument
cfg.DocumentTypes = LocalDocuments | StashedDocuments | NewsDocuments
}
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,
newCharmClient,
spinner.Tick(m.stash.spinner),
)
}
if cfg.DocumentTypes&LocalDocument != 0 {
if cfg.DocumentTypes&LocalDocuments != 0 {
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
m.stash.loaded |= loadedStash | loadedNews
m.stash.loaded |= StashedDocuments | NewsDocuments
m.stash.loadingFromNetwork = false
}
@ -347,7 +347,7 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
m.keygenState = keygenFinished
// Even though it failed, news/stash loading is finished
m.stash.loaded |= loadedStash | loadedNews
m.stash.loaded |= StashedDocuments | NewsDocuments
m.stash.loadingFromNetwork = false
case keygenSuccessMsg: