alternating timer types
This commit is contained in:
@ -1,148 +1,84 @@
|
||||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:sendtrain/models/activity_model.dart';
|
||||
import 'package:sendtrain/widgets/activity_action_view.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;
|
||||
late ActivityModel _activity;
|
||||
|
||||
// int _viewCount = 0;
|
||||
bool _active = false;
|
||||
int _tickCount = 0;
|
||||
int _currentAction = 0;
|
||||
ActivityModel? _activity;
|
||||
Timer? _periodicTimer;
|
||||
List<String> _actionMap = [];
|
||||
|
||||
int get tickCount => _tickCount;
|
||||
int get currentSet => _currentSet;
|
||||
int get currentRep => _currentRep;
|
||||
int get currentAction => _currentAction;
|
||||
int get totalSets => _totalSets;
|
||||
int get totalReps => _totalReps;
|
||||
bool get active => _active;
|
||||
ActivityModel? get activity => _activity;
|
||||
Timer? get periodicTimer => _periodicTimer;
|
||||
String get actionType => _actionMap[_currentAction];
|
||||
|
||||
void setAction(int actionNum, int setNum, String state) {
|
||||
_currentAction = actionNum;
|
||||
_currentSet = setNum;
|
||||
_currentState = state;
|
||||
stopTimer();
|
||||
void setup(ActivityModel activity) {
|
||||
// if there isn't an activity yet
|
||||
// or we're coming from another activity
|
||||
// setup new timer
|
||||
if (_activity == null || activity.id != _activity?.id) {
|
||||
_periodicTimer?.cancel();
|
||||
_activity = activity;
|
||||
_currentAction = 0;
|
||||
_actionMap = [];
|
||||
// for now we just do alternating rest/sets
|
||||
// in the future, we'll make this more modifiable
|
||||
for (int i = 0; i < activity.actions[0].activityActionSet.total; i++) {
|
||||
_actionMap.addAll(['Set', 'Rest']);
|
||||
|
||||
if (_currentState == "rest") {
|
||||
startTimer();
|
||||
}
|
||||
getValue();
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void incrementAction() {
|
||||
_currentAction++;
|
||||
void getValue() {
|
||||
if (_actionMap[_currentAction] == "Rest") {
|
||||
_tickCount = _activity!.actions[0].activityActionSet.rest ~/ 1000;
|
||||
|
||||
if (_currentState == "rest") {
|
||||
_currentState = "rep";
|
||||
_currentSet++;
|
||||
stopTimer();
|
||||
} else {
|
||||
_currentState = "rest";
|
||||
startTimer();
|
||||
_tickCount = _activity!.actions[0].activityActionSet.reps.rest ~/ 1000;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setupTimer(ActivityModel activity) {
|
||||
_activity = 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;
|
||||
_currentState = "rep";
|
||||
// ActivityActionView av = actionViews[_viewCount];
|
||||
// av.
|
||||
}
|
||||
|
||||
void pauseTimer() {
|
||||
_active = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void stopTimer() {
|
||||
_active = false;
|
||||
void pause() {
|
||||
_periodicTimer?.cancel();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void startTimer() {
|
||||
if (_currentState == 'rep') {
|
||||
// do nothing for now
|
||||
} else {
|
||||
void setAction(int actionNum, String type) {
|
||||
// always pause if we're manually taversing
|
||||
if (type == 'manual') { pause(); }
|
||||
_currentAction = actionNum;
|
||||
getValue();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
_active = true;
|
||||
_tickCount = _activity.actions[0].activityActionSet.rest ~/ 10000;
|
||||
void nextAction(String type) {
|
||||
_currentAction + 1 >= _actionMap.length
|
||||
? pause()
|
||||
: setAction(_currentAction + 1, type);
|
||||
}
|
||||
|
||||
_periodicTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
|
||||
if (_tickCount == 0) {
|
||||
incrementAction();
|
||||
bool isActive() {
|
||||
return (_periodicTimer != null && _periodicTimer!.isActive) ? true : false;
|
||||
}
|
||||
|
||||
|
||||
// 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++;
|
||||
// }
|
||||
void start() {
|
||||
if (_activity != null) {
|
||||
_periodicTimer?.cancel();
|
||||
_periodicTimer =
|
||||
Timer.periodic(const Duration(seconds: 1), (Timer timer) {
|
||||
if (_tickCount <= 0) {
|
||||
nextAction('automatic');
|
||||
} else {
|
||||
if (_active) {
|
||||
_tickCount--;
|
||||
}
|
||||
_tickCount--;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 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();
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
Reference in New Issue
Block a user