From 86374c5efbdffbdd68b1254a0475b5d3e7168d29 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Fri, 4 Aug 2023 10:38:39 +0200 Subject: [PATCH] refactor(neon): Adjust --- .../neon/neon/lib/src/blocs/accounts.dart | 4 ++-- packages/neon/neon/lib/src/blocs/apps.dart | 2 +- .../neon/neon/lib/src/blocs/capabilities.dart | 8 +++---- .../lib/src/blocs/push_notifications.dart | 4 ++-- .../neon/neon/lib/src/blocs/user_details.dart | 8 +++---- .../neon/neon/lib/src/models/account.dart | 22 ++++++------------- .../neon/neon/lib/src/models/account.g.dart | 2 -- .../neon/lib/src/widgets/user_avatar.dart | 2 +- 8 files changed, 21 insertions(+), 31 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/accounts.dart b/packages/neon/neon/lib/src/blocs/accounts.dart index d79fae77..747efa84 100644 --- a/packages/neon/neon/lib/src/blocs/accounts.dart +++ b/packages/neon/neon/lib/src/blocs/accounts.dart @@ -257,7 +257,7 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState return _capabilitiesBlocs[account.id] = CapabilitiesBloc( _requestManager, - account.client, + account, ); } @@ -276,7 +276,7 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState return _userDetailsBlocs[account.id] = UserDetailsBloc( _requestManager, - account.client, + account, ); } diff --git a/packages/neon/neon/lib/src/blocs/apps.dart b/packages/neon/neon/lib/src/blocs/apps.dart index 3ea9b0e6..3abf1864 100644 --- a/packages/neon/neon/lib/src/blocs/apps.dart +++ b/packages/neon/neon/lib/src/blocs/apps.dart @@ -196,7 +196,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates @override Future refresh() async { await _requestManager.wrapNextcloud, CoreNavigationApps>( - _account.client.id, + _account.id, 'apps-apps', apps, () async => _account.client.core.getNavigationApps(), diff --git a/packages/neon/neon/lib/src/blocs/capabilities.dart b/packages/neon/neon/lib/src/blocs/capabilities.dart index adc265db..b4d4c902 100644 --- a/packages/neon/neon/lib/src/blocs/capabilities.dart +++ b/packages/neon/neon/lib/src/blocs/capabilities.dart @@ -21,13 +21,13 @@ abstract class CapabilitiesBlocStates { class CapabilitiesBloc extends InteractiveBloc implements CapabilitiesBlocEvents, CapabilitiesBlocStates { CapabilitiesBloc( this._requestManager, - this._client, + this._account, ) { unawaited(refresh()); } final RequestManager _requestManager; - final NextcloudClient _client; + final Account _account; @override void dispose() { @@ -41,10 +41,10 @@ class CapabilitiesBloc extends InteractiveBloc implements CapabilitiesBlocEvents @override Future refresh() async { await _requestManager.wrapNextcloud( - _client.id, + _account.id, 'capabilities', capabilities, - () async => _client.core.getCapabilities(), + () async => _account.client.core.getCapabilities(), (final response) => response.ocs.data, ); } diff --git a/packages/neon/neon/lib/src/blocs/push_notifications.dart b/packages/neon/neon/lib/src/blocs/push_notifications.dart index 15e3d6c0..6bde33b2 100644 --- a/packages/neon/neon/lib/src/blocs/push_notifications.dart +++ b/packages/neon/neon/lib/src/blocs/push_notifications.dart @@ -126,7 +126,7 @@ class PushNotificationsBloc extends Bloc implements PushNotificationsBlocEvents, for (final account in accounts) { try { await account.client.notifications.removeDevice(); - await UnifiedPush.unregister(account.client.id); + await UnifiedPush.unregister(account.id); await _storage.remove(_keyLastEndpoint(account)); } catch (e) { debugPrint('Failed to unregister device: $e'); @@ -137,7 +137,7 @@ class PushNotificationsBloc extends Bloc implements PushNotificationsBlocEvents, Future _registerUnifiedPushInstances(final List accounts) async { // Notifications will only work on accounts with app password for (final account in accounts.where((final a) => a.password != null)) { - await UnifiedPush.registerApp(account.client.id); + await UnifiedPush.registerApp(account.id); } } } diff --git a/packages/neon/neon/lib/src/blocs/user_details.dart b/packages/neon/neon/lib/src/blocs/user_details.dart index 94d31ab7..d7a67c99 100644 --- a/packages/neon/neon/lib/src/blocs/user_details.dart +++ b/packages/neon/neon/lib/src/blocs/user_details.dart @@ -18,13 +18,13 @@ abstract class UserDetailsBlocStates { class UserDetailsBloc extends InteractiveBloc implements UserDetailsBlocEvents, UserDetailsBlocStates { UserDetailsBloc( this._requestManager, - this._client, + this._account, ) { unawaited(refresh()); } final RequestManager _requestManager; - final NextcloudClient _client; + final Account _account; @override void dispose() { @@ -39,10 +39,10 @@ class UserDetailsBloc extends InteractiveBloc implements UserDetailsBlocEvents, @override Future refresh() async { await _requestManager.wrapNextcloud( - _client.id, + _account.id, 'user-details', userDetails, - () async => _client.provisioningApi.getCurrentUser(), + () async => _account.client.provisioningApi.getCurrentUser(), (final response) => response.ocs.data, ); } diff --git a/packages/neon/neon/lib/src/models/account.dart b/packages/neon/neon/lib/src/models/account.dart index 7d413503..2d56003b 100644 --- a/packages/neon/neon/lib/src/models/account.dart +++ b/packages/neon/neon/lib/src/models/account.dart @@ -30,7 +30,7 @@ class Account implements Credentials { required this.username, this.password, this.userAgent, - }) : _client = NextcloudClient( + }) : client = NextcloudClient( serverURL, loginName: username, password: password, @@ -60,20 +60,10 @@ class Account implements Credentials { @override int get hashCode => serverURL.hashCode + username.hashCode; - String get id => client.id; + final NextcloudClient client; - String get humanReadableID => client.humanReadableID; - - final NextcloudClient _client; - - NextcloudClient get client => _client; -} - -Map _idCache = {}; - -extension NextcloudClientHelpers on NextcloudClient { String get id { - final key = '$username@$baseURL'; + final key = '$username@$serverURL'; if (_idCache[key] != null) { return _idCache[key]!; } @@ -81,12 +71,14 @@ extension NextcloudClientHelpers on NextcloudClient { } String get humanReadableID { - final uri = Uri.parse(baseURL); + final uri = Uri.parse(serverURL); // Maybe also show path if it is not '/' ? - return '${username!}@${uri.port != 443 ? '${uri.host}:${uri.port}' : uri.host}'; + return '$username@${uri.port != 443 ? '${uri.host}:${uri.port}' : uri.host}'; } } +Map _idCache = {}; + extension AccountFind on Iterable { Account? tryFind(final String? accountID) => firstWhereOrNull((final account) => account.id == accountID); Account find(final String accountID) => firstWhere((final account) => account.id == accountID); diff --git a/packages/neon/neon/lib/src/models/account.g.dart b/packages/neon/neon/lib/src/models/account.g.dart index fa9b929e..ea607165 100644 --- a/packages/neon/neon/lib/src/models/account.g.dart +++ b/packages/neon/neon/lib/src/models/account.g.dart @@ -8,7 +8,6 @@ part of 'account.dart'; Account _$AccountFromJson(Map json) => Account( serverURL: json['serverURL'] as String, - loginName: json['loginName'] as String, username: json['username'] as String, password: json['password'] as String?, userAgent: json['userAgent'] as String?, @@ -16,7 +15,6 @@ Account _$AccountFromJson(Map json) => Account( Map _$AccountToJson(Account instance) => { 'serverURL': instance.serverURL, - 'loginName': instance.loginName, 'username': instance.username, 'password': instance.password, 'userAgent': instance.userAgent, diff --git a/packages/neon/neon/lib/src/widgets/user_avatar.dart b/packages/neon/neon/lib/src/widgets/user_avatar.dart index 69964f19..4ecc7de4 100644 --- a/packages/neon/neon/lib/src/widgets/user_avatar.dart +++ b/packages/neon/neon/lib/src/widgets/user_avatar.dart @@ -25,7 +25,7 @@ class NeonUserAvatar extends StatefulWidget { this.backgroundColor, this.foregroundColor, super.key, - }) : username = username ?? account.client.username!; + }) : username = username ?? account.username; final Account account; final String username;