import 'dart:async'; import 'package:flutter/material.dart'; import 'package:sendtrain/models/activity_model.dart'; class ActivityTimerModel with ChangeNotifier { late int _activityId; late int _tickCount; late int _currentSet; late int _totalSets; late int _currentRep; late int _totalReps; late String _currentState; late Timer? _periodicTimer; bool _active = false; int get tickCount => _tickCount; int get currentSet => _currentSet; int get currentRep => _currentRep; int get totalSets => _totalSets; int get totalReps => _totalReps; bool get active => _active; void setupTimer(ActivityModel activity) { _activityId = activity.id; _currentSet = 0; _currentRep = 0; _totalSets = activity.actions[0].activityActionSet.total; _totalReps = activity.actions[0].activityActionSet.reps.amounts[0]; _tickCount = activity.actions[0].activityActionSet.rest ~/ 10000; } void startTimer(ActivityModel activity) { _active = true; _periodicTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) { if (_tickCount == 0) { if (_currentRep + 1 == activity.actions[0].activityActionSet.reps.amounts[_currentSet]) { _currentSet++; if (_currentSet == activity.actions[0].activityActionSet.total) { timer.cancel(); _active = false; setupTimer(activity); } _currentRep = 0; _totalReps = activity.actions[0].activityActionSet.reps.amounts[_currentSet]; _tickCount = activity.actions[0].activityActionSet.rest ~/ 10000; } else { _currentRep++; } } else { _tickCount--; } notifyListeners(); }); } }