Skip to content

Commit

Permalink
feat: sort folders ending with numbers manually
Browse files Browse the repository at this point in the history
this allows folders like `Music 2` and `Music 12` to be sorted properly based on number not text
  • Loading branch information
MSOB7YY committed Jul 2, 2024
1 parent 8b42d4c commit a5badc2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
6 changes: 3 additions & 3 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ android {
debug {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}

release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}

certificate {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
Expand Down
76 changes: 74 additions & 2 deletions lib/controller/folders_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,79 @@ class Folders {
return false;
});
}
map.addEntries(newFolders);
map.sortBy((e) => e.key.folderName.toLowerCase());

map.addEntries(newFolders); // dont clear

final parsedMap = _buildParsedMap(map.keys.map((e) => e.folderName.toLowerCase()));
final sorted = map.entries.toList()
..sort(
(entryA, entryB) {
final a = entryA.key.folderName.toLowerCase();
final b = entryB.key.folderName.toLowerCase();
final parsedA = parsedMap[a];
final parsedB = parsedMap[b];
if (parsedA != null && parsedB != null) {
if (parsedA.startAtIndex == parsedB.startAtIndex) {
// -- basically checking textPart is enough to know but we check startAtIndex to speed things up.
if (parsedA.textPart == parsedB.textPart) {
final numbersCompare = parsedA.extractedNumber.compareTo(parsedB.extractedNumber);
if (numbersCompare != 0) return numbersCompare;
}
}
}
return a.compareTo(b);
},
);

map.assignAllEntries(sorted); // we clear after building new sorted one
}

Map<String, _ParsedResult?> _buildParsedMap(Iterable<String> names) {
final parsedMap = <String, _ParsedResult?>{};
for (final n in names) {
parsedMap[n] = _parseNumberAtEnd(n);
}
return parsedMap;
}

_ParsedResult? _parseNumberAtEnd(String text) {
final codes = text.codeUnits;
final codesL = codes.length;
bool wasAddingNumber = false;
final charCodes = <int>[];
for (int i = codesL - 1; i >= 0; i--) {
final code = codes[i];
if (code >= 0x0030 && code <= 0x0039) {
// -- from 0 to 9
wasAddingNumber = true;
charCodes.add(code);
} else {
if (wasAddingNumber) break;
}
}
if (charCodes.isNotEmpty) {
final startIndex = codes.length - charCodes.length;
return _ParsedResult(
extractedNumber: int.parse(String.fromCharCodes(charCodes.reversed)),
charactersCount: charCodes.length,
startAtIndex: startIndex,
textPart: text.substring(0, startIndex),
);
}
return null;
}
}

class _ParsedResult {
final int extractedNumber;
final int charactersCount;
final int startAtIndex;
final String textPart;

const _ParsedResult({
required this.extractedNumber,
required this.charactersCount,
required this.startAtIndex,
required this.textPart,
});
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: namida
description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter
publish_to: "none"
version: 2.8.8-beta+240701234
version: 2.8.9-beta+240702128

environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down

0 comments on commit a5badc2

Please sign in to comment.