From 293e2cae95dd55bde49d6ae0c00e3a07854c671d Mon Sep 17 00:00:00 2001 From: Vishnu Mohandas Date: Tue, 21 Apr 2020 17:18:56 +0530 Subject: [PATCH] Fix issues on Samsung phones --- android/app/build.gradle | 13 +++++++++++++ lib/db/db_helper.dart | 4 ++++ lib/models/photo.dart | 15 +++++++++++++-- lib/photo_provider.dart | 16 ++++++++++++---- lib/photo_sync_manager.dart | 1 - lib/ui/album_list_widget.dart | 4 ++-- lib/utils/important_items_filter.dart | 9 ++++++--- pubspec.lock | 2 +- pubspec.yaml | 2 +- 9 files changed, 52 insertions(+), 14 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index dc8412e95..0c7e0b372 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -55,6 +55,19 @@ android { } } +rootProject.allprojects { + subprojects { + project.configurations.all { + resolutionStrategy.eachDependency { details -> + if (details.requested.group == 'com.github.bumptech.glide' + && details.requested.name.contains('glide')) { + details.useVersion "4.9.0" + } + } + } + } +} + flutter { source '../..' } diff --git a/lib/db/db_helper.dart b/lib/db/db_helper.dart index ecd957b01..9ab79e3ca 100644 --- a/lib/db/db_helper.dart +++ b/lib/db/db_helper.dart @@ -15,6 +15,7 @@ class DatabaseHelper { static final columnUploadedFileId = 'uploaded_file_id'; static final columnLocalId = 'local_id'; static final columnLocalPath = 'local_path'; + static final columnRelativePath = 'relative_path'; static final columnThumbnailPath = 'thumbnail_path'; static final columnPath = 'path'; static final columnHash = 'hash'; @@ -51,6 +52,7 @@ class DatabaseHelper { $columnLocalId TEXT, $columnUploadedFileId INTEGER NOT NULL, $columnLocalPath TEXT NOT NULL, + $columnRelativePath TEXT NOT NULL, $columnThumbnailPath TEXT NOT NULL, $columnPath TEXT, $columnHash TEXT NOT NULL, @@ -146,6 +148,7 @@ class DatabaseHelper { row[columnUploadedFileId] = photo.uploadedFileId == null ? -1 : photo.uploadedFileId; row[columnLocalPath] = photo.localPath; + row[columnRelativePath] = photo.relativePath; row[columnThumbnailPath] = photo.thumbnailPath; row[columnPath] = photo.path; row[columnHash] = photo.hash; @@ -160,6 +163,7 @@ class DatabaseHelper { photo.localId = row[columnLocalId]; photo.uploadedFileId = row[columnUploadedFileId]; photo.localPath = row[columnLocalPath]; + photo.relativePath = row[columnRelativePath]; photo.thumbnailPath = row[columnThumbnailPath]; photo.path = row[columnPath]; photo.hash = row[columnHash]; diff --git a/lib/models/photo.dart b/lib/models/photo.dart index 9ca466a4b..1b49f07ff 100644 --- a/lib/models/photo.dart +++ b/lib/models/photo.dart @@ -1,6 +1,8 @@ import 'dart:io'; import 'package:crypto/crypto.dart'; +import 'package:logger/logger.dart'; +import 'package:path/path.dart'; import 'package:photo_manager/photo_manager.dart'; class Photo { @@ -9,6 +11,7 @@ class Photo { String localId; String path; String localPath; + String relativePath; String thumbnailPath; String hash; int createTimestamp; @@ -26,12 +29,20 @@ class Photo { static Future fromAsset(AssetEntity asset) async { Photo photo = Photo(); - var file = (await asset.originFile); photo.uploadedFileId = -1; photo.localId = asset.id; + var file = await asset.originFile; photo.localPath = file.path; + if (Platform.isAndroid) { + photo.relativePath = dirname((asset.relativePath.endsWith("/") + ? asset.relativePath + : asset.relativePath + "/") + + asset.title); + } else { + photo.relativePath = dirname(photo.localPath); + } photo.hash = getHash(file); - photo.thumbnailPath = file.path; + photo.thumbnailPath = photo.localPath; photo.createTimestamp = asset.createDateTime.microsecondsSinceEpoch; return photo; } diff --git a/lib/photo_provider.dart b/lib/photo_provider.dart index 8d91cc26f..b8a62aa19 100644 --- a/lib/photo_provider.dart +++ b/lib/photo_provider.dart @@ -88,7 +88,8 @@ class PhotoProvider extends ChangeNotifier { if (!result) { print("Did not get permission"); } - var galleryList = await PhotoManager.getAssetPathList(type: RequestType.image); + var galleryList = + await PhotoManager.getAssetPathList(type: RequestType.image); galleryList.sort((s1, s2) { return s2.assetCount.compareTo(s1.assetCount); @@ -98,6 +99,13 @@ class PhotoProvider extends ChangeNotifier { this.list.addAll(galleryList); } + Future refreshAllGalleryProperties() async { + for (var gallery in list) { + await gallery.refreshPathProperties(); + } + notifyListeners(); + } + PathProvider getOrCreatePathProvider(AssetPathEntity pathEntity) { pathProviderMap[pathEntity] ??= PathProvider(pathEntity); return pathProviderMap[pathEntity]; @@ -149,13 +157,13 @@ class PathProvider extends ChangeNotifier { void delete(AssetEntity entity) async { final result = await PhotoManager.editor.deleteWithIds([entity.id]); if (result.isNotEmpty) { - await path.refreshPathProperties(dt: path.fetchDatetime); + await Future.delayed(Duration(seconds: 3)); + await provider.refreshAllGalleryProperties(); final list = - await path.getAssetListRange(start: 0, end: provider.list.length); + await path.getAssetListRange(start: 0, end: this.list.length); printListLength("deleted"); this.list.clear(); this.list.addAll(list); - notifyListeners(); } } diff --git a/lib/photo_sync_manager.dart b/lib/photo_sync_manager.dart index 951e62be4..82fef4af3 100644 --- a/lib/photo_sync_manager.dart +++ b/lib/photo_sync_manager.dart @@ -96,7 +96,6 @@ class PhotoSyncManager { Future _downloadDiff(List diff, SharedPreferences prefs) async { var externalPath = (await getApplicationDocumentsDirectory()).path; - _logger.i("External path: " + externalPath); var path = externalPath + "/photos/"; for (Photo photo in diff) { var localPath = path + basename(photo.path); diff --git a/lib/ui/album_list_widget.dart b/lib/ui/album_list_widget.dart index 26d33e4f1..a4198a08e 100644 --- a/lib/ui/album_list_widget.dart +++ b/lib/ui/album_list_widget.dart @@ -6,6 +6,7 @@ import 'package:myapp/models/album.dart'; import 'package:myapp/models/photo.dart'; import 'package:myapp/ui/album_widget.dart'; import 'package:myapp/ui/image_widget.dart'; +import 'package:path/path.dart' as path; class AlbumListWidget extends StatefulWidget { final List photos; @@ -41,8 +42,7 @@ class _AlbumListWidgetState extends State { List _getAlbums(List photos) { final albumMap = new LinkedHashMap>(); for (Photo photo in photos) { - final splitPath = photo.localPath.split("/"); - final folder = splitPath[splitPath.length - 2]; + final folder = path.basename(photo.relativePath); if (!albumMap.containsKey(folder)) { albumMap[folder] = new List(); } diff --git a/lib/utils/important_items_filter.dart b/lib/utils/important_items_filter.dart index b9d0a529f..9fe3e29c4 100644 --- a/lib/utils/important_items_filter.dart +++ b/lib/utils/important_items_filter.dart @@ -1,12 +1,15 @@ import 'package:myapp/models/photo.dart'; import 'package:myapp/utils/gallery_items_filter.dart'; +import 'package:path/path.dart'; class ImportantItemsFilter implements GalleryItemsFilter { @override bool shouldInclude(Photo photo) { // TODO: Improve logic - return photo.localPath.contains("/Camera/") || - photo.localPath.contains("/Download/") || - photo.localPath.contains("/Screenshots/"); + final String folder = basename(photo.relativePath); + return folder == "Camera" || + folder == "DCIM" || + folder == "Download" || + folder == "Screenshot"; } } diff --git a/pubspec.lock b/pubspec.lock index 330735481..3c0f9d5b0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -190,7 +190,7 @@ packages: name: photo_manager url: "https://pub.dartlang.org" source: hosted - version: "0.4.8" + version: "0.5.1-dev.5" photo_view: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 11f995549..d89c0d320 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,7 +23,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.2 - photo_manager: ^0.4.8 + photo_manager: ^0.5.1-dev.5 provider: ^3.1.0 sqflite: ^1.3.0 path_provider: ^1.6.5