Files
MrixsCraft-launcher/internal/ui/ui.go
Vladimir Zagainov e1371e226a 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
2026-06-10 20:12:03 +03:00

81 lines
2.6 KiB
Go

// package ui contains the Fyne GUI bootstrap and wiring.
package ui
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"
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/ui/theme"
)
// Launch initialises the Fyne application with the Minecraft theme and shows the main window.
func Launch(client *auth.Client, session *auth.Session, settings config.Settings) {
a := app.New()
a.Settings().SetTheme(&theme.MinecraftTheme{})
w := a.NewWindow("MrixsCraft")
w.Resize(fyne.NewSize(float32(settings.Width), float32(settings.Height)))
w.CenterOnScreen()
// 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
}
onSettings := func() {
screens.SettingsScreen(w, func(memoryMB int, extraArgs string) {
settings.MemoryMB = memoryMB
settings.ExtraArgs = extraArgs
_ = config.Save(settings)
})
}
// 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, refreshServerList, isServerListLoading, serverListError)
w.SetContent(content)
})
}
w.ShowAndRun()
}