feat: implement dynamic server list loading from API
Add internal/api package to fetch server list from /api/servers.json endpoint Replace hardcoded server list with dynamic API call Implement loading states, error handling, and refresh capability
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/app"
|
||||
|
||||
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/api"
|
||||
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/auth"
|
||||
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/config"
|
||||
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/ui/screens"
|
||||
@@ -20,12 +21,13 @@ func Launch(client *auth.Client, session *auth.Session, settings config.Settings
|
||||
w.Resize(fyne.NewSize(float32(settings.Width), float32(settings.Height)))
|
||||
w.CenterOnScreen()
|
||||
|
||||
// TODO: fetch from /api/servers.json
|
||||
serverList := []screens.Modpack{
|
||||
{Slug: "hitech", Name: "HiTech 1.21"},
|
||||
{Slug: "vanilla", Name: "Vanilla 1.20"},
|
||||
}
|
||||
// State for server list loading
|
||||
var serverList []screens.Modpack
|
||||
var isServerListLoading bool
|
||||
var serverListError error
|
||||
_ = serverListError // Use variable to prevent "declared and not used" error
|
||||
|
||||
// Callback functions
|
||||
onPlay := func() {
|
||||
// TODO: launch selected modpack
|
||||
}
|
||||
@@ -38,15 +40,38 @@ func Launch(client *auth.Client, session *auth.Session, settings config.Settings
|
||||
})
|
||||
}
|
||||
|
||||
content := screens.MainScreen(w, client, session, serverList, onPlay, onSettings)
|
||||
// Function to refresh server list
|
||||
var refreshServerList func()
|
||||
refreshServerList = func() {
|
||||
isServerListLoading = true
|
||||
serverListError = nil
|
||||
// Update UI to show loading state
|
||||
content := screens.MainScreen(w, client, session, serverList, onPlay, onSettings, refreshServerList, isServerListLoading, serverListError)
|
||||
w.SetContent(content)
|
||||
|
||||
// Fetch server list in goroutine to avoid blocking UI
|
||||
go func() {
|
||||
serverList, serverListError = api.FetchServerList(settings.ServerURL)
|
||||
isServerListLoading = false
|
||||
// Update UI with result
|
||||
content := screens.MainScreen(w, client, session, serverList, onPlay, onSettings, refreshServerList, isServerListLoading, serverListError)
|
||||
w.SetContent(content)
|
||||
}()
|
||||
}
|
||||
|
||||
// Initial load of server list
|
||||
content := screens.MainScreen(w, client, session, serverList, onPlay, onSettings, refreshServerList, isServerListLoading, serverListError)
|
||||
w.SetContent(content)
|
||||
|
||||
// Start fetching server list in background
|
||||
go refreshServerList()
|
||||
|
||||
// If no session, show login modal after the window is rendered.
|
||||
if session == nil {
|
||||
w.Show()
|
||||
screens.LoginScreen(w, client, func(sess *auth.Session) {
|
||||
// Refresh UI with logged-in state.
|
||||
content := screens.MainScreen(w, client, sess, serverList, onPlay, onSettings)
|
||||
content := screens.MainScreen(w, client, sess, serverList, onPlay, onSettings, refreshServerList, isServerListLoading, serverListError)
|
||||
w.SetContent(content)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user