photo_gallery/lib/src/models/medium.dart

169 lines
4.1 KiB
Dart
Raw Normal View History

2020-08-14 18:07:27 +08:00
part of photogallery;
/// A medium in the gallery.
///
/// It can be of image or video [mediumType].
@immutable
class Medium {
/// A unique identifier for the medium.
final String id;
/// The medium filename.
final String? filename;
/// The medium title
final String? title;
2020-08-14 18:07:27 +08:00
/// The medium type.
2021-03-18 12:00:31 +00:00
final MediumType? mediumType;
2020-08-14 18:07:27 +08:00
/// The medium width.
2021-03-18 12:00:31 +00:00
final int? width;
2020-08-14 18:07:27 +08:00
/// The medium height.
2021-03-18 12:00:31 +00:00
final int? height;
2020-08-14 18:07:27 +08:00
/// The medium orientation.
final int? orientation;
2021-01-10 21:43:18 +08:00
/// The medium mimeType.
2021-03-18 12:00:31 +00:00
final String? mimeType;
2021-01-10 21:43:18 +08:00
2020-08-18 16:12:06 +08:00
/// The duration of video
final int duration;
2020-08-14 18:07:27 +08:00
/// The date at which the photo or video was taken.
2021-03-18 12:00:31 +00:00
final DateTime? creationDate;
2020-08-14 18:07:27 +08:00
/// The date at which the photo or video was modified.
2021-03-18 12:00:31 +00:00
final DateTime? modifiedDate;
2020-08-14 18:07:27 +08:00
Medium({
2021-03-18 12:00:31 +00:00
required this.id,
this.filename,
this.title,
2020-08-14 18:07:27 +08:00
this.mediumType,
this.width,
this.height,
this.orientation = 0,
2021-01-10 21:43:18 +08:00
this.mimeType,
2021-03-18 12:00:31 +00:00
this.duration = 0,
2020-08-14 18:07:27 +08:00
this.creationDate,
this.modifiedDate,
});
/// Creates a medium from platform channel protocol.
Medium.fromJson(dynamic json)
: id = json["id"],
filename = json["filename"],
title = json["title"],
2020-08-14 18:07:27 +08:00
mediumType = jsonToMediumType(json["mediumType"]),
width = json["width"],
height = json["height"],
orientation = json["orientation"],
2021-01-10 21:43:18 +08:00
mimeType = json["mimeType"],
2020-08-18 16:12:06 +08:00
duration = json['duration'] ?? 0,
2020-08-14 18:07:27 +08:00
creationDate = json['creationDate'] != null
? DateTime.fromMillisecondsSinceEpoch(json['creationDate'])
: null,
modifiedDate = json['modifiedDate'] != null
? DateTime.fromMillisecondsSinceEpoch(json['modifiedDate'])
: null;
static Medium fromMap(Map map) {
return Medium(
id: map['id'],
filename: map['filename'],
title: map['title'],
2020-08-14 18:07:27 +08:00
mediumType: jsonToMediumType(map['mediumType']),
width: map['width'],
height: map['height'],
orientation: map['orientation'],
2021-01-10 21:43:18 +08:00
mimeType: map["mimeType"],
2020-08-14 18:07:27 +08:00
creationDate: map['creationDate'],
modifiedDate: map['modifiedDate'],
);
}
Map toMap() {
return {
"id": this.id,
"filename": this.filename,
"title": this.title,
2020-08-14 18:07:27 +08:00
"mediumType": mediumTypeToJson(this.mediumType),
"height": this.height,
"orientation": this.orientation,
2021-01-10 21:43:18 +08:00
"mimeType": this.mimeType,
2020-08-14 18:07:27 +08:00
"width": this.width,
"creationDate": this.creationDate,
"modifiedDate": this.modifiedDate,
};
}
/// Get a JPEG thumbnail's data for this medium.
Future<List<int>> getThumbnail({
2021-03-18 12:00:31 +00:00
int? width,
int? height,
bool? highQuality = false,
2020-08-14 18:07:27 +08:00
}) {
return PhotoGallery.getThumbnail(
mediumId: id,
width: width,
height: height,
mediumType: mediumType,
highQuality: highQuality,
);
}
/// Get the original file.
Future<File> getFile() {
return PhotoGallery.getFile(
mediumId: id,
mediumType: mediumType,
);
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Medium &&
runtimeType == other.runtimeType &&
id == other.id &&
filename == other.filename &&
title == other.title &&
2020-08-14 18:07:27 +08:00
mediumType == other.mediumType &&
width == other.width &&
height == other.height &&
orientation == other.orientation &&
2021-01-10 21:43:18 +08:00
mimeType == other.mimeType &&
2020-08-14 18:07:27 +08:00
creationDate == other.creationDate &&
modifiedDate == other.modifiedDate;
@override
int get hashCode =>
id.hashCode ^
filename.hashCode ^
title.hashCode ^
2020-08-14 18:07:27 +08:00
mediumType.hashCode ^
width.hashCode ^
height.hashCode ^
orientation.hashCode ^
2021-01-10 21:43:18 +08:00
mimeType.hashCode ^
2020-08-14 18:07:27 +08:00
creationDate.hashCode ^
modifiedDate.hashCode;
@override
String toString() {
return 'Medium{id: $id, '
'filename: $filename, '
'title: $title, '
2020-08-14 18:07:27 +08:00
'mediumType: $mediumType, '
'width: $width, '
'height: $height, '
'orientation: $orientation, '
2021-01-10 21:43:18 +08:00
'mimeType: $mimeType, '
2020-08-14 18:07:27 +08:00
'creationDate: $creationDate, '
'modifiedDate: $modifiedDate}';
}
}