feat(app): reconnect on foreground and keep the protocol warning
This commit is contained in:
@@ -11,6 +11,21 @@ import 'settings_store.dart';
|
||||
/// the shared client's own `ping` request; tests inject a recorded callback.
|
||||
typedef PingCallback = Future<Map<String, dynamic>> Function();
|
||||
|
||||
/// The integer wire protocol this app speaks. Mirrors `PROTOCOL_VERSION` in
|
||||
/// `web/src/protocol.js` and `ProtocolVersion` in `internal/protocol/identity.go`.
|
||||
const int clientProtocolVersion = 1;
|
||||
|
||||
/// The warning naming both values when the last `pong` reported a different
|
||||
/// protocol. A server that omits the field (an older build) is never flagged,
|
||||
/// and the values are compared as numbers, never as strings — the web's
|
||||
/// `protocolMismatch` rule.
|
||||
String? protocolMismatchWarning(num? serverProtocol) {
|
||||
if (serverProtocol == null) return null;
|
||||
if (serverProtocol == clientProtocolVersion) return null;
|
||||
return 'Protocol mismatch: this page speaks protocol '
|
||||
'$clientProtocolVersion, the server reports $serverProtocol.';
|
||||
}
|
||||
|
||||
/// Owns the Settings screen's draft text, validation and the actions that drive
|
||||
/// the shared [transport.PipelinesClient].
|
||||
///
|
||||
@@ -55,6 +70,7 @@ class SettingsController extends ChangeNotifier {
|
||||
String _buildVersion = '';
|
||||
String _buildCommit = '';
|
||||
String _serverTime = '';
|
||||
num? _serverProtocol;
|
||||
String _pingError = '';
|
||||
bool _pingInFlight = false;
|
||||
bool _pingRequestedForConnected = false;
|
||||
@@ -76,6 +92,12 @@ class SettingsController extends ChangeNotifier {
|
||||
String get buildVersion => _buildVersion;
|
||||
String get buildCommit => _buildCommit;
|
||||
String get serverTime => _serverTime;
|
||||
|
||||
/// The warning for a last `pong` whose protocol differs from this app's.
|
||||
/// The last reported value is kept across a drop (as the web Settings page
|
||||
/// keeps `serverProtocol`), so a foreground reconnect cannot hide a known
|
||||
/// mismatch while the new socket is still connecting.
|
||||
String? get protocolWarning => protocolMismatchWarning(_serverProtocol);
|
||||
String get pingError => _pingError;
|
||||
bool get pingInFlight => _pingInFlight;
|
||||
|
||||
@@ -236,6 +258,7 @@ class SettingsController extends ChangeNotifier {
|
||||
: '${build ?? ''}'.trim();
|
||||
_buildCommit = build is Map ? '${build['commit'] ?? ''}'.trim() : '';
|
||||
_serverTime = '${reply['time'] ?? ''}'.trim();
|
||||
_serverProtocol = _reportedProtocol(reply['protocol']);
|
||||
_pingError = '';
|
||||
} catch (error) {
|
||||
if (!_isCurrentPing(token)) return;
|
||||
@@ -251,6 +274,16 @@ class SettingsController extends ChangeNotifier {
|
||||
bool _isCurrentPing(int token) =>
|
||||
!_disposed && token == _pingToken && _pingRequestedForConnected;
|
||||
|
||||
/// The `pong.protocol` value as the number the comparison needs. A missing,
|
||||
/// blank or non-numeric value is absent — never coerced to zero, which would
|
||||
/// invent a mismatch.
|
||||
num? _reportedProtocol(Object? value) {
|
||||
if (value is num) return value;
|
||||
final text = '${value ?? ''}'.trim();
|
||||
if (text.isEmpty) return null;
|
||||
return num.tryParse(text);
|
||||
}
|
||||
|
||||
void _clearPingDetails() {
|
||||
_pingRequestedForConnected = false;
|
||||
_pingInFlight = false;
|
||||
@@ -258,6 +291,9 @@ class SettingsController extends ChangeNotifier {
|
||||
_buildVersion = '';
|
||||
_buildCommit = '';
|
||||
_serverTime = '';
|
||||
// _serverProtocol is deliberately retained: it belongs to the server the
|
||||
// last pong described, and its warning must stay visible through the
|
||||
// reconnect the app performs when it returns to the foreground.
|
||||
}
|
||||
|
||||
String _describe(Object error) {
|
||||
|
||||
@@ -77,6 +77,18 @@ class SettingsConnectionSection extends StatelessWidget {
|
||||
SettingsStatusCard(controller: controller),
|
||||
const SizedBox(height: settingsTitleGap),
|
||||
const SettingsTrustWarning(),
|
||||
if (controller.protocolWarning case final warning?) ...<Widget>[
|
||||
const SizedBox(height: settingsTitleGap),
|
||||
// The web page appends the same alert after the trust card. A live
|
||||
// region announces a mismatch the operator did not ask to reveal.
|
||||
Semantics(
|
||||
liveRegion: true,
|
||||
child: Text(
|
||||
warning,
|
||||
style: settingsTextStyle(context, error: true),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user