ente/lib/models/collection.dart

161 lines
3.9 KiB
Dart
Raw Normal View History

2020-10-09 18:43:59 +00:00
import 'dart:convert';
import 'package:flutter/foundation.dart';
class Collection {
final int id;
final int ownerID;
final String encryptedKey;
final String keyDecryptionNonce;
final String name;
final CollectionType type;
2020-10-10 22:03:25 +00:00
final String encryptedPath;
2020-10-10 23:44:04 +00:00
final String pathDecryptionNonce;
2020-10-10 22:03:25 +00:00
final int creationTime;
2020-10-09 18:43:59 +00:00
final List<String> sharees;
Collection(
this.id,
this.ownerID,
this.encryptedKey,
this.keyDecryptionNonce,
this.name,
this.type,
2020-10-10 22:03:25 +00:00
this.encryptedPath,
2020-10-10 23:44:04 +00:00
this.pathDecryptionNonce,
2020-10-10 22:03:25 +00:00
this.creationTime,
2020-10-09 18:43:59 +00:00
this.sharees,
);
static Collection emptyCollection() {
2020-10-10 23:44:04 +00:00
return Collection(
null, null, null, null, null, null, null, null, null, List<String>());
2020-10-09 18:43:59 +00:00
}
Collection copyWith({
int id,
int ownerID,
String encryptedKey,
String keyDecryptionNonce,
String name,
CollectionType type,
2020-10-10 22:03:25 +00:00
String encryptedPath,
2020-10-10 23:44:04 +00:00
String pathDecryptionNonce,
2020-10-10 22:03:25 +00:00
int creationTime,
2020-10-09 18:43:59 +00:00
List<String> sharees,
}) {
return Collection(
id ?? this.id,
ownerID ?? this.ownerID,
encryptedKey ?? this.encryptedKey,
keyDecryptionNonce ?? this.keyDecryptionNonce,
name ?? this.name,
type ?? this.type,
2020-10-10 22:03:25 +00:00
encryptedPath ?? this.encryptedPath,
2020-10-10 23:44:04 +00:00
encryptedPath ?? this.pathDecryptionNonce,
2020-10-10 22:03:25 +00:00
creationTime ?? this.creationTime,
2020-10-09 18:43:59 +00:00
sharees ?? this.sharees,
);
}
Map<String, dynamic> toMap() {
return {
'id': id,
'ownerID': ownerID,
'encryptedKey': encryptedKey,
'keyDecryptionNonce': keyDecryptionNonce,
'name': name,
'type': typeToString(type),
2020-10-10 22:03:25 +00:00
'creationTime': creationTime,
'encryptedPath': encryptedPath,
2020-10-10 23:44:04 +00:00
'pathDecryptionNonce': pathDecryptionNonce,
2020-10-09 18:43:59 +00:00
'sharees': sharees,
};
}
factory Collection.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return Collection(
map['id'],
map['ownerID'],
map['encryptedKey'],
map['keyDecryptionNonce'],
map['name'],
typeFromString(map['type']),
2020-10-10 22:03:25 +00:00
map['encryptedPath'],
2020-10-10 23:44:04 +00:00
map['pathDecryptionNonce'],
2020-10-10 22:03:25 +00:00
map['creationTime'],
map['sharees'] == null ? null : List<String>.from(map['sharees']),
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-10 23:44:04 +00:00
return 'Collection(id: $id, ownerID: $ownerID, encryptedKey: $encryptedKey, keyDecryptionNonce: $keyDecryptionNonce, name: $name, type: $type, encryptedPath: $encryptedPath, pathDecryptionNonce: $pathDecryptionNonce, creationTime: $creationTime, sharees: $sharees)';
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 &&
o.ownerID == ownerID &&
o.encryptedKey == encryptedKey &&
o.keyDecryptionNonce == keyDecryptionNonce &&
o.name == name &&
o.type == type &&
2020-10-10 22:03:25 +00:00
o.encryptedPath == encryptedPath &&
2020-10-10 23:44:04 +00:00
o.pathDecryptionNonce == pathDecryptionNonce &&
2020-10-10 22:03:25 +00:00
o.creationTime == creationTime &&
listEquals(o.sharees, sharees);
2020-10-09 18:43:59 +00:00
}
@override
int get hashCode {
return id.hashCode ^
ownerID.hashCode ^
encryptedKey.hashCode ^
keyDecryptionNonce.hashCode ^
name.hashCode ^
type.hashCode ^
2020-10-10 22:03:25 +00:00
encryptedPath.hashCode ^
2020-10-10 23:44:04 +00:00
pathDecryptionNonce.hashCode ^
2020-10-10 22:03:25 +00:00
creationTime.hashCode ^
sharees.hashCode;
2020-10-09 18:43:59 +00:00
}
}
CollectionType typeFromString(String type) {
2020-10-09 18:43:59 +00:00
switch (type) {
case "folder":
return CollectionType.folder;
case "favorites":
return CollectionType.favorites;
}
return CollectionType.album;
}
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
enum CollectionType {
folder,
favorites,
album,
}