-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_user_task.dart
66 lines (54 loc) · 1.84 KB
/
audio_user_task.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
part of pulmonary_monitor_app;
/// A user task handling audio recordings.
///
/// The [widget] returns an [AudioMeasurePage] that can be shown on the UI.
///
/// When the recording is started (calling the [onRecord] method),
/// the background task collecting sensor measures is started.
class AudioUserTask extends UserTask {
static const String AUDIO_TYPE = 'audio';
final StreamController<int> _countDownController =
StreamController.broadcast();
Stream<int> get countDownEvents => _countDownController.stream;
Timer? _timer;
/// Duration of audio recording in seconds.
int recordingDuration = 10;
AudioUserTask(super.executor, [this.recordingDuration = 10]);
@override
bool get hasWidget => true;
@override
Widget? get widget => AudioMeasurePage(audioUserTask: this);
/// Callback when recording is to start.
/// When recording is started, background sensing is also started.
void onRecord() {
backgroundTaskExecutor.start();
// start the countdown, once tick pr. second.
_timer = Timer.periodic(const Duration(seconds: 1), (_) {
_countDownController.add(--recordingDuration);
if (recordingDuration <= 0) {
_timer?.cancel();
_countDownController.close();
// stop the background sensing and mark this task as done
backgroundTaskExecutor.stop();
super.onDone();
}
});
}
}
/// A factory that can [create] a [UserTask] based on the type of app task.
/// In this case an [AudioUserTask].
class PulmonaryUserTaskFactory implements UserTaskFactory {
@override
List<String> types = [
AudioUserTask.AUDIO_TYPE,
];
@override
UserTask create(AppTaskExecutor executor) {
switch (executor.task.type) {
case AudioUserTask.AUDIO_TYPE:
return AudioUserTask(executor);
default:
return BackgroundSensingUserTask(executor);
}
}
}