update when coming back to installed mods

This commit is contained in:
Vilsol 2022-06-07 03:30:47 +03:00
parent 9d7b5730a2
commit 4b30bffc79
2 changed files with 58 additions and 37 deletions

View file

@ -22,7 +22,7 @@ type installedModsList struct {
root components.RootModel root components.RootModel
list list.Model list list.Model
parent tea.Model parent tea.Model
items chan []list.Item items chan listUpdate
err chan string err chan string
error *components.ErrorComponent error *components.ErrorComponent
@ -34,29 +34,7 @@ func NewInstalledMods(root components.RootModel, parent tea.Model) tea.Model {
return parent return parent
} }
items := make([]list.Item, len(currentProfile.Mods)) l := list.New([]list.Item{}, utils.NewItemDelegate(), root.Size().Width, root.Size().Height-root.Height())
i := 0
for reference := range currentProfile.Mods {
r := reference
items[i] = utils.SimpleItem[installedModsList]{
ItemTitle: reference,
Activate: func(msg tea.Msg, currentModel installedModsList) (tea.Model, tea.Cmd) {
return NewModMenu(root, currentModel, utils.Mod{
Name: r,
Reference: r,
}), nil
},
}
i++
}
sort.Slice(items, func(i, j int) bool {
a := items[i].(utils.SimpleItem[installedModsList])
b := items[j].(utils.SimpleItem[installedModsList])
return ascDesc(sortOrderDesc, a.ItemTitle < b.ItemTitle)
})
l := list.New(items, utils.NewItemDelegate(), root.Size().Width, root.Size().Height-root.Height())
l.SetShowStatusBar(true) l.SetShowStatusBar(true)
l.SetShowFilter(true) l.SetShowFilter(true)
l.SetFilteringEnabled(true) l.SetFilteringEnabled(true)
@ -83,10 +61,46 @@ func NewInstalledMods(root components.RootModel, parent tea.Model) tea.Model {
root: root, root: root,
list: l, list: l,
parent: parent, parent: parent,
items: make(chan []list.Item), items: make(chan listUpdate),
err: make(chan string), err: make(chan string),
} }
return m
}
func (m installedModsList) Init() tea.Cmd {
m.LoadModData()
return utils.Ticker()
}
func (m installedModsList) LoadModData() {
currentProfile := m.root.GetCurrentProfile()
if currentProfile == nil {
return
}
items := make([]list.Item, len(currentProfile.Mods))
i := 0
for reference := range currentProfile.Mods {
r := reference
items[i] = utils.SimpleItem[installedModsList]{
ItemTitle: reference,
Activate: func(msg tea.Msg, currentModel installedModsList) (tea.Model, tea.Cmd) {
return NewModMenu(m.root, currentModel, utils.Mod{
Name: r,
Reference: r,
}), nil
},
}
i++
}
sort.Slice(items, func(i, j int) bool {
a := items[i].(utils.SimpleItem[installedModsList])
b := items[j].(utils.SimpleItem[installedModsList])
return ascDesc(sortOrderDesc, a.ItemTitle < b.ItemTitle)
})
go func() { go func() {
references := make([]string, len(currentProfile.Mods)) references := make([]string, len(currentProfile.Mods))
i := 0 i := 0
@ -95,7 +109,7 @@ func NewInstalledMods(root components.RootModel, parent tea.Model) tea.Model {
i++ i++
} }
mods, err := ficsit.Mods(context.TODO(), root.GetAPIClient(), ficsit.ModFilter{ mods, err := ficsit.Mods(context.TODO(), m.root.GetAPIClient(), ficsit.ModFilter{
References: references, References: references,
}) })
@ -116,7 +130,7 @@ func NewInstalledMods(root components.RootModel, parent tea.Model) tea.Model {
SimpleItem: utils.SimpleItem[installedModsList]{ SimpleItem: utils.SimpleItem[installedModsList]{
ItemTitle: mods.Mods.Mods[i].Name, ItemTitle: mods.Mods.Mods[i].Name,
Activate: func(msg tea.Msg, currentModel installedModsList) (tea.Model, tea.Cmd) { Activate: func(msg tea.Msg, currentModel installedModsList) (tea.Model, tea.Cmd) {
return NewModMenu(root, currentModel, utils.Mod{ return NewModMenu(m.root, currentModel, utils.Mod{
Name: mod.Name, Name: mod.Name,
Reference: mod.Mod_reference, Reference: mod.Mod_reference,
}), nil }), nil
@ -132,14 +146,18 @@ func NewInstalledMods(root components.RootModel, parent tea.Model) tea.Model {
return ascDesc(sortOrderDesc, a.Extra.Mod_reference < b.Extra.Mod_reference) return ascDesc(sortOrderDesc, a.Extra.Mod_reference < b.Extra.Mod_reference)
}) })
m.items <- items m.items <- listUpdate{
Items: items,
Done: true,
}
}() }()
return m go func() {
m.items <- listUpdate{
Items: items,
Done: false,
} }
}()
func (m installedModsList) Init() tea.Cmd {
return utils.Ticker()
} }
func (m installedModsList) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m installedModsList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@ -181,9 +199,12 @@ func (m installedModsList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case utils.TickMsg: case utils.TickMsg:
select { select {
case items := <-m.items: case items := <-m.items:
cmd := m.list.SetItems(items) cmd := m.list.SetItems(items.Items)
if items.Done {
m.list.StopSpinner() m.list.StopSpinner()
return m, cmd return m, cmd
}
return m, tea.Batch(utils.Ticker(), cmd)
case err := <-m.err: case err := <-m.err:
errorComponent, cmd := components.NewErrorComponent(err, time.Second*5) errorComponent, cmd := components.NewErrorComponent(err, time.Second*5)
m.error = errorComponent m.error = errorComponent

View file

@ -33,7 +33,7 @@ func NewModMenu(root components.RootModel, parent tea.Model, mod utils.Mod) tea.
ItemTitle: "Remove Mod", ItemTitle: "Remove Mod",
Activate: func(msg tea.Msg, currentModel modMenu) (tea.Model, tea.Cmd) { Activate: func(msg tea.Msg, currentModel modMenu) (tea.Model, tea.Cmd) {
root.GetCurrentProfile().RemoveMod(mod.Reference) root.GetCurrentProfile().RemoveMod(mod.Reference)
return currentModel.parent, nil return currentModel.parent, currentModel.parent.Init()
}, },
}, },
utils.SimpleItem[modMenu]{ utils.SimpleItem[modMenu]{