diff --git a/packages/auth/lib/data/repositories/auth_repository_imp.dart b/packages/auth/lib/data/repositories/auth_repository_imp.dart index 523efad..6aa2e1a 100644 --- a/packages/auth/lib/data/repositories/auth_repository_imp.dart +++ b/packages/auth/lib/data/repositories/auth_repository_imp.dart @@ -34,13 +34,15 @@ class AuthRepositoryImpl implements AuthRepository { @override Future captcha() async { - final response = await safeCall( - call: - () async => await _httpClient.post( - 'captcha/', - headers: {'Content-Type': 'application/json'}, - fromJson: CaptchaResponseModel.fromJson - ), + final response = await safeCall( + call: () async { + var res = await _httpClient.post( + 'captcha/', + fromJson: CaptchaResponseModel.fromJson, + ); + + return res.data; + }, onSuccess: (response) { iLog(response); }, @@ -48,8 +50,7 @@ class AuthRepositoryImpl implements AuthRepository { throw Exception('Error during captcha : $error'); }, ); - - return response?.data; + return response; } @override diff --git a/packages/auth/lib/presentation/widget/captcha/logic.dart b/packages/auth/lib/presentation/widget/captcha/logic.dart new file mode 100644 index 0000000..de379b6 --- /dev/null +++ b/packages/auth/lib/presentation/widget/captcha/logic.dart @@ -0,0 +1,38 @@ +import 'package:flutter/material.dart'; +import 'package:rasadyar_auth/data/di/auth_di.dart'; +import 'package:rasadyar_auth/data/models/response/captcha/captcha_response_model.dart'; +import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart'; +import 'package:rasadyar_core/core.dart'; + +class CaptchaWidgetLogic extends GetxController + with StateMixin { + TextEditingController textController = TextEditingController(); + GlobalKey formKey = GlobalKey(); + AuthRepositoryImpl authRepository = diAuth.get(); + + @override + void onInit() { + super.onInit(); + + getCaptcha(); + } + + + @override + void onClose() { + super.onClose(); + } + + Future getCaptcha() async { + change(null, status: RxStatus.loading()); + safeCall( + call: () async => await authRepository.captcha(), + onSuccess: (value) { + change(value, status: RxStatus.success()); + }, + onError: (error, stackTrace) { + change(null, status: RxStatus.error(error.toString())); + }, + ); + } +} diff --git a/packages/auth/lib/presentation/widget/captcha/view.dart b/packages/auth/lib/presentation/widget/captcha/view.dart new file mode 100644 index 0000000..4d42576 --- /dev/null +++ b/packages/auth/lib/presentation/widget/captcha/view.dart @@ -0,0 +1,98 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:rasadyar_auth/presentation/widget/clear_button.dart'; +import 'package:rasadyar_core/core.dart'; + +import 'logic.dart'; + +class CaptchaWidget extends GetView { + CaptchaWidget({super.key}); + + final CaptchaWidgetLogic logic = Get.put(CaptchaWidgetLogic()); + + @override + Widget build(BuildContext context) { + return Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Container( + width: 135, + height: 48, + clipBehavior: Clip.antiAliasWithSaveLayer, + decoration: BoxDecoration( + color: AppColor.whiteNormalHover, + border: Border.all(color: Colors.grey.shade300), + borderRadius: BorderRadius.circular(8), + ), + child: Expanded(child:controller.obx((state) { + return Container(color: Colors.blue,); + }, + onLoading: const Center( + child: CircularProgressIndicator( + color: AppColor.blueNormal, + strokeWidth: 2, + ), + ), + )), + ), + const SizedBox(height: 20), + IconButton( + padding: EdgeInsets.zero, + onPressed: controller.getCaptcha, + icon: Icon(CupertinoIcons.refresh, size: 16), + ), + Expanded( + child: Form( + key: controller.formKey, + autovalidateMode: AutovalidateMode.onUserInteraction, + child: TextFormField( + controller: controller.textController, + decoration: InputDecoration( + border: OutlineInputBorder( + borderRadius: BorderRadius.circular(8), + gapPadding: 11, + ), + labelText: 'کد امنیتی', + labelStyle: AppFonts.yekan13, + errorStyle: AppFonts.yekan10.copyWith( + color: AppColor.redNormal, + fontSize: 8, + ), + suffixIconConstraints: BoxConstraints( + maxHeight: 24, + minHeight: 24, + maxWidth: 24, + minWidth: 24, + ), + suffix: + controller.textController.text + .trim() + .isNotEmpty + ? clearButton(() => controller.textController.clear()) + : null, + counterText: '', + ), + keyboardType: TextInputType.numberWithOptions( + decimal: false, + signed: false, + ), + maxLines: 1, + maxLength: 6, + onChanged: (value) {}, + validator: (value) { + if (value == null || value.isEmpty) { + return 'کد امنیتی را وارد کنید'; + } + /*if (value != controller.captchaCode.toString()) { + return '⚠️کد امنیتی وارد شده اشتباه است'; + }*/ + return null; + }, + style: AppFonts.yekan13, + ), + ), + ), + ], + ); + } +} diff --git a/packages/core/lib/infrastructure/remote/dio_remote.dart b/packages/core/lib/infrastructure/remote/dio_remote.dart index 9422cc1..113def3 100644 --- a/packages/core/lib/infrastructure/remote/dio_remote.dart +++ b/packages/core/lib/infrastructure/remote/dio_remote.dart @@ -1,10 +1,9 @@ import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:pretty_dio_logger/pretty_dio_logger.dart'; +import 'package:rasadyar_core/core.dart'; import 'package:rasadyar_core/infrastructure/remote/interfaces/i_form_data.dart'; -import 'dio_form_data.dart'; -import 'dio_response.dart'; import 'interfaces/i_http_client.dart'; class DioRemote implements IHttpClient { @@ -29,7 +28,7 @@ class DioRemote implements IHttpClient { Map? headers, ProgressCallback? onReceiveProgress, }) async { - final response = await _dio.get( + final response = await _dio.get( path, queryParameters: queryParameters, options: Options(headers: headers), @@ -48,7 +47,7 @@ class DioRemote implements IHttpClient { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final response = await _dio.post( + final response = await _dio.post( path, data: data, queryParameters: queryParameters, @@ -57,6 +56,8 @@ class DioRemote implements IHttpClient { onReceiveProgress: onReceiveProgress, ); + eLog(response.toString()); + if (fromJson != null) { final rawData = response.data; final parsedData = @@ -65,7 +66,7 @@ class DioRemote implements IHttpClient { return DioResponse(response); } - return DioResponse(response); + return DioResponse(response ); } @override @@ -77,7 +78,7 @@ class DioRemote implements IHttpClient { ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { - final response = await _dio.put( + final response = await _dio.put( path, data: data, queryParameters: queryParameters, @@ -124,7 +125,7 @@ class DioRemote implements IHttpClient { Map? headers, ProgressCallback? onSendProgress, }) async { - final response = await _dio.post( + final response = await _dio.post( path, data: (formData as DioFormData).raw, options: Options(headers: headers, contentType: 'multipart/form-data'), diff --git a/packages/core/lib/infrastructure/remote/dio_response.dart b/packages/core/lib/infrastructure/remote/dio_response.dart index 97a14c3..30f54eb 100644 --- a/packages/core/lib/infrastructure/remote/dio_response.dart +++ b/packages/core/lib/infrastructure/remote/dio_response.dart @@ -2,7 +2,7 @@ import 'interfaces/i_http_response.dart'; import 'package:dio/dio.dart'; class DioResponse implements IHttpResponse { - final Response _response; + final Response _response; DioResponse(this._response); diff --git a/packages/core/lib/utils/safe_call_utils.dart b/packages/core/lib/utils/safe_call_utils.dart index 116101f..cb9a3e4 100644 --- a/packages/core/lib/utils/safe_call_utils.dart +++ b/packages/core/lib/utils/safe_call_utils.dart @@ -1,4 +1,5 @@ import 'package:flutter/foundation.dart'; +import 'package:rasadyar_core/core.dart'; typedef AsyncCallback = Future Function(); typedef ErrorCallback = void Function(dynamic error, StackTrace? stackTrace); @@ -27,12 +28,14 @@ Future safeCall({ final result = await call(); + iLog(result.toString()); + if (showSuccess) { (onShowSuccessMessage ?? _defaultShowSuccessMessage)(); } onSuccess?.call(result); - return result; // اضافه کردن بازگشت نتیجه + return result; } catch (error, stackTrace) { if (showError) {