Skip to content

Commit

Permalink
feat: fallback to folder cover
Browse files Browse the repository at this point in the history
closes #122
  • Loading branch information
MSOB7YY committed Feb 10, 2024
1 parent d7bb2bf commit 62263d5
Show file tree
Hide file tree
Showing 27 changed files with 157 additions and 53 deletions.
12 changes: 9 additions & 3 deletions lib/controller/indexer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class Indexer {
final allTracksMappedByPath = <Track, TrackExtended>{}.obs;
final trackStatsMap = <Track, TrackStats>{}.obs;

var allFolderCovers = <String, String>{}; // {directoryPath, imagePath}

/// Used to prevent duplicated track (by filename).
final Map<String, bool> _currentFileNamesMap = {};

Expand All @@ -80,12 +82,12 @@ class Indexer {
return alltracks;
}

Map<String, (TrackExtended, int)> get backupMediaStoreIDS => _backupMediaStoreIDS;
Map<String, (Track, int)> get backupMediaStoreIDS => _backupMediaStoreIDS;

bool imageObtainedBefore(String imagePath) => _artworksMap[imagePath] != null || _artworksMapFullRes[imagePath] != null;

/// {imagePath: (TrackExtended, id)};
final _backupMediaStoreIDS = <String, (TrackExtended, int)>{};
final _backupMediaStoreIDS = <String, (Track, int)>{};
final artworksMap = <String, Uint8List?>{};
final _artworksMap = <String, Completer<void>>{};
final _artworksMapFullRes = <String, Completer<void>>{};
Expand Down Expand Up @@ -1213,18 +1215,22 @@ class Indexer {
'allAvailableDirectories': allAvailableDirectories,
'directoriesToExclude': settings.directoriesToExclude.toList(),
'extensions': kAudioFileExtensions,
'imageExtensions': kImageFilesExtensions,
};

final mapResult = await getFilesTypeIsolate.thready(parameters);

final allPaths = mapResult['allPaths'] as Set<String>;
final excludedByNoMedia = mapResult['pathsExcludedByNoMedia'] as Set<String>;
final folderCovers = mapResult['folderCovers'] as Map<String, String>;

tracksExcludedByNoMedia.value += excludedByNoMedia.length;

// ignore: invalid_use_of_protected_member
allAudioFiles.value = allPaths;

allFolderCovers = folderCovers;

printy("Paths Found: ${allPaths.length}");
return allPaths;
}
Expand Down Expand Up @@ -1349,7 +1355,7 @@ class Indexer {
lyrics: '',
);
tracks.add((trext, e.id));
_backupMediaStoreIDS[trext.pathToImage] = (trext, e.id);
_backupMediaStoreIDS[trext.pathToImage] = (trext.toTrack(), e.id);
});
return tracks;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/core/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ const Set<String> kVideoFilesExtensions = {
};

/// Extensions used to filter m3u files
const Set<String> kM3UPlaylistsExtensions = {'m3u', 'm3u8', 'M3U', 'M3U8'};
const Set<String> kM3UPlaylistsExtensions = {'.m3u', '.m3u8', '.M3U', '.M3U8'};

const Set<String> kImageFilesExtensions = {'.png', '.jpg', '.jpeg', '.bmp', '.gif', '.webp'};

const kDefaultOrientations = <DeviceOrientation>[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown];
const kDefaultLang = NamidaLanguage(
Expand Down
10 changes: 7 additions & 3 deletions lib/core/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ export 'package:dart_extensions/dart_extensions.dart';
extension TracksSelectableUtils on List<Selectable> {
String get displayTrackKeyword => length.displayTrackKeyword;

List<String> toImagePaths([int? limit = 4]) {
final l = <String>[];
List<Track> toImageTracks([int? limit = 4]) {
final l = <Track>[];
for (final p in withLimit(limit)) {
l.add(p.track.pathToImage);
l.add(p.track);
}
if (l.length == limit) {
// -- return 1 image if all were the same.
if (l.toSet().length == 1) return [l.first];
}
return l;
}

List<String> toImagePaths([int? limit = 4]) {
return toImageTracks(limit).map((e) => e.pathToImage).toList();
}
}

extension TracksWithDatesUtils on List<TrackWithDate> {
Expand Down
25 changes: 24 additions & 1 deletion lib/core/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ bool checkIfQueueSameAsAllTracks(List<Selectable> queue) {
/// 'allAvailableDirectories': <Directory, bool>{},
/// 'directoriesToExclude': <String>[],
/// 'extensions': <String>{},
/// 'imageExtensions': <String>{},
/// 'respectNoMedia': bool ?? true,
/// }
/// ```
Expand All @@ -534,16 +535,25 @@ bool checkIfQueueSameAsAllTracks(List<Selectable> queue) {
/// {
/// 'allPaths': <String>{},
/// 'pathsExcludedByNoMedia': <String>{},
/// 'folderCovers': <String, String>{},
/// }
/// ```
Map<String, Set<String>> getFilesTypeIsolate(Map parameters) {
Map<String, Object> getFilesTypeIsolate(Map parameters) {
final allAvailableDirectories = parameters['allAvailableDirectories'] as Map<Directory, bool>;
final directoriesToExclude = parameters['directoriesToExclude'] as List<String>? ?? [];
final extensions = parameters['extensions'] as Set<String>;
final imageExtensions = parameters['imageExtensions'] as Set<String>? ?? {};
final respectNoMedia = parameters['respectNoMedia'] as bool? ?? true;

final allPaths = <String>{};
final excludedByNoMedia = <String>{};
final folderCovers = <String, String>{};
final folderCoversValidity = <String, bool>{};

final fillFolderCovers = imageExtensions.isNotEmpty;

// "thumb", "album", "albumart", etc.. are covered by the check `element.contains(filename)`.
final coversNames = ["folder", "front", "cover", "thumbnail", "albumartsmall"];

allAvailableDirectories.keys.toList().loop((d, index) {
final hasNoMedia = allAvailableDirectories[d] ?? false;
Expand All @@ -552,6 +562,18 @@ Map<String, Set<String>> getFilesTypeIsolate(Map parameters) {
if (systemEntity is File) {
final path = systemEntity.path;

if (fillFolderCovers) {
final dirPath = path.getDirectoryPath;
if (folderCoversValidity[dirPath] == null || folderCoversValidity[dirPath] == false) {
if (imageExtensions.any((ext) => path.endsWith(ext))) {
folderCovers[dirPath] = path;
final filename = path.getFilenameWOExt.toLowerCase();
folderCoversValidity[dirPath] = coversNames.any((element) => element.contains(filename));
continue;
}
}
}

// -- skip if hidden
if (path.startsWith('.')) continue;

Expand All @@ -578,6 +600,7 @@ Map<String, Set<String>> getFilesTypeIsolate(Map parameters) {
return {
'allPaths': allPaths,
'pathsExcludedByNoMedia': excludedByNoMedia,
'folderCovers': folderCovers,
};
}

Expand Down
3 changes: 2 additions & 1 deletion lib/packages/miniplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,8 @@ class _TrackImage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ArtworkWidget(
key: Key(track.path),
key: Key(track.pathToImage),
track: track,
path: track.pathToImage,
thumbnailSize: context.width,
compressed: false,
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/dialogs/edit_tags_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,8 @@ Future<void> _editMultipleTracksTags(List<Track> tracksPre) async {
: MultiArtworkContainer(
heroTag: 'edittags_artwork',
size: Get.width / 3,
paths: tracks.toImagePaths(),
tracks: tracks.toImageTracks(),
fallbackToFolderCover: false,
onTopWidget: tracks.length > 3
? Positioned(
right: 0,
Expand Down
4 changes: 3 additions & 1 deletion lib/ui/dialogs/general_popup_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ Future<void> showGeneralPopupDialog(
tag: heroTag ?? '$comingFromQueue${index}_sussydialogs_${tracks.first.path}$additionalHero',
child: ArtworkWidget(
key: Key(tracks.pathToImage),
track: tracks.trackOfImage,
path: tracks.pathToImage,
thumbnailSize: 60,
forceSquared: forceSquared,
Expand All @@ -686,7 +687,8 @@ Future<void> showGeneralPopupDialog(
MultiArtworkContainer(
heroTag: heroTag ?? 'edittags_artwork',
size: 60,
paths: tracks.toImagePaths(),
tracks: tracks.toImageTracks(),
fallbackToFolderCover: false,
margin: EdgeInsets.zero,
),
const SizedBox(width: 12.0),
Expand Down
1 change: 1 addition & 0 deletions lib/ui/dialogs/track_info_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Future<void> showTrackInfoDialog(
tag: '$comingFromQueue${index}_sussydialogs_${trackExt.path}$additionalHero',
child: ArtworkWidget(
key: Key(trackExt.pathToImage),
track: trackExt.toTrack(),
path: trackExt.pathToImage,
thumbnailSize: 120,
forceSquared: settings.forceSquaredTrackThumbnail.value,
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ class _MixesCardState extends State<_MixesCard> {
tag: 'mix_thumbnail_${widget.title}${widget.index}',
child: ArtworkWidget(
key: Key(track?.pathToImage ?? ''),
track: track,
compressed: false,
blur: 1.5,
borderRadius: titleInside ? 12.0 : 8.0,
Expand Down Expand Up @@ -1201,6 +1202,7 @@ class _TrackCardState extends State<_TrackCard> with LoadingItemsDelayMixin {
children: [
ArtworkWidget(
key: Key(track.path),
track: track,
blur: 0.0,
forceSquared: true,
path: track.pathToImage,
Expand Down
1 change: 1 addition & 0 deletions lib/ui/pages/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ class SearchPage extends StatelessWidget {
const SizedBox(width: 4.0),
ArtworkWidget(
key: Key(tracks.pathToImage),
track: tracks.trackOfImage,
thumbnailSize: 48.0,
path: tracks.pathToImage,
forceSquared: true,
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/pages/subpages/album_tracks_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AlbumTracksPage extends StatelessWidget {
? MultiArtworkContainer(
size: Get.width * 0.35,
heroTag: 'album_$albumIdentifier',
paths: [tracks.pathToImage],
tracks: [tracks.trackOfImage ?? kDummyTrack],
)
: Container(
margin: const EdgeInsets.symmetric(horizontal: 12.0),
Expand All @@ -53,6 +53,7 @@ class AlbumTracksPage extends StatelessWidget {
tag: 'album_$albumIdentifier',
child: ArtworkWidget(
key: Key(tracks.pathToImage),
track: tracks.trackOfImage,
thumbnailSize: Get.width * 0.35,
forceSquared: false,
path: tracks.pathToImage,
Expand Down
1 change: 1 addition & 0 deletions lib/ui/pages/subpages/artist_tracks_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ArtistTracksPage extends StatelessWidget {
child: ContainerWithBorder(
child: ArtworkWidget(
key: Key(tracks.pathToImage),
track: tracks.trackOfImage,
thumbnailSize: Get.width * 0.35,
path: tracks.pathToImage,
forceSquared: true,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/subpages/genre_tracks_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GenreTracksPage extends StatelessWidget {
imageWidget: MultiArtworkContainer(
size: Get.width * 0.35,
heroTag: 'genre_$name',
paths: tracks.toImagePaths(),
tracks: tracks.toImageTracks(),
),
tracks: tracks,
),
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/pages/subpages/playlist_tracks_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class _HistoryTracksPageState extends State<HistoryTracksPage> {
imageWidget: MultiArtworkContainer(
heroTag: 'playlist_$k_PLAYLIST_NAME_HISTORY',
size: Get.width * 0.35,
paths: historyTracks.toImagePaths(),
tracks: historyTracks.toImageTracks(),
),
bottomPadding: 8.0,
),
Expand Down Expand Up @@ -215,7 +215,7 @@ class MostPlayedTracksPage extends StatelessWidget {
imageWidget: MultiArtworkContainer(
heroTag: 'playlist_$k_PLAYLIST_NAME_MOST_PLAYED',
size: Get.width * 0.35,
paths: tracks.toImagePaths(),
tracks: tracks.toImageTracks(),
),
tracks: tracks,
bottomPadding: bottomPadding,
Expand Down Expand Up @@ -431,7 +431,7 @@ class _NormalPlaylistTracksPageState extends State<NormalPlaylistTracksPage> wit
imageWidget: MultiArtworkContainer(
heroTag: 'playlist_${playlist.name}',
size: Get.width * 0.35,
paths: tracks.toImagePaths(),
tracks: tracks.toImageTracks(),
),
tracks: tracks,
),
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/subpages/queue_tracks_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class QueueTracksPage extends StatelessWidget {
imageWidget: MultiArtworkContainer(
size: Get.width * 0.35,
heroTag: 'queue_${queue.date}',
paths: queue.tracks.toImagePaths(),
tracks: queue.tracks.toImageTracks(),
),
tracks: queue.tracks,
),
Expand Down
Loading

0 comments on commit 62263d5

Please sign in to comment.