added provider class for timer
This commit is contained in:
parent
932e9cd6a4
commit
9ffa0d178c
@ -1,4 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:sendtrain/models/activity_timer_model.dart';
|
||||
import 'package:sendtrain/screens/activities_screen.dart';
|
||||
import 'package:sendtrain/screens/sessions_screen.dart';
|
||||
|
||||
@ -88,5 +90,10 @@ class _AppState extends State<App> {
|
||||
}
|
||||
|
||||
void main() {
|
||||
runApp(const SendTrain());
|
||||
runApp(
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => ActivityTimerModel(),
|
||||
child: const SendTrain(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
60
lib/models/activity_timer_model.dart
Normal file
60
lib/models/activity_timer_model.dart
Normal file
@ -0,0 +1,60 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:sendtrain/models/activity_model.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;
|
||||
|
||||
bool _active = false;
|
||||
|
||||
int get tickCount => _tickCount;
|
||||
int get currentSet => _currentSet;
|
||||
int get currentRep => _currentRep;
|
||||
int get totalSets => _totalSets;
|
||||
int get totalReps => _totalReps;
|
||||
bool get active => _active;
|
||||
|
||||
void setupTimer(ActivityModel 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;
|
||||
}
|
||||
|
||||
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();
|
||||
});
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:sendtrain/classes/activity_action.dart';
|
||||
import 'package:sendtrain/classes/media.dart';
|
||||
import 'package:sendtrain/models/activity_model.dart';
|
||||
import 'package:sendtrain/models/activity_timer_model.dart';
|
||||
import 'package:sendtrain/widgets/activity_action_view.dart';
|
||||
import 'package:sendtrain/widgets/media_card.dart';
|
||||
|
||||
@ -17,44 +17,16 @@ class ActivityView extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ActivityViewState extends State<ActivityView> {
|
||||
Timer? _periodicTimer;
|
||||
int _tickCount = 0;
|
||||
int _currentSet = 1;
|
||||
int _currentRep = 1;
|
||||
String _currentState = 'Rep';
|
||||
|
||||
void _startPeriodicTimer() {
|
||||
_tickCount = widget.activity.actions[0].activityActionSet.rest ~/ 10000;
|
||||
const oneSecond = Duration(seconds: 1);
|
||||
_periodicTimer = Timer.periodic(oneSecond, (Timer timer) {
|
||||
setState(() {
|
||||
if (_tickCount == 0) {
|
||||
if (_currentSet ==
|
||||
widget.activity.actions[0].activityActionSet.total) {
|
||||
timer.cancel();
|
||||
}
|
||||
|
||||
if (_currentRep ==
|
||||
widget.activity.actions[0].activityActionSet.reps
|
||||
.amounts[_currentSet]) {
|
||||
_currentSet++;
|
||||
_currentRep = 1;
|
||||
_tickCount =
|
||||
widget.activity.actions[0].activityActionSet.rest ~/ 10000;
|
||||
} else {
|
||||
_currentRep++;
|
||||
}
|
||||
} else {
|
||||
_tickCount--;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
ActivityModel activity = widget.activity;
|
||||
|
||||
ActivityTimerModel atm = Provider.of<ActivityTimerModel>(context, listen: false);
|
||||
|
||||
if (atm.active == false) {
|
||||
atm.setupTimer(activity);
|
||||
}
|
||||
|
||||
var content = [
|
||||
AppBar(
|
||||
// surfaceTintColor: ThemeData.dark(useMaterial3: true).colorScheme.primary,
|
||||
@ -101,27 +73,34 @@ class _ActivityViewState extends State<ActivityView> {
|
||||
IconButton(
|
||||
iconSize: 30,
|
||||
icon: const Icon(Icons.play_arrow_rounded),
|
||||
onPressed: _startPeriodicTimer),
|
||||
onPressed: () =>
|
||||
atm.startTimer(activity)),
|
||||
IconButton(
|
||||
iconSize: 30,
|
||||
iconSize: 36,
|
||||
icon: const Icon(Icons.skip_next_rounded),
|
||||
onPressed: () {})
|
||||
])),
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: Text(
|
||||
style: const TextStyle(fontSize: 25),
|
||||
textAlign: TextAlign.center,
|
||||
'$_tickCount'),
|
||||
),
|
||||
flex: 1,
|
||||
child: Consumer<ActivityTimerModel>(
|
||||
builder: (context, atm, child) {
|
||||
return Text(
|
||||
style: const TextStyle(fontSize: 25),
|
||||
textAlign: TextAlign.center,
|
||||
'${atm.tickCount}');
|
||||
},
|
||||
)),
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(right: 10),
|
||||
child: Text(
|
||||
style: const TextStyle(fontSize: 15),
|
||||
textAlign: TextAlign.right,
|
||||
'Set: $_currentSet/3 \nRep: $_currentRep/5'))),
|
||||
child: Consumer<ActivityTimerModel>(
|
||||
builder: (context, atm, child) {
|
||||
return Text(
|
||||
style: const TextStyle(fontSize: 15),
|
||||
textAlign: TextAlign.right,
|
||||
'Set: ${atm.currentSet + 1}/${atm.totalSets}\nRep: ${atm.currentRep + 1}/${atm.totalReps}');
|
||||
}))),
|
||||
])));
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start, children: content);
|
||||
|
Loading…
x
Reference in New Issue
Block a user