nc-photos/lib/account.dart

142 lines
3.7 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
2021-11-12 22:13:02 +01:00
import 'package:nc_photos/ci_string.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/string_extension.dart';
2021-08-06 19:11:00 +02:00
import 'package:nc_photos/type.dart';
2021-04-10 06:28:12 +02:00
/// Details of a remote Nextcloud server account
class Account with EquatableMixin {
Account(
this.scheme,
String address,
this.username,
this.password,
List<String> roots,
2021-09-15 08:58:06 +02:00
) : address = address.trimRightAny("/"),
2021-04-10 06:28:12 +02:00
_roots = roots.map((e) => e.trimRightAny("/")).toList() {
if (scheme != "http" && scheme != "https") {
2021-09-15 08:58:06 +02:00
throw const FormatException("scheme is neither http or https");
2021-04-10 06:28:12 +02:00
}
}
Account copyWith({
2021-07-23 22:05:57 +02:00
String? scheme,
String? address,
2021-11-12 22:13:02 +01:00
CiString? username,
2021-07-23 22:05:57 +02:00
String? password,
List<String>? roots,
2021-04-10 06:28:12 +02:00
}) {
return Account(
scheme ?? this.scheme,
address ?? this.address,
username ?? this.username,
password ?? this.password,
roots ?? _roots,
);
}
@override
toString() {
return "$runtimeType {"
"scheme: '$scheme', "
2021-09-19 13:02:26 +02:00
"address: '${kDebugMode ? address : "***"}', "
"username: '${kDebugMode ? username : "***"}', "
2021-07-23 22:05:57 +02:00
"password: '${password.isNotEmpty == true ? (kDebugMode ? password : '***') : null}', "
2021-04-10 06:28:12 +02:00
"roots: List {'${roots.join('\', \'')}'}, "
"}";
}
2021-08-06 19:11:00 +02:00
Account.fromJson(JsonObj json)
2021-04-10 06:28:12 +02:00
: scheme = json["scheme"],
address = json["address"],
2021-11-12 22:13:02 +01:00
username = CiString(json["username"]),
2021-04-10 06:28:12 +02:00
password = json["password"],
_roots = json["roots"].cast<String>();
2021-08-06 19:11:00 +02:00
JsonObj toJson() => {
2021-04-10 06:28:12 +02:00
"scheme": scheme,
"address": address,
2021-11-12 22:13:02 +01:00
"username": username.toString(),
2021-04-10 06:28:12 +02:00
"password": password,
"roots": _roots,
};
@override
List<Object> get props => [scheme, address, username, password, _roots];
List<String> get roots => _roots;
final String scheme;
final String address;
2021-11-12 22:13:02 +01:00
final CiString username;
2021-04-10 06:28:12 +02:00
final String password;
final List<String> _roots;
}
2021-10-19 21:49:41 +02:00
class AccountSettings with EquatableMixin {
const AccountSettings({
this.isEnableFaceRecognitionApp = true,
this.shareFolder = "",
});
2021-10-19 21:49:41 +02:00
factory AccountSettings.fromJson(JsonObj json) {
return AccountSettings(
isEnableFaceRecognitionApp: json["isEnableFaceRecognitionApp"] ?? true,
shareFolder: json["shareFolder"] ?? "",
);
2021-10-19 21:49:41 +02:00
}
JsonObj toJson() => {
"isEnableFaceRecognitionApp": isEnableFaceRecognitionApp,
"shareFolder": shareFolder,
};
2021-10-19 21:49:41 +02:00
@override
toString() {
return "$runtimeType {"
"isEnableFaceRecognitionApp: $isEnableFaceRecognitionApp, "
"shareFolder: $shareFolder, "
2021-10-19 21:49:41 +02:00
"}";
}
AccountSettings copyWith({
bool? isEnableFaceRecognitionApp,
String? shareFolder,
}) {
return AccountSettings(
isEnableFaceRecognitionApp:
isEnableFaceRecognitionApp ?? this.isEnableFaceRecognitionApp,
shareFolder: shareFolder ?? this.shareFolder,
);
2021-10-19 21:49:41 +02:00
}
@override
get props => [
isEnableFaceRecognitionApp,
shareFolder,
];
final bool isEnableFaceRecognitionApp;
/// Path of the share folder
///
/// Share folder is where files shared with you are initially placed. Must
/// match the value of share_folder in config.php
final String shareFolder;
2021-10-19 21:49:41 +02:00
}
2021-04-10 06:28:12 +02:00
extension AccountExtension on Account {
String get url => "$scheme://$address";
/// Compare the server identity of two Accounts
///
/// Return true if two Accounts point to the same user on server. Be careful
/// that this does NOT mean that the two Accounts are identical (e.g., they
/// can have difference password)
bool compareServerIdentity(Account other) {
return scheme == other.scheme &&
address == other.address &&
username == other.username;
}
2021-04-10 06:28:12 +02:00
}