Files

194 lines
6.3 KiB
Dart
Raw Permalink Normal View History

2025-05-10 12:16:27 +03:30
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
2025-07-28 15:57:30 +03:30
import 'package:rasadyar_inspection/data/model/request/login_request/login_request_model.dart';
import 'package:rasadyar_inspection/data/model/response/auth/auth_response_model.dart';
import 'package:rasadyar_inspection/data/repositories/auth/auth_repository_imp.dart';
import 'package:rasadyar_inspection/data/utils/dio_exception_handeler.dart';
import 'package:rasadyar_inspection/injection/inspection_di.dart';
import 'package:rasadyar_inspection/presentation/routes/app_routes.dart';
import 'package:rasadyar_inspection/presentation/widget/captcha/logic.dart';
2025-05-10 12:16:27 +03:30
enum AuthType { useAndPass, otp }
enum AuthStatus { init }
enum OtpStatus { init, sent, verified, reSend }
2025-08-02 12:19:25 +03:30
class AuthLogic extends GetxController with GetTickerProviderStateMixin {
2025-05-17 15:24:06 +03:30
GlobalKey<FormState> formKey = GlobalKey<FormState>();
2025-08-02 12:19:25 +03:30
late AnimationController _textAnimationController;
late Animation<double> textAnimation;
RxBool showCard = false.obs;
RxBool rememberMe = false.obs;
2025-05-10 12:16:27 +03:30
Rx<GlobalKey<FormState>> formKeyOtp = GlobalKey<FormState>().obs;
Rx<GlobalKey<FormState>> formKeySentOtp = GlobalKey<FormState>().obs;
2025-06-02 10:30:19 +03:30
Rx<TextEditingController> usernameController = TextEditingController().obs;
2025-05-10 12:16:27 +03:30
Rx<TextEditingController> passwordController = TextEditingController().obs;
Rx<TextEditingController> phoneOtpNumberController = TextEditingController().obs;
2025-05-10 12:16:27 +03:30
Rx<TextEditingController> otpCodeController = TextEditingController().obs;
2025-05-14 15:01:03 +03:30
2025-05-17 15:24:06 +03:30
var captchaController = Get.find<CaptchaWidgetLogic>();
2025-05-10 12:16:27 +03:30
RxnString phoneNumber = RxnString(null);
2025-05-17 15:24:06 +03:30
RxBool isLoading = false.obs;
2025-06-03 22:52:21 +03:30
RxBool isDisabled = true.obs;
2025-05-18 09:32:43 +03:30
TokenStorageService tokenStorageService = Get.find<TokenStorageService>();
2025-05-17 15:24:06 +03:30
2025-05-10 12:16:27 +03:30
Rx<AuthType> authType = AuthType.useAndPass.obs;
Rx<AuthStatus> authStatus = AuthStatus.init.obs;
Rx<OtpStatus> otpStatus = OtpStatus.init.obs;
RxInt secondsRemaining = 120.obs;
Timer? _timer;
2025-07-28 15:57:30 +03:30
AuthRepositoryImpl authRepository = diInspection.get<AuthRepositoryImpl>();
2025-05-14 15:01:03 +03:30
final Module _module = Get.arguments;
2025-08-02 12:19:25 +03:30
@override
void onInit() {
super.onInit();
_textAnimationController =
AnimationController(vsync: this, duration: const Duration(milliseconds: 1200))
..repeat(reverse: true, count: 2).whenComplete(() {
showCard.value = true;
});
textAnimation = CurvedAnimation(parent: _textAnimationController, curve: Curves.easeInOut);
}
@override
void onClose() {
_timer?.cancel();
super.onClose();
}
2025-05-10 12:16:27 +03:30
void startTimer() {
_timer?.cancel();
secondsRemaining.value = 120;
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
if (secondsRemaining.value > 0) {
secondsRemaining.value--;
} else {
timer.cancel();
}
});
}
void stopTimer() {
_timer?.cancel();
}
String get timeFormatted {
final minutes = secondsRemaining.value ~/ 60;
final seconds = secondsRemaining.value % 60;
return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
}
2025-05-17 15:24:06 +03:30
bool _isFormValid() {
final isCaptchaValid = captchaController.formKey.currentState?.validate() ?? false;
2025-05-17 15:24:06 +03:30
final isFormValid = formKey.currentState?.validate() ?? false;
return isCaptchaValid && isFormValid;
}
2025-05-14 15:01:03 +03:30
2025-05-17 15:24:06 +03:30
LoginRequestModel _buildLoginRequest() {
2025-06-02 10:30:19 +03:30
final phone = usernameController.value.text;
2025-05-17 15:24:06 +03:30
final pass = passwordController.value.text;
final code = captchaController.textController.value.text;
final key = captchaController.captchaKey.value;
return LoginRequestModel.createWithCaptcha(
username: phone,
password: pass,
captchaCode: code,
captchaKey: key!,
);
}
2025-07-28 15:57:30 +03:30
Future<void> submitLoginForm() async {
2025-05-17 15:24:06 +03:30
if (!_isFormValid()) return;
2025-07-28 15:57:30 +03:30
2025-05-17 15:24:06 +03:30
final loginRequestModel = _buildLoginRequest();
isLoading.value = true;
await safeCall<AuthResponseModel?>(
2025-07-28 15:57:30 +03:30
call: () async => authRepository.login(authRequest: loginRequestModel.toJson()),
2025-05-17 17:07:44 +03:30
onSuccess: (result) async {
await tokenStorageService.saveModule(_module);
2025-09-06 14:50:02 +03:30
/*await tokenStorageService.saveRefreshToken(result?.refresh ?? '');
await tokenStorageService.saveAccessToken(result?.access ?? '');
if (rememberMe.value) {
await tokenStorageService.saveUserPass(
UserLocalModel(
username: usernameController.value.text,
password: passwordController.value.text,
module: _module,
),
);
2025-09-06 14:50:02 +03:30
}*/
2025-07-28 15:57:30 +03:30
Get.offAllNamed(InspectionRoutes.init);
2025-05-17 15:24:06 +03:30
},
onError: (error, stackTrace) {
if (error is DioException) {
2025-07-28 15:57:30 +03:30
diInspection.get<DioErrorHandler>().handle(error);
2025-05-17 15:24:06 +03:30
}
captchaController.getCaptcha();
},
);
isLoading.value = false;
2025-07-28 15:57:30 +03:30
}
2025-06-03 22:52:21 +03:30
Future<void> submitLoginForm2() async {
if (!_isFormValid()) return;
//AuthRepositoryImpl authTmp = diAuth.get<AuthRepositoryImpl>(instanceName: 'newUrl');
2025-06-03 22:52:21 +03:30
isLoading.value = true;
2025-07-28 15:57:30 +03:30
/* await safeCall<UserProfileModel?>(
2025-06-03 23:57:25 +03:30
call: () => authTmp.login(
2025-06-03 22:52:21 +03:30
authRequest: {
2025-07-24 16:57:34 +03:30
"username": usernameController.value.text,
2025-06-03 22:52:21 +03:30
"password": passwordController.value.text,
},
),
onSuccess: (result) async {
await tokenStorageService.saveModule(_module);
await tokenStorageService.saveAccessToken(result?.accessToken ?? '');
await tokenStorageService.saveRefreshToken(result?.accessToken ?? '');
},
onError: (error, stackTrace) {
if (error is DioException) {
// diAuth.get<DioErrorHandler>().handle(error);
2025-06-03 22:52:21 +03:30
}
captchaController.getCaptcha();
},
);*/
2025-06-03 22:52:21 +03:30
isLoading.value = false;
}
Future<void> getUserInfo(String value) async {
isLoading.value = true;
/*await safeCall<UserInfoModel?>(
2025-06-03 22:52:21 +03:30
call: () async => await authRepository.getUserInfo(value),
onSuccess: (result) async {
if (result != null) {
//await newSetupAuthDI(result.backend ?? '');
2025-06-03 22:52:21 +03:30
await tokenStorageService.saveApiKey(result.apiKey ?? '');
await tokenStorageService.saveBaseUrl(result.backend ?? '');
}
},
onError: (error, stackTrace) {
if (error is DioException) {
// diAuth.get<DioErrorHandler>().handle(error);
2025-06-03 22:52:21 +03:30
}
captchaController.getCaptcha();
},
);*/
2025-06-03 22:52:21 +03:30
isLoading.value = false;
2025-05-17 15:24:06 +03:30
}
2025-05-10 12:16:27 +03:30
}