photo_gallery/lib/photo_gallery.dart

116 lines
3.1 KiB
Dart
Raw Normal View History

2020-08-14 18:07:27 +08:00
library photogallery;
import 'dart:async';
import 'dart:io';
2021-03-18 12:00:31 +00:00
import 'dart:typed_data';
2020-08-14 18:07:27 +08:00
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.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({
2021-03-18 12:00:31 +00:00
required MediumType mediumType,
2020-08-14 18:07:27 +08:00
}) async {
final json = await _channel.invokeMethod('listAlbums', {
'mediumType': mediumTypeToJson(mediumType),
});
return json.map<Album>((x) => Album.fromJson(x)).toList();
}
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,
required int total,
int? skip,
int? take,
2020-08-14 18:07:27 +08:00
}) async {
final json = await _channel.invokeMethod('listMedia', {
'albumId': album.id,
'mediumType': mediumTypeToJson(album.mediumType),
2020-08-14 18:07:27 +08:00
'total': total,
'skip': skip,
'take': take,
});
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,
bool? highQuality,
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,
});
return bytes;
}
2020-08-22 17:02:16 +08:00
/// Get album thumbnail by album id
2021-03-18 12:00:31 +00:00
static Future<List<int>> getAlbumThumbnail({
required String albumId,
MediumType? mediumType,
int? width,
int? height,
bool? highQuality,
2020-08-14 18:07:27 +08:00
}) async {
final bytes = await _channel.invokeMethod('getAlbumThumbnail', {
'albumId': albumId,
'mediumType': mediumTypeToJson(mediumType),
2020-08-14 18:07:27 +08:00
'width': width,
'height': height,
'highQuality': highQuality,
});
return bytes;
}
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,
2020-08-14 18:07:27 +08:00
}) async {
final path = await _channel.invokeMethod('getFile', {
'mediumId': mediumId,
'mediumType': mediumTypeToJson(mediumType),
}) as String;
return File(path);
}
2021-01-10 21:09:25 +08:00
static Future<void> cleanCache() async {
_channel.invokeMethod('cleanCache', {});
}
2020-08-14 18:07:27 +08:00
}