diff --git a/internal/application/application.go b/internal/application/application.go index aa76523..f9827df 100644 --- a/internal/application/application.go +++ b/internal/application/application.go @@ -18,6 +18,7 @@ import ( "rhemawavetech.com/rhemawave/internal/license" "rhemawavetech.com/rhemawave/internal/logger" "rhemawavetech.com/rhemawave/internal/mixer" + "rhemawavetech.com/rhemawave/internal/network" "rhemawavetech.com/rhemawave/internal/page" "rhemawavetech.com/rhemawave/internal/shaderManager" "rhemawavetech.com/rhemawave/internal/sync" @@ -31,6 +32,15 @@ const ( apiURL = "https://portal.rhemawavetech.com/api/v1" ) +type State int + +const ( + NEEDS_INTERNET State = iota + NEEDS_LICENSE_KEY + NEEDS_EMAIL_ADDRESS + READY +) + type Application struct { Window *sdl.Window License *license.Response @@ -40,8 +50,12 @@ type Application struct { DB *gorm.DB Config atomic.Pointer[db.AppConfig] PageRouter *page.Router + state State } +var lastNetworkCheckTime time.Time +var networkIsUp bool + // ---------------------------------------------------------------------------- // Main Entry Point // ---------------------------------------------------------------------------- @@ -54,6 +68,8 @@ func Run() { var initialConfig db.AppConfig var result *gorm.DB + networkIsUp = false + logger.Info("Starting Application") homeDir, err = os.UserHomeDir() @@ -145,6 +161,62 @@ func Run() { app.runEventLoop() } +func (app *Application) checkState() { + if app.state == READY { + return + } + + // app.state = 0 means that there is no state set + // Check for an internet connection first + if !networkIsUp { + if lastNetworkCheckTime.IsZero() || time.Since(lastNetworkCheckTime) >= time.Second { + networkManager, err := network.New() + if err != nil { + logger.Error("error obtaining a network manager %v", err) + return + } + lastNetworkCheckTime = time.Now() + status, err := networkManager.CheckInternetConnection() + if err != nil { + logger.Error("error checking for internet connection %v", err) + return + } + if status.StatusCode != network.StatusWifiConnectedInternetUp && + status.StatusCode != network.StatusEthernetConnectedInternetUp { + if app.state != NEEDS_INTERNET { + app.PageRouter.NavigateTo(page.WIFI) + app.state = NEEDS_INTERNET + } + return + } + networkIsUp = true // ← internet confirmed, don't check again + } else { + return // throttled, and we don't yet know if network is up — can't proceed + } + } + + // Then check for a license key + if app.Config.Load().LicenseKey == "" { + if app.state != NEEDS_LICENSE_KEY { + app.PageRouter.NavigateTo(page.LICENSE_KEY) + app.state = NEEDS_LICENSE_KEY + } + return + } + + // Finally, check for an email address + if app.Config.Load().EmailAddress == "" { + if app.state != NEEDS_EMAIL_ADDRESS { + app.PageRouter.NavigateTo(page.EMAIL_ADDRESS) + app.state = NEEDS_EMAIL_ADDRESS + } + return + } + + app.state = READY + app.PageRouter.NavigateTo(page.HOME) +} + func (app *Application) refreshCache() { var freshConfig db.AppConfig if result := app.DB.First(&freshConfig); result.Error != nil { @@ -277,16 +349,15 @@ func (app *Application) runEventLoop() { if app.PageRouter == nil || app.PageRouter.ActivePage == nil { logger.Fatal("No initial page loaded, Router is empty") } - const frameDelay = time.Second / targetFPS app.running = true lastTime := time.Now() - logger.Info("Entering event loop") for app.running { frameStart := time.Now() + app.checkState() app.handleEvents() if !app.running { break