import '../../about/internal/application/DeploymentRecord.dart'; import '../../about/internal/application/ServerProfile.dart'; import '../parent/ParentViewModel.dart'; import 'ConnectHome.dart'; class ViewHome extends ParentViewModel { ConnectHome connection; ViewHome(super.context, this.connection); /// Re-reads servers and history. Called on open and whenever a child screen /// pops back having changed something. void loadDashboard() async { try { final servers = await getDataManager().getServers(); final history = await getDataManager().getDeployments(); connection.onDashboardLoaded(servers, history); } catch (error) { handleError(error, loadDashboard, () {}, "Retry"); } } /// Inserts or updates [server] in storage, then refreshes the dashboard. void saveServer(ServerProfile server) async { try { final servers = await getDataManager().getServers(); final index = servers.indexWhere((s) => s.id == server.id); if (index >= 0) { servers[index] = server; } else { servers.add(server); } await getDataManager().setServers(servers); connection.onServerSaved(server); loadDashboard(); } catch (error) { handleError(error, () => saveServer(server), () {}, "Retry"); } } /// Removes [server] and its stored password. void deleteServer(ServerProfile server) async { try { final servers = await getDataManager().getServers(); servers.removeWhere((s) => s.id == server.id); await getDataManager().setServers(servers); await getDataManager().deleteServerSecret(server.id); loadDashboard(); } catch (error) { handleError(error, () => deleteServer(server), () {}, "Retry"); } } /// Appends a deployment outcome to the history. void recordDeployment(DeploymentRecord record) async { try { await getDataManager().addDeployment(record); loadDashboard(); } catch (error) { handleError(error, () => recordDeployment(record), () {}, "Retry"); } } void clearHistory() async { try { await getDataManager().clearDeployments(); loadDashboard(); } catch (error) { handleError(error, clearHistory, () {}, "Retry"); } } /// Advances the theme dark → light → system → dark and persists the choice. void cycleTheme(String current) async { try { final next = switch (current) { 'dark' => 'light', 'light' => 'system', _ => 'dark', }; await getDataManager().setThemeMode(next); connection.onThemeChanged(next); } catch (error) { handleError(error, () => cycleTheme(current), () {}, "Retry"); } } }