100 lines
2.6 KiB
Dart
100 lines
2.6 KiB
Dart
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import '../../../../backend/api/commands.dart' as commands;
|
|
|
|
part 'settings_controller.g.dart';
|
|
|
|
class SettingsState {
|
|
const SettingsState({
|
|
required this.hostname,
|
|
required this.savePath,
|
|
required this.autoAccept,
|
|
required this.saveHistory,
|
|
required this.enableTls,
|
|
});
|
|
|
|
final String hostname;
|
|
final String savePath;
|
|
final bool autoAccept;
|
|
final bool saveHistory;
|
|
final bool enableTls;
|
|
|
|
SettingsState copyWith({
|
|
String? hostname,
|
|
String? savePath,
|
|
bool? autoAccept,
|
|
bool? saveHistory,
|
|
bool? enableTls,
|
|
}) {
|
|
return SettingsState(
|
|
hostname: hostname ?? this.hostname,
|
|
savePath: savePath ?? this.savePath,
|
|
autoAccept: autoAccept ?? this.autoAccept,
|
|
saveHistory: saveHistory ?? this.saveHistory,
|
|
enableTls: enableTls ?? this.enableTls,
|
|
);
|
|
}
|
|
}
|
|
|
|
@riverpod
|
|
class SettingsController extends _$SettingsController {
|
|
@override
|
|
Future<SettingsState> build() async {
|
|
final values = await Future.wait([
|
|
commands.getHostname(),
|
|
commands.getSavePath(),
|
|
commands.getAutoAccept(),
|
|
commands.getSaveHistory(),
|
|
commands.getEnableTls(),
|
|
]);
|
|
|
|
return SettingsState(
|
|
hostname: values[0] as String,
|
|
savePath: values[1] as String,
|
|
autoAccept: values[2] as bool,
|
|
saveHistory: values[3] as bool,
|
|
enableTls: values[4] as bool,
|
|
);
|
|
}
|
|
|
|
Future<void> updateHostname(String value) async {
|
|
await commands.setHostname(hostname: value);
|
|
final current = state.value;
|
|
if (current != null) {
|
|
state = AsyncData(current.copyWith(hostname: value));
|
|
}
|
|
}
|
|
|
|
Future<void> updateSavePath(String value) async {
|
|
await commands.setSavePath(savePath: value);
|
|
final current = state.value;
|
|
if (current != null) {
|
|
state = AsyncData(current.copyWith(savePath: value));
|
|
}
|
|
}
|
|
|
|
Future<void> updateAutoAccept(bool value) async {
|
|
await commands.setAutoAccept(autoAccept: value);
|
|
final current = state.value;
|
|
if (current != null) {
|
|
state = AsyncData(current.copyWith(autoAccept: value));
|
|
}
|
|
}
|
|
|
|
Future<void> updateSaveHistory(bool value) async {
|
|
await commands.setSaveHistory(saveHistory: value);
|
|
final current = state.value;
|
|
if (current != null) {
|
|
state = AsyncData(current.copyWith(saveHistory: value));
|
|
}
|
|
}
|
|
|
|
Future<void> updateEnableTls(bool value) async {
|
|
await commands.setEnableTls(enableTls: value);
|
|
final current = state.value;
|
|
if (current != null) {
|
|
state = AsyncData(current.copyWith(enableTls: value));
|
|
}
|
|
}
|
|
}
|