Skip to content

Commit

Permalink
fix: write audios json as string doubled
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoFasan committed Oct 11, 2024
1 parent 99b7eb7 commit c48b55e
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions lib/services/filesystem_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class FileSystemService {

Future<void> init() async {
_authorized = await _hasStoragePermission();

if (_authorized) return;
_authorized = await _requestStoragePermission();

Expand Down Expand Up @@ -63,33 +64,23 @@ class FileSystemService {
}

Future<void> saveAudioList(List<Audio> audios) async {
final String content = jsonEncode(audios.map((a) => a.toJson()).toList());
final Map<String, dynamic> content = {
'audios': audios.map((a) => a.toJson()).toList()
};
await _createOrUpdateJsonFile(_audioListFileName, content);
}

Future<dynamic> _readPreferences() async {
final String filePath =
_getFilePath(_preferencesFileName, _jsonFileExtension);
final bool exists = await _fileExists(filePath);
if (!exists) return {'shouldSkipSponsors': true, 'downloadsQueueSize': 3};
return await _readJsonFile(filePath);
}

Future<void> _updatePreferences(dynamic preferences) async {
final String filePath =
_getFilePath(_preferencesFileName, _jsonFileExtension);
await _writeJsonFile(filePath, preferences);
}

Future<List<Audio>> readAudioList() async {
final String filePath =
_getFilePath(_audioListFileName, _jsonFileExtension);

final bool exists = await _fileExists(filePath);
if (!exists) return [];

final dynamic content = await _readJsonFile(filePath);

final List<Audio> audioList =
List<Audio>.from(content.map((a) => Audio.fromJson(a)));
List<Audio>.from(content['audios'].map((a) => Audio.fromJson(a)));

final Iterable<Future<Audio?>> validFilesFutures =
audioList.map((audio) async {
Expand All @@ -102,13 +93,27 @@ class FileSystemService {
return validFiles.whereType<Audio>().toList();
}

Future<Map<String, dynamic>> _readPreferences() async {
final String filePath =
_getFilePath(_preferencesFileName, _jsonFileExtension);
final bool exists = await _fileExists(filePath);
if (!exists) return {'shouldSkipSponsors': true, 'downloadsQueueSize': 3};
return await _readJsonFile(filePath);
}

Future<void> _updatePreferences(dynamic preferences) async {
final String filePath =
_getFilePath(_preferencesFileName, _jsonFileExtension);
await _writeJsonFile(filePath, preferences);
}

Future<bool> _isValidAudioFile(Audio audio) async {
final String path = _getFilePath(audio.id, _audioFileExtension);
return await _fileExists(path);
}

Future<String> _createOrUpdateJsonFile(
String fileName, String content) async {
String fileName, Map<String, dynamic> content) async {
final String filePath = _getFilePath(fileName, _jsonFileExtension);
final bool exists = await _fileExists(filePath);
if (!exists) await _createFile(filePath);
Expand Down Expand Up @@ -140,13 +145,13 @@ class FileSystemService {
await file.create();
}

Future<dynamic> _readJsonFile(String path) async {
Future<Map<String, dynamic>> _readJsonFile(String path) async {
final File file = File(path);
final String content = await file.readAsString();
return jsonDecode(content);
}

Future<void> _writeJsonFile(String path, dynamic content) async {
Future<void> _writeJsonFile(String path, Map<String, dynamic> content) async {
final File file = File(path);
await file.writeAsString(jsonEncode(content));
}
Expand Down

0 comments on commit c48b55e

Please sign in to comment.