aboutsummaryrefslogtreecommitdiffstats
path: root/lib/data/data_providers/radio_presets_provider.dart
blob: af14997b47770fd00773b95758101726feb63552 (plain)
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
import 'package:flutter_ics_homescreen/export.dart';

class RadioPreset {
  final int frequency;
  final String name;

  RadioPreset({required this.frequency, required this.name});
}

class RadioPresets {
  final List<RadioPreset> fmPresets;

  RadioPresets({required this.fmPresets});
}

final radioPresetsProvider = Provider((ref) {
  final presetsFilename = ref.read(appConfigProvider).radioConfig.presets;
  if (presetsFilename.isEmpty) {
    return RadioPresets(fmPresets: []);
  }
  try {
    print("Reading radio presets $presetsFilename");
    var presetsFile = File(presetsFilename);
    String content = presetsFile.readAsStringSync();
    final dynamic yamlMap = loadYaml(content);

    List<RadioPreset> presets = [];
    if (yamlMap.containsKey('fm')) {
      dynamic list = yamlMap['fm'];
      if (list is YamlList) {
        for (var element in list) {
          if ((element is YamlMap) &&
              element.containsKey('frequency') &&
              element.containsKey('name')) {
            presets.add(RadioPreset(
                frequency: element['frequency'].toInt(),
                name: element['name'].toString()));
          }
        }
      }
    }
    return RadioPresets(fmPresets: presets);
  } catch (_) {
    debugPrint("Exception reading presets!");
    return RadioPresets(fmPresets: []);
  }
});