@@ -49,6 +49,7 @@ class AppState extends State<CmdBridgeApp> {
4949 bool _loadingData = false ;
5050 bool _foregroundRefresh = true ;
5151 DateTime ? _lastAutoRefreshAt;
52+ DateTime ? _lastPageOpenRefreshAt;
5253 int _lastLogVersion = - 1 ;
5354 int _lastModelVersion = 0 ;
5455
@@ -87,25 +88,6 @@ class AppState extends State<CmdBridgeApp> {
8788 super .dispose ();
8889 }
8990
90- Duration _refreshIntervalForPage (_InfoPage page) {
91- switch (page) {
92- case _InfoPage .account:
93- return const Duration (seconds: 20 );
94- case _InfoPage .plan:
95- return const Duration (seconds: 15 );
96- case _InfoPage .usage:
97- return const Duration (seconds: 8 );
98- case _InfoPage .limits:
99- return const Duration (seconds: 8 );
100- case _InfoPage .models:
101- return const Duration (seconds: 30 );
102- case _InfoPage .proxy:
103- return const Duration (seconds: 1 );
104- case _InfoPage .cost:
105- return const Duration (seconds: 30 );
106- }
107- }
108-
10991 void _startActivePageRefresh () {
11092 _pageRefreshTimer? .cancel ();
11193 _pageRefreshTimer = Timer .periodic (const Duration (seconds: 1 ), (_) {
@@ -123,9 +105,6 @@ class AppState extends State<CmdBridgeApp> {
123105 void _refreshVisiblePage () {
124106 if (! mounted || _panel != _Panel .main) return ;
125107
126- final now = DateTime .now ();
127- final interval = _refreshIntervalForPage (_infoPage);
128-
129108 if (_showLog && _lastLogVersion != LogStore .version) {
130109 _lastLogVersion = LogStore .version;
131110 setState (() {});
@@ -142,25 +121,30 @@ class AppState extends State<CmdBridgeApp> {
142121 setState (() {});
143122 return ;
144123 }
145-
146- if (_loadingData) return ;
147- if (_lastAutoRefreshAt != null && now.difference (_lastAutoRefreshAt! ) < interval) {
148- return ;
149- }
150-
151- _lastAutoRefreshAt = now;
152- _refreshData (silent: true , foreground: false );
153124 }
154125
155126 void _setInfoPage (_InfoPage page) {
156127 _infoPage = page;
157128 _selectedModelIndex = 0 ;
158129 _infoScrollCtrl.jumpTo (0 );
159- _lastAutoRefreshAt = null ;
160130 _refreshVisiblePage ();
131+ _refreshDataOnPageOpen ();
161132 setState (() {});
162133 }
163134
135+ /// Refresh data once in the background when a page is opened, throttled so
136+ /// rapid page switching does not spam the Command Code API. Continuous
137+ /// per-page polling was removed; use `[r]` for a manual foreground refresh.
138+ void _refreshDataOnPageOpen () {
139+ final now = DateTime .now ();
140+ if (_lastPageOpenRefreshAt != null &&
141+ now.difference (_lastPageOpenRefreshAt! ).inSeconds < 10 ) {
142+ return ;
143+ }
144+ _lastPageOpenRefreshAt = now;
145+ _refreshData (silent: true , foreground: false );
146+ }
147+
164148 Color _notifColor () {
165149 final msg = _status;
166150 if (msg.startsWith ('Data refreshed' ) || msg.contains ('Copied' )) return Colors .green;
@@ -457,6 +441,10 @@ class AppState extends State<CmdBridgeApp> {
457441 }
458442 _loadingData = true ;
459443 _foregroundRefresh = foreground;
444+ _lastAutoRefreshAt = DateTime .now ();
445+ if (foreground) {
446+ _lastPageOpenRefreshAt = _lastAutoRefreshAt;
447+ }
460448 if (! silent) {
461449 _setStatus ('Fetching data...' );
462450 }
0 commit comments