815fc48a15
- Use const Iperf3Result constructor in tests - Remove unused import from widget_test.dart - Fix unnecessary underscore warnings in Consumer builders - Use initializing formal for iperf3Service field - Wire up main.dart to call Iperf3BinaryPathProvider.initialize()
58 lines
1.5 KiB
Dart
58 lines
1.5 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:iperf3gui/data/services/iperf3_service.dart';
|
|
import 'package:iperf3gui/domain/models/test_configuration.dart';
|
|
import 'package:iperf3gui/domain/models/iperf3_result.dart';
|
|
|
|
/// State of the test run.
|
|
enum TestRunState { idle, running, completed, error }
|
|
|
|
/// ViewModel for managing test run state.
|
|
class TestRunViewModel extends ChangeNotifier {
|
|
final Iperf3Service iperf3Service;
|
|
|
|
TestRunState _state = TestRunState.idle;
|
|
TestRunState get state => _state;
|
|
|
|
Iperf3Result? _result;
|
|
Iperf3Result? get result => _result;
|
|
|
|
TestConfiguration? _currentConfig;
|
|
TestConfiguration? get currentConfig => _currentConfig;
|
|
|
|
TestRunViewModel({required this.iperf3Service});
|
|
|
|
/// Start a test with the given configuration.
|
|
Future<void> runTest(TestConfiguration config) async {
|
|
if (!config.isValid) {
|
|
_state = TestRunState.error;
|
|
notifyListeners();
|
|
return;
|
|
}
|
|
|
|
_state = TestRunState.running;
|
|
_currentConfig = config;
|
|
_result = null;
|
|
notifyListeners();
|
|
|
|
try {
|
|
final result = await iperf3Service.run(config);
|
|
_result = result;
|
|
_state = result.exitCode == 0
|
|
? TestRunState.completed
|
|
: TestRunState.error;
|
|
} catch (e) {
|
|
_state = TestRunState.error;
|
|
} finally {
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
/// Reset to idle state.
|
|
void reset() {
|
|
_state = TestRunState.idle;
|
|
_result = null;
|
|
_currentConfig = null;
|
|
notifyListeners();
|
|
}
|
|
}
|