ente/lib/models/collection.dart

302 lines
7.4 KiB
Dart
Raw Normal View History

2020-10-09 18:43:59 +00:00
import 'dart:convert';
2020-11-02 14:38:59 +00:00
import 'package:flutter/foundation.dart';
2020-10-09 18:43:59 +00:00
class Collection {
final int id;
2020-11-02 14:38:59 +00:00
final User owner;
2020-10-09 18:43:59 +00:00
final String encryptedKey;
final String keyDecryptionNonce;
final String name;
final String encryptedName;
final String nameDecryptionNonce;
2020-10-09 18:43:59 +00:00
final CollectionType type;
2020-10-21 22:22:09 +00:00
final CollectionAttributes attributes;
2020-11-02 14:38:59 +00:00
final List<User> sharees;
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.encryptedName,
this.nameDecryptionNonce,
2020-10-09 18:43:59 +00:00
this.type,
2020-10-21 22:22:09 +00:00
this.attributes,
2020-11-02 14:38:59 +00:00
this.sharees,
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-11-02 14:38:59 +00:00
Collection copyWith({
int id,
User owner,
String encryptedKey,
String keyDecryptionNonce,
String name,
String encryptedName,
String nameDecryptionNonce,
2020-11-02 14:38:59 +00:00
CollectionType type,
CollectionAttributes attributes,
List<User> sharees,
int updationTime,
bool isDeleted,
}) {
return Collection(
id ?? this.id,
owner ?? this.owner,
encryptedKey ?? this.encryptedKey,
keyDecryptionNonce ?? this.keyDecryptionNonce,
name ?? this.name,
encryptedName ?? this.encryptedName,
nameDecryptionNonce ?? this.nameDecryptionNonce,
2020-11-02 14:38:59 +00:00
type ?? this.type,
attributes ?? this.attributes,
sharees ?? this.sharees,
updationTime ?? this.updationTime,
isDeleted: isDeleted ?? this.isDeleted,
);
}
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,
'encryptedName': encryptedName,
'nameDecryptionNonce': nameDecryptionNonce,
'type': typeToString(type),
2020-10-21 22:22:09 +00:00
'attributes': attributes?.toMap(),
2020-11-02 14:38:59 +00:00
'sharees': sharees?.map((x) => x?.toMap())?.toList(),
2020-10-31 12:48:41 +00:00
'updationTime': updationTime,
2020-11-02 14:38:59 +00:00
'isDeleted': isDeleted,
2020-10-09 18:43:59 +00:00
};
}
factory Collection.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
2020-11-02 14:38:59 +00:00
final sharees = (map['sharees'] == null || map['sharees'].length == 0)
? List<User>()
: List<User>.from(map['sharees'].map((x) => User.fromMap(x)));
2020-10-09 18:43:59 +00:00
return Collection(
map['id'],
2020-11-02 14:38:59 +00:00
User.fromMap(map['owner']),
2020-10-09 18:43:59 +00:00
map['encryptedKey'],
map['keyDecryptionNonce'],
map['name'],
map['encryptedName'],
map['nameDecryptionNonce'],
typeFromString(map['type']),
2020-10-21 22:22:09 +00:00
CollectionAttributes.fromMap(map['attributes']),
2020-11-02 14:38:59 +00:00
sharees,
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() {
return 'Collection(id: $id, owner: $owner, encryptedKey: $encryptedKey, keyDecryptionNonce: $keyDecryptionNonce, name: $name, encryptedName: $encryptedName, nameDecryptionNonce: $nameDecryptionNonce, type: $type, attributes: $attributes, sharees: $sharees, updationTime: $updationTime, isDeleted: $isDeleted)';
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.encryptedName == encryptedName &&
o.nameDecryptionNonce == nameDecryptionNonce &&
2020-10-09 18:43:59 +00:00
o.type == type &&
2020-10-21 22:22:09 +00:00
o.attributes == attributes &&
2020-11-02 14:38:59 +00:00
listEquals(o.sharees, sharees) &&
o.updationTime == updationTime &&
o.isDeleted == isDeleted;
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 ^
encryptedName.hashCode ^
nameDecryptionNonce.hashCode ^
2020-10-09 18:43:59 +00:00
type.hashCode ^
2020-10-21 22:22:09 +00:00
attributes.hashCode ^
2020-11-02 14:38:59 +00:00
sharees.hashCode ^
updationTime.hashCode ^
isDeleted.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;
final int version;
2021-01-24 06:12:10 +00:00
2020-10-21 22:22:09 +00:00
CollectionAttributes({
this.encryptedPath,
this.pathDecryptionNonce,
this.version,
2020-10-21 22:22:09 +00:00
});
CollectionAttributes copyWith({
String encryptedPath,
String pathDecryptionNonce,
int version,
2020-10-21 22:22:09 +00:00
}) {
return CollectionAttributes(
encryptedPath: encryptedPath ?? this.encryptedPath,
pathDecryptionNonce: pathDecryptionNonce ?? this.pathDecryptionNonce,
version: version ?? this.version,
2020-10-21 22:22:09 +00:00
);
}
Map<String, dynamic> toMap() {
final map = Map<String, dynamic>();
if (encryptedPath != null) {
map['encryptedPath'] = encryptedPath;
}
if (pathDecryptionNonce != null) {
map['pathDecryptionNonce'] = pathDecryptionNonce;
}
map['version'] = version ?? 0;
2020-10-21 22:22:09 +00:00
return map;
}
factory CollectionAttributes.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return CollectionAttributes(
encryptedPath: map['encryptedPath'],
pathDecryptionNonce: map['pathDecryptionNonce'],
version: map['version'] ?? 0,
2020-10-21 22:22:09 +00:00
);
}
String toJson() => json.encode(toMap());
factory CollectionAttributes.fromJson(String source) =>
CollectionAttributes.fromMap(json.decode(source));
@override
String toString() =>
'CollectionAttributes(encryptedPath: $encryptedPath, pathDecryptionNonce: $pathDecryptionNonce, version: $version)';
2020-10-21 22:22:09 +00:00
@override
bool operator ==(Object o) {
if (identical(this, o)) return true;
return o is CollectionAttributes &&
o.encryptedPath == encryptedPath &&
o.pathDecryptionNonce == pathDecryptionNonce &&
o.version == version;
2020-10-21 22:22:09 +00:00
}
@override
int get hashCode =>
encryptedPath.hashCode ^ pathDecryptionNonce.hashCode ^ version.hashCode;
2020-10-21 22:22:09 +00:00
}
2020-10-31 16:11:43 +00:00
2020-11-02 14:38:59 +00:00
class User {
2020-10-31 16:11:43 +00:00
int id;
String email;
String name;
2020-11-02 14:38:59 +00:00
User({
2020-10-31 16:11:43 +00:00
this.id,
this.email,
this.name,
});
2020-11-02 14:38:59 +00:00
User copyWith({
2020-10-31 16:11:43 +00:00
int id,
String email,
String name,
}) {
2020-11-02 14:38:59 +00:00
return User(
2020-10-31 16:11:43 +00:00
id: id ?? this.id,
email: email ?? this.email,
name: name ?? this.name,
);
}
Map<String, dynamic> toMap() {
return {
'id': id,
'email': email,
'name': name,
};
}
2020-11-02 14:38:59 +00:00
factory User.fromMap(Map<String, dynamic> map) {
2020-10-31 16:11:43 +00:00
if (map == null) return null;
2020-11-02 14:38:59 +00:00
return User(
2020-10-31 16:11:43 +00:00
id: map['id'],
email: map['email'],
name: map['name'],
);
}
String toJson() => json.encode(toMap());
2020-11-02 14:38:59 +00:00
factory User.fromJson(String source) => User.fromMap(json.decode(source));
2020-10-31 16:11:43 +00:00
@override
String toString() => 'CollectionOwner(id: $id, email: $email, name: $name)';
@override
bool operator ==(Object o) {
if (identical(this, o)) return true;
2020-11-02 14:38:59 +00:00
return o is User && o.id == id && o.email == email && o.name == name;
2020-10-31 16:11:43 +00:00
}
@override
int get hashCode => id.hashCode ^ email.hashCode ^ name.hashCode;
}