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';
|
2022-12-31 16:43:05 +03:00
|
|
|
import 'package:video_thumbnail/video_thumbnail.dart';
|
2022-12-25 12:30:20 +03:00
|
|
|
import '../controller/gallery_controller.dart';
|
|
|
|
|
2022-12-31 16:43:05 +03:00
|
|
|
enum MediaType { image, video }
|
|
|
|
|
2022-12-25 12:30:20 +03:00
|
|
|
class MediaFile {
|
2022-12-31 16:43:05 +03:00
|
|
|
late Medium medium;
|
2023-01-01 17:15:26 +03:00
|
|
|
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;
|
2023-01-01 17:15:26 +03:00
|
|
|
bool get isVideo => type == MediaType.video;
|
|
|
|
bool get isImage => type == MediaType.image;
|
|
|
|
|
2022-12-29 08:45:28 +03:00
|
|
|
MediaFile({required this.medium}) {
|
2023-01-01 17:15:26 +03:00
|
|
|
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
|
|
|
}
|
2023-01-01 17:15:26 +03:00
|
|
|
MediaFile.file({required this.id, required this.file, required this.type}) {
|
2023-01-01 17:55:42 +03:00
|
|
|
_noMedium = true;
|
2023-01-01 17:15:26 +03:00
|
|
|
medium = Medium(
|
|
|
|
id: id,
|
|
|
|
mediumType:
|
|
|
|
type == MediaType.image ? MediumType.image : MediumType.video);
|
2022-12-31 16:43:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-01-06 16:08:30 +03:00
|
|
|
quality: 100,
|
2023-01-01 17:55:42 +03:00
|
|
|
)
|
|
|
|
: await getData();
|
|
|
|
} else {
|
|
|
|
thumbnail =
|
|
|
|
Uint8List.fromList(await medium.getThumbnail(highQuality: true));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
thumbnailFailed = true;
|
2022-12-31 16:43:05 +03:00
|
|
|
}
|
2022-12-25 12:30:20 +03:00
|
|
|
}
|
2022-12-31 16:43:05 +03:00
|
|
|
return thumbnail;
|
2022-12-25 12:30:20 +03:00
|
|
|
}
|
|
|
|
|
2022-12-29 08:45:28 +03:00
|
|
|
Future<File> getFile() async {
|
2022-12-31 16:43:05 +03:00
|
|
|
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
|
|
|
}
|