asset.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'package:hive/hive.dart';
  2. import 'package:immich_mobile/constants/hive_box.dart';
  3. import 'package:openapi/api.dart';
  4. import 'package:photo_manager/photo_manager.dart';
  5. /// Asset (online or local)
  6. class Asset {
  7. Asset.remote(this.remote) {
  8. local = null;
  9. }
  10. Asset.local(this.local) {
  11. remote = null;
  12. }
  13. late final AssetResponseDto? remote;
  14. late final AssetEntity? local;
  15. bool get isRemote => remote != null;
  16. bool get isLocal => local != null;
  17. String get deviceId =>
  18. isRemote ? remote!.deviceId : Hive.box(userInfoBox).get(deviceIdKey);
  19. String get deviceAssetId => isRemote ? remote!.deviceAssetId : local!.id;
  20. String get id => isLocal ? local!.id : remote!.id;
  21. double? get latitude =>
  22. isLocal ? local!.latitude : remote!.exifInfo?.latitude?.toDouble();
  23. double? get longitude =>
  24. isLocal ? local!.longitude : remote!.exifInfo?.longitude?.toDouble();
  25. DateTime get createdAt =>
  26. isLocal ? local!.createDateTime : DateTime.parse(remote!.createdAt);
  27. bool get isImage => isLocal
  28. ? local!.type == AssetType.image
  29. : remote!.type == AssetTypeEnum.IMAGE;
  30. String get duration => isRemote
  31. ? remote!.duration
  32. : Duration(seconds: local!.duration).toString();
  33. /// use only for tests
  34. set createdAt(DateTime val) {
  35. if (isRemote) {
  36. remote!.createdAt = val.toIso8601String();
  37. }
  38. }
  39. @override
  40. bool operator ==(other) {
  41. if (other is! Asset) return false;
  42. return id == other.id && isLocal == other.isLocal;
  43. }
  44. @override
  45. int get hashCode => id.hashCode;
  46. Map<String, dynamic> toJson() {
  47. final json = <String, dynamic>{};
  48. if (isLocal) {
  49. json["local"] = _assetEntityToJson(local!);
  50. } else {
  51. json["remote"] = remote!.toJson();
  52. }
  53. return json;
  54. }
  55. static Asset? fromJson(dynamic value) {
  56. if (value is Map) {
  57. final json = value.cast<String, dynamic>();
  58. final l = json["local"];
  59. if (l != null) {
  60. return Asset.local(_assetEntityFromJson(l));
  61. } else {
  62. return Asset.remote(AssetResponseDto.fromJson(json["remote"]));
  63. }
  64. }
  65. return null;
  66. }
  67. }
  68. Map<String, dynamic> _assetEntityToJson(AssetEntity a) {
  69. final json = <String, dynamic>{};
  70. json["id"] = a.id;
  71. json["typeInt"] = a.typeInt;
  72. json["width"] = a.width;
  73. json["height"] = a.height;
  74. json["duration"] = a.duration;
  75. json["orientation"] = a.orientation;
  76. json["isFavorite"] = a.isFavorite;
  77. json["title"] = a.title;
  78. json["createDateSecond"] = a.createDateSecond;
  79. json["modifiedDateSecond"] = a.modifiedDateSecond;
  80. json["latitude"] = a.latitude;
  81. json["longitude"] = a.longitude;
  82. json["mimeType"] = a.mimeType;
  83. json["subtype"] = a.subtype;
  84. return json;
  85. }
  86. AssetEntity? _assetEntityFromJson(dynamic value) {
  87. if (value is Map) {
  88. final json = value.cast<String, dynamic>();
  89. return AssetEntity(
  90. id: json["id"],
  91. typeInt: json["typeInt"],
  92. width: json["width"],
  93. height: json["height"],
  94. duration: json["duration"],
  95. orientation: json["orientation"],
  96. isFavorite: json["isFavorite"],
  97. title: json["title"],
  98. createDateSecond: json["createDateSecond"],
  99. modifiedDateSecond: json["modifiedDateSecond"],
  100. latitude: json["latitude"],
  101. longitude: json["longitude"],
  102. mimeType: json["mimeType"],
  103. subtype: json["subtype"],
  104. );
  105. }
  106. return null;
  107. }