Skip to content

Commit

Permalink
Optimize the sorting logic of local gallery images
Browse files Browse the repository at this point in the history
优化本地画廊图片的排序逻辑
  • Loading branch information
jiangtian616 committed Jan 17, 2024
1 parent ff59c20 commit f0a2a8d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 49 deletions.
2 changes: 2 additions & 0 deletions changelog/v7.4.11+150.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- 网络超时提示文本本地化
- 瀑布流布局下,增加图片最大长度限制
- 优化画廊详情信息UI
- 优化本地画廊图片的排序逻辑
- 修复在某些情况下,下载画廊部分图片状态错误的bug
- 修复瀑布流大布局中,画廊标题和标签居中的bug
- 修复E站更新后无法重置图片配额的bug
Expand Down Expand Up @@ -39,6 +40,7 @@
- Localize the network timeout prompt text
- Add the maximum length limit of the image under the waterfall layout
- Optimize the gallery details information UI
- Optimize the sorting logic of local gallery images
- Fix the bug that the status of downloading some gallery images is incorrect under some circumstances
- Fix the bug that the gallery title and tags are centered in the waterfall layout
- Fix the bug that the image quota cannot be reset
Expand Down
33 changes: 12 additions & 21 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,11 @@ PODS:
- Flutter
- receive_sharing_intent (0.0.1):
- Flutter
- screen_brightness_ios (0.1.0):
- Flutter
- SDWebImage (5.16.0):
- SDWebImage/Core (= 5.16.0)
- SDWebImage/Core (5.16.0)
- Sentry/HybridSDK (8.7.3):
- SentryPrivate (= 8.7.3)
- sentry_flutter (0.0.1):
- Flutter
- FlutterMacOS
- Sentry/HybridSDK (= 8.7.3)
- SentryPrivate (8.7.3)
- share_plus (0.0.1):
- Flutter
- smart_auth (0.0.1):
Expand Down Expand Up @@ -104,7 +99,7 @@ PODS:
- Flutter
- video_player_avfoundation (0.0.1):
- Flutter
- wakelock (0.0.1):
- wakelock_plus (0.0.1):
- Flutter
- webview_flutter_wkwebview (0.0.1):
- Flutter
Expand All @@ -123,15 +118,15 @@ DEPENDENCIES:
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- permission_handler_apple (from `.symlinks/plugins/permission_handler_apple/ios`)
- receive_sharing_intent (from `.symlinks/plugins/receive_sharing_intent/ios`)
- sentry_flutter (from `.symlinks/plugins/sentry_flutter/ios`)
- screen_brightness_ios (from `.symlinks/plugins/screen_brightness_ios/ios`)
- share_plus (from `.symlinks/plugins/share_plus/ios`)
- smart_auth (from `.symlinks/plugins/smart_auth/ios`)
- sqflite (from `.symlinks/plugins/sqflite/ios`)
- sqlite3_flutter_libs (from `.symlinks/plugins/sqlite3_flutter_libs/ios`)
- system_network_proxy (from `.symlinks/plugins/system_network_proxy/ios`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
- video_player_avfoundation (from `.symlinks/plugins/video_player_avfoundation/ios`)
- wakelock (from `.symlinks/plugins/wakelock/ios`)
- wakelock_plus (from `.symlinks/plugins/wakelock_plus/ios`)
- webview_flutter_wkwebview (from `.symlinks/plugins/webview_flutter_wkwebview/ios`)

SPEC REPOS:
Expand All @@ -140,8 +135,6 @@ SPEC REPOS:
- DKPhotoGallery
- FMDB
- SDWebImage
- Sentry
- SentryPrivate
- sqlite3
- SwiftyGif
- Toast
Expand Down Expand Up @@ -173,8 +166,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/permission_handler_apple/ios"
receive_sharing_intent:
:path: ".symlinks/plugins/receive_sharing_intent/ios"
sentry_flutter:
:path: ".symlinks/plugins/sentry_flutter/ios"
screen_brightness_ios:
:path: ".symlinks/plugins/screen_brightness_ios/ios"
share_plus:
:path: ".symlinks/plugins/share_plus/ios"
smart_auth:
Expand All @@ -189,8 +182,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/url_launcher_ios/ios"
video_player_avfoundation:
:path: ".symlinks/plugins/video_player_avfoundation/ios"
wakelock:
:path: ".symlinks/plugins/wakelock/ios"
wakelock_plus:
:path: ".symlinks/plugins/wakelock_plus/ios"
webview_flutter_wkwebview:
:path: ".symlinks/plugins/webview_flutter_wkwebview/ios"

Expand All @@ -211,10 +204,8 @@ SPEC CHECKSUMS:
path_provider_foundation: 37748e03f12783f9de2cb2c4eadfaa25fe6d4852
permission_handler_apple: 44366e37eaf29454a1e7b1b7d736c2cceaeb17ce
receive_sharing_intent: c0d87310754e74c0f9542947e7cbdf3a0335a3b1
screen_brightness_ios: 715ca807df953bf676d339f11464e438143ee625
SDWebImage: 2aea163b50bfcb569a2726b6a754c54a4506fcf6
Sentry: c7a86f43510a7d5678d4de28d78c28ab351d295b
sentry_flutter: f2710a4f537e9a7143b02562b8754ccdc8224751
SentryPrivate: 2eaabf598a46d4b9b8822aef766df2a84caf2e6f
share_plus: 056a1e8ac890df3e33cb503afffaf1e9b4fbae68
smart_auth: 4bedbc118723912d0e45a07e8ab34039c19e04f2
sqflite: 6d358c025f5b867b29ed92fc697fd34924e11904
Expand All @@ -225,8 +216,8 @@ SPEC CHECKSUMS:
Toast: 91b396c56ee72a5790816f40d3a94dd357abc196
url_launcher_ios: ae1517e5e344f5544fb090b079e11f399dfbe4d2
video_player_avfoundation: e489aac24ef5cf7af82702979ed16f2a5ef84cff
wakelock: d0fc7c864128eac40eba1617cb5264d9c940b46f
webview_flutter_wkwebview: b7e70ef1ddded7e69c796c7390ee74180182971f
wakelock_plus: 8b09852c8876491e4b6d179e17dfe2a0b5f60d47
webview_flutter_wkwebview: 2e2d318f21a5e036e2c3f26171342e95908bd60a

PODFILE CHECKSUM: 370170548b26ccd3d9d76d5af60f3c771131cfa7

Expand Down
2 changes: 1 addition & 1 deletion ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
13 changes: 1 addition & 12 deletions lib/src/service/archive_download_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,7 @@ class ArchiveDownloadService extends GetxController with GridBasePageServiceMixi
List<io.File> imageFiles;
try {
imageFiles = directory.listSync().whereType<io.File>().where((image) => FileUtil.isImageExtension(image.path)).toList()
..sort((a, b) {
String aName = basenameWithoutExtension(a.path);
String bName = basenameWithoutExtension(b.path);

int? aIndex = int.tryParse(aName);
int? bIndex = int.tryParse(bName);

if (aIndex != null && bIndex != null) {
return aIndex - bIndex;
}
return aName.compareTo(bName);
});
..sort(FileUtil.compareComicImagesOrderSimple);
} on Exception catch (e) {
toast('getUnpackedImagesFailedMsg'.tr, isShort: false);
Log.uploadError(e, extraInfos: {'dirs': directory.parent.listSync()});
Expand Down
15 changes: 1 addition & 14 deletions lib/src/service/local_gallery_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import 'package:path/path.dart';

import '../model/gallery_image.dart';
import '../pages/download/grid/mixin/grid_download_page_service_mixin.dart';
import '../pages/download/list/local/local_gallery_list_page_logic.dart';
import '../setting/download_setting.dart';
import '../utils/log.dart';
import '../utils/toast_util.dart';
import '../widget/loading_state_indicator.dart';
import 'archive_download_service.dart';

Expand Down Expand Up @@ -97,18 +95,7 @@ class LocalGalleryService extends GetxController with GridBasePageServiceMixin {

List<GalleryImage> getGalleryImages(LocalGallery gallery) {
List<File> imageFiles = Directory(gallery.path).listSync().whereType<File>().where((image) => FileUtil.isImageExtension(image.path)).toList()
..sort((a, b) {
String aName = basenameWithoutExtension(a.path);
String bName = basenameWithoutExtension(b.path);

int? aIndex = int.tryParse(aName);
int? bIndex = int.tryParse(bName);

if (aIndex != null && bIndex != null) {
return aIndex - bIndex;
}
return aName.compareTo(bName);
});
..sort(FileUtil.compareComicImagesOrder);

return imageFiles
.map(
Expand Down
44 changes: 44 additions & 0 deletions lib/src/utils/file_util.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:math';

import 'package:path/path.dart';

Expand All @@ -17,4 +18,47 @@ class FileUtil {
static bool isJHenTaiFile(File file) {
return basename(file.path) == '.nomedia' || _galleryPathPattern.hasMatch(file.parent.path) || _archivePathPattern.hasMatch(file.parent.path);
}

static int compareComicImagesOrderSimple(File a, File b) {
String aName = basenameWithoutExtension(a.path);
String bName = basenameWithoutExtension(b.path);

int? aIndex = int.tryParse(aName);
int? bIndex = int.tryParse(bName);

if (aIndex != null && bIndex != null) {
return aIndex - bIndex;
}

return aName.compareTo(bName);
}

static int compareComicImagesOrder(File a, File b) {
final RegExp regExp = RegExp(r'(\d+)');

String aName = basenameWithoutExtension(a.path);
String bName = basenameWithoutExtension(b.path);

final Iterable<Match> matchesA = regExp.allMatches(aName);
final Iterable<Match> matchesB = regExp.allMatches(bName);

List<int> numbersA = matchesA.map((m) => int.parse(m.group(0)!)).toList();
List<int> numbersB = matchesB.map((m) => int.parse(m.group(0)!)).toList();

if (numbersA.isEmpty || numbersB.isEmpty) {
return aName.compareTo(bName);
}

for (int i = 0; i < min(numbersA.length, numbersB.length); i++) {
if (numbersA[i] != numbersB[i]) {
return numbersA[i].compareTo(numbersB[i]);
}
}

return aName.compareTo(bName);
}
}

void main() {
print(FileUtil.compareComicImagesOrderSimple(File('image10_1.jpg'), File('image1_1.jpg')));
}

0 comments on commit f0a2a8d

Please sign in to comment.