Migrate account to use generated copyWith

This commit is contained in:
Ming Ming 2023-06-05 01:13:42 +08:00
parent 3dcc03bba2
commit 6598515c29
6 changed files with 126 additions and 86 deletions

View file

@ -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<String> 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<String> 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<String>? 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<String>(),
id: result["id"],
scheme: result["scheme"],
address: result["address"],
userId: CiString(result["userId"]),
username2: result["username2"],
password: result["password"],
roots: result["roots"].cast<String>(),
);
}
@ -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<String> 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<String> _roots;
final List<String> roots;
/// versioning of this class, use to upgrade old persisted accounts
static const version = 2;

View file

@ -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<String>? 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<String>? ?? 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()}}";
}
}

View file

@ -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) {

View file

@ -265,13 +265,13 @@ class _SignInState extends State<SignIn> {
Future<void> _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<Account>(context, Connect.routeName,

View file

@ -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,
);

View file

@ -314,7 +314,15 @@ Account buildAccount({
String password = "pass",
List<String> 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
///