feat : refresh login
test: core
This commit is contained in:
@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
|
||||
|
||||
extension ColorUtils on Color {
|
||||
Color _darken([double amount = 0.1]) {
|
||||
assert(amount >= 0 && amount <= 1, 'مقدار تیرگی باید بین 0 و 1 باشد');
|
||||
assert(amount >= 0 && amount <= 1, 'Amount must be between 0 and 1');
|
||||
final hslColor = HSLColor.fromColor(this);
|
||||
final newLightness = (hslColor.lightness - amount).clamp(0.0, 1.0);
|
||||
final hslDarkerColor = hslColor.withLightness(newLightness);
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
typedef AsyncCallback<T> = Future<T> Function();
|
||||
import '../core.dart';
|
||||
|
||||
typedef AppAsyncCallback<T> = Future<T> Function();
|
||||
typedef ErrorCallback = void Function(dynamic error, StackTrace? stackTrace);
|
||||
typedef VoidCallback = void Function();
|
||||
|
||||
// تعریف دقیق تابع safeCall
|
||||
Future<void> safeCall<T>({
|
||||
required AsyncCallback<T> call,
|
||||
/// this is global safe call function
|
||||
/// A utility function to safely cal l an asynchronous function with error
|
||||
/// handling and optional loading, success, and error messages.
|
||||
///
|
||||
Future<void> gSafeCall<T>({
|
||||
required AppAsyncCallback<T> call,
|
||||
Function(T result)? onSuccess,
|
||||
ErrorCallback? onError,
|
||||
VoidCallback? onComplete,
|
||||
@@ -17,6 +20,8 @@ Future<void> safeCall<T>({
|
||||
bool showSuccess = false,
|
||||
bool showToast = false,
|
||||
bool showSnackBar = false,
|
||||
bool retryOnAuthError = false,
|
||||
Function()? onTokenRefresh,
|
||||
Function()? onShowLoading,
|
||||
Function()? onHideLoading,
|
||||
Function()? onShowSuccessMessage,
|
||||
@@ -34,18 +39,34 @@ Future<void> safeCall<T>({
|
||||
}
|
||||
|
||||
onSuccess?.call(result);
|
||||
|
||||
|
||||
} catch (error, stackTrace) {
|
||||
if (showError) {
|
||||
(onShowErrorMessage ?? _defaultShowErrorMessage)();
|
||||
if (retryOnAuthError && isTokenExpiredError(error)) {
|
||||
try {
|
||||
await onTokenRefresh?.call();
|
||||
final retryResult = await call();
|
||||
if (showSuccess) {
|
||||
(onShowSuccessMessage ?? _defaultShowSuccessMessage)();
|
||||
}
|
||||
onSuccess?.call(retryResult);
|
||||
return;
|
||||
} catch (retryError, retryStackTrace) {
|
||||
if (showError) {
|
||||
(onShowErrorMessage ?? _defaultShowErrorMessage)();
|
||||
}
|
||||
onError?.call(retryError, retryStackTrace);
|
||||
if (kDebugMode) {
|
||||
print('safeCall retry error: $retryError\n$retryStackTrace');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (showError) {
|
||||
(onShowErrorMessage ?? _defaultShowErrorMessage)();
|
||||
}
|
||||
onError?.call(error, stackTrace);
|
||||
if (kDebugMode) {
|
||||
print('safeCall error: $error\n$stackTrace');
|
||||
}
|
||||
}
|
||||
|
||||
onError?.call(error, stackTrace);
|
||||
if (kDebugMode) {
|
||||
print('safeCall error: $error\n$stackTrace');
|
||||
}
|
||||
|
||||
} finally {
|
||||
if (showLoading) {
|
||||
(onHideLoading ?? _defaultHideLoading)();
|
||||
@@ -69,4 +90,8 @@ void _defaultShowSuccessMessage() {
|
||||
|
||||
void _defaultShowErrorMessage() {
|
||||
// پیادهسازی پیشفرض
|
||||
}
|
||||
}
|
||||
|
||||
bool isTokenExpiredError(dynamic error) {
|
||||
return error is DioException && error.response?.statusCode == 401;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user