@@ -6,9 +6,12 @@ import 'package:pslab/view/widgets/guide_widget.dart';
6
6
import 'package:pslab/providers/accelerometer_state_provider.dart' ;
7
7
import 'package:pslab/view/widgets/common_scaffold_widget.dart' ;
8
8
import 'package:pslab/view/widgets/accelerometer_card.dart' ;
9
+ import 'package:pslab/others/csv_service.dart' ;
10
+ import 'package:pslab/view/logged_data_screen.dart' ;
9
11
10
12
import '../providers/accelerometer_config_provider.dart' ;
11
13
import '../theme/colors.dart' ;
14
+ import '../constants.dart' ;
12
15
import 'accelerometer_config_screen.dart' ;
13
16
14
17
class AccelerometerScreen extends StatefulWidget {
@@ -22,6 +25,27 @@ class _AccelerometerScreenState extends State<AccelerometerScreen> {
22
25
AppLocalizations appLocalizations = getIt.get <AppLocalizations >();
23
26
bool _showGuide = false ;
24
27
static const imagePath = 'assets/images/bh1750_schematic.png' ;
28
+ final CsvService _csvService = CsvService ();
29
+ late AccelerometerStateProvider _provider;
30
+
31
+ @override
32
+ void initState () {
33
+ super .initState ();
34
+ _provider = AccelerometerStateProvider ();
35
+ WidgetsBinding .instance.addPostFrameCallback ((_) {
36
+ if (mounted) {
37
+ _provider.initializeSensors ();
38
+ }
39
+ });
40
+ }
41
+
42
+ @override
43
+ void dispose () {
44
+ _provider.disposeSensors ();
45
+ _provider.dispose ();
46
+ super .dispose ();
47
+ }
48
+
25
49
void _showInstrumentGuide () {
26
50
setState (() {
27
51
_showGuide = true ;
@@ -80,7 +104,7 @@ class _AccelerometerScreenState extends State<AccelerometerScreen> {
80
104
if (value != null ) {
81
105
switch (value) {
82
106
case 'show_logged_data' :
83
- // TODO
107
+ _navigateToLoggedData ();
84
108
break ;
85
109
case 'accelerometer_config' :
86
110
_navigateToConfig ();
@@ -90,6 +114,19 @@ class _AccelerometerScreenState extends State<AccelerometerScreen> {
90
114
});
91
115
}
92
116
117
+ Future <void > _navigateToLoggedData () async {
118
+ await Navigator .push (
119
+ context,
120
+ MaterialPageRoute (
121
+ builder: (context) => LoggedDataScreen (
122
+ instrumentName: 'accelerometer' ,
123
+ appBarName: appLocalizations.accelerometer,
124
+ instrumentIcon: instrumentIcons[7 ],
125
+ ),
126
+ ),
127
+ );
128
+ }
129
+
93
130
void _navigateToConfig () {
94
131
Navigator .push (
95
132
context,
@@ -102,36 +139,124 @@ class _AccelerometerScreenState extends State<AccelerometerScreen> {
102
139
);
103
140
}
104
141
142
+ Future <void > _toggleRecording () async {
143
+ if (_provider.isRecording) {
144
+ final data = _provider.stopRecording ();
145
+ await _showSaveFileDialog (data);
146
+ } else {
147
+ _provider.startRecording ();
148
+ ScaffoldMessenger .of (context).showSnackBar (
149
+ SnackBar (
150
+ content: Text (
151
+ '${appLocalizations .recordingStarted }...' ,
152
+ style: TextStyle (color: snackBarContentColor),
153
+ ),
154
+ backgroundColor: snackBarBackgroundColor,
155
+ ),
156
+ );
157
+ }
158
+ }
159
+
160
+ Future <void > _showSaveFileDialog (List <List <dynamic >> data) async {
161
+ final TextEditingController filenameController = TextEditingController ();
162
+ final String defaultFilename = '' ;
163
+ filenameController.text = defaultFilename;
164
+
165
+ final String ? fileName = await showDialog <String >(
166
+ context: context,
167
+ builder: (context) {
168
+ return AlertDialog (
169
+ title: Text (appLocalizations.saveRecording),
170
+ content: TextField (
171
+ controller: filenameController,
172
+ decoration: InputDecoration (
173
+ hintText: appLocalizations.enterFileName,
174
+ labelText: appLocalizations.fileName,
175
+ ),
176
+ ),
177
+ actions: [
178
+ TextButton (
179
+ onPressed: () => Navigator .pop (context),
180
+ child: Text (appLocalizations.cancel.toUpperCase ()),
181
+ ),
182
+ ElevatedButton (
183
+ onPressed: () {
184
+ Navigator .pop (context, filenameController.text);
185
+ },
186
+ child: Text (appLocalizations.save),
187
+ ),
188
+ ],
189
+ );
190
+ },
191
+ );
192
+
193
+ if (fileName != null ) {
194
+ _csvService.writeMetaData ('accelerometer' , data);
195
+ final file =
196
+ await _csvService.saveCsvFile ('accelerometer' , fileName, data);
197
+ if (mounted) {
198
+ if (file != null ) {
199
+ ScaffoldMessenger .of (context).showSnackBar (
200
+ SnackBar (
201
+ content: Text (
202
+ '${appLocalizations .fileSaved }: ${file .path .split ('/' ).last }' ,
203
+ style: TextStyle (color: snackBarContentColor),
204
+ ),
205
+ backgroundColor: snackBarBackgroundColor,
206
+ ),
207
+ );
208
+ } else {
209
+ ScaffoldMessenger .of (context).showSnackBar (
210
+ SnackBar (
211
+ content: Text (
212
+ appLocalizations.failedToSave,
213
+ style: TextStyle (color: snackBarContentColor),
214
+ ),
215
+ backgroundColor: snackBarBackgroundColor,
216
+ ),
217
+ );
218
+ }
219
+ }
220
+ }
221
+ }
222
+
105
223
@override
106
224
Widget build (BuildContext context) {
107
- return MultiProvider (
108
- providers: [
109
- ChangeNotifierProvider <AccelerometerStateProvider >(
110
- create: (_) => AccelerometerStateProvider ()..initializeSensors (),
111
- ),
112
- ],
225
+ return ChangeNotifierProvider <AccelerometerStateProvider >.value (
226
+ value: _provider,
113
227
child: Stack (children: [
114
- CommonScaffold (
115
- title: appLocalizations.accelerometerTitle,
116
- onGuidePressed: _showInstrumentGuide,
117
- onOptionsPressed: _showOptionsMenu,
118
- body: SafeArea (
228
+ Consumer <AccelerometerStateProvider >(
229
+ builder: (context, provider, child) {
230
+ return CommonScaffold (
231
+ title: appLocalizations.accelerometerTitle,
232
+ onGuidePressed: _showInstrumentGuide,
233
+ onOptionsPressed: _showOptionsMenu,
234
+ onRecordPressed: _toggleRecording,
235
+ isRecording: provider.isRecording,
236
+ body: SafeArea (
119
237
child: Column (
120
- children: [
121
- Expanded (
122
- child: AccelerometerCard (
123
- color: xOrientationChartLineColor,
124
- axis: appLocalizations.xAxis)),
125
- Expanded (
126
- child: AccelerometerCard (
127
- color: yOrientationChartLineColor,
128
- axis: appLocalizations.yAxis)),
129
- Expanded (
130
- child: AccelerometerCard (
131
- color: zOrientationChartLineColor,
132
- axis: appLocalizations.zAxis)),
133
- ],
134
- ))),
238
+ children: [
239
+ Expanded (
240
+ child: AccelerometerCard (
241
+ color: xOrientationChartLineColor,
242
+ axis: appLocalizations.xAxis),
243
+ ),
244
+ Expanded (
245
+ child: AccelerometerCard (
246
+ color: yOrientationChartLineColor,
247
+ axis: appLocalizations.yAxis),
248
+ ),
249
+ Expanded (
250
+ child: AccelerometerCard (
251
+ color: zOrientationChartLineColor,
252
+ axis: appLocalizations.zAxis),
253
+ ),
254
+ ],
255
+ ),
256
+ ),
257
+ );
258
+ },
259
+ ),
135
260
if (_showGuide)
136
261
InstrumentOverviewDrawer (
137
262
instrumentName: appLocalizations.accelerometer,
0 commit comments