From 39ca440da494c0e51f3dff970fce8c72260080ee Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Fri, 29 Sep 2023 08:21:37 +0200 Subject: [PATCH] feat(neon): make result constructors constant Signed-off-by: Nikolas Rimikis --- packages/neon/neon/lib/src/bloc/result.dart | 31 ++++++++----------- .../lib/src/blocs/login_check_account.dart | 2 +- .../src/blocs/login_check_server_status.dart | 2 +- .../neon/neon/lib/src/blocs/login_flow.dart | 2 +- .../neon/lib/src/blocs/unified_search.dart | 10 +++--- .../neon/lib/src/blocs/user_statuses.dart | 4 +-- .../neon/lib/src/utils/request_manager.dart | 2 +- packages/neon/neon/test/result_test.dart | 10 +++--- 8 files changed, 29 insertions(+), 34 deletions(-) diff --git a/packages/neon/neon/lib/src/bloc/result.dart b/packages/neon/neon/lib/src/bloc/result.dart index 66240ccd..96593a3a 100644 --- a/packages/neon/neon/lib/src/bloc/result.dart +++ b/packages/neon/neon/lib/src/bloc/result.dart @@ -9,26 +9,21 @@ class Result { required this.isCached, }); - factory Result.loading() => const Result( - null, - null, - isLoading: true, - isCached: false, - ); + const Result.loading() + : data = null, + error = null, + isLoading = true, + isCached = false; - factory Result.success(final T data) => Result( - data, - null, - isLoading: false, - isCached: false, - ); + const Result.success(this.data) + : error = null, + isLoading = false, + isCached = false; - factory Result.error(final Object error) => Result( - null, - error, - isLoading: false, - isCached: false, - ); + const Result.error(this.error) + : data = null, + isLoading = false, + isCached = false; final T? data; final Object? error; diff --git a/packages/neon/neon/lib/src/blocs/login_check_account.dart b/packages/neon/neon/lib/src/blocs/login_check_account.dart index 1d1b7bf0..5762e709 100644 --- a/packages/neon/neon/lib/src/blocs/login_check_account.dart +++ b/packages/neon/neon/lib/src/blocs/login_check_account.dart @@ -45,7 +45,7 @@ class LoginCheckAccountBloc extends InteractiveBloc @override Future refresh() async { - state.add(Result.loading()); + state.add(const Result.loading()); try { final client = NextcloudClient( diff --git a/packages/neon/neon/lib/src/blocs/login_check_server_status.dart b/packages/neon/neon/lib/src/blocs/login_check_server_status.dart index 73a42f57..5cde597d 100644 --- a/packages/neon/neon/lib/src/blocs/login_check_server_status.dart +++ b/packages/neon/neon/lib/src/blocs/login_check_server_status.dart @@ -38,7 +38,7 @@ class LoginCheckServerStatusBloc extends InteractiveBloc @override Future refresh() async { - state.add(Result.loading()); + state.add(const Result.loading()); try { final client = NextcloudClient( diff --git a/packages/neon/neon/lib/src/blocs/login_flow.dart b/packages/neon/neon/lib/src/blocs/login_flow.dart index 2f640bdd..6c7f0a2c 100644 --- a/packages/neon/neon/lib/src/blocs/login_flow.dart +++ b/packages/neon/neon/lib/src/blocs/login_flow.dart @@ -51,7 +51,7 @@ class LoginFlowBloc extends InteractiveBloc implements LoginFlowBlocEvents, Logi @override Future refresh() async { try { - init.add(Result.loading()); + init.add(const Result.loading()); final initResponse = await _client.core.clientFlowLoginV2.init(); init.add(Result.success(initResponse.body)); diff --git a/packages/neon/neon/lib/src/blocs/unified_search.dart b/packages/neon/neon/lib/src/blocs/unified_search.dart index e99933ab..94305ab6 100644 --- a/packages/neon/neon/lib/src/blocs/unified_search.dart +++ b/packages/neon/neon/lib/src/blocs/unified_search.dart @@ -48,7 +48,7 @@ class UnifiedSearchBloc extends InteractiveBloc implements UnifiedSearchBlocEven @override BehaviorSubject>?>> results = - BehaviorSubject.seeded(Result.success(null)); + BehaviorSubject.seeded(const Result.success(null)); @override void dispose() { @@ -76,13 +76,13 @@ class UnifiedSearchBloc extends InteractiveBloc implements UnifiedSearchBlocEven @override void disable() { enabled.add(false); - results.add(Result.success(null)); + results.add(const Result.success(null)); _term = ''; } Future _search() async { if (_term.isEmpty) { - results.add(Result.success(null)); + results.add(const Result.success(null)); return; } @@ -107,12 +107,12 @@ class UnifiedSearchBloc extends InteractiveBloc implements UnifiedSearchBlocEven final Iterable providers, ) sync* { for (final provider in providers) { - yield MapEntry(provider, Result.loading()); + yield MapEntry(provider, const Result.loading()); } } Future _searchProvider(final CoreUnifiedSearchProvider provider) async { - _updateResults(provider, Result.loading()); + _updateResults(provider, const Result.loading()); try { final response = await _account.client.core.unifiedSearch.search( providerId: provider.id, diff --git a/packages/neon/neon/lib/src/blocs/user_statuses.dart b/packages/neon/neon/lib/src/blocs/user_statuses.dart index eee802de..24a24c29 100644 --- a/packages/neon/neon/lib/src/blocs/user_statuses.dart +++ b/packages/neon/neon/lib/src/blocs/user_statuses.dart @@ -58,7 +58,7 @@ class UserStatusesBloc extends InteractiveBloc implements UserStatusesBlocEvents } try { - _updateStatus(username, Result.loading()); + _updateStatus(username, const Result.loading()); UserStatusPublicInterface? data; @@ -90,7 +90,7 @@ class UserStatusesBloc extends InteractiveBloc implements UserStatusesBlocEvents _updateStatus(username, Result.success(data)); } catch (e, s) { if (e is DynamiteApiException && e.statusCode == 404) { - _updateStatus(username, Result.success(null)); + _updateStatus(username, const Result.success(null)); return; } debugPrint(e.toString()); diff --git a/packages/neon/neon/lib/src/utils/request_manager.dart b/packages/neon/neon/lib/src/utils/request_manager.dart index 0d5b8bc9..8c1dee02 100644 --- a/packages/neon/neon/lib/src/utils/request_manager.dart +++ b/packages/neon/neon/lib/src/utils/request_manager.dart @@ -102,7 +102,7 @@ class RequestManager { final int retries = 0, ]) async { // emit loading state with the current value if present - final value = subject.valueOrNull?.copyWith(isLoading: true) ?? Result.loading(); + final value = subject.valueOrNull?.asLoading() ?? const Result.loading(); subject.add(value); final key = '$clientID-$k'; diff --git a/packages/neon/neon/test/result_test.dart b/packages/neon/neon/test/result_test.dart index 89f0c4e0..bd4171a7 100644 --- a/packages/neon/neon/test/result_test.dart +++ b/packages/neon/neon/test/result_test.dart @@ -31,7 +31,7 @@ void main() { test('Transform to loading', () { const data = 'someData'; - final a = Result.success(data); + const a = Result.success(data); const b = Result( data, null, @@ -46,8 +46,8 @@ void main() { test('data check', () { const data = 'someData'; - final a = Result.loading(); - final b = Result.success(data); + const a = Result.loading(); + const b = Result.success(data); const c = Result( data, null, @@ -68,7 +68,7 @@ void main() { test('error check', () { const error = 'someError'; - final a = Result.error(error); + const a = Result.error(error); expect(a.hasError, true); }); @@ -76,7 +76,7 @@ void main() { test('transform', () { const data = 1; - final a = Result.success(data); + const a = Result.success(data); String transformer(final int data) => data.toString();