Skip to content

Commit ca884e1

Browse files
Jashan32marcnause
authored andcommitted
Implement update period configuration in SoundMeter
1 parent 3a68e77 commit ca884e1

7 files changed

Lines changed: 99 additions & 16 deletions

File tree

lib/l10n/app_en.arb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@
381381
"luxmeterConfigurations": "Lux Meter Configurations",
382382
"updatePeriod": "Update Period",
383383
"luxmeterUpdatePeriodHint": "Please provide time interval at which data will be updated (100 ms to 1000 ms)",
384+
"soundMeterUpdatePeriodHint": "Please provide time interval at which data will be updated (100 ms to 1000 ms)",
384385
"highLimit": "High Limit",
385386
"lowLimit": "Low Limit",
386387
"luxmeterHighLimitHint": "Please provide the maximum limit of lux value to be recorded (10 Lx to 10000 Lx)",

lib/l10n/app_hi.arb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@
379379
"luxmeterConfigurations": "लक्स मीटर सेटिंग्स",
380380
"updatePeriod": "अपडेट टाइम",
381381
"luxmeterUpdatePeriodHint": "वो टाइम इंटरवल बताएं जिस पर डेटा अपडेट होगा (100 ms से 1000 ms)",
382+
"soundMeterUpdatePeriodHint": "वो टाइम इंटरवल बताएं जिस पर डेटा अपडेट होगा (100 ms से 1000 ms)",
382383
"highLimit": "हाई लिमिट",
383384
"lowLimit": "लो लिमिट",
384385
"luxmeterHighLimitHint": "रिकॉर्ड करने के लिए लक्स की मैक्स लिमिट बताएं (10 Lx से 10000 Lx)",

lib/l10n/app_it.arb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@
381381
"luxmeterConfigurations": "Configurazioni Luxmetro",
382382
"updatePeriod": "Periodo di Aggiornamento",
383383
"luxmeterUpdatePeriodHint": "Fornisci l'intervallo di tempo in cui i dati verranno aggiornati (da 100 ms a 1000 ms)",
384+
"soundMeterUpdatePeriodHint": "Fornisci l'intervallo di tempo in cui i dati verranno aggiornati (da 100 ms a 1000 ms)",
384385
"highLimit": "Limite Massimo",
385386
"luxmeterHighLimitHint": "Inserisci il limite massimo del valore di lux da registrare (da 10 Lx a 10000 Lx)",
386387
"sensorGain": "Guadagno del Sensore",

lib/models/soundmeter_config.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
class SoundMeterConfig {
2+
final int updatePeriod;
23
final bool includeLocationData;
34
const SoundMeterConfig({
5+
this.updatePeriod = 1000,
46
this.includeLocationData = true,
57
});
68
SoundMeterConfig copyWith({
9+
int? updatePeriod,
710
bool? includeLocationData,
811
}) {
912
return SoundMeterConfig(
13+
updatePeriod: updatePeriod ?? this.updatePeriod,
1014
includeLocationData: includeLocationData ?? this.includeLocationData,
1115
);
1216
}
1317

1418
Map<String, dynamic> toJson() {
1519
return {
20+
'updatePeriod': updatePeriod,
1621
'includeLocationData': includeLocationData,
1722
};
1823
}
1924

2025
factory SoundMeterConfig.fromJson(Map<String, dynamic> json) {
2126
return SoundMeterConfig(
27+
updatePeriod: json['updatePeriod'] ?? 1000,
2228
includeLocationData: json['includeLocationData'] ?? true,
2329
);
2430
}

lib/providers/soundmeter_config_provider.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class SoundMeterConfigProvider extends ChangeNotifier {
2424
await prefs.setString('sound_config', json.encode(_config.toJson()));
2525
}
2626

27+
void updateUpdatePeriod(int updatePeriod) {
28+
_config = _config.copyWith(updatePeriod: updatePeriod);
29+
notifyListeners();
30+
_saveConfigToPrefs();
31+
}
32+
2733
void updateConfig(SoundMeterConfig newConfig) {
2834
_config = newConfig;
2935
notifyListeners();

lib/providers/soundmeter_state_provider.dart

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SoundMeterStateProvider extends ChangeNotifier {
3838
bool get isRecording => _isRecording;
3939
bool get isPlayingBack => _isPlayingBack;
4040
bool get isPlaybackPaused => _isPlaybackPaused;
41+
int? _playbackStartTimestamp;
4142

4243
SoundMeterConfigProvider? _configProvider;
4344

@@ -49,6 +50,27 @@ class SoundMeterStateProvider extends ChangeNotifier {
4950

5051
void setConfigProvider(SoundMeterConfigProvider configProvider) {
5152
_configProvider = configProvider;
53+
_configProvider?.addListener(_onConfigChanged);
54+
}
55+
56+
Future<void> _onConfigChanged() async {
57+
await disposeSensors();
58+
_resetDbData();
59+
initializeSensors(onError: onSensorError);
60+
}
61+
62+
void _resetDbData() {
63+
_dbData.clear();
64+
_timeData.clear();
65+
dbChartData.clear();
66+
_dbSum = 0;
67+
_dataCount = 0;
68+
_dbMin = 0;
69+
_dbMax = 0;
70+
_currentTime = 0;
71+
_currentDb = 0;
72+
_startTime = DateTime.now().millisecondsSinceEpoch / 1000.0;
73+
notifyListeners();
5274
}
5375

5476
SoundMeterConfigProvider? get configProvider => _configProvider;
@@ -114,13 +136,18 @@ class SoundMeterStateProvider extends ChangeNotifier {
114136

115137
_startTime = DateTime.now().millisecondsSinceEpoch / 1000.0;
116138

117-
_timeTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
118-
_currentTime =
119-
(DateTime.now().millisecondsSinceEpoch / 1000.0) - _startTime;
120-
_updateData();
121-
notifyListeners();
122-
});
139+
final intervalMs = _configProvider?.config.updatePeriod ?? 1000;
140+
141+
_timeTimer = Timer.periodic(
142+
Duration(milliseconds: intervalMs),
143+
(timer) {
144+
_currentTime =
145+
(DateTime.now().millisecondsSinceEpoch / 1000.0) - _startTime;
123146

147+
_updateData();
148+
notifyListeners();
149+
},
150+
);
124151
_audioTimer = Timer.periodic(const Duration(milliseconds: 100), (timer) {
125152
if (_audioJack != null && _audioJack!.isListening()) {
126153
final audioData = _audioJack!.read();
@@ -159,15 +186,16 @@ class SoundMeterStateProvider extends ChangeNotifier {
159186
return dbSPL.clamp(20.0, 120.0);
160187
}
161188

162-
void disposeSensors() async {
189+
Future<void> disposeSensors() async {
163190
_timeTimer?.cancel();
164191
_audioTimer?.cancel();
165192
await _audioJack?.close();
166193
_audioJack = null;
167194
}
168195

169196
void startPlayback(List<List<dynamic>> data) {
170-
if (data.length <= 1) return;
197+
if (data.length <= 2) return;
198+
_playbackStartTimestamp = int.tryParse(data[2][0].toString())?.toInt();
171199

172200
_isPlayingBack = true;
173201
_isPlaybackPaused = false;
@@ -198,7 +226,10 @@ class SoundMeterStateProvider extends ChangeNotifier {
198226
final currentRow = _playbackData![_playbackIndex];
199227
if (currentRow.length > 2) {
200228
_currentDb = double.tryParse(currentRow[2].toString()) ?? 0.0;
201-
_currentTime = (_playbackIndex - 1).toDouble();
229+
final timestamp = int.tryParse(currentRow[0].toString())?.toInt();
230+
if (timestamp != null && _playbackStartTimestamp != null) {
231+
_currentTime = (timestamp - _playbackStartTimestamp!) / 1000.0;
232+
}
202233
_updateData();
203234
_playbackIndex++;
204235
notifyListeners();
@@ -278,6 +309,7 @@ class SoundMeterStateProvider extends ChangeNotifier {
278309
if (_locationStream != null) {
279310
_locationStream!.cancel();
280311
}
312+
_configProvider?.removeListener(_onConfigChanged);
281313
_playbackTimer?.cancel();
282314
disposeSensors();
283315
super.dispose();

lib/view/soundmeter_config_screen.dart

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@ class SoundMeterConfigScreen extends StatefulWidget {
1515

1616
class _SoundMeterConfigScreenState extends State<SoundMeterConfigScreen> {
1717
AppLocalizations get appLocalizations => getIt.get<AppLocalizations>();
18+
final TextEditingController _updatePeriodController = TextEditingController();
1819
@override
1920
void initState() {
2021
super.initState();
22+
23+
WidgetsBinding.instance.addPostFrameCallback((_) {
24+
final provider =
25+
Provider.of<SoundMeterConfigProvider>(context, listen: false);
26+
_updatePeriodController.text = provider.config.updatePeriod.toString();
27+
});
2128
}
2229

2330
@override
2431
void dispose() {
32+
_updatePeriodController.dispose();
2533
super.dispose();
2634
}
2735

@@ -62,13 +70,41 @@ class _SoundMeterConfigScreenState extends State<SoundMeterConfigScreen> {
6270
child: Consumer<SoundMeterConfigProvider>(
6371
builder: (context, provider, child) {
6472
return SingleChildScrollView(
65-
child: ConfigCheckboxItem(
66-
title: appLocalizations.locationData,
67-
subtitle: appLocalizations.locationDataHint,
68-
value: provider.config.includeLocationData,
69-
onChanged: (value) {
70-
provider.updateIncludeLocationData(value);
71-
},
73+
child: Column(
74+
children: [
75+
ConfigInputItem(
76+
title: appLocalizations.updatePeriod,
77+
value:
78+
'${provider.config.updatePeriod} ${appLocalizations.ms}',
79+
controller: _updatePeriodController,
80+
onChanged: (value) {
81+
final intValue = int.tryParse(value);
82+
if (intValue != null &&
83+
intValue >= 100 &&
84+
intValue <= 1000) {
85+
provider.updateUpdatePeriod(intValue);
86+
} else {
87+
ScaffoldMessenger.of(context).showSnackBar(
88+
SnackBar(
89+
content: Text(
90+
appLocalizations.updatePeriodErrorMessage,
91+
style: TextStyle(color: snackBarContentColor),
92+
),
93+
backgroundColor: snackBarBackgroundColor),
94+
);
95+
}
96+
},
97+
hint: appLocalizations.soundMeterUpdatePeriodHint,
98+
),
99+
ConfigCheckboxItem(
100+
title: appLocalizations.locationData,
101+
subtitle: appLocalizations.locationDataHint,
102+
value: provider.config.includeLocationData,
103+
onChanged: (value) {
104+
provider.updateIncludeLocationData(value);
105+
},
106+
),
107+
],
72108
),
73109
);
74110
},

0 commit comments

Comments
 (0)