99 lines
2.6 KiB
Dart
99 lines
2.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:sendtrain/models/activity_model.dart';
|
|
|
|
class ActivityTimerModel with ChangeNotifier {
|
|
int _actionCounter = 0;
|
|
ActivityModel? _activity;
|
|
List _sets = [];
|
|
int _currentActionNum = 0;
|
|
int _currentSetNum = 0;
|
|
Timer? _periodicTimer;
|
|
|
|
int get actionCount => _actionCounter;
|
|
int get currentActionNum => _currentActionNum;
|
|
dynamic get currentAction => currentSet[_currentActionNum];
|
|
int get currentSetNum => _currentSetNum;
|
|
dynamic get currentSet => _sets[_currentSetNum];
|
|
ActivityModel? get activity => _activity;
|
|
List get sets => _sets;
|
|
Timer? get periodicTimer => _periodicTimer;
|
|
bool get isActive => _isActive();
|
|
void get pause => _periodicTimer!.cancel();
|
|
|
|
void setup(ActivityModel activity) {
|
|
_activity = activity;
|
|
_sets = activity.actions[0].items();
|
|
_currentActionNum = 0;
|
|
_currentSetNum = 0;
|
|
setActionCount();
|
|
}
|
|
|
|
bool isCurrentItem(int setNum, int actionNum) {
|
|
if (setNum == _currentSetNum && actionNum == _currentActionNum) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
int totalActions() {
|
|
int count = 0;
|
|
for (int i = 0; i < _sets.length; i++) {
|
|
count = count + _sets[i].length as int;
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
void setActionCount() {
|
|
_actionCounter = currentAction['amount'];
|
|
}
|
|
|
|
void start() {
|
|
_periodicTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
|
|
switch (currentAction['type']) {
|
|
// we don't want to count down
|
|
// if its repititions
|
|
case 'repititions':
|
|
break;
|
|
case 'seconds':
|
|
if (_actionCounter > 0) {
|
|
_actionCounter--;
|
|
} else {
|
|
nextAction(_currentActionNum + 1);
|
|
setActionCount();
|
|
}
|
|
}
|
|
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
void setAction(int setNum, int actionNum, String type) {
|
|
_currentActionNum = actionNum;
|
|
_currentSetNum = setNum;
|
|
notifyListeners();
|
|
}
|
|
|
|
void nextAction(int nextActionIndex) {
|
|
if (currentSet.length > nextActionIndex) {
|
|
setAction(_currentSetNum, nextActionIndex, 'automatic');
|
|
} else if (_sets.length > _currentSetNum + 1) {
|
|
// if the item isn't in the set
|
|
// increment the set and reset action index
|
|
setAction(_currentSetNum + 1, 0, 'automatic');
|
|
} else {
|
|
// if we're done all the sets
|
|
// cancel timer and reset activity
|
|
_periodicTimer!.cancel();
|
|
setup(_activity!);
|
|
}
|
|
}
|
|
|
|
bool _isActive() {
|
|
return (_periodicTimer != null && _periodicTimer!.isActive) ? true : false;
|
|
}
|
|
}
|