SendTrain/lib/models/activity_timer_model.dart
2024-12-06 17:33:33 -05:00

172 lines
4.8 KiB
Dart

import 'dart:async';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:sendtrain/models/activity_model.dart';
class ActivityTimerModel with ChangeNotifier {
int _actionCount = 0;
ActivityModel? _activity;
List _actions = [];
int _currentActionNum = 0;
int _currentSetNum = 0;
Timer? _periodicTimer;
int get actionCount => _actionCount;
int get currentActionNum => _currentActionNum;
int get currentSetNum => _currentSetNum;
ActivityModel? get activity => _activity;
List get actions => _actions;
Timer? get periodicTimer => _periodicTimer;
String get currentActionType => _actions[_currentSetNum][_currentActionNum]['type'];
void get pause => _periodicTimer!.cancel();
void setup(ActivityModel activity) {
_activity = activity;
_actions = activity.actions[0].items();
_currentActionNum = 0;
_currentSetNum = 0;
setActionCount();
}
bool isActive() {
return (_periodicTimer != null && _periodicTimer!.isActive) ? true : false;
}
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 < _actions.length; i++) {
count = count + _actions[i].length as int;
}
return count;
}
void setActionCount() {
_actionCount = _actions[_currentSetNum][_currentActionNum]['amount'];
}
void start() {
_periodicTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
_actionCount--;
notifyListeners();
});
}
void setAction(int setNum, int actionNum, String type) {
_currentActionNum = actionNum;
_currentSetNum = setNum;
notifyListeners();
}
// void nextAction(String type) {
// setAction(_currentActionNum + 1, _getSet(), type);
// }
// int _actionCount = 0;
// int _currentAction = 0;
// ActivityModel? _activity;
// Timer? _periodicTimer;
// List<String> _actionMap = [];
// int get actionCount => _actionCount;
// int get currentAction => _currentAction;
// ActivityModel? get activity => _activity;
// Timer? get periodicTimer => _periodicTimer;
// String get actionType => _actionMap[_currentAction];
// String get setType => _activity != null
// ? _activity!.actions[0].activityActionSet.reps.type
// : 'n/a';
// String? get repType => actionState();
// List get sets => _activity!.actions[0].items();
// 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
// int totalActions = activity.actions[0].activityActionSet.total;
// // log(activity.actions[0].activityActionSet.type);
// // if (activity.actions[0].activityActionSet.type == 'alternating') {
// // totalActions = totalActions * 4;
// // log('were in $totalActions');
// // }
// for (int i = 0; i < totalActions; i++) {
// _actionMap.addAll(['Set', 'Rest']);
// }
// getValue();
// }
// }
// String actionState() {
// if (actionType == 'Set') {
// return setType == 'seconds' ? 'Seconds' : 'Repititions';
// }
// return 'Seconds';
// }
// void getValue() {
// if (_actionMap[_currentAction] == "Rest") {
// _actionCount = _activity!.actions[0].activityActionSet.rest ~/ 1000;
// } else {
// _actionCount = _activity!.actions[0].activityActionSet.reps.amounts[0];
// }
// }
// void pause() {
// _periodicTimer?.cancel();
// notifyListeners();
// }
// void setAction(int actionNum, String type) {
// // always pause if we're manually taversing
// if (type == 'manual' && setType == 'seconds') {
// pause();
// }
// _currentAction = actionNum;
// getValue();
// notifyListeners();
// }
// void nextAction(String type) {
// _currentAction + 1 >= _actionMap.length
// ? pause()
// : setAction(_currentAction + 1, type);
// }
// bool isActive() {
// return (_periodicTimer != null && _periodicTimer!.isActive) ? true : false;
// }
// void start() {
// if (_activity != null) {
// _periodicTimer?.cancel();
// _periodicTimer =
// Timer.periodic(const Duration(seconds: 1), (Timer timer) {
// if (_actionCount <= 0) {
// nextAction('automatic');
// } else if (actionState() != 'Repititions') {
// _actionCount--;
// }
// notifyListeners();
// });
// }
// }
}