From c3c93a82f5af47e7b95a58f62cf027cf70578ba9 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Thu, 5 Dec 2024 21:38:14 +0800 Subject: [PATCH] [compass_app] Standardize on `Result` factories rather than constructors (#2538) From my review of the recipes PR in https://github.com/flutter/website/pull/11444#pullrequestreview-2480423811. --- .../repositories/auth/auth_repository_dev.dart | 4 ++-- .../booking/booking_repository_local.dart | 4 ++-- .../itinerary_config_repository_memory.dart | 2 +- .../app/lib/data/services/api/api_client.dart | 18 +++++++++--------- .../lib/data/services/api/auth_api_client.dart | 2 +- .../services/shared_preferences_service.dart | 2 +- .../booking/booking_create_use_case.dart | 2 +- .../booking/booking_share_use_case.dart | 2 +- .../booking/view_models/booking_viewmodel.dart | 2 +- compass_app/app/lib/utils/result.dart | 12 ++++++------ .../fakes/services/fake_auth_api_client.dart | 2 +- 11 files changed, 26 insertions(+), 26 deletions(-) diff --git a/compass_app/app/lib/data/repositories/auth/auth_repository_dev.dart b/compass_app/app/lib/data/repositories/auth/auth_repository_dev.dart index 5c56b9621..aba56512f 100644 --- a/compass_app/app/lib/data/repositories/auth/auth_repository_dev.dart +++ b/compass_app/app/lib/data/repositories/auth/auth_repository_dev.dart @@ -16,12 +16,12 @@ class AuthRepositoryDev extends AuthRepository { required String email, required String password, }) async { - return Result.ok(null); + return const Result.ok(null); } /// Logout is always successful in dev scenarios @override Future> logout() async { - return Result.ok(null); + return const Result.ok(null); } } diff --git a/compass_app/app/lib/data/repositories/booking/booking_repository_local.dart b/compass_app/app/lib/data/repositories/booking/booking_repository_local.dart index 4a340d7c2..a41660257 100644 --- a/compass_app/app/lib/data/repositories/booking/booking_repository_local.dart +++ b/compass_app/app/lib/data/repositories/booking/booking_repository_local.dart @@ -31,7 +31,7 @@ class BookingRepositoryLocal implements BookingRepository { // Bookings created come without id, we need to assign one final bookingWithId = booking.copyWith(id: _sequentialId++); _bookings.add(bookingWithId); - return Result.ok(null); + return const Result.ok(null); } @override @@ -92,6 +92,6 @@ class BookingRepositoryLocal implements BookingRepository { @override Future> delete(int id) async { _bookings.removeWhere((booking) => booking.id == id); - return Result.ok(null); + return const Result.ok(null); } } diff --git a/compass_app/app/lib/data/repositories/itinerary_config/itinerary_config_repository_memory.dart b/compass_app/app/lib/data/repositories/itinerary_config/itinerary_config_repository_memory.dart index 5bd9cc0d5..c819c7fef 100644 --- a/compass_app/app/lib/data/repositories/itinerary_config/itinerary_config_repository_memory.dart +++ b/compass_app/app/lib/data/repositories/itinerary_config/itinerary_config_repository_memory.dart @@ -22,6 +22,6 @@ class ItineraryConfigRepositoryMemory implements ItineraryConfigRepository { ItineraryConfig itineraryConfig, ) async { _itineraryConfig = itineraryConfig; - return Result.ok(true); + return const Result.ok(true); } } diff --git a/compass_app/app/lib/data/services/api/api_client.dart b/compass_app/app/lib/data/services/api/api_client.dart index 58f2d7a0a..5068c443b 100644 --- a/compass_app/app/lib/data/services/api/api_client.dart +++ b/compass_app/app/lib/data/services/api/api_client.dart @@ -53,7 +53,7 @@ class ApiClient { return Result.ok( json.map((element) => Continent.fromJson(element)).toList()); } else { - return Result.error(const HttpException("Invalid response")); + return const Result.error(HttpException("Invalid response")); } } on Exception catch (error) { return Result.error(error); @@ -74,7 +74,7 @@ class ApiClient { return Result.ok( json.map((element) => Destination.fromJson(element)).toList()); } else { - return Result.error(const HttpException("Invalid response")); + return const Result.error(HttpException("Invalid response")); } } on Exception catch (error) { return Result.error(error); @@ -97,7 +97,7 @@ class ApiClient { json.map((element) => Activity.fromJson(element)).toList(); return Result.ok(activities); } else { - return Result.error(const HttpException("Invalid response")); + return const Result.error(HttpException("Invalid response")); } } on Exception catch (error) { return Result.error(error); @@ -119,7 +119,7 @@ class ApiClient { json.map((element) => BookingApiModel.fromJson(element)).toList(); return Result.ok(bookings); } else { - return Result.error(const HttpException("Invalid response")); + return const Result.error(HttpException("Invalid response")); } } on Exception catch (error) { return Result.error(error); @@ -139,7 +139,7 @@ class ApiClient { final booking = BookingApiModel.fromJson(jsonDecode(stringData)); return Result.ok(booking); } else { - return Result.error(const HttpException("Invalid response")); + return const Result.error(HttpException("Invalid response")); } } on Exception catch (error) { return Result.error(error); @@ -160,7 +160,7 @@ class ApiClient { final booking = BookingApiModel.fromJson(jsonDecode(stringData)); return Result.ok(booking); } else { - return Result.error(const HttpException("Invalid response")); + return const Result.error(HttpException("Invalid response")); } } on Exception catch (error) { return Result.error(error); @@ -180,7 +180,7 @@ class ApiClient { final user = UserApiModel.fromJson(jsonDecode(stringData)); return Result.ok(user); } else { - return Result.error(const HttpException("Invalid response")); + return const Result.error(HttpException("Invalid response")); } } on Exception catch (error) { return Result.error(error); @@ -197,9 +197,9 @@ class ApiClient { final response = await request.close(); // Response 204 "No Content", delete was successful if (response.statusCode == 204) { - return Result.ok(null); + return const Result.ok(null); } else { - return Result.error(const HttpException("Invalid response")); + return const Result.error(HttpException("Invalid response")); } } on Exception catch (error) { return Result.error(error); diff --git a/compass_app/app/lib/data/services/api/auth_api_client.dart b/compass_app/app/lib/data/services/api/auth_api_client.dart index 7c2191b46..89e309674 100644 --- a/compass_app/app/lib/data/services/api/auth_api_client.dart +++ b/compass_app/app/lib/data/services/api/auth_api_client.dart @@ -32,7 +32,7 @@ class AuthApiClient { final stringData = await response.transform(utf8.decoder).join(); return Result.ok(LoginResponse.fromJson(jsonDecode(stringData))); } else { - return Result.error(const HttpException("Login error")); + return const Result.error(HttpException("Login error")); } } on Exception catch (error) { return Result.error(error); diff --git a/compass_app/app/lib/data/services/shared_preferences_service.dart b/compass_app/app/lib/data/services/shared_preferences_service.dart index 05a684601..24a67a9b5 100644 --- a/compass_app/app/lib/data/services/shared_preferences_service.dart +++ b/compass_app/app/lib/data/services/shared_preferences_service.dart @@ -32,7 +32,7 @@ class SharedPreferencesService { _log.finer('Replaced token'); await sharedPreferences.setString(_tokenKey, token); } - return Result.ok(null); + return const Result.ok(null); } on Exception catch (e) { _log.warning('Failed to set token', e); return Result.error(e); diff --git a/compass_app/app/lib/domain/use_cases/booking/booking_create_use_case.dart b/compass_app/app/lib/domain/use_cases/booking/booking_create_use_case.dart index eff951159..4b750f0a3 100644 --- a/compass_app/app/lib/domain/use_cases/booking/booking_create_use_case.dart +++ b/compass_app/app/lib/domain/use_cases/booking/booking_create_use_case.dart @@ -98,7 +98,7 @@ class BookingCreateUseCase { case Ok>(): final destination = result.value .firstWhere((destination) => destination.ref == destinationRef); - return Ok(destination); + return Result.ok(destination); case Error>(): return Result.error(result.error); } diff --git a/compass_app/app/lib/domain/use_cases/booking/booking_share_use_case.dart b/compass_app/app/lib/domain/use_cases/booking/booking_share_use_case.dart index cff2f852b..b7d66f0c7 100644 --- a/compass_app/app/lib/domain/use_cases/booking/booking_share_use_case.dart +++ b/compass_app/app/lib/domain/use_cases/booking/booking_share_use_case.dart @@ -37,7 +37,7 @@ class BookingShareUseCase { try { await _share(text); _log.fine('Shared booking'); - return Result.ok(null); + return const Result.ok(null); } on Exception catch (error) { _log.severe('Failed to share booking', error); return Result.error(error); diff --git a/compass_app/app/lib/ui/booking/view_models/booking_viewmodel.dart b/compass_app/app/lib/ui/booking/view_models/booking_viewmodel.dart index c2e44ead6..725d478eb 100644 --- a/compass_app/app/lib/ui/booking/view_models/booking_viewmodel.dart +++ b/compass_app/app/lib/ui/booking/view_models/booking_viewmodel.dart @@ -61,7 +61,7 @@ class BookingViewModel extends ChangeNotifier { _log.fine('Created Booking'); _booking = result.value; notifyListeners(); - return Result.ok(null); + return const Result.ok(null); case Error(): _log.warning('Booking error: ${result.error}'); notifyListeners(); diff --git a/compass_app/app/lib/utils/result.dart b/compass_app/app/lib/utils/result.dart index 5370700b0..8512963e4 100644 --- a/compass_app/app/lib/utils/result.dart +++ b/compass_app/app/lib/utils/result.dart @@ -18,11 +18,11 @@ sealed class Result { const Result(); - /// Creates an instance of Result containing a value - factory Result.ok(T value) => Ok(value); + /// Creates a successful [Result], completed with the specified [value]. + const factory Result.ok(T value) = Ok._; - /// Create an instance of Result containing an error - factory Result.error(Exception error) => Error(error); + /// Creates an error [Result], completed with the specified [error]. + const factory Result.error(Exception error) = Error._; /// Convenience method to cast to Ok Ok get asOk => this as Ok; @@ -33,7 +33,7 @@ sealed class Result { /// Subclass of Result for values final class Ok extends Result { - const Ok(this.value); + const Ok._(this.value); /// Returned value in result final T value; @@ -44,7 +44,7 @@ final class Ok extends Result { /// Subclass of Result for errors final class Error extends Result { - const Error(this.error); + const Error._(this.error); /// Returned error in result final Exception error; diff --git a/compass_app/app/testing/fakes/services/fake_auth_api_client.dart b/compass_app/app/testing/fakes/services/fake_auth_api_client.dart index 714e2e7c3..af4ea340c 100644 --- a/compass_app/app/testing/fakes/services/fake_auth_api_client.dart +++ b/compass_app/app/testing/fakes/services/fake_auth_api_client.dart @@ -11,7 +11,7 @@ class FakeAuthApiClient implements AuthApiClient { @override Future> login(LoginRequest loginRequest) async { if (loginRequest.email == 'EMAIL' && loginRequest.password == 'PASSWORD') { - return Result.ok(const LoginResponse(token: 'TOKEN', userId: '123')); + return const Result.ok(LoginResponse(token: 'TOKEN', userId: '123')); } return Result.error(Exception('ERROR!')); }