abd696b0d9
Add the Settings destination under lib/features/settings with an editable websocket endpoint, truthful connection states, one ping per connect/mount, workspace validation and persistence under new feature-owned keys. Material and Cupertino controls, wrapping copy at large text, pending ping state, and isolated probe tools plus widget/unit coverage.
108 lines
3.3 KiB
Dart
108 lines
3.3 KiB
Dart
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
/// The app's own settings keys. They are deliberately independent of the legacy
|
|
/// web/Android storage: no value is imported, migrated or written under an old
|
|
/// key, and nothing but the endpoint and the server workspace is persisted.
|
|
const String settingsServerUrlKey = 'pipelines.next.serverUrl.v1';
|
|
const String settingsWorkspaceKey = 'pipelines.next.workspace.v1';
|
|
|
|
/// The persisted settings, read as a pair. Both values are trimmed strings; an
|
|
/// absent or unreadable value is empty rather than an error.
|
|
class SettingsValues {
|
|
const SettingsValues({this.serverUrl = '', this.workspace = ''});
|
|
|
|
final String serverUrl;
|
|
final String workspace;
|
|
}
|
|
|
|
/// The persistence seam used by the screen and its tests. Saves report whether
|
|
/// the value is actually stored, so the UI never claims a save that failed.
|
|
abstract interface class SettingsStore {
|
|
Future<SettingsValues> load();
|
|
|
|
Future<bool> saveServerUrl(String value);
|
|
|
|
Future<bool> saveWorkspace(String value);
|
|
}
|
|
|
|
/// `shared_preferences`-backed storage under the `pipelines.next` keys.
|
|
///
|
|
/// Reads are tolerant (an unavailable store means "nothing saved yet"). Saves
|
|
/// return false instead of throwing, letting the screen keep the in-memory
|
|
/// value while saying plainly that it was not stored.
|
|
class SharedPreferencesSettingsStore implements SettingsStore {
|
|
const SharedPreferencesSettingsStore();
|
|
|
|
Future<SharedPreferences?> _preferences() async {
|
|
try {
|
|
return await SharedPreferences.getInstance();
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<SettingsValues> load() async {
|
|
final preferences = await _preferences();
|
|
if (preferences == null) return const SettingsValues();
|
|
try {
|
|
return SettingsValues(
|
|
serverUrl: preferences.getString(settingsServerUrlKey) ?? '',
|
|
workspace: preferences.getString(settingsWorkspaceKey) ?? '',
|
|
);
|
|
} catch (_) {
|
|
return const SettingsValues();
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<bool> saveServerUrl(String value) async {
|
|
final preferences = await _preferences();
|
|
if (preferences == null) return false;
|
|
try {
|
|
return await preferences.setString(settingsServerUrlKey, value.trim());
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<bool> saveWorkspace(String value) async {
|
|
final preferences = await _preferences();
|
|
if (preferences == null) return false;
|
|
try {
|
|
return await preferences.setString(settingsWorkspaceKey, value.trim());
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// An in-memory store for previews and tests. It never touches the platform
|
|
/// store, so no production preference can be mutated by a fixture.
|
|
class MemorySettingsStore implements SettingsStore {
|
|
MemorySettingsStore({this.serverUrl = '', this.workspace = ''});
|
|
|
|
String serverUrl;
|
|
String workspace;
|
|
bool failSaves = false;
|
|
|
|
@override
|
|
Future<SettingsValues> load() async =>
|
|
SettingsValues(serverUrl: serverUrl, workspace: workspace);
|
|
|
|
@override
|
|
Future<bool> saveServerUrl(String value) async {
|
|
if (failSaves) return false;
|
|
serverUrl = value.trim();
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
Future<bool> saveWorkspace(String value) async {
|
|
if (failSaves) return false;
|
|
workspace = value.trim();
|
|
return true;
|
|
}
|
|
}
|