-- Navigation System for Virtual Smartphone -- Handles screen transitions and state management -- Screen registry - maps screen names to RML file paths local screens = { home = "screens/home.rml", lock = "screens/lock.rml", dialer = "screens/dialer.rml", contacts = "screens/contacts.rml", messages = "screens/messages.rml", settings = "screens/settings.rml", browser = "screens/browser.rml" } -- Use global state to persist across document loads -- Initialize only if not already set if not _G.nav_state then _G.nav_state = { history = {}, current_screen = "home" } end -- Local references for convenience local history = _G.nav_state.history local function get_current() return _G.nav_state.current_screen end local function set_current(s) _G.nav_state.current_screen = s end -- Navigate to a screen by name function navigateTo(screen_name) print("navigateTo called with: " .. tostring(screen_name)) local path = screens[screen_name] if path then -- Push current screen to history before navigating table.insert(history, get_current()) set_current(screen_name) -- Load the new screen using C++ function local success = loadScreen(path) if success then print("Navigated to: " .. screen_name .. " (history depth: " .. #history .. ")") else -- Restore previous state on failure set_current(table.remove(history)) print("Failed to navigate to: " .. screen_name) end return success else print("Unknown screen: " .. screen_name) return false end end -- Go back to previous screen function goBack() print("goBack called (history depth: " .. #history .. ")") if #history > 0 then local previous = table.remove(history) local path = screens[previous] if path then set_current(previous) loadScreen(path) print("Back to: " .. previous) return true end else print("No history to go back to") end return false end -- Go to home screen (clear history) function goHome() -- Clear the history table for i = #history, 1, -1 do history[i] = nil end set_current("home") loadScreen(screens.home) print("Navigated to home") end -- Get current screen name function getCurrentScreen() return get_current() end -- Check if we can go back function canGoBack() return #history > 0 end -- Clear navigation history function clearHistory() for i = #history, 1, -1 do history[i] = nil end end -- Get history depth function getHistoryDepth() return #history end print("Navigation system initialized (current: " .. get_current() .. ", history: " .. #history .. ")")