2020-08-14 18:07:27 +08:00
|
|
|
library photogallery;
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:ui' as ui;
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
|
|
|
|
part 'src/common/medium_type.dart';
|
|
|
|
part 'src/image_providers/album_thumbnail_provider.dart';
|
|
|
|
part 'src/image_providers/photo_provider.dart';
|
|
|
|
part 'src/image_providers/thumbnail_provider.dart';
|
|
|
|
part 'src/models/album.dart';
|
|
|
|
part 'src/models/media_page.dart';
|
|
|
|
part 'src/models/medium.dart';
|
|
|
|
|
|
|
|
/// Accessing the native photo gallery.
|
|
|
|
class PhotoGallery {
|
|
|
|
static const MethodChannel _channel = const MethodChannel('photo_gallery');
|
|
|
|
|
2020-08-22 17:02:16 +08:00
|
|
|
/// List all available gallery albums and counts number of items of [MediumType].
|
2020-08-14 18:07:27 +08:00
|
|
|
static Future<List<Album>> listAlbums({
|
2023-05-11 21:56:25 +08:00
|
|
|
MediumType? mediumType,
|
2023-05-13 15:49:58 +08:00
|
|
|
bool newest = true,
|
|
|
|
bool hideIfEmpty = true,
|
2020-08-14 18:07:27 +08:00
|
|
|
}) async {
|
|
|
|
final json = await _channel.invokeMethod('listAlbums', {
|
|
|
|
'mediumType': mediumTypeToJson(mediumType),
|
2023-05-13 15:49:58 +08:00
|
|
|
'newest': newest,
|
2023-05-03 18:54:23 +08:00
|
|
|
'hideIfEmpty': hideIfEmpty,
|
2020-08-14 18:07:27 +08:00
|
|
|
});
|
2023-07-24 00:42:07 +08:00
|
|
|
return json.map<Album>((album) => Album.fromJson(album, mediumType, newest)).toList();
|
2020-08-14 18:07:27 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 17:02:16 +08:00
|
|
|
/// List all available media in a specific album, support pagination of media
|
2020-08-14 18:07:27 +08:00
|
|
|
static Future<MediaPage> _listMedia({
|
2021-03-18 12:00:31 +00:00
|
|
|
required Album album,
|
|
|
|
int? skip,
|
|
|
|
int? take,
|
2023-07-24 00:33:23 +08:00
|
|
|
bool? lightWeight,
|
2020-08-14 18:07:27 +08:00
|
|
|
}) async {
|
|
|
|
final json = await _channel.invokeMethod('listMedia', {
|
|
|
|
'albumId': album.id,
|
2020-08-22 17:01:33 +08:00
|
|
|
'mediumType': mediumTypeToJson(album.mediumType),
|
2023-05-13 15:49:58 +08:00
|
|
|
'newest': album.newest,
|
2020-08-14 18:07:27 +08:00
|
|
|
'skip': skip,
|
|
|
|
'take': take,
|
2023-07-24 00:33:23 +08:00
|
|
|
'lightWeight': lightWeight,
|
2020-08-14 18:07:27 +08:00
|
|
|
});
|
2020-08-22 17:01:33 +08:00
|
|
|
return MediaPage.fromJson(album, json);
|
2020-08-14 18:07:27 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 17:02:16 +08:00
|
|
|
/// Get medium metadata by medium id
|
2020-08-14 18:07:27 +08:00
|
|
|
static Future<Medium> getMedium({
|
2021-03-18 12:00:31 +00:00
|
|
|
required String mediumId,
|
|
|
|
MediumType? mediumType,
|
2020-08-14 18:07:27 +08:00
|
|
|
}) async {
|
|
|
|
final json = await _channel.invokeMethod('getMedium', {
|
|
|
|
'mediumId': mediumId,
|
|
|
|
'mediumType': mediumTypeToJson(mediumType),
|
|
|
|
});
|
|
|
|
return Medium.fromJson(json);
|
|
|
|
}
|
|
|
|
|
2020-08-22 17:02:16 +08:00
|
|
|
/// Get medium thumbnail by medium id
|
2021-03-18 12:00:31 +00:00
|
|
|
static Future<List<int>> getThumbnail({
|
|
|
|
required String mediumId,
|
|
|
|
MediumType? mediumType,
|
|
|
|
int? width,
|
|
|
|
int? height,
|
2021-03-30 22:30:21 +08:00
|
|
|
bool? highQuality = false,
|
2020-08-14 18:07:27 +08:00
|
|
|
}) async {
|
|
|
|
final bytes = await _channel.invokeMethod('getThumbnail', {
|
|
|
|
'mediumId': mediumId,
|
|
|
|
'mediumType': mediumTypeToJson(mediumType),
|
|
|
|
'width': width,
|
|
|
|
'height': height,
|
|
|
|
'highQuality': highQuality,
|
|
|
|
});
|
2021-04-15 20:35:53 +08:00
|
|
|
if (bytes == null) throw "Failed to fetch thumbnail of medium $mediumId";
|
2021-03-30 22:30:21 +08:00
|
|
|
return new List<int>.from(bytes);
|
2020-08-14 18:07:27 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 17:02:16 +08:00
|
|
|
/// Get album thumbnail by album id
|
2023-07-25 22:36:41 +08:00
|
|
|
static Future<List<int>> getAlbumThumbnail({
|
2021-03-18 12:00:31 +00:00
|
|
|
required String albumId,
|
|
|
|
MediumType? mediumType,
|
2023-05-13 15:49:58 +08:00
|
|
|
bool newest = true,
|
2021-03-18 12:00:31 +00:00
|
|
|
int? width,
|
|
|
|
int? height,
|
2021-03-30 22:30:21 +08:00
|
|
|
bool? highQuality = false,
|
2020-08-14 18:07:27 +08:00
|
|
|
}) async {
|
|
|
|
final bytes = await _channel.invokeMethod('getAlbumThumbnail', {
|
|
|
|
'albumId': albumId,
|
2020-09-20 18:34:26 +08:00
|
|
|
'mediumType': mediumTypeToJson(mediumType),
|
2023-05-13 15:49:58 +08:00
|
|
|
'newest': newest,
|
2020-08-14 18:07:27 +08:00
|
|
|
'width': width,
|
|
|
|
'height': height,
|
|
|
|
'highQuality': highQuality,
|
|
|
|
});
|
2023-07-25 22:36:41 +08:00
|
|
|
if (bytes == null) throw "Failed to fetch thumbnail of album $albumId";
|
|
|
|
return new List<int>.from(bytes);
|
2020-08-14 18:07:27 +08:00
|
|
|
}
|
|
|
|
|
2020-08-22 17:02:16 +08:00
|
|
|
/// get medium file by medium id
|
2020-08-14 18:07:27 +08:00
|
|
|
static Future<File> getFile({
|
2021-03-18 12:00:31 +00:00
|
|
|
required String mediumId,
|
|
|
|
MediumType? mediumType,
|
2021-04-13 22:02:35 +08:00
|
|
|
String? mimeType,
|
2020-08-14 18:07:27 +08:00
|
|
|
}) async {
|
|
|
|
final path = await _channel.invokeMethod('getFile', {
|
|
|
|
'mediumId': mediumId,
|
|
|
|
'mediumType': mediumTypeToJson(mediumType),
|
2021-04-13 22:02:35 +08:00
|
|
|
'mimeType': mimeType,
|
|
|
|
}) as String?;
|
|
|
|
if (path == null) throw "Cannot get file $mediumId with type $mimeType";
|
2020-08-14 18:07:27 +08:00
|
|
|
return File(path);
|
|
|
|
}
|
2021-01-10 21:09:25 +08:00
|
|
|
|
2023-07-24 00:42:07 +08:00
|
|
|
/// Delete medium by medium id
|
|
|
|
static Future<void> deleteMedium({
|
|
|
|
required String mediumId,
|
|
|
|
MediumType? mediumType,
|
|
|
|
}) async {
|
|
|
|
await _channel.invokeMethod('deleteMedium', {
|
|
|
|
'mediumId': mediumId,
|
|
|
|
'mediumType': mediumTypeToJson(mediumType),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-10 21:09:25 +08:00
|
|
|
static Future<void> cleanCache() async {
|
|
|
|
_channel.invokeMethod('cleanCache', {});
|
|
|
|
}
|
2020-08-14 18:07:27 +08:00
|
|
|
}
|