diff --git a/app/lib/account.dart b/app/lib/account.dart index dc830f2f..ce84423b 100644 --- a/app/lib/account.dart +++ b/app/lib/account.dart @@ -1,6 +1,7 @@ import 'dart:math'; import 'package:clock/clock.dart'; +import 'package:copy_with/copy_with.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; @@ -14,44 +15,25 @@ import 'package:to_string/to_string.dart'; part 'account.g.dart'; /// Details of a remote Nextcloud server account +@genCopyWith @npLog @toString class Account with EquatableMixin { - Account( - this.id, - this.scheme, - String address, - this.userId, - this.username2, - this.password, - List roots, - ) : address = address.trimRightAny("/"), - _roots = roots.map((e) => e.trimRightAny("/")).toList() { + Account({ + required this.id, + required this.scheme, + required String address, + required this.userId, + required this.username2, + required this.password, + required List roots, + }) : address = address.trimRightAny("/"), + roots = roots.map((e) => e.trimRightAny("/")).toList() { if (scheme != "http" && scheme != "https") { throw const FormatException("scheme is neither http or https"); } } - Account copyWith({ - String? id, - String? scheme, - String? address, - CiString? userId, - String? username2, - String? password, - List? roots, - }) { - return Account( - id ?? this.id, - scheme ?? this.scheme, - address ?? this.address, - userId ?? this.userId, - username2 ?? this.username2, - password ?? this.password, - roots ?? List.of(_roots), - ); - } - static String newId() { final timestamp = clock.now().millisecondsSinceEpoch; final random = Random().nextInt(0xFFFFFF); @@ -75,13 +57,13 @@ class Account with EquatableMixin { } } return Account( - result["id"], - result["scheme"], - result["address"], - CiString(result["userId"]), - result["username2"], - result["password"], - result["roots"].cast(), + id: result["id"], + scheme: result["scheme"], + address: result["address"], + userId: CiString(result["userId"]), + username2: result["username2"], + password: result["password"], + roots: result["roots"].cast(), ); } @@ -93,13 +75,11 @@ class Account with EquatableMixin { "userId": userId.toString(), "username2": username2, "password": password, - "roots": _roots, + "roots": roots, }; @override - get props => [id, scheme, address, userId, username2, password, _roots]; - - List get roots => _roots; + get props => [id, scheme, address, userId, username2, password, roots]; final String id; final String scheme; @@ -114,8 +94,9 @@ class Account with EquatableMixin { @Format(r"${$?.isNotEmpty ? (kDebugMode ? $? : '***') : null}") final String password; + @deepCopy @Format(r"${$?.toReadableString()}") - final List _roots; + final List roots; /// versioning of this class, use to upgrade old persisted accounts static const version = 2; diff --git a/app/lib/account.g.dart b/app/lib/account.g.dart index 2851a133..cc8842a0 100644 --- a/app/lib/account.g.dart +++ b/app/lib/account.g.dart @@ -2,6 +2,57 @@ part of 'account.dart'; +// ************************************************************************** +// CopyWithLintRuleGenerator +// ************************************************************************** + +// ignore_for_file: library_private_types_in_public_api, duplicate_ignore + +// ************************************************************************** +// CopyWithGenerator +// ************************************************************************** + +abstract class $AccountCopyWithWorker { + Account call( + {String? id, + String? scheme, + String? address, + CiString? userId, + String? username2, + String? password, + List? roots}); +} + +class _$AccountCopyWithWorkerImpl implements $AccountCopyWithWorker { + _$AccountCopyWithWorkerImpl(this.that); + + @override + Account call( + {dynamic id, + dynamic scheme, + dynamic address, + dynamic userId, + dynamic username2, + dynamic password, + dynamic roots}) { + return Account( + id: id as String? ?? that.id, + scheme: scheme as String? ?? that.scheme, + address: address as String? ?? that.address, + userId: userId as CiString? ?? that.userId, + username2: username2 as String? ?? that.username2, + password: password as String? ?? that.password, + roots: roots as List? ?? List.of(that.roots)); + } + + final Account that; +} + +extension $AccountCopyWith on Account { + $AccountCopyWithWorker get copyWith => _$copyWith; + $AccountCopyWithWorker get _$copyWith => _$AccountCopyWithWorkerImpl(this); +} + // ************************************************************************** // NpLogGenerator // ************************************************************************** @@ -27,6 +78,6 @@ extension _$AccountUpgraderV1NpLog on AccountUpgraderV1 { extension _$AccountToString on Account { String _$toString() { // ignore: unnecessary_string_interpolations - return "Account {id: $id, scheme: $scheme, address: ${kDebugMode ? address : '***'}, userId: ${kDebugMode ? userId : '***'}, username2: ${kDebugMode ? username2 : '***'}, password: ${password.isNotEmpty ? (kDebugMode ? password : '***') : null}, _roots: ${_roots.toReadableString()}}"; + return "Account {id: $id, scheme: $scheme, address: ${kDebugMode ? address : '***'}, userId: ${kDebugMode ? userId : '***'}, username2: ${kDebugMode ? username2 : '***'}, password: ${password.isNotEmpty ? (kDebugMode ? password : '***') : null}, roots: ${roots.toReadableString()}}"; } } diff --git a/app/lib/bloc/app_password_exchange.dart b/app/lib/bloc/app_password_exchange.dart index f42fbdd6..8acc7cf4 100644 --- a/app/lib/bloc/app_password_exchange.dart +++ b/app/lib/bloc/app_password_exchange.dart @@ -225,14 +225,14 @@ class AppPasswordExchangeBloc try { final response = ev.appPasswordResponse; final account = Account( - Account.newId(), - response.server.scheme, - response.server.authority + + id: Account.newId(), + scheme: response.server.scheme, + address: response.server.authority + (response.server.path.isEmpty ? "" : response.server.path), - response.loginName.toCi(), - response.loginName, - response.appPassword, - [""], + userId: response.loginName.toCi(), + username2: response.loginName, + password: response.appPassword, + roots: [""], ); emit(AppPasswordExchangeBlocResult(account)); } catch (e, stacktrace) { diff --git a/app/lib/legacy/sign_in.dart b/app/lib/legacy/sign_in.dart index b6a181f9..4b3ba6df 100644 --- a/app/lib/legacy/sign_in.dart +++ b/app/lib/legacy/sign_in.dart @@ -265,13 +265,13 @@ class _SignInState extends State { Future _connect() async { _formKey.currentState!.save(); Account? account = Account( - Account.newId(), - _formValue.scheme, - _formValue.address, - _formValue.username.toCi(), - _formValue.username, - _formValue.password, - [""], + id: Account.newId(), + scheme: _formValue.scheme, + address: _formValue.address, + userId: _formValue.username.toCi(), + username2: _formValue.username, + password: _formValue.password, + roots: [""], ); _log.info("[_connect] Try connecting with account: $account"); account = await Navigator.pushNamed(context, Connect.routeName, diff --git a/app/test/account_test.dart b/app/test/account_test.dart index 13bc257e..2485a668 100644 --- a/app/test/account_test.dart +++ b/app/test/account_test.dart @@ -36,13 +36,13 @@ void _fromJson() { upgraderV1: null, ), Account( - "123456", - "https", - "example.com", - "00000000-1111-aaaa-bbbb-223344ccddee".toCi(), - "admin", - "123456", - ["test1", "test2"], + id: "123456", + scheme: "https", + address: "example.com", + userId: "00000000-1111-aaaa-bbbb-223344ccddee".toCi(), + username2: "admin", + password: "123456", + roots: ["test1", "test2"], ), ); } @@ -113,22 +113,22 @@ void _upgraderV1Ldap() { void _constructTrimAddress() { expect( Account( - "123456", - "https", - "example.com/", - "00000000-1111-aaaa-bbbb-223344ccddee".toCi(), - "admin", - "123456", - ["test1", "test2"], + id: "123456", + scheme: "https", + address: "example.com/", + userId: "00000000-1111-aaaa-bbbb-223344ccddee".toCi(), + username2: "admin", + password: "123456", + roots: ["test1", "test2"], ), Account( - "123456", - "https", - "example.com", - "00000000-1111-aaaa-bbbb-223344ccddee".toCi(), - "admin", - "123456", - ["test1", "test2"], + id: "123456", + scheme: "https", + address: "example.com", + userId: "00000000-1111-aaaa-bbbb-223344ccddee".toCi(), + username2: "admin", + password: "123456", + roots: ["test1", "test2"], ), ); } @@ -139,13 +139,13 @@ void _constructTrimAddress() { void _constructInvalidScheme() { expect( () => Account( - "123456", - "ssh", - "example.com/", - "00000000-1111-aaaa-bbbb-223344ccddee".toCi(), - "admin", - "123456", - ["test1", "test2"], + id: "123456", + scheme: "ssh", + address: "example.com/", + userId: "00000000-1111-aaaa-bbbb-223344ccddee".toCi(), + username2: "admin", + password: "123456", + roots: ["test1", "test2"], ), throwsFormatException, ); diff --git a/app/test/test_util.dart b/app/test/test_util.dart index 8ae033f2..e6c364f9 100644 --- a/app/test/test_util.dart +++ b/app/test/test_util.dart @@ -314,7 +314,15 @@ Account buildAccount({ String password = "pass", List roots = const [""], }) => - Account(id, scheme, address, userId.toCi(), username2, password, roots); + Account( + id: id, + scheme: scheme, + address: address, + userId: userId.toCi(), + username2: username2, + password: password, + roots: roots, + ); /// Build a mock [File] pointing to a album JSON file ///