diff --git a/lib/screens/home.dart b/lib/screens/home.dart index bc319d2..cef9d45 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -2,7 +2,9 @@ import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:path/path.dart' as path; import 'package:taglib_ffi/taglib_ffi.dart'; +import 'package:tuple_dart/tuple_dart.dart'; import 'package:window_manager/window_manager.dart'; import '../browser/browser.dart'; @@ -16,6 +18,7 @@ import '../processors/saver.dart'; import '../processors/scanner.dart'; import '../utils/events.dart'; import '../utils/logger.dart'; +import '../utils/track_utils.dart'; import 'settings.dart'; import 'transcoder.dart'; @@ -302,14 +305,30 @@ class _TraxHomePageState extends State if (result == null) return; // we need to parse them + int index = 1; TrackList tracks = []; TagLib tagLib = TagLib(); eventBus.fire(BackgroundActionStartEvent(BackgroundAction.import)); for (String? filepath in result.paths) { if (filepath == null) continue; Track track = Track.parse(filepath, tagLib); + if (track.tags != null) { + String filename = path.basenameWithoutExtension(filepath); + Tuple2 parts = TrackUtils.parseTrackFilename(filename); + if (track.tags!.title == "") { + track.tags!.title = parts.item1; + } + if (track.tags!.trackIndex == 0) { + if (parts.item2 != 0) { + track.tags!.trackIndex = parts.item2; + } else { + track.tags!.trackIndex = index; + } + } + } tracks.add(track); await Future.delayed(Duration.zero, () {}); + index++; } eventBus.fire(BackgroundActionEndEvent(BackgroundAction.import)); diff --git a/lib/utils/track_utils.dart b/lib/utils/track_utils.dart index ef67845..1b244f6 100644 --- a/lib/utils/track_utils.dart +++ b/lib/utils/track_utils.dart @@ -1,4 +1,7 @@ +import 'dart:math'; + import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:tuple_dart/tuple_dart.dart'; import '../model/track.dart'; @@ -10,6 +13,34 @@ extension TrackUtils on Track { _t = t; } + static Tuple2 parseTrackFilename(String filename) { + // find a separator + var index = -1; + var index1 = filename.indexOf('-'); + var index2 = filename.indexOf('.'); + if (index1 == -1 && index2 == -1) { + return Tuple2(filename, 0); + } else if (index1 == -1) { + index = index2; + } else if (index2 == -1) { + index = index1; + } else { + index = min(index1, index2); + } + + // new get prefix and check it is only digits + var prefix = filename.substring(0, index).trim(); + if (prefix.isEmpty) return Tuple2(filename, 0); + if (prefix.contains(RegExp(r'[^0-9]'))) return Tuple2(filename, 0); + + // get the rest of the filename + var rest = filename.substring(index + 1).trim(); + if (rest.isEmpty) return Tuple2(filename, 0); + + // return the parts + return Tuple2(rest, int.parse(prefix)); + } + String get displayTitle { return getDisplayTitle(safeTags.title); } diff --git a/macos/Podfile.lock b/macos/Podfile.lock index 80ff75f..e789971 100644 --- a/macos/Podfile.lock +++ b/macos/Podfile.lock @@ -126,4 +126,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: ec3a267b951cf4ed1540d33015451fed0e200941 -COCOAPODS: 1.14.3 +COCOAPODS: 1.15.0 diff --git a/pubspec.lock b/pubspec.lock index f38f4e8..101e3a2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -701,6 +701,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.1" + tuple_dart: + dependency: "direct main" + description: + name: tuple_dart + sha256: "0db473430e680e43111ae7d0cf4e1c222f0dc20350378c845b70c4daa4195708" + url: "https://pub.dev" + source: hosted + version: "1.4.2" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 049a911..3164c2a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -43,6 +43,7 @@ dependencies: sqflite: ^2.2.6 taglib_ffi: ^0.1.0 window_manager: ^0.3.2 + tuple_dart: ^1.4.2 dev_dependencies: flutter_test: