Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed dcm warnings. #108

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions lib/common/interceptors/auth_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,14 @@ final class AuthInterceptor extends QueuedInterceptorsWrapper {

final retryResult = await _retryRequest(err.requestOptions..addAuthHeader(tokens.accessToken));

switch (retryResult) {
case ResultOk(data: final response):
return handler.resolve(response);
case ResultFailed(:final failure):
return super.onError(failure.original, handler);
}
return _handleRetryResult(retryResult, handler);

case ResultFailed(:final failure):
await _onLogout();
final originalException = failure.original;
final dioException = originalException is DioException ? originalException : null;
return dioException != null ? handler.reject(dioException) : null;

return dioException == null ? null : handler.reject(dioException);
}
}

Expand All @@ -82,13 +79,26 @@ final class AuthInterceptor extends QueuedInterceptorsWrapper {
queryParameters: requestOptions.queryParameters,
cancelToken: requestOptions.cancelToken,
options: requestOptions.toOptions(),
onReceiveProgress: requestOptions.onReceiveProgress,
onSendProgress: requestOptions.onSendProgress,
onReceiveProgress: requestOptions.onReceiveProgress,
);

return Result.ok(response);
} on DioException catch (e, s) {
return Result.failed(Failure(original: e, trace: s));
} on DioException catch (error, s) {
return Result.failed(Failure(original: error, trace: s));
}
}

void _handleRetryResult(
// ignore: avoid-dynamic
Result<Response<dynamic>, Failure<DioException>> retryResult,
ErrorInterceptorHandler handler,
) {
switch (retryResult) {
case ResultOk(data: final response):
return handler.resolve(response);
case ResultFailed(:final failure):
return super.onError(failure.original, handler);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import 'package:flutter_template/core/architecture/domain/entity/request_operation.dart';
import 'package:flutter_template/persistence/storage/tokens_storage/data/auth_tokens_storage_dto.dart';

/// Interface for the token management service
/// Interface for the token management service.
abstract interface class ITokenOperationsService {
/// refresh Tokens
/// Refresh Tokens.
RequestOperation<AuthTokensStorageDto> refreshTokens(String refreshToken);

/// Refresh token
/// Refresh token.
Future<void> saveTokens(AuthTokensStorageDto tokens);

/// Get Access Token from storage
/// Get Access Token from storage.
Future<String?> getAccessToken();

/// Get Refresh Token from storage
/// Get Refresh Token from storage.
Future<String?> getRefreshToken();
}
8 changes: 4 additions & 4 deletions lib/core/failures/api_failure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ base class ApiFailure extends Failure<DioException> {
/// Original error status code from [DioException].
final int? responseBodyCode;

/// Message
/// Message.
final String? message;

/// {@macro api_failure.class}
Expand All @@ -24,13 +24,13 @@ base class ApiFailure extends Failure<DioException> {
this.message,
});

// TODO(anyone): add getters for special status codes
// TODO(anyone): Add getters for special status codes.
/// Sample: ExpiredToken - refresh token has expired
// bool get isExpiredRefreshToken => statusCode == 102;
/// bool get isExpiredRefreshToken => statusCode == 102
}

/// {@template timeout_failure.class}
/// TimeoutException
/// TimeoutException.
/// {@endtemplate}
final class TimeoutFailure extends ApiFailure {
/// {@macro timeout_failure.class}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import 'package:flutter_template/persistence/storage/tokens_storage/data/auth_to
/// Interface IRefreshTokensRepository.
/// {@endtemplate}
abstract interface class IRefreshTokensRepository {
/// Refresh Tokens
/// Refresh Tokens.
RequestOperation<AuthTokensStorageDto> refreshTokens(String refreshToken);

/// Initialize repository
/// Initialize repository.
RequestOperation<void> saveTokens(AuthTokensStorageDto tokens);

/// Get Access Token from storage
/// Get Access Token from storage.
RequestOperation<String?> getAccessToken();

/// Get Refresh Token from storage
/// Get Refresh Token from storage.
RequestOperation<String?> getRefreshToken();
}
10 changes: 5 additions & 5 deletions lib/persistence/storage/tokens_storage/i_tokens_storage.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'package:flutter_template/persistence/storage/tokens_storage/data/auth_tokens_storage_dto.dart';

/// Storage for tokens
/// Storage for tokens.
abstract interface class ITokensStorage {
/// Get accessToken
/// Get accessToken.
Future<String?> get accessToken;

/// Get refreshToken
/// Get refreshToken.
Future<String?> get refreshToken;

/// Save tokens
/// Save tokens.
Future<void> saveTokens(AuthTokensStorageDto tokens);

/// Clear storage
/// Clear storage.
Future<void> clear();
}
16 changes: 8 additions & 8 deletions lib/persistence/storage/tokens_storage/tokens_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import 'package:flutter_template/persistence/storage/tokens_storage/data/auth_to
import 'package:flutter_template/persistence/storage/tokens_storage/i_tokens_storage.dart';

/// {@template tokens_storage.class}
/// Implementation [ITokensStorage]
/// Implementation [ITokensStorage].
/// {@endtemplate}
final class TokensStorage implements ITokensStorage {
final FlutterSecureStorage _secureStorage;

/// {@macro tokens_storage.class}
const TokensStorage(this._secureStorage);

@override
Future<String?> get accessToken => _secureStorage.read(key: TokensStorageKeys.accessToken.keyName);

@override
Future<String?> get refreshToken => _secureStorage.read(key: TokensStorageKeys.refreshToken.keyName);

/// {@macro tokens_storage.class}
const TokensStorage(this._secureStorage);

@override
Future<void> saveTokens(AuthTokensStorageDto tokens) async {
await _secureStorage.write(key: TokensStorageKeys.accessToken.keyName, value: tokens.accessToken);
Expand All @@ -31,15 +31,15 @@ final class TokensStorage implements ITokensStorage {
}
}

/// Keys for [TokensStorage]
/// Keys for [TokensStorage].
enum TokensStorageKeys {
/// @nodoc
/// Access token.
accessToken('app_access_token'),

/// @nodoc
/// Refresh token.
refreshToken('app_refresh_token');

/// Key name
/// Key name.
final String keyName;

const TokensStorageKeys(this.keyName);
Expand Down
3 changes: 1 addition & 2 deletions lib/runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ Future<void> run(Environment env) async {
}

// TODO(init-project): Initialize Crashlytics.
// _setupCrashlytics();

// TODO(init-project): change as needed or remove.
// TODO(init-project): Change as needed or remove.
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

await _runApp(env);
Expand Down
40 changes: 24 additions & 16 deletions lib/util/extensions/closures.dart
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
// ignore_for_file: prefer-match-file-name

/// Let extension
/// The closure that is passed as a parameter to extension methods.
typedef Closure<R, T> = R Function(T it);

/// Let extension.
extension LetX<T extends Object> on T {
/// Executes a closure, where the argument is the value itself
R let<R>(R Function(T it) closure) {
/// Executes a closure, where the argument is the value itself.
R let<R>(Closure<R, T> closure) {
return closure(this);
}

/// Executes a closure, where the argument is the value itself
R? run<R>(R? Function(T it) closure) {
/// Executes a closure, where the argument is the value itself.
R? run<R>(Closure<R?, T> closure) {
return closure(this);
}
}

/// Let nullable extension
/// Let nullable extension.
extension LetNullableX<T extends Object> on T? {
/// Executes a closure on a nullable value, where the argument is the value itself
R? let<R>(R Function(T it) closure) {
return this != null ? closure(this!) : null;
/// Executes a closure on a nullable value, where the argument is the value itself.
R? let<R>(Closure<R, T> closure) {
final object = this;

return object == null ? null : closure(object);
}

/// Executes a closure on a nullable value, where the argument is the value itself
R? run<R>(R? Function(T it) closure) {
return this != null ? closure(this!) : null;
/// Executes a closure on a nullable value, where the argument is the value itself.
R? run<R>(Closure<R?, T> closure) {
final object = this;

return object == null ? null : closure(object);
}
}

/// Also extension
/// Also extension.
extension AlsoX<T extends Object?> on T {
/// Additionally executes a closure and returns the value itself
T also(void Function(T it) closure) {
/// Additionally executes a closure and returns the value itself.
T also(Closure<void, T> closure) {
closure(this);

return this;
}
}

/// Repeat extension
/// Repeat extension.
extension RepeatX<T extends Object?> on T {
/// Returns a Iterable<T> containing this object repeated [times] times.
Iterable<T> repeat(int times) {
Expand Down
12 changes: 6 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,10 @@ packages:
dependency: transitive
description:
name: firebase_core_web
sha256: d585bdf3c656c3f7821ba1bd44da5f13365d22fcecaf5eb75c4295246aaa83c0
sha256: "5377eaac3b9fe8aaf22638d87f92b62784f23572e132dfc029195e84d6cb37de"
url: "https://pub.dev"
source: hosted
version: "2.10.0"
version: "2.12.0"
firebase_crashlytics:
dependency: "direct main"
description:
Expand Down Expand Up @@ -998,10 +998,10 @@ packages:
dependency: transitive
description:
name: sqflite_common
sha256: "28d8c66baee4968519fb8bd6cdbedad982d6e53359091f0b74544a9f32ec72d5"
sha256: "3da423ce7baf868be70e2c0976c28a1bb2f73644268b7ffa7d2e08eab71f16a4"
url: "https://pub.dev"
source: hosted
version: "2.5.3"
version: "2.5.4"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -1198,10 +1198,10 @@ packages:
dependency: transitive
description:
name: win32
sha256: "464f5674532865248444b4c3daca12bd9bf2d7c47f759ce2617986e7229494a8"
sha256: "8cb58b45c47dcb42ab3651533626161d6b67a2921917d8d429791f76972b3480"
url: "https://pub.dev"
source: hosted
version: "5.2.0"
version: "5.3.0"
xdg_directories:
dependency: transitive
description:
Expand Down
Loading