Skip to content

Commit

Permalink
Merge branch 'Fix/keep-files'
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoFasan committed Oct 11, 2024
2 parents 3015c16 + c48b55e commit a91c429
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 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
28 changes: 26 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.0.0"
lists:
dependency: transitive
description:
name: lists
sha256: "4ca5c19ae4350de036a7e996cdd1ee39c93ac0a2b840f4915459b7d0a7d4ab27"
url: "https://pub.dev"
source: hosted
version: "1.0.1"
logging:
dependency: transitive
description:
name: logging
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -765,6 +781,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.2"
unicode:
dependency: transitive
description:
name: unicode
sha256: "0f69e46593d65245774d4f17125c6084d2c20b4e473a983f6e21b7d7762218f1"
url: "https://pub.dev"
source: hosted
version: "0.3.1"
universal_io:
dependency: transitive
description:
Expand Down Expand Up @@ -833,10 +857,10 @@ packages:
dependency: "direct main"
description:
name: youtube_explode_dart
sha256: "133a65907e6cf839ac7643d92dc5c56b37fcebe4f0a8f0e67716dffa500c0ef0"
sha256: "08bac196966c61abbdc13c00afb7b92f0f7aca438b53f052a5737145c2232df8"
url: "https://pub.dev"
source: hosted
version: "2.2.2"
version: "2.3.0"
sdks:
dart: ">=3.5.0 <4.0.0"
flutter: ">=3.22.0"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies:
sdk: flutter
shared_preferences: ^2.3.2
get: ^4.6.6
youtube_explode_dart: ^2.2.2
youtube_explode_dart: ^2.3.0
path_provider: ^2.1.4
http: ^1.2.2
just_audio: ^0.9.40
Expand Down

0 comments on commit a91c429

Please sign in to comment.