added sound during countdown, and upgraded minsdkversion
This commit is contained in:
@ -2,6 +2,9 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_sound/public/flutter_sound_player.dart';
|
||||
import 'package:flutter_sound/flutter_sound.dart';
|
||||
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
|
||||
import 'package:sendtrain/database/database.dart';
|
||||
import 'package:sendtrain/models/action_model.dart';
|
||||
@ -11,6 +14,7 @@ class ActionTimer with ChangeNotifier {
|
||||
double _progress = 0;
|
||||
int _currentTime = 0;
|
||||
final List<ItemScrollController> _scrollControllers = [];
|
||||
final FlutterSoundPlayer _mPlayer = FlutterSoundPlayer();
|
||||
|
||||
ActionTimer();
|
||||
|
||||
@ -51,27 +55,41 @@ class ActionTimer with ChangeNotifier {
|
||||
}
|
||||
|
||||
void setup(ActionModel actionModel, ItemScrollController scrollController,
|
||||
[bool resetOnLoad = true]) {
|
||||
[bool resetOnLoad = true]) async {
|
||||
_scrollControllers.add(scrollController);
|
||||
|
||||
if (resetOnLoad) {
|
||||
if (this.actionModel == actionModel) {
|
||||
reset();
|
||||
_scrollControllers.add(scrollController);
|
||||
}
|
||||
|
||||
this.actionModel = actionModel;
|
||||
setAction(currentAction.id);
|
||||
}
|
||||
|
||||
_scrollControllers.add(scrollController);
|
||||
}
|
||||
|
||||
Future pause() async =>
|
||||
await actionModel?.updateStatus(ActionStatus.paused).whenComplete(() {
|
||||
_periodicTimer?.cancel();
|
||||
notifyListeners();
|
||||
|
||||
// _mPlayer.stopPlayer();
|
||||
// Be careful : you must `close` the audio session when you have finished with it.
|
||||
});
|
||||
|
||||
Future start() async {
|
||||
await actionModel!.updateStatus(ActionStatus.started);
|
||||
await _mPlayer.openPlayer();
|
||||
|
||||
Uint8List? countTone;
|
||||
Uint8List? finishTone;
|
||||
await rootBundle
|
||||
.load('assets/audio/count_tone.wav')
|
||||
.then((data) => countTone = data.buffer.asUint8List());
|
||||
await rootBundle
|
||||
.load('assets/audio/count_finish.mp3')
|
||||
.then((data) => finishTone = data.buffer.asUint8List());
|
||||
|
||||
// start timer
|
||||
if (_periodicTimer == null || _periodicTimer!.isActive == false) {
|
||||
@ -83,8 +101,15 @@ class ActionTimer with ChangeNotifier {
|
||||
case RepType.time:
|
||||
_currentTime--;
|
||||
|
||||
if (_currentTime <= 3 && _currentTime != 0) {
|
||||
await _mPlayer.startPlayer(
|
||||
fromDataBuffer: countTone, codec: Codec.pcm16WAV);
|
||||
}
|
||||
|
||||
if (_currentTime == 0) {
|
||||
// move to next action
|
||||
await _mPlayer.startPlayer(
|
||||
fromDataBuffer: finishTone, codec: Codec.mp3);
|
||||
await setAction(state['currentAction'] + 1);
|
||||
}
|
||||
|
||||
@ -97,18 +122,21 @@ class ActionTimer with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future close() async =>
|
||||
await actionModel!.updateStatus(ActionStatus.complete).whenComplete(() {
|
||||
Future close() async => await actionModel!
|
||||
.updateStatus(ActionStatus.complete)
|
||||
.whenComplete(() async {
|
||||
_periodicTimer!.cancel();
|
||||
_mPlayer.closePlayer();
|
||||
notifyListeners();
|
||||
});
|
||||
|
||||
Future reset() async {
|
||||
await actionModel!.updateStatus(ActionStatus.pending);
|
||||
await actionModel!.updateState(json.encode(_stateConstructor()));
|
||||
await actionModel?.updateStatus(ActionStatus.pending);
|
||||
await actionModel?.updateState(json.encode(_stateConstructor()));
|
||||
_periodicTimer?.cancel();
|
||||
_progress = 0;
|
||||
_scrollControllers.clear();
|
||||
_mPlayer.closePlayer();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@ -134,39 +162,49 @@ class ActionTimer with ChangeNotifier {
|
||||
}
|
||||
|
||||
setAction(int actionNum, [bool isManual = false]) async {
|
||||
Item item = allActions[actionNum];
|
||||
Map newState = state;
|
||||
if (actionNum < allActions.length) {
|
||||
Item item = allActions[actionNum];
|
||||
Map newState = state;
|
||||
|
||||
newState['currentAction'] = actionNum;
|
||||
newState['currentSet'] = item.parentId;
|
||||
newState['currentRep'] = item.id;
|
||||
newState['currentTime'] = _currentTime = item.value!;
|
||||
newState['currentAction'] = actionNum;
|
||||
newState['currentSet'] = item.parentId;
|
||||
newState['currentRep'] = item.id;
|
||||
newState['currentTime'] = _currentTime = item.value!;
|
||||
|
||||
await actionModel!
|
||||
.updateState(json.encode(newState))
|
||||
.whenComplete(() async {
|
||||
if (isManual) {
|
||||
await pause();
|
||||
await updateProgress();
|
||||
}
|
||||
await actionModel!
|
||||
.updateState(json.encode(newState))
|
||||
.whenComplete(() async {
|
||||
// if manual select, pause next action
|
||||
if (isManual) {
|
||||
await pause();
|
||||
await updateProgress();
|
||||
}
|
||||
|
||||
int index = currentAction.parentId != null
|
||||
? currentAction.parentId!
|
||||
: currentAction.id;
|
||||
int index = currentAction.parentId != null
|
||||
? currentAction.parentId!
|
||||
: currentAction.id;
|
||||
|
||||
for (int i = 0; i < _scrollControllers.length; i++) {
|
||||
ItemScrollController sc = _scrollControllers[i];
|
||||
if (_scrollControllers.isNotEmpty) {
|
||||
for (int i = 0; i < _scrollControllers.length; i++) {
|
||||
ItemScrollController sc = _scrollControllers[i];
|
||||
|
||||
sc.scrollTo(
|
||||
index: index,
|
||||
duration: Duration(milliseconds: 500),
|
||||
curve: Curves.easeInOutCubic);
|
||||
}
|
||||
// _scrollController?.scrollTo(
|
||||
// index: index,
|
||||
// duration: Duration(milliseconds: 500),
|
||||
// curve: Curves.easeInOutCubic);
|
||||
});
|
||||
sc.scrollTo(
|
||||
index: index,
|
||||
duration: Duration(milliseconds: 500),
|
||||
curve: Curves.easeInOutCubic);
|
||||
}
|
||||
}
|
||||
// _scrollController?.scrollTo(
|
||||
// index: index,
|
||||
// duration: Duration(milliseconds: 500),
|
||||
// curve: Curves.easeInOutCubic);
|
||||
});
|
||||
} else {
|
||||
await actionModel?.updateStatus(ActionStatus.complete).whenComplete(() {
|
||||
_periodicTimer?.cancel();
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
Reference in New Issue
Block a user