From 7898c4bde3faa283064206283e00d7ccad36a2b9 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Fri, 12 May 2023 18:08:16 +0800 Subject: [PATCH] remove unnecessary "total" parameter of "listMedia" API, remove unnecessary "total" property of "MediaPage", use "total" of "Album" instead. --- .../morbit/photogallery/PhotoGalleryPlugin.kt | 54 ++++++++++--------- ios/Classes/SwiftPhotoGalleryPlugin.swift | 1 - lib/photo_gallery.dart | 2 - lib/src/models/album.dart | 1 - lib/src/models/media_page.dart | 11 +--- test/photo_gallery_test.dart | 1 - test/utils/generator.dart | 12 ++--- test/utils/mock_handler.dart | 2 - 8 files changed, 34 insertions(+), 50 deletions(-) diff --git a/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt b/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt index b83a31e..f67970c 100644 --- a/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt +++ b/android/src/main/kotlin/com/morbit/photogallery/PhotoGalleryPlugin.kt @@ -106,12 +106,11 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { val albumId = call.argument("albumId") val mediumType = call.argument("mediumType") val newest = call.argument("newest") - val total = call.argument("total") val skip = call.argument("skip") val take = call.argument("take") executor.submit { result.success( - listMedia(mediumType, albumId!!, newest!!, total!!, skip, take) + listMedia(mediumType, albumId!!, newest!!, skip, take) ) } } @@ -309,35 +308,38 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { return albumMap } - private fun listMedia(mediumType: String?, albumId: String, newest: Boolean, total: Int, skip: Int?, take: Int?): Map { + private fun listMedia(mediumType: String?, albumId: String, newest: Boolean, skip: Int?, take: Int?): Map { return when (mediumType) { imageType -> { - listImages(albumId, newest, total, skip, take) + listImages(albumId, newest, skip, take) } videoType -> { - listVideos(albumId, newest, total, skip, take) + listVideos(albumId, newest, skip, take) } else -> { - val images = listImages(albumId, newest, total, skip, take).get("items") as List> - val videos = listVideos(albumId, newest, total, skip, take).get("items") as List> - var items = (images +videos).sortedWith(compareBy> {it.get("creationDate") as Long}.thenBy { it.get("modifiedDate") as Long }) + val images = listImages(albumId, newest, null, null)["items"] as List> + val videos = listVideos(albumId, newest, null, null)["items"] as List> + var items = (images + videos).sortedWith(compareBy> { it["creationDate"] as Long }.thenBy { it["modifiedDate"] as Long }) if (newest) { items = items.reversed() } + if (skip != null || take != null) { + val start = skip ?: 0 + val total = items.size + val end = if (take == null) total else Integer.min(start + take, total) + items = items.subList(start, end) + } mapOf( "newest" to newest, "start" to (skip ?: 0), - "total" to total, "items" to items ) } } } - private fun listImages(albumId: String, newest: Boolean, total: Int, skip: Int?, take: Int?): Map { + private fun listImages(albumId: String, newest: Boolean, skip: Int?, take: Int?): Map { val media = mutableListOf>() - val offset = skip ?: 0 - val limit = take ?: (total - offset) this.context.run { val isSelection = albumId != allAlbumId @@ -362,18 +364,21 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { // Sort putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, orderBy) // Limit & Offset - putInt(ContentResolver.QUERY_ARG_LIMIT, limit) - putInt(ContentResolver.QUERY_ARG_OFFSET, offset) + if (take != null) putInt(ContentResolver.QUERY_ARG_LIMIT, take) + if (skip != null) putInt(ContentResolver.QUERY_ARG_OFFSET, skip) }, null ) } else { + val offset = if (skip != null) "OFFSET $skip" else "" + val limit = if (take != null) "LIMIT $take" else "" + imageCursor = this.contentResolver.query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageMetadataProjection, selection, selectionArgs, - "$orderBy LIMIT $limit OFFSET $offset" + "$orderBy $offset $limit" ) } @@ -386,16 +391,13 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { return mapOf( "newest" to newest, - "start" to offset, - "total" to total, + "start" to skip, "items" to media ) } - private fun listVideos(albumId: String, newest: Boolean, total: Int, skip: Int?, take: Int?): Map { + private fun listVideos(albumId: String, newest: Boolean, skip: Int?, take: Int?): Map { val media = mutableListOf>() - val offset = skip ?: 0 - val limit = take ?: (total - offset) this.context.run { val isSelection = albumId != allAlbumId @@ -420,18 +422,21 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { // Sort putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, orderBy) // Limit & Offset - putInt(ContentResolver.QUERY_ARG_LIMIT, limit) - putInt(ContentResolver.QUERY_ARG_OFFSET, offset) + if (take != null) putInt(ContentResolver.QUERY_ARG_LIMIT, take) + if (skip != null) putInt(ContentResolver.QUERY_ARG_OFFSET, skip) }, null ) } else { + val offset = if (skip != null) "OFFSET $skip" else "" + val limit = if (take != null) "LIMIT $take" else "" + videoCursor = this.contentResolver.query( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoMetadataProjection, selection, selectionArgs, - "$orderBy LIMIT $limit OFFSET $offset" + "$orderBy $offset $limit" ) } @@ -444,8 +449,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler { return mapOf( "newest" to newest, - "start" to offset, - "total" to total, + "start" to skip, "items" to media ) } diff --git a/ios/Classes/SwiftPhotoGalleryPlugin.swift b/ios/Classes/SwiftPhotoGalleryPlugin.swift index cb1f830..a1c7c56 100644 --- a/ios/Classes/SwiftPhotoGalleryPlugin.swift +++ b/ios/Classes/SwiftPhotoGalleryPlugin.swift @@ -195,7 +195,6 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin { return [ "newest": newest, "start": start, - "total": total, "items": items, ] } diff --git a/lib/photo_gallery.dart b/lib/photo_gallery.dart index 70f8ed8..eb0788c 100644 --- a/lib/photo_gallery.dart +++ b/lib/photo_gallery.dart @@ -36,7 +36,6 @@ class PhotoGallery { static Future _listMedia({ required Album album, bool newest = true, - required int total, int? skip, int? take, }) async { @@ -44,7 +43,6 @@ class PhotoGallery { 'albumId': album.id, 'mediumType': mediumTypeToJson(album.mediumType), 'newest': newest, - 'total': total, 'skip': skip, 'take': take, }); diff --git a/lib/src/models/album.dart b/lib/src/models/album.dart index c989133..cddc9ee 100644 --- a/lib/src/models/album.dart +++ b/lib/src/models/album.dart @@ -37,7 +37,6 @@ class Album { return PhotoGallery._listMedia( album: this, newest: newest, - total: this.count, skip: skip, take: take, ); diff --git a/lib/src/models/media_page.dart b/lib/src/models/media_page.dart index 48e617b..1e49c94 100644 --- a/lib/src/models/media_page.dart +++ b/lib/src/models/media_page.dart @@ -11,9 +11,6 @@ class MediaPage { /// The start offset for those media. final int start; - /// The total number of items. - final int total; - /// The current items. final List items; @@ -21,13 +18,12 @@ class MediaPage { int get end => start + items.length; ///Indicates whether this page is the last in the album. - bool get isLast => end >= total; + bool get isLast => end >= album.count; /// Creates a range of media from platform channel protocol. MediaPage.fromJson(this.album, dynamic json) : newest = json['newest'], start = json['start'], - total = json['total'], items = json['items'].map((x) => Medium.fromJson(x)).toList(); /// Gets the next page of media in the album. @@ -36,7 +32,6 @@ class MediaPage { return PhotoGallery._listMedia( album: album, newest: newest, - total: total, skip: end, take: items.length, ); @@ -49,18 +44,16 @@ class MediaPage { runtimeType == other.runtimeType && album == other.album && start == other.start && - total == other.total && listEquals(items, other.items); @override int get hashCode => - album.hashCode ^ start.hashCode ^ total.hashCode ^ items.hashCode; + album.hashCode ^ start.hashCode ^ items.hashCode; @override String toString() { return 'MediaPage{album: $album, ' 'start: $start, ' - 'total: $total, ' 'items: $items}'; } } diff --git a/test/photo_gallery_test.dart b/test/photo_gallery_test.dart index 62602ff..3607bc1 100644 --- a/test/photo_gallery_test.dart +++ b/test/photo_gallery_test.dart @@ -39,7 +39,6 @@ void main() { album: allAlbum, mediumType: mediumType, newest: newest, - total: allAlbum.count, skip: skip, take: take, ); diff --git a/test/utils/generator.dart b/test/utils/generator.dart index 3675750..ddc794b 100644 --- a/test/utils/generator.dart +++ b/test/utils/generator.dart @@ -31,25 +31,21 @@ class Generator { required String albumId, MediumType? mediumType, bool newest = true, - int total = 10, int? skip, int? take, }) { skip = skip ?? 0; - take = take ?? (total - skip); - + take = take ?? 10; var items = []; int index = skip; while (index < skip + take) { - items.add(generateMediaJson( - mediumId: index.toString(), mediumType: mediumType)); + items.add(generateMediaJson(mediumId: index.toString(), mediumType: mediumType)); index++; } return { "newest": newest, "start": skip, - "total": total, "items": items, }; } @@ -74,7 +70,6 @@ class Generator { required Album album, MediumType? mediumType, bool newest = true, - required int total, int? skip, int? take, }) { @@ -82,9 +77,8 @@ class Generator { albumId: album.id, mediumType: mediumType, newest: newest, - total: total, skip: skip, - take: take, + take: take ?? album.count, ); return MediaPage.fromJson(album, json); } diff --git a/test/utils/mock_handler.dart b/test/utils/mock_handler.dart index 589a48f..f56d76d 100644 --- a/test/utils/mock_handler.dart +++ b/test/utils/mock_handler.dart @@ -13,14 +13,12 @@ Future mockMethodCallHandler(MethodCall call) async { String albumId = call.arguments['albumId']; MediumType? mediumType = jsonToMediumType(call.arguments['mediumType']); bool newest = call.arguments['newest']; - int? total = call.arguments['total']; int? skip = call.arguments['skip']; int? take = call.arguments['take']; dynamic mediaPage = Generator.generateMediaPageJson( albumId: albumId, mediumType: mediumType, newest: newest, - total: total ?? 10, skip: skip, take: take, );