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: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();
// });
// }
}

View File

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

View File

@ -21,12 +21,8 @@ class _ActivityViewState extends State<ActivityView> {
ActivityModel activity = widget.activity;
ActivityTimerModel atm =
Provider.of<ActivityTimerModel>(context, listen: false);
// ActivityActionView activityActionView =
// ActivityActionView(action: activity.actions[0]);
if (atm.active == false) {
atm.setupTimer(activity);
}
atm.setup(activity);
return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
AppBar(
@ -60,23 +56,31 @@ class _ActivityViewState extends State<ActivityView> {
ActivityActionView(action: activity.actions[0]),
Container(
height: MediaQuery.sizeOf(context).height * .07,
color:
ThemeData.dark(useMaterial3: true).colorScheme.primaryContainer,
color: Theme.of(context).colorScheme.primaryContainer,
child: Row(children: [
// LinearProgressIndicator(
// value: 0.5,
// minHeight: 100,
// color: Theme.of(context).colorScheme.error,
// semanticsLabel: 'Linear progress indicator',
// ),
Expanded(
flex: 1,
child: Flex(direction: Axis.horizontal, children: [
IconButton(
iconSize: 30,
icon: const Icon(Icons.play_arrow_rounded),
onPressed: () => {
atm.startTimer()
}),
Consumer<ActivityTimerModel>(builder: (context, atm, child) {
return IconButton(
iconSize: 30,
icon: atm.isActive()
? const Icon(Icons.pause_rounded)
: const Icon(Icons.play_arrow_rounded),
onPressed: () =>
{atm.isActive() ? atm.pause() : atm.start()});
}),
IconButton(
iconSize: 36,
icon: const Icon(Icons.skip_next_rounded),
onPressed: () {
atm.incrementAction();
atm.nextAction('manual');
})
])),
Expanded(
@ -96,9 +100,10 @@ class _ActivityViewState extends State<ActivityView> {
child: Consumer<ActivityTimerModel>(
builder: (context, atm, child) {
return Text(
style: const TextStyle(fontSize: 15),
style: const TextStyle(fontSize: 20),
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']),
ActivityModel(
id: 1,
id: 2,
title: 'Projecting',
type: 'fundamental',
categories: ['technique', 'conditioning'],
@ -104,7 +104,7 @@ class SessionCard extends StatelessWidget {
],
resources: ['https://www.youtube.com/watch?v=dyAvbUvY_PU']),
ActivityModel(
id: 1,
id: 3,
title: 'Weighted Pull Ups',
type: 'fundamental',
categories: ['Strength', 'Power'],