Skip to content

Commit

Permalink
parse filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Apr 26, 2024
1 parent 0b29b28 commit 3a88410
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -302,14 +305,30 @@ class _TraxHomePageState extends State<TraxHomePage>
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<String, int> 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));

Expand Down
31 changes: 31 additions & 0 deletions lib/utils/track_utils.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -10,6 +13,34 @@ extension TrackUtils on Track {
_t = t;
}

static Tuple2<String, int> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: ec3a267b951cf4ed1540d33015451fed0e200941

COCOAPODS: 1.14.3
COCOAPODS: 1.15.0
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3a88410

Please sign in to comment.