ente/lib/models/collection.dart

243 lines
5.5 KiB
Dart
Raw Normal View History

2020-10-09 18:43:59 +00:00
import 'dart:convert';
class Collection {
final int id;
2020-10-31 16:11:43 +00:00
final CollectionOwner owner;
2020-10-09 18:43:59 +00:00
final String encryptedKey;
final String keyDecryptionNonce;
final String name;
final CollectionType type;
2020-10-21 22:22:09 +00:00
final CollectionAttributes attributes;
2020-10-31 12:48:41 +00:00
final int updationTime;
final bool isDeleted;
2020-10-09 18:43:59 +00:00
Collection(
this.id,
2020-10-31 16:11:43 +00:00
this.owner,
2020-10-09 18:43:59 +00:00
this.encryptedKey,
this.keyDecryptionNonce,
this.name,
this.type,
2020-10-21 22:22:09 +00:00
this.attributes,
2020-10-31 12:48:41 +00:00
this.updationTime, {
this.isDeleted = false,
});
2020-10-09 18:43:59 +00:00
2020-10-21 22:22:09 +00:00
static CollectionType typeFromString(String type) {
switch (type) {
case "folder":
return CollectionType.folder;
case "favorites":
return CollectionType.favorites;
}
return CollectionType.album;
}
static String typeToString(CollectionType type) {
switch (type) {
case CollectionType.folder:
return "folder";
case CollectionType.favorites:
return "favorites";
default:
return "album";
}
}
2020-10-09 18:43:59 +00:00
Map<String, dynamic> toMap() {
return {
'id': id,
2020-10-31 16:11:43 +00:00
'owner': owner?.toMap(),
2020-10-09 18:43:59 +00:00
'encryptedKey': encryptedKey,
'keyDecryptionNonce': keyDecryptionNonce,
'name': name,
'type': typeToString(type),
2020-10-21 22:22:09 +00:00
'attributes': attributes?.toMap(),
2020-10-31 12:48:41 +00:00
'updationTime': updationTime,
2020-10-09 18:43:59 +00:00
};
}
factory Collection.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return Collection(
map['id'],
2020-10-31 16:11:43 +00:00
CollectionOwner.fromMap(map['owner']),
2020-10-09 18:43:59 +00:00
map['encryptedKey'],
map['keyDecryptionNonce'],
map['name'],
typeFromString(map['type']),
2020-10-21 22:22:09 +00:00
CollectionAttributes.fromMap(map['attributes']),
2020-10-31 12:48:41 +00:00
map['updationTime'],
isDeleted: map['isDeleted'] ?? false,
2020-10-09 18:43:59 +00:00
);
}
String toJson() => json.encode(toMap());
factory Collection.fromJson(String source) =>
Collection.fromMap(json.decode(source));
@override
String toString() {
2020-10-31 16:11:43 +00:00
return 'Collection(id: $id, owner: ${owner.toString()} encryptedKey: $encryptedKey, keyDecryptionNonce: $keyDecryptionNonce, name: $name, type: $type, attributes: $attributes, creationTime: $updationTime)';
2020-10-09 18:43:59 +00:00
}
@override
bool operator ==(Object o) {
if (identical(this, o)) return true;
return o is Collection &&
o.id == id &&
2020-10-31 16:11:43 +00:00
o.owner == owner &&
2020-10-09 18:43:59 +00:00
o.encryptedKey == encryptedKey &&
o.keyDecryptionNonce == keyDecryptionNonce &&
o.name == name &&
o.type == type &&
2020-10-21 22:22:09 +00:00
o.attributes == attributes &&
2020-10-31 12:48:41 +00:00
o.updationTime == updationTime;
2020-10-09 18:43:59 +00:00
}
@override
int get hashCode {
return id.hashCode ^
2020-10-31 16:11:43 +00:00
owner.hashCode ^
2020-10-09 18:43:59 +00:00
encryptedKey.hashCode ^
keyDecryptionNonce.hashCode ^
name.hashCode ^
type.hashCode ^
2020-10-21 22:22:09 +00:00
attributes.hashCode ^
2020-10-31 12:48:41 +00:00
updationTime.hashCode;
2020-10-09 18:43:59 +00:00
}
}
2020-10-09 18:43:59 +00:00
enum CollectionType {
folder,
favorites,
album,
}
2020-10-21 22:22:09 +00:00
class CollectionAttributes {
final String encryptedPath;
final String pathDecryptionNonce;
CollectionAttributes({
this.encryptedPath,
this.pathDecryptionNonce,
});
CollectionAttributes copyWith({
String encryptedPath,
String pathDecryptionNonce,
}) {
return CollectionAttributes(
encryptedPath: encryptedPath ?? this.encryptedPath,
pathDecryptionNonce: pathDecryptionNonce ?? this.pathDecryptionNonce,
);
}
Map<String, dynamic> toMap() {
final map = Map<String, dynamic>();
if (encryptedPath != null) {
map['encryptedPath'] = encryptedPath;
}
if (pathDecryptionNonce != null) {
map['pathDecryptionNonce'] = pathDecryptionNonce;
}
return map;
}
factory CollectionAttributes.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return CollectionAttributes(
encryptedPath: map['encryptedPath'],
pathDecryptionNonce: map['pathDecryptionNonce'],
);
}
String toJson() => json.encode(toMap());
factory CollectionAttributes.fromJson(String source) =>
CollectionAttributes.fromMap(json.decode(source));
@override
String toString() =>
'CollectionAttributes(encryptedPath: $encryptedPath, pathDecryptionNonce: $pathDecryptionNonce)';
@override
bool operator ==(Object o) {
if (identical(this, o)) return true;
return o is CollectionAttributes &&
o.encryptedPath == encryptedPath &&
o.pathDecryptionNonce == pathDecryptionNonce;
}
@override
int get hashCode => encryptedPath.hashCode ^ pathDecryptionNonce.hashCode;
}
2020-10-31 16:11:43 +00:00
class CollectionOwner {
int id;
String email;
String name;
CollectionOwner({
this.id,
this.email,
this.name,
});
CollectionOwner copyWith({
int id,
String email,
String name,
}) {
return CollectionOwner(
id: id ?? this.id,
email: email ?? this.email,
name: name ?? this.name,
);
}
Map<String, dynamic> toMap() {
return {
'id': id,
'email': email,
'name': name,
};
}
factory CollectionOwner.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return CollectionOwner(
id: map['id'],
email: map['email'],
name: map['name'],
);
}
String toJson() => json.encode(toMap());
factory CollectionOwner.fromJson(String source) =>
CollectionOwner.fromMap(json.decode(source));
@override
String toString() => 'CollectionOwner(id: $id, email: $email, name: $name)';
@override
bool operator ==(Object o) {
if (identical(this, o)) return true;
return o is CollectionOwner &&
o.id == id &&
o.email == email &&
o.name == name;
}
@override
int get hashCode => id.hashCode ^ email.hashCode ^ name.hashCode;
}