114 lines
4.5 KiB
Dart
114 lines
4.5 KiB
Dart
import '../../core/transport/pipelines_client.dart' as transport;
|
||
|
||
/// Field and explanatory copy, matching the current web Settings destination
|
||
/// (web/src/settings.js) and the Android SettingsScreen it quotes.
|
||
const String serverUrlPlaceholder = 'ws://192.168.1.20:8081/connect';
|
||
|
||
const String serverUrlHelper =
|
||
'ws:// only on a trusted LAN/VPN, or wss:// for TLS. '
|
||
'Use the authenticated web proxy’s /connect address when sign-in is required.';
|
||
|
||
const String workspacePlaceholder = '/Users/you/repos/pipelines';
|
||
|
||
const String workspaceHelper =
|
||
'An absolute directory that already exists on the execution server. For '
|
||
'Docker, mount the host directory at the same absolute path with its '
|
||
"owner's UID/GID. This is not the phone's storage.";
|
||
|
||
const String workspacePathError =
|
||
'The workspace must be an absolute path that exists on the execution '
|
||
'server.';
|
||
|
||
const String trustTitle = 'Trusted LAN/VPN only';
|
||
|
||
const String trustBody =
|
||
'Use the authenticated web address for remote access. Direct API access '
|
||
'has no authentication and must stay on a trusted LAN/VPN. ws:// traffic '
|
||
'is unencrypted; prefer wss://. Never enter provider credentials here.';
|
||
|
||
/// The workspace draft is valid when it is untouched (empty) or absolute. A
|
||
/// relative path is the one rejected value; the execution server resolves the
|
||
/// path, so the phone's filesystem is irrelevant.
|
||
String? validateWorkspacePath(String value) {
|
||
final trimmed = value.trim();
|
||
if (trimmed.isEmpty) return null;
|
||
if (!trimmed.startsWith('/')) return workspacePathError;
|
||
return null;
|
||
}
|
||
|
||
/// Saving requires a real absolute path, not just "no error": the web Settings
|
||
/// page deliberately refuses a blank field and records that strictness.
|
||
bool canSaveWorkspace(String value) {
|
||
final trimmed = value.trim();
|
||
return trimmed.isNotEmpty && validateWorkspacePath(trimmed) == null;
|
||
}
|
||
|
||
/// The persisted workspace line. An absent value is named, never rendered as a
|
||
/// zero or an empty gap.
|
||
String savedWorkspaceLine(String value) {
|
||
final trimmed = value.trim();
|
||
return 'Saved workspace: ${trimmed.isEmpty ? '(none)' : trimmed}';
|
||
}
|
||
|
||
/// The exact shared connecting sentence used by the banner and status card.
|
||
String connectingLabel(int attempt) => 'Connecting (attempt $attempt)\u2026';
|
||
|
||
/// `Server build {version} ({commit})`, with whichever parts the ping reported.
|
||
String serverBuildLabel({String? version, String? commit}) {
|
||
final v = (version ?? '').trim();
|
||
final c = (commit ?? '').trim();
|
||
final parts = <String>[if (v.isNotEmpty) v, if (c.isNotEmpty) '($c)'];
|
||
if (parts.isEmpty) return '';
|
||
return 'Server build ${parts.join(' ')}';
|
||
}
|
||
|
||
/// The pending sentence shown between `Connected` and the ping reply. It is a
|
||
/// request in flight, not a missing value.
|
||
const String serverDetailsPending = 'Loading server details\u2026';
|
||
|
||
/// The status card's lines in the web/Android order. While the ping is in
|
||
/// flight the card says it is loading. A completed reply names the build and
|
||
/// time, saying "unavailable" only when that reply omitted them; a failed ping
|
||
/// is one explicit line instead of two "unavailable" lines plus the error.
|
||
List<String> connectionCardLines(
|
||
transport.ConnectionState state, {
|
||
String? buildVersion,
|
||
String? buildCommit,
|
||
String? serverTime,
|
||
String pingError = '',
|
||
bool pingInFlight = false,
|
||
}) {
|
||
switch (state.kind) {
|
||
case transport.ConnectionKind.connected:
|
||
final build = serverBuildLabel(
|
||
version: buildVersion,
|
||
commit: buildCommit,
|
||
);
|
||
final time = (serverTime ?? '').trim();
|
||
final failure = pingError.trim();
|
||
if (pingInFlight && build.isEmpty && time.isEmpty && failure.isEmpty) {
|
||
return const <String>['Connected', serverDetailsPending];
|
||
}
|
||
if (failure.isNotEmpty && build.isEmpty && time.isEmpty) {
|
||
return <String>['Connected', failure];
|
||
}
|
||
return <String>[
|
||
'Connected',
|
||
build.isEmpty ? 'Server build unavailable' : build,
|
||
time.isEmpty ? 'Server time unavailable' : 'Server time $time',
|
||
if (failure.isNotEmpty) failure,
|
||
];
|
||
case transport.ConnectionKind.connecting:
|
||
return <String>[connectingLabel(state.attempt)];
|
||
case transport.ConnectionKind.disconnected:
|
||
final reason = state.reason.trim();
|
||
return <String>[
|
||
'Disconnected',
|
||
if (reason.isNotEmpty) reason,
|
||
if (state.retrying) 'Retrying with backoff\u2026',
|
||
];
|
||
case transport.ConnectionKind.idle:
|
||
return const <String>['Not connected'];
|
||
}
|
||
}
|