gallery_picker/lib/models/media_file.dart

111 lines
2.9 KiB
Dart
Raw Normal View History

2022-12-29 08:45:28 +03:00
import 'dart:async';
2022-12-25 12:30:20 +03:00
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:photo_gallery/photo_gallery.dart';
import 'package:video_thumbnail/video_thumbnail.dart';
2022-12-25 12:30:20 +03:00
import '../controller/gallery_controller.dart';
enum MediaType { image, video }
2022-12-25 12:30:20 +03:00
class MediaFile {
late Medium medium;
late MediaType type;
2022-12-25 12:30:20 +03:00
Uint8List? thumbnail;
2022-12-29 08:45:28 +03:00
Uint8List? data;
late String id;
bool thumbnailFailed = false;
File? file;
2023-01-01 17:55:42 +03:00
bool _noMedium = false;
bool get isVideo => type == MediaType.video;
bool get isImage => type == MediaType.image;
2022-12-29 08:45:28 +03:00
MediaFile({required this.medium}) {
type = medium.mediumType == MediumType.video
? MediaType.video
: MediaType.image;
2022-12-29 08:45:28 +03:00
id = medium.id;
2022-12-25 12:30:20 +03:00
}
MediaFile.file({required this.id, required this.file, required this.type}) {
2023-01-01 17:55:42 +03:00
_noMedium = true;
medium = Medium(
id: id,
mediumType:
type == MediaType.image ? MediumType.image : MediumType.video);
}
Future<Uint8List?> getThumbnail() async {
2023-01-01 17:55:42 +03:00
if (thumbnail == null) {
try {
if (_noMedium) {
thumbnail = isVideo
? await VideoThumbnail.thumbnailData(
video: file!.path,
imageFormat: ImageFormat.JPEG,
maxWidth:
128, // specify the width of the thumbnail, let the height auto-scaled to keep the source aspect ratio
quality: 25,
)
: await getData();
} else {
thumbnail =
Uint8List.fromList(await medium.getThumbnail(highQuality: true));
}
} catch (e) {
thumbnailFailed = true;
}
2022-12-25 12:30:20 +03:00
}
return thumbnail;
2022-12-25 12:30:20 +03:00
}
2022-12-29 08:45:28 +03:00
Future<File> getFile() async {
if (file == null) {
file = await medium.getFile();
return file!;
} else {
return file!;
}
2022-12-29 08:45:28 +03:00
}
Future<Uint8List> getData() async {
if (file == null) {
await getFile();
}
2023-01-01 17:55:42 +03:00
data ??= await file!.readAsBytes();
2022-12-29 08:45:28 +03:00
return data!;
}
void unselect({PhoneGalleryController? controller}) {
if (controller != null) {
controller.unselectMedia(this);
} else {
if (GetInstance().isRegistered<PhoneGalleryController>()) {
Get.find<PhoneGalleryController>().unselectMedia(this);
}
}
2022-12-25 12:30:20 +03:00
}
2022-12-29 08:45:28 +03:00
void select({PhoneGalleryController? controller}) {
if (controller != null) {
controller.selectMedia(this);
} else {
if (GetInstance().isRegistered<PhoneGalleryController>()) {
Get.find<PhoneGalleryController>().selectMedia(this);
}
}
2022-12-25 12:30:20 +03:00
}
2022-12-29 08:45:28 +03:00
bool? isSelected({PhoneGalleryController? controller}) {
if (controller != null) {
return controller.isSelectedMedia(this);
} else {
if (GetInstance().isRegistered<PhoneGalleryController>()) {
return Get.find<PhoneGalleryController>().isSelectedMedia(this);
} else {
return null;
}
}
}
2022-12-25 12:30:20 +03:00
}