alternating timer types

This commit is contained in:
Joshua Burman 2024-12-05 00:44:13 -05:00
parent 29479e8aba
commit 5bae1aa416
4 changed files with 88 additions and 162 deletions

View File

@ -1,148 +1,84 @@
import 'dart:async'; import 'dart:async';
import 'dart:developer';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:sendtrain/models/activity_model.dart'; import 'package:sendtrain/models/activity_model.dart';
import 'package:sendtrain/widgets/activity_action_view.dart';
class ActivityTimerModel with ChangeNotifier { class ActivityTimerModel with ChangeNotifier {
late int _activityId; int _tickCount = 0;
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 _currentAction = 0; int _currentAction = 0;
ActivityModel? _activity;
Timer? _periodicTimer;
List<String> _actionMap = [];
int get tickCount => _tickCount; int get tickCount => _tickCount;
int get currentSet => _currentSet;
int get currentRep => _currentRep;
int get currentAction => _currentAction; int get currentAction => _currentAction;
int get totalSets => _totalSets; ActivityModel? get activity => _activity;
int get totalReps => _totalReps; Timer? get periodicTimer => _periodicTimer;
bool get active => _active; String get actionType => _actionMap[_currentAction];
void setAction(int actionNum, int setNum, String state) { void setup(ActivityModel activity) {
_currentAction = actionNum; // if there isn't an activity yet
_currentSet = setNum; // or we're coming from another activity
_currentState = state; // setup new timer
stopTimer(); 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() { void getValue() {
_currentAction++; if (_actionMap[_currentAction] == "Rest") {
_tickCount = _activity!.actions[0].activityActionSet.rest ~/ 1000;
if (_currentState == "rest") {
_currentState = "rep";
_currentSet++;
stopTimer();
} else { } else {
_currentState = "rest"; _tickCount = _activity!.actions[0].activityActionSet.reps.rest ~/ 1000;
startTimer();
} }
notifyListeners();
} }
void setupTimer(ActivityModel activity) { void pause() {
_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;
_periodicTimer?.cancel(); _periodicTimer?.cancel();
notifyListeners(); notifyListeners();
} }
void startTimer() { void setAction(int actionNum, String type) {
if (_currentState == 'rep') { // always pause if we're manually taversing
// do nothing for now if (type == 'manual') { pause(); }
} else { _currentAction = actionNum;
getValue();
notifyListeners();
}
_active = true; void nextAction(String type) {
_tickCount = _activity.actions[0].activityActionSet.rest ~/ 10000; _currentAction + 1 >= _actionMap.length
? pause()
: setAction(_currentAction + 1, type);
}
_periodicTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) { bool isActive() {
if (_tickCount == 0) { return (_periodicTimer != null && _periodicTimer!.isActive) ? true : false;
incrementAction(); }
void start() {
// if (_currentRep + 1 == if (_activity != null) {
// _activity.actions[0].activityActionSet.reps.amounts[_currentSet]) { _periodicTimer?.cancel();
// _currentSet++; _periodicTimer =
// if (_currentSet == _activity.actions[0].activityActionSet.total) { Timer.periodic(const Duration(seconds: 1), (Timer timer) {
// timer.cancel(); if (_tickCount <= 0) {
// _active = false; nextAction('automatic');
// setupTimer(_activity);
// }
// _currentRep = 0;
// _totalReps =
// _activity.actions[0].activityActionSet.reps.amounts[_currentSet];
// _tickCount = _activity.actions[0].activityActionSet.rest ~/ 10000;
// } else {
// _currentRep++;
// }
} else { } else {
if (_active) { _tickCount--;
_tickCount--;
}
} }
notifyListeners(); 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();
// });
// }
} }

View File

@ -9,38 +9,18 @@ import 'package:sendtrain/widgets/action_card.dart';
class ActivityActionView extends StatefulWidget { class ActivityActionView extends StatefulWidget {
ActivityActionView({super.key, required this.action}); ActivityActionView({super.key, required this.action});
ActivityAction action; ActivityAction action;
// ActivityTimerModel activityTimerModel;
// get incrementActivity => ActivityActionViewState().incrementActivity();
@override @override
State<ActivityActionView> createState() => ActivityActionViewState(); State<ActivityActionView> createState() => ActivityActionViewState();
// void incrementActivity() => _ActivityActionViewState().incrementActivity();
} }
class ActivityActionViewState extends State<ActivityActionView> { class ActivityActionViewState extends State<ActivityActionView> {
int _index = 0;
int _currentAction = 0;
// int _actionCount = 0;
// int _currentState = "Active"
// void incrementActivity(int actionNum) {
// setState(() {
// atm.setAction(actionNum);
// });
// }
// void setActivity(int currentAction) {
// setState(() {
// widget.atm.setAction(currentAction);
// });
// }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
int actionCount = 0; int actionCount = 0;
ActivityTimerModel atm = Provider.of<ActivityTimerModel>(context, listen: true); ActivityTimerModel atm =
Provider.of<ActivityTimerModel>(context, listen: true);
return Expanded( return Expanded(
child: ListView.builder( child: ListView.builder(
@ -56,13 +36,15 @@ class ActivityActionViewState extends State<ActivityActionView> {
contents.add(Card( contents.add(Card(
elevation: 0.5, elevation: 0.5,
// surfaceTintColor: actionCount == _index ? Colors.white : , shadowColor: Theme.of(context).colorScheme.shadow,
color: currentAction == atm.currentAction
? Theme.of(context).colorScheme.secondaryContainer
: Theme.of(context).colorScheme.onSecondary,
clipBehavior: Clip.antiAlias, clipBehavior: Clip.antiAlias,
child: GestureDetector( child: GestureDetector(
onTap: () { onTap: () {
log('we tappn');
setState(() { setState(() {
atm.setAction(currentAction, index, "rep"); atm.setAction(currentAction, 'manual');
}); });
}, },
child: Row(children: [ child: Row(children: [
@ -70,8 +52,8 @@ class ActivityActionViewState extends State<ActivityActionView> {
return Ink( return Ink(
padding: const EdgeInsets.all(15), padding: const EdgeInsets.all(15),
color: currentAction == atm.currentAction color: currentAction == atm.currentAction
? const Color.fromARGB(255, 49, 154, 52) ? Theme.of(context).colorScheme.onPrimary
: const Color(0xff3A5FB6), : Theme.of(context).colorScheme.primaryContainer,
child: Text('Set: ${index + 1} ')); child: Text('Set: ${index + 1} '));
}), }),
Expanded( Expanded(
@ -82,11 +64,14 @@ class ActivityActionViewState extends State<ActivityActionView> {
actionCount++; actionCount++;
contents.add(Card( contents.add(Card(
color: currentAction + 1 == atm.currentAction
? Theme.of(context).colorScheme.secondaryContainer
: Theme.of(context).colorScheme.onSecondary,
clipBehavior: Clip.antiAlias, clipBehavior: Clip.antiAlias,
child: GestureDetector( child: GestureDetector(
onTap: () { onTap: () {
setState(() { setState(() {
atm.setAction(currentAction + 1, index, "rest"); atm.setAction(currentAction + 1, 'manual');
}); });
}, },
child: Row(children: [ child: Row(children: [
@ -94,8 +79,8 @@ class ActivityActionViewState extends State<ActivityActionView> {
return Ink( return Ink(
padding: const EdgeInsets.all(15), padding: const EdgeInsets.all(15),
color: currentAction + 1 == atm.currentAction color: currentAction + 1 == atm.currentAction
? const Color.fromARGB(255, 49, 154, 52) ? Theme.of(context).colorScheme.onPrimary
: const Color(0xff3A5FB6), : Theme.of(context).colorScheme.primaryContainer,
child: Text('Set: ${index + 1} ')); child: Text('Set: ${index + 1} '));
}), }),
Expanded( Expanded(

View File

@ -21,12 +21,8 @@ class _ActivityViewState extends State<ActivityView> {
ActivityModel activity = widget.activity; ActivityModel activity = widget.activity;
ActivityTimerModel atm = ActivityTimerModel atm =
Provider.of<ActivityTimerModel>(context, listen: false); Provider.of<ActivityTimerModel>(context, listen: false);
// ActivityActionView activityActionView =
// ActivityActionView(action: activity.actions[0]);
if (atm.active == false) { atm.setup(activity);
atm.setupTimer(activity);
}
return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
AppBar( AppBar(
@ -60,23 +56,31 @@ class _ActivityViewState extends State<ActivityView> {
ActivityActionView(action: activity.actions[0]), ActivityActionView(action: activity.actions[0]),
Container( Container(
height: MediaQuery.sizeOf(context).height * .07, height: MediaQuery.sizeOf(context).height * .07,
color: color: Theme.of(context).colorScheme.primaryContainer,
ThemeData.dark(useMaterial3: true).colorScheme.primaryContainer,
child: Row(children: [ child: Row(children: [
// LinearProgressIndicator(
// value: 0.5,
// minHeight: 100,
// color: Theme.of(context).colorScheme.error,
// semanticsLabel: 'Linear progress indicator',
// ),
Expanded( Expanded(
flex: 1, flex: 1,
child: Flex(direction: Axis.horizontal, children: [ child: Flex(direction: Axis.horizontal, children: [
IconButton( Consumer<ActivityTimerModel>(builder: (context, atm, child) {
iconSize: 30, return IconButton(
icon: const Icon(Icons.play_arrow_rounded), iconSize: 30,
onPressed: () => { icon: atm.isActive()
atm.startTimer() ? const Icon(Icons.pause_rounded)
}), : const Icon(Icons.play_arrow_rounded),
onPressed: () =>
{atm.isActive() ? atm.pause() : atm.start()});
}),
IconButton( IconButton(
iconSize: 36, iconSize: 36,
icon: const Icon(Icons.skip_next_rounded), icon: const Icon(Icons.skip_next_rounded),
onPressed: () { onPressed: () {
atm.incrementAction(); atm.nextAction('manual');
}) })
])), ])),
Expanded( Expanded(
@ -96,9 +100,10 @@ class _ActivityViewState extends State<ActivityView> {
child: Consumer<ActivityTimerModel>( child: Consumer<ActivityTimerModel>(
builder: (context, atm, child) { builder: (context, atm, child) {
return Text( return Text(
style: const TextStyle(fontSize: 15), style: const TextStyle(fontSize: 20),
textAlign: TextAlign.right, textAlign: TextAlign.right,
'Set: ${atm.currentSet + 1}/${atm.totalSets}\nRep: ${atm.currentRep + 1}/${atm.totalReps}'); "${atm.actionType}");
// 'Set: ${atm.currentSet + 1}/${atm.totalSets}\nRep: ${atm.currentRep + 1}/${atm.totalReps}');
}))), }))),
])) ]))
]); ]);

View File

@ -67,7 +67,7 @@ class SessionCard extends StatelessWidget {
], ],
resources: ['https://www.youtube.com/watch?v=bLz0xp1PEm4']), resources: ['https://www.youtube.com/watch?v=bLz0xp1PEm4']),
ActivityModel( ActivityModel(
id: 1, id: 2,
title: 'Projecting', title: 'Projecting',
type: 'fundamental', type: 'fundamental',
categories: ['technique', 'conditioning'], categories: ['technique', 'conditioning'],
@ -104,7 +104,7 @@ class SessionCard extends StatelessWidget {
], ],
resources: ['https://www.youtube.com/watch?v=dyAvbUvY_PU']), resources: ['https://www.youtube.com/watch?v=dyAvbUvY_PU']),
ActivityModel( ActivityModel(
id: 1, id: 3,
title: 'Weighted Pull Ups', title: 'Weighted Pull Ups',
type: 'fundamental', type: 'fundamental',
categories: ['Strength', 'Power'], categories: ['Strength', 'Power'],