Skip to content

Commit

Permalink
Simplify LocalAudioService (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier authored May 6, 2024
1 parent c636c19 commit 06f22bd
Showing 1 changed file with 14 additions and 34 deletions.
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});

0 comments on commit 06f22bd

Please sign in to comment.