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