Skip to content

Commit f894ff2

Browse files
authored
refactor: simplify code and hide non-implemented instruments (#3003)
1 parent 292419c commit f894ff2

File tree

2 files changed

+69
-253
lines changed

2 files changed

+69
-253
lines changed

lib/constants.dart

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,6 @@ const oscilloscopeScreenTitleKey = 'oscilloscope_screen_title';
1212

1313
AppLocalizations appLocalizations = getIt.get<AppLocalizations>();
1414

15-
List<String> instrumentHeadings = [
16-
'OSCILLOSCOPE',
17-
'MULTIMETER',
18-
'LOGIC ANALYZER',
19-
'SENSORS',
20-
'WAVE GENERATOR',
21-
'POWER SOURCE',
22-
'LUX METER',
23-
'ACCELEROMETER',
24-
'BAROMETER',
25-
'COMPASS',
26-
'GYROSCOPE',
27-
'THERMOMETER',
28-
'ROBOTIC ARM',
29-
'GAS SENSOR',
30-
'DUST SENSOR',
31-
'SOUND METER'
32-
];
33-
34-
List<String> instrumentDesc = [
35-
'Allows observation of varying signal voltages',
36-
'Measure voltage, current, resistance and capacitance',
37-
'Captures and displays signals from digital systems',
38-
'Allows logging of data returned by sensor connected',
39-
'Generates arbitrary analog and digital waveforms',
40-
'Generates programmable voltage and currents',
41-
'Measures the ambient light intensity',
42-
'Measures the Linear acceleration in XYZ directions',
43-
'Measures the atmospheric pressure',
44-
'Three axes magnetometer pointing to magnetic north',
45-
'Measures rate of rotation about XYZ axis',
46-
'To measure the ambient temperature',
47-
'Controls servos of a robotic arm',
48-
'Air quality sensor for detecting a wide range of gases, including NH3, NOx, alcohol, benzene, smoke and CO2',
49-
'Dust sensor is used to measure air quality in terms of particles per square meter',
50-
'To measure the loudness in the environment in decibel(dB)'
51-
];
52-
5315
List<String> instrumentIcons = [
5416
'assets/icons/tile_icon_oscilloscope.png',
5517
'assets/icons/tile_icon_multimeter.png',

lib/view/instruments_screen.dart

Lines changed: 69 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -13,194 +13,43 @@ class InstrumentsScreen extends StatefulWidget {
1313
State<StatefulWidget> createState() => _InstrumentsScreenState();
1414
}
1515

16+
class _InstrumentData {
17+
final String heading;
18+
final String description;
19+
final String name;
20+
21+
_InstrumentData(this.heading, this.description, this.name);
22+
}
23+
1624
class _InstrumentsScreenState extends State<InstrumentsScreen> {
1725
List<int> _filteredIndices = <int>[];
1826
AppLocalizations appLocalizations = getIt.get<AppLocalizations>();
19-
late List<String> instrumentHeadings;
20-
late List<String> instrumentDesc;
27+
late List<_InstrumentData> _instrumentDatas;
2128

2229
void _onItemTapped(int index) {
23-
switch (index) {
24-
case 0:
25-
if (Navigator.canPop(context) &&
26-
ModalRoute.of(context)?.settings.name == '/oscilloscope') {
27-
Navigator.popUntil(context, ModalRoute.withName('/oscilloscope'));
28-
} else {
29-
Navigator.pushNamedAndRemoveUntil(
30-
context,
31-
'/oscilloscope',
32-
(route) => route.isFirst,
33-
);
34-
}
35-
break;
36-
case 1:
37-
if (Navigator.canPop(context) &&
38-
ModalRoute.of(context)?.settings.name == '/multimeter') {
39-
Navigator.popUntil(context, ModalRoute.withName('/multimeter'));
40-
} else {
41-
Navigator.pushNamedAndRemoveUntil(
42-
context,
43-
'/multimeter',
44-
(route) => route.isFirst,
45-
);
46-
}
47-
break;
48-
case 2:
49-
if (Navigator.canPop(context) &&
50-
ModalRoute.of(context)?.settings.name == '/logicAnalyzer') {
51-
Navigator.popUntil(context, ModalRoute.withName('/logicAnalyzer'));
52-
} else {
53-
Navigator.pushNamedAndRemoveUntil(
54-
context,
55-
'/logicAnalyzer',
56-
(route) => route.isFirst,
57-
);
58-
}
59-
break;
60-
case 3:
61-
if (Navigator.canPop(context) &&
62-
ModalRoute.of(context)?.settings.name == '/sensors') {
63-
Navigator.popUntil(context, ModalRoute.withName('/sensors'));
64-
} else {
65-
Navigator.pushNamedAndRemoveUntil(
66-
context,
67-
'/sensors',
68-
(route) => route.isFirst,
69-
);
70-
}
71-
break;
72-
case 5:
73-
if (Navigator.canPop(context) &&
74-
ModalRoute.of(context)?.settings.name == '/powerSource') {
75-
Navigator.popUntil(context, ModalRoute.withName('/powerSource'));
76-
} else {
77-
Navigator.pushNamedAndRemoveUntil(
78-
context,
79-
'/powerSource',
80-
(route) => route.isFirst,
81-
);
82-
}
83-
break;
84-
case 6:
85-
if (Navigator.canPop(context) &&
86-
ModalRoute.of(context)?.settings.name == '/luxmeter') {
87-
Navigator.popUntil(context, ModalRoute.withName('/luxmeter'));
88-
} else {
89-
Navigator.pushNamedAndRemoveUntil(
90-
context,
91-
'/luxmeter',
92-
(route) => route.isFirst,
93-
);
94-
}
95-
break;
96-
case 7:
97-
if (Navigator.canPop(context) &&
98-
ModalRoute.of(context)?.settings.name == '/accelerometer') {
99-
Navigator.popUntil(context, ModalRoute.withName('/accelerometer'));
100-
} else {
101-
Navigator.pushNamedAndRemoveUntil(
102-
context,
103-
'/accelerometer',
104-
(route) => route.isFirst,
105-
);
106-
}
107-
break;
108-
case 8:
109-
if (Navigator.canPop(context) &&
110-
ModalRoute.of(context)?.settings.name == '/barometer') {
111-
Navigator.popUntil(context, ModalRoute.withName('/barometer'));
112-
} else {
113-
Navigator.pushNamedAndRemoveUntil(
114-
context,
115-
'/barometer',
116-
(route) => route.isFirst,
117-
);
118-
}
119-
break;
120-
case 10:
121-
if (Navigator.canPop(context) &&
122-
ModalRoute.of(context)?.settings.name == '/gyroscope') {
123-
Navigator.popUntil(context, ModalRoute.withName('/gyroscope'));
124-
} else {
125-
Navigator.pushNamedAndRemoveUntil(
126-
context,
127-
'/gyroscope',
128-
(route) => route.isFirst,
129-
);
130-
}
131-
break;
132-
case 11:
133-
if (Navigator.canPop(context) &&
134-
ModalRoute.of(context)?.settings.name == '/thermometer') {
135-
Navigator.popUntil(context, ModalRoute.withName('/thermometer'));
136-
} else {
137-
Navigator.pushNamedAndRemoveUntil(
138-
context,
139-
'/thermometer',
140-
(route) => route.isFirst,
141-
);
142-
}
143-
break;
144-
case 12:
145-
if (Navigator.canPop(context) &&
146-
ModalRoute.of(context)?.settings.name == '/roboticArm') {
147-
Navigator.popUntil(context, ModalRoute.withName('/roboticArm'));
148-
} else {
149-
Navigator.pushNamedAndRemoveUntil(
150-
context,
151-
'/roboticArm',
152-
(route) => route.isFirst,
153-
);
154-
}
155-
break;
156-
case 15:
157-
if (Navigator.canPop(context) &&
158-
ModalRoute.of(context)?.settings.name == '/soundmeter') {
159-
Navigator.popUntil(context, ModalRoute.withName('/soundmeter'));
160-
} else {
161-
Navigator.pushNamedAndRemoveUntil(
162-
context,
163-
'/soundmeter',
164-
(route) => route.isFirst,
165-
);
166-
}
167-
break;
168-
case 9:
169-
if (Navigator.canPop(context) &&
170-
ModalRoute.of(context)?.settings.name == '/compass') {
171-
Navigator.popUntil(context, ModalRoute.withName('/compass'));
172-
} else {
173-
Navigator.pushNamedAndRemoveUntil(
174-
context,
175-
'/compass',
176-
(route) => route.isFirst,
177-
);
178-
}
179-
break;
180-
case 4:
181-
if (Navigator.canPop(context) &&
182-
ModalRoute.of(context)?.settings.name == '/waveGenerator') {
183-
Navigator.popUntil(context, ModalRoute.withName('/waveGenerator'));
184-
} else {
185-
Navigator.pushNamedAndRemoveUntil(
186-
context,
187-
'/waveGenerator',
188-
(route) => route.isFirst,
189-
);
190-
}
191-
default:
192-
break;
30+
_InstrumentData instrument = _instrumentDatas[index];
31+
32+
if (Navigator.canPop(context) &&
33+
ModalRoute.of(context)?.settings.name == instrument.name) {
34+
Navigator.popUntil(context, ModalRoute.withName(instrument.name));
35+
} else {
36+
Navigator.pushNamedAndRemoveUntil(
37+
context,
38+
instrument.name,
39+
(route) => route.isFirst,
40+
);
19341
}
19442
}
19543

19644
void _filterInstruments(String query) {
19745
setState(() {
19846
if (query.isEmpty) {
19947
_filteredIndices =
200-
List<int>.generate(instrumentHeadings.length, (index) => index);
48+
List<int>.generate(_instrumentDatas.length, (index) => index);
20149
} else {
202-
_filteredIndices = List.generate(instrumentHeadings.length, (i) => i)
203-
.where((i) => instrumentHeadings[i]
50+
_filteredIndices = List.generate(_instrumentDatas.length, (i) => i)
51+
.where((i) => _instrumentDatas[i]
52+
.heading
20453
.toLowerCase()
20554
.contains(query.toLowerCase()))
20655
.toList();
@@ -236,44 +85,43 @@ class _InstrumentsScreenState extends State<InstrumentsScreen> {
23685
});
23786
}
23887
});
239-
instrumentHeadings = [
240-
appLocalizations.oscilloscope.toUpperCase(),
241-
appLocalizations.multimeter.toUpperCase(),
242-
appLocalizations.logicAnalyzer.toUpperCase(),
243-
appLocalizations.sensors.toUpperCase(),
244-
appLocalizations.waveGenerator.toUpperCase(),
245-
appLocalizations.powerSource.toUpperCase(),
246-
appLocalizations.luxMeter.toUpperCase(),
247-
appLocalizations.accelerometer.toUpperCase(),
248-
appLocalizations.barometer.toUpperCase(),
249-
appLocalizations.compass.toUpperCase(),
250-
appLocalizations.gyroscope.toUpperCase(),
251-
appLocalizations.thermometer.toUpperCase(),
252-
appLocalizations.roboticArm.toUpperCase(),
253-
appLocalizations.gasSensor.toUpperCase(),
254-
appLocalizations.dustSensor.toUpperCase(),
255-
appLocalizations.soundMeter.toUpperCase(),
256-
];
257-
instrumentDesc = [
258-
appLocalizations.oscilloscopeDesc,
259-
appLocalizations.multimeterDesc,
260-
appLocalizations.logicAnalyzerDesc,
261-
appLocalizations.sensorsDesc,
262-
appLocalizations.waveGeneratorDesc,
263-
appLocalizations.powerSourceDesc,
264-
appLocalizations.luxMeterDesc,
265-
appLocalizations.accelerometerDesc,
266-
appLocalizations.barometerDesc,
267-
appLocalizations.compassDesc,
268-
appLocalizations.gyroscopeDesc,
269-
appLocalizations.thermometerDesc,
270-
appLocalizations.roboticArmDesc,
271-
appLocalizations.gasSensorDesc,
272-
appLocalizations.dustSensorDesc,
273-
appLocalizations.soundMeterDesc,
88+
89+
_instrumentDatas = [
90+
_InstrumentData(appLocalizations.oscilloscope,
91+
appLocalizations.oscilloscopeDesc, '/oscilloscope'),
92+
_InstrumentData(appLocalizations.multimeter,
93+
appLocalizations.multimeterDesc, '/multimeter'),
94+
_InstrumentData(appLocalizations.logicAnalyzer,
95+
appLocalizations.logicAnalyzerDesc, '/logicAnalyzer'),
96+
_InstrumentData(
97+
appLocalizations.sensors, appLocalizations.sensorsDesc, '/sensors'),
98+
_InstrumentData(appLocalizations.waveGenerator,
99+
appLocalizations.waveGeneratorDesc, '/waveGenerator'),
100+
_InstrumentData(appLocalizations.powerSource,
101+
appLocalizations.powerSourceDesc, '/powerSource'),
102+
_InstrumentData(appLocalizations.luxMeter, appLocalizations.luxMeterDesc,
103+
'/luxmeter'),
104+
_InstrumentData(appLocalizations.accelerometer,
105+
appLocalizations.accelerometerDesc, '/accelerometer'),
106+
_InstrumentData(appLocalizations.barometer,
107+
appLocalizations.barometerDesc, '/barometer'),
108+
_InstrumentData(
109+
appLocalizations.compass, appLocalizations.compassDesc, '/compass'),
110+
_InstrumentData(appLocalizations.gyroscope,
111+
appLocalizations.gyroscopeDesc, '/gyroscope'),
112+
_InstrumentData(appLocalizations.thermometer,
113+
appLocalizations.thermometerDesc, '/thermometer'),
114+
_InstrumentData(appLocalizations.roboticArm,
115+
appLocalizations.roboticArmDesc, '/roboticArm'),
116+
// Instruments below are not yet implemented.
117+
//_InstrumentData(appLocalizations.gasSensor, appLocalizations.gasSensorDesc, '/gassensor'),
118+
//_InstrumentData(appLocalizations.dustSensor, appLocalizations.dustSensorDesc, '/dustsensor'),
119+
_InstrumentData(appLocalizations.soundMeter,
120+
appLocalizations.soundMeterDesc, '/soundmeter'),
274121
];
122+
275123
_filteredIndices =
276-
List<int>.generate(instrumentHeadings.length, (index) => index);
124+
List<int>.generate(_instrumentDatas.length, (index) => index);
277125
WidgetsBinding.instance.addPostFrameCallback((_) {
278126
_setPortraitOrientation();
279127
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
@@ -352,8 +200,11 @@ class _InstrumentsScreenState extends State<InstrumentsScreen> {
352200
return GestureDetector(
353201
onTap: () => _onItemTapped(originalIndex),
354202
child: ApplicationsListItem(
355-
heading: instrumentHeadings[originalIndex],
356-
description: instrumentDesc[originalIndex],
203+
heading: _instrumentDatas[originalIndex]
204+
.heading
205+
.toUpperCase(),
206+
description: _instrumentDatas[originalIndex]
207+
.description,
357208
instrumentIcon:
358209
instrumentIcons[originalIndex],
359210
),
@@ -372,8 +223,11 @@ class _InstrumentsScreenState extends State<InstrumentsScreen> {
372223
return GestureDetector(
373224
onTap: () => _onItemTapped(originalIndex),
374225
child: ApplicationsListItem(
375-
heading: instrumentHeadings[originalIndex],
376-
description: instrumentDesc[originalIndex],
226+
heading: _instrumentDatas[originalIndex]
227+
.heading
228+
.toUpperCase(),
229+
description: _instrumentDatas[originalIndex]
230+
.description,
377231
instrumentIcon:
378232
instrumentIcons[originalIndex],
379233
),

0 commit comments

Comments
 (0)