feat: add TestRunViewModel with state management
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
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 Iperf3Service iperf3Service})
|
||||
: _iperf3Service = 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user