mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
migrate: riverpod + freezed (#870)
This commit is contained in:
@@ -1,45 +1,57 @@
|
||||
import 'package:fl_lib/fl_lib.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:server_box/core/extension/ssh_client.dart';
|
||||
import 'package:server_box/data/model/app/scripts/script_consts.dart';
|
||||
import 'package:server_box/data/model/server/server.dart';
|
||||
import 'package:server_box/data/model/server/server_private_info.dart';
|
||||
import 'package:server_box/data/model/server/systemd.dart';
|
||||
import 'package:server_box/data/provider/server.dart';
|
||||
|
||||
final class SystemdProvider {
|
||||
late final VNode<Server> _si;
|
||||
part 'systemd.freezed.dart';
|
||||
part 'systemd.g.dart';
|
||||
|
||||
SystemdProvider.init(Spi spi) {
|
||||
_si = ServerProvider.pick(spi: spi)!;
|
||||
getUnits();
|
||||
}
|
||||
@freezed
|
||||
abstract class SystemdState with _$SystemdState {
|
||||
const factory SystemdState({
|
||||
@Default(false) bool isBusy,
|
||||
@Default(<SystemdUnit>[]) List<SystemdUnit> units,
|
||||
@Default(SystemdScopeFilter.all) SystemdScopeFilter scopeFilter,
|
||||
}) = _SystemdState;
|
||||
}
|
||||
|
||||
final isBusy = false.vn;
|
||||
final units = <SystemdUnit>[].vn;
|
||||
final scopeFilter = SystemdScopeFilter.all.vn;
|
||||
@riverpod
|
||||
class SystemdNotifier extends _$SystemdNotifier {
|
||||
late final ServerState _si;
|
||||
|
||||
void dispose() {
|
||||
isBusy.dispose();
|
||||
units.dispose();
|
||||
scopeFilter.dispose();
|
||||
@override
|
||||
SystemdState build(Spi spi) {
|
||||
final si = ref.read(individualServerNotifierProvider(spi.id));
|
||||
_si = si;
|
||||
// Async initialization
|
||||
Future.microtask(() => getUnits());
|
||||
return const SystemdState();
|
||||
}
|
||||
|
||||
List<SystemdUnit> get filteredUnits {
|
||||
switch (scopeFilter.value) {
|
||||
switch (state.scopeFilter) {
|
||||
case SystemdScopeFilter.all:
|
||||
return units.value;
|
||||
return state.units;
|
||||
case SystemdScopeFilter.system:
|
||||
return units.value.where((unit) => unit.scope == SystemdUnitScope.system).toList();
|
||||
return state.units.where((unit) => unit.scope == SystemdUnitScope.system).toList();
|
||||
case SystemdScopeFilter.user:
|
||||
return units.value.where((unit) => unit.scope == SystemdUnitScope.user).toList();
|
||||
return state.units.where((unit) => unit.scope == SystemdUnitScope.user).toList();
|
||||
}
|
||||
}
|
||||
|
||||
void setScopeFilter(SystemdScopeFilter filter) {
|
||||
state = state.copyWith(scopeFilter: filter);
|
||||
}
|
||||
|
||||
Future<void> getUnits() async {
|
||||
isBusy.value = true;
|
||||
state = state.copyWith(isBusy: true);
|
||||
|
||||
try {
|
||||
final client = _si.value.client;
|
||||
final client = _si.client;
|
||||
final result = await client!.execForOutput(_getUnitsCmd);
|
||||
final units = result.split('\n');
|
||||
|
||||
@@ -57,12 +69,11 @@ final class SystemdProvider {
|
||||
|
||||
final parsedUserUnits = await _parseUnitObj(userUnits, SystemdUnitScope.user);
|
||||
final parsedSystemUnits = await _parseUnitObj(systemUnits, SystemdUnitScope.system);
|
||||
this.units.value = [...parsedUserUnits, ...parsedSystemUnits];
|
||||
state = state.copyWith(units: [...parsedUserUnits, ...parsedSystemUnits], isBusy: false);
|
||||
} catch (e, s) {
|
||||
dprint('Parse systemd', e, s);
|
||||
state = state.copyWith(isBusy: false);
|
||||
}
|
||||
|
||||
isBusy.value = false;
|
||||
}
|
||||
|
||||
Future<List<SystemdUnit>> _parseUnitObj(List<String> unitNames, SystemdUnitScope scope) async {
|
||||
@@ -75,7 +86,7 @@ for unit in ${unitNames_.join(' ')}; do
|
||||
echo -n "\n${ScriptConstants.separator}\n"
|
||||
done
|
||||
''';
|
||||
final client = _si.value.client!;
|
||||
final client = _si.client!;
|
||||
final result = await client.execForOutput(script);
|
||||
final units = result.split(ScriptConstants.separator);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user