Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify LocalAudioService #709

Merged
merged 1 commit into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 14 additions & 34 deletions lib/src/local_audio/local_audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'dart:io';
import 'package:audio_metadata_reader/audio_metadata_reader.dart';
import 'package:flutter/foundation.dart';

import '../../media_file_x.dart';
import '../../data.dart';
import '../../media_file_x.dart';
import '../settings/settings_service.dart';

class LocalAudioService {
Expand All @@ -28,55 +28,35 @@ class LocalAudioService {

Future<List<String>> init({@visibleForTesting String? testDir}) async {
final result = await compute(_init, testDir ?? _settingsService.directory);

_audios = result.$2;

_audiosController.add(true);

return result.$1;
audios = result.audios;
return result.failedImports;
}

Future<void> dispose() async {
await _audiosController.close();
}
}

FutureOr<(List<String>, Set<Audio>?)> _init(String? directory) async {
Set<Audio>? newAudios = {};
FutureOr<ImportResult> _init(String? directory) async {
Set<Audio> newAudios = {};
List<String> failedImports = [];

if (directory != null) {
final allFileSystemEntities = Set<FileSystemEntity>.from(
await _getFlattenedFileSystemEntities(path: directory),
);

final onlyFiles = <FileSystemEntity>[];

for (var fileSystemEntity in allFileSystemEntities) {
if (!await FileSystemEntity.isDirectory(fileSystemEntity.path) &&
fileSystemEntity.isValidMedia) {
onlyFiles.add(fileSystemEntity);
}
}
for (var e in onlyFiles) {
for (var e in Directory(directory)
.listSync(recursive: true, followLinks: false)
.whereType<File>()
.where((e) => e.isValidMedia)
.toList()) {
try {
final metadata = await readMetadata(File(e.path), getImage: true);
final audio = Audio.fromMetadata(path: e.path, data: metadata);

newAudios.add(audio);
final metadata = await readMetadata(e, getImage: true);
newAudios.add(Audio.fromMetadata(path: e.path, data: metadata));
} catch (error) {
failedImports.add(e.path);
}
}
}

return (failedImports, newAudios);
return (audios: newAudios, failedImports: failedImports);
}

Future<List<FileSystemEntity>> _getFlattenedFileSystemEntities({
required String path,
}) async {
return await Directory(path)
.list(recursive: true, followLinks: false)
.toList();
}
typedef ImportResult = ({List<String> failedImports, Set<Audio> audios});