remove unnecessary "total" parameter of "listMedia" API, remove unnecessary "total" property of "MediaPage", use "total" of "Album" instead.

This commit is contained in:
Wenqi Li 2023-05-12 18:08:16 +08:00
parent 6976ab8d3b
commit 7898c4bde3
8 changed files with 34 additions and 50 deletions

View File

@ -106,12 +106,11 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
val albumId = call.argument<String>("albumId") val albumId = call.argument<String>("albumId")
val mediumType = call.argument<String>("mediumType") val mediumType = call.argument<String>("mediumType")
val newest = call.argument<Boolean>("newest") val newest = call.argument<Boolean>("newest")
val total = call.argument<Int>("total")
val skip = call.argument<Int>("skip") val skip = call.argument<Int>("skip")
val take = call.argument<Int>("take") val take = call.argument<Int>("take")
executor.submit { executor.submit {
result.success( 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 return albumMap
} }
private fun listMedia(mediumType: String?, albumId: String, newest: Boolean, total: Int, skip: Int?, take: Int?): Map<String, Any?> { private fun listMedia(mediumType: String?, albumId: String, newest: Boolean, skip: Int?, take: Int?): Map<String, Any?> {
return when (mediumType) { return when (mediumType) {
imageType -> { imageType -> {
listImages(albumId, newest, total, skip, take) listImages(albumId, newest, skip, take)
} }
videoType -> { videoType -> {
listVideos(albumId, newest, total, skip, take) listVideos(albumId, newest, skip, take)
} }
else -> { else -> {
val images = listImages(albumId, newest, total, skip, take).get("items") as List<Map<String, Any?>> val images = listImages(albumId, newest, null, null)["items"] as List<Map<String, Any?>>
val videos = listVideos(albumId, newest, total, skip, take).get("items") as List<Map<String, Any?>> val videos = listVideos(albumId, newest, null, null)["items"] as List<Map<String, Any?>>
var items = (images +videos).sortedWith(compareBy<Map<String, Any?>> {it.get("creationDate") as Long}.thenBy { it.get("modifiedDate") as Long }) var items = (images + videos).sortedWith(compareBy<Map<String, Any?>> { it["creationDate"] as Long }.thenBy { it["modifiedDate"] as Long })
if (newest) { if (newest) {
items = items.reversed() 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( mapOf(
"newest" to newest, "newest" to newest,
"start" to (skip ?: 0), "start" to (skip ?: 0),
"total" to total,
"items" to items "items" to items
) )
} }
} }
} }
private fun listImages(albumId: String, newest: Boolean, total: Int, skip: Int?, take: Int?): Map<String, Any> { private fun listImages(albumId: String, newest: Boolean, skip: Int?, take: Int?): Map<String, Any?> {
val media = mutableListOf<Map<String, Any?>>() val media = mutableListOf<Map<String, Any?>>()
val offset = skip ?: 0
val limit = take ?: (total - offset)
this.context.run { this.context.run {
val isSelection = albumId != allAlbumId val isSelection = albumId != allAlbumId
@ -362,18 +364,21 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
// Sort // Sort
putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, orderBy) putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, orderBy)
// Limit & Offset // Limit & Offset
putInt(ContentResolver.QUERY_ARG_LIMIT, limit) if (take != null) putInt(ContentResolver.QUERY_ARG_LIMIT, take)
putInt(ContentResolver.QUERY_ARG_OFFSET, offset) if (skip != null) putInt(ContentResolver.QUERY_ARG_OFFSET, skip)
}, },
null null
) )
} else { } else {
val offset = if (skip != null) "OFFSET $skip" else ""
val limit = if (take != null) "LIMIT $take" else ""
imageCursor = this.contentResolver.query( imageCursor = this.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
imageMetadataProjection, imageMetadataProjection,
selection, selection,
selectionArgs, selectionArgs,
"$orderBy LIMIT $limit OFFSET $offset" "$orderBy $offset $limit"
) )
} }
@ -386,16 +391,13 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
return mapOf( return mapOf(
"newest" to newest, "newest" to newest,
"start" to offset, "start" to skip,
"total" to total,
"items" to media "items" to media
) )
} }
private fun listVideos(albumId: String, newest: Boolean, total: Int, skip: Int?, take: Int?): Map<String, Any> { private fun listVideos(albumId: String, newest: Boolean, skip: Int?, take: Int?): Map<String, Any?> {
val media = mutableListOf<Map<String, Any?>>() val media = mutableListOf<Map<String, Any?>>()
val offset = skip ?: 0
val limit = take ?: (total - offset)
this.context.run { this.context.run {
val isSelection = albumId != allAlbumId val isSelection = albumId != allAlbumId
@ -420,18 +422,21 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
// Sort // Sort
putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, orderBy) putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, orderBy)
// Limit & Offset // Limit & Offset
putInt(ContentResolver.QUERY_ARG_LIMIT, limit) if (take != null) putInt(ContentResolver.QUERY_ARG_LIMIT, take)
putInt(ContentResolver.QUERY_ARG_OFFSET, offset) if (skip != null) putInt(ContentResolver.QUERY_ARG_OFFSET, skip)
}, },
null null
) )
} else { } else {
val offset = if (skip != null) "OFFSET $skip" else ""
val limit = if (take != null) "LIMIT $take" else ""
videoCursor = this.contentResolver.query( videoCursor = this.contentResolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
videoMetadataProjection, videoMetadataProjection,
selection, selection,
selectionArgs, selectionArgs,
"$orderBy LIMIT $limit OFFSET $offset" "$orderBy $offset $limit"
) )
} }
@ -444,8 +449,7 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
return mapOf( return mapOf(
"newest" to newest, "newest" to newest,
"start" to offset, "start" to skip,
"total" to total,
"items" to media "items" to media
) )
} }

View File

@ -195,7 +195,6 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
return [ return [
"newest": newest, "newest": newest,
"start": start, "start": start,
"total": total,
"items": items, "items": items,
] ]
} }

View File

@ -36,7 +36,6 @@ class PhotoGallery {
static Future<MediaPage> _listMedia({ static Future<MediaPage> _listMedia({
required Album album, required Album album,
bool newest = true, bool newest = true,
required int total,
int? skip, int? skip,
int? take, int? take,
}) async { }) async {
@ -44,7 +43,6 @@ class PhotoGallery {
'albumId': album.id, 'albumId': album.id,
'mediumType': mediumTypeToJson(album.mediumType), 'mediumType': mediumTypeToJson(album.mediumType),
'newest': newest, 'newest': newest,
'total': total,
'skip': skip, 'skip': skip,
'take': take, 'take': take,
}); });

View File

@ -37,7 +37,6 @@ class Album {
return PhotoGallery._listMedia( return PhotoGallery._listMedia(
album: this, album: this,
newest: newest, newest: newest,
total: this.count,
skip: skip, skip: skip,
take: take, take: take,
); );

View File

@ -11,9 +11,6 @@ class MediaPage {
/// The start offset for those media. /// The start offset for those media.
final int start; final int start;
/// The total number of items.
final int total;
/// The current items. /// The current items.
final List<Medium> items; final List<Medium> items;
@ -21,13 +18,12 @@ class MediaPage {
int get end => start + items.length; int get end => start + items.length;
///Indicates whether this page is the last in the album. ///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. /// Creates a range of media from platform channel protocol.
MediaPage.fromJson(this.album, dynamic json) MediaPage.fromJson(this.album, dynamic json)
: newest = json['newest'], : newest = json['newest'],
start = json['start'], start = json['start'],
total = json['total'],
items = json['items'].map<Medium>((x) => Medium.fromJson(x)).toList(); items = json['items'].map<Medium>((x) => Medium.fromJson(x)).toList();
/// Gets the next page of media in the album. /// Gets the next page of media in the album.
@ -36,7 +32,6 @@ class MediaPage {
return PhotoGallery._listMedia( return PhotoGallery._listMedia(
album: album, album: album,
newest: newest, newest: newest,
total: total,
skip: end, skip: end,
take: items.length, take: items.length,
); );
@ -49,18 +44,16 @@ class MediaPage {
runtimeType == other.runtimeType && runtimeType == other.runtimeType &&
album == other.album && album == other.album &&
start == other.start && start == other.start &&
total == other.total &&
listEquals(items, other.items); listEquals(items, other.items);
@override @override
int get hashCode => int get hashCode =>
album.hashCode ^ start.hashCode ^ total.hashCode ^ items.hashCode; album.hashCode ^ start.hashCode ^ items.hashCode;
@override @override
String toString() { String toString() {
return 'MediaPage{album: $album, ' return 'MediaPage{album: $album, '
'start: $start, ' 'start: $start, '
'total: $total, '
'items: $items}'; 'items: $items}';
} }
} }

View File

@ -39,7 +39,6 @@ void main() {
album: allAlbum, album: allAlbum,
mediumType: mediumType, mediumType: mediumType,
newest: newest, newest: newest,
total: allAlbum.count,
skip: skip, skip: skip,
take: take, take: take,
); );

View File

@ -31,25 +31,21 @@ class Generator {
required String albumId, required String albumId,
MediumType? mediumType, MediumType? mediumType,
bool newest = true, bool newest = true,
int total = 10,
int? skip, int? skip,
int? take, int? take,
}) { }) {
skip = skip ?? 0; skip = skip ?? 0;
take = take ?? (total - skip); take = take ?? 10;
var items = []; var items = [];
int index = skip; int index = skip;
while (index < skip + take) { while (index < skip + take) {
items.add(generateMediaJson( items.add(generateMediaJson(mediumId: index.toString(), mediumType: mediumType));
mediumId: index.toString(), mediumType: mediumType));
index++; index++;
} }
return { return {
"newest": newest, "newest": newest,
"start": skip, "start": skip,
"total": total,
"items": items, "items": items,
}; };
} }
@ -74,7 +70,6 @@ class Generator {
required Album album, required Album album,
MediumType? mediumType, MediumType? mediumType,
bool newest = true, bool newest = true,
required int total,
int? skip, int? skip,
int? take, int? take,
}) { }) {
@ -82,9 +77,8 @@ class Generator {
albumId: album.id, albumId: album.id,
mediumType: mediumType, mediumType: mediumType,
newest: newest, newest: newest,
total: total,
skip: skip, skip: skip,
take: take, take: take ?? album.count,
); );
return MediaPage.fromJson(album, json); return MediaPage.fromJson(album, json);
} }

View File

@ -13,14 +13,12 @@ Future<dynamic> mockMethodCallHandler(MethodCall call) async {
String albumId = call.arguments['albumId']; String albumId = call.arguments['albumId'];
MediumType? mediumType = jsonToMediumType(call.arguments['mediumType']); MediumType? mediumType = jsonToMediumType(call.arguments['mediumType']);
bool newest = call.arguments['newest']; bool newest = call.arguments['newest'];
int? total = call.arguments['total'];
int? skip = call.arguments['skip']; int? skip = call.arguments['skip'];
int? take = call.arguments['take']; int? take = call.arguments['take'];
dynamic mediaPage = Generator.generateMediaPageJson( dynamic mediaPage = Generator.generateMediaPageJson(
albumId: albumId, albumId: albumId,
mediumType: mediumType, mediumType: mediumType,
newest: newest, newest: newest,
total: total ?? 10,
skip: skip, skip: skip,
take: take, take: take,
); );