asset.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. Map<String, dynamic> toJson() {
  40. final json = <String, dynamic>{};
  41. if (isLocal) {
  42. json["local"] = _assetEntityToJson(local!);
  43. } else {
  44. json["remote"] = remote!.toJson();
  45. }
  46. return json;
  47. }
  48. static Asset? fromJson(dynamic value) {
  49. if (value is Map) {
  50. final json = value.cast<String, dynamic>();
  51. final l = json["local"];
  52. if (l != null) {
  53. return Asset.local(_assetEntityFromJson(l));
  54. } else {
  55. return Asset.remote(AssetResponseDto.fromJson(json["remote"]));
  56. }
  57. }
  58. return null;
  59. }
  60. }
  61. Map<String, dynamic> _assetEntityToJson(AssetEntity a) {
  62. final json = <String, dynamic>{};
  63. json["id"] = a.id;
  64. json["typeInt"] = a.typeInt;
  65. json["width"] = a.width;
  66. json["height"] = a.height;
  67. json["duration"] = a.duration;
  68. json["orientation"] = a.orientation;
  69. json["isFavorite"] = a.isFavorite;
  70. json["title"] = a.title;
  71. json["createDateSecond"] = a.createDateSecond;
  72. json["modifiedDateSecond"] = a.modifiedDateSecond;
  73. json["latitude"] = a.latitude;
  74. json["longitude"] = a.longitude;
  75. json["mimeType"] = a.mimeType;
  76. json["subtype"] = a.subtype;
  77. return json;
  78. }
  79. AssetEntity? _assetEntityFromJson(dynamic value) {
  80. if (value is Map) {
  81. final json = value.cast<String, dynamic>();
  82. return AssetEntity(
  83. id: json["id"],
  84. typeInt: json["typeInt"],
  85. width: json["width"],
  86. height: json["height"],
  87. duration: json["duration"],
  88. orientation: json["orientation"],
  89. isFavorite: json["isFavorite"],
  90. title: json["title"],
  91. createDateSecond: json["createDateSecond"],
  92. modifiedDateSecond: json["modifiedDateSecond"],
  93. latitude: json["latitude"],
  94. longitude: json["longitude"],
  95. mimeType: json["mimeType"],
  96. subtype: json["subtype"],
  97. );
  98. }
  99. return null;
  100. }