remove unnecessary "total" parameter of "listMedia" API, remove unnecessary "total" property of "MediaPage", use "total" of "Album" instead.
This commit is contained in:
parent
6976ab8d3b
commit
7898c4bde3
@ -106,12 +106,11 @@ class PhotoGalleryPlugin : FlutterPlugin, MethodCallHandler {
|
||||
val albumId = call.argument<String>("albumId")
|
||||
val mediumType = call.argument<String>("mediumType")
|
||||
val newest = call.argument<Boolean>("newest")
|
||||
val total = call.argument<Int>("total")
|
||||
val skip = call.argument<Int>("skip")
|
||||
val take = call.argument<Int>("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<String, Any?> {
|
||||
private fun listMedia(mediumType: String?, albumId: String, newest: Boolean, skip: Int?, take: Int?): Map<String, Any?> {
|
||||
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<Map<String, Any?>>
|
||||
val videos = listVideos(albumId, newest, total, skip, take).get("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 })
|
||||
val images = listImages(albumId, newest, null, null)["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["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<String, Any> {
|
||||
private fun listImages(albumId: String, newest: Boolean, skip: Int?, take: Int?): Map<String, Any?> {
|
||||
val media = mutableListOf<Map<String, Any?>>()
|
||||
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<String, Any> {
|
||||
private fun listVideos(albumId: String, newest: Boolean, skip: Int?, take: Int?): Map<String, Any?> {
|
||||
val media = mutableListOf<Map<String, Any?>>()
|
||||
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
|
||||
)
|
||||
}
|
||||
|
@ -195,7 +195,6 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
||||
return [
|
||||
"newest": newest,
|
||||
"start": start,
|
||||
"total": total,
|
||||
"items": items,
|
||||
]
|
||||
}
|
||||
|
@ -36,7 +36,6 @@ class PhotoGallery {
|
||||
static Future<MediaPage> _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,
|
||||
});
|
||||
|
@ -37,7 +37,6 @@ class Album {
|
||||
return PhotoGallery._listMedia(
|
||||
album: this,
|
||||
newest: newest,
|
||||
total: this.count,
|
||||
skip: skip,
|
||||
take: take,
|
||||
);
|
||||
|
@ -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<Medium> 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<Medium>((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}';
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,6 @@ void main() {
|
||||
album: allAlbum,
|
||||
mediumType: mediumType,
|
||||
newest: newest,
|
||||
total: allAlbum.count,
|
||||
skip: skip,
|
||||
take: take,
|
||||
);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -13,14 +13,12 @@ Future<dynamic> 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,
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user