Skip to content

Instantly share code, notes, and snippets.

@M001T
Created December 27, 2021 13:38
Show Gist options
  • Save M001T/4399172c01ecbe7a2ea191b83d5b145b to your computer and use it in GitHub Desktop.
Save M001T/4399172c01ecbe7a2ea191b83d5b145b to your computer and use it in GitHub Desktop.
import 'package:cpf_cnpj_validator/cnpj_validator.dart';
import 'package:cpf_cnpj_validator/cpf_validator.dart';
import 'package:data_connection_checker/data_connection_checker.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:get/get.dart';
import '../../../clean/ui/widgets/molecules/error_alert_body/error_alert_body.dart';
import '../../../clean/ui/widgets/organisms/loader/loader_widget.dart';
import '../../../features/auth/data/models/sms/sms_model.dart';
import '../../../features/auth/data/repositories/verification_repository.dart';
import '../../../features/auth/domain/repositories/i_verification_repository.dart';
import '../../../features/auth/domain/usecases/verify_cpf_usecase.dart';
import '../../../features/auth/domain/usecases/verify_dob_usecase.dart';
import '../../../infrastructure/adapters/network_info_adapter.dart';
import '../../../infrastructure/data/datasources/remote/remote_datasource.dart';
import '../../../infrastructure/helpers/usecase_helper.dart';
import '../../../shared/extensions/string_extensions.dart';
class PasswordRecoveryController extends GetxController {
@override
Future<void> onInit() async {
KeyboardVisibility.onChange.listen(setKeyBoardIsShown);
keyboardIsOpen.value = false;
step.value = 1;
super.onInit();
}
IVerificationRepository repository = VerificationRepository(
remoteDataSource: RemoteDataSource(
api: Modular.get<Dio>(),
networkInfo:
NetworkInfoAdapter(connectionChecker: DataConnectionChecker()),
),
);
var option = "".obs;
var forBusiness = false.obs;
var document = "".obs;
var date = "".obs;
final scroll = ScrollController();
void setKeyBoardIsShown(bool value) => keyboardIsOpen.value = value;
final keyboardIsOpen = false.obs;
final step = 1.obs;
increaseCurrentStep() => step.value++;
String get stepIndicatorMessage {
if (step.value == 1) return 'Faltam só alguns passos!';
if (step.value == 2) return 'Quase lá!';
if (step.value == 3) return 'Confirme seus dados!';
if (step.value == 4) return 'Última etapa!';
return '';
}
String type = '';
String email = '';
String countryCode = '';
String cellNumber = '';
String dateOfBirth = '';
String cpf = '';
String codeNumber = '';
bool userHasRegisteredData = false;
bool get isSubmitAvailable {
bool documentValid = forBusiness.value
? isCnpjValid(document.value)
: isCpfValid(document.value);
bool dateValid = isDateValid(date.value);
return documentValid && dateValid;
}
bool isCpfValid(String cpf) {
try {
return CPFValidator.isValid(cpf);
} on Exception catch (e) {
print("error: $e");
return false;
}
}
bool isCnpjValid(String cnpj) {
try {
return CNPJValidator.isValid(cnpj);
} on Exception catch (e) {
print("error: $e");
return false;
}
}
bool isDateValid(String date) {
if (date.length == 10) {
try {
int checkYear = int.parse(date[6] + date[7] + date[8] + date[9]);
int checkDay = int.parse(date[0] + date[1]);
int checkMonth = int.parse(date[3] + date[4]);
DateTime now = DateTime.now();
if ((checkDay >= 1 && checkDay <= 31) &&
(checkMonth >= 1 && checkMonth <= 12) &&
now.year >= checkYear) {
if (!forBusiness.value)
return checkYear >= 1940 &&
olderThan18(checkDay, checkMonth, checkYear);
return true;
}
} on Exception catch (e) {
print("error: $e");
}
}
return false;
}
bool olderThan18(int day, int month, int year) {
try {
DateTime now = DateTime.now();
int age = now.year - year;
if (month > now.month) age--;
if (month == now.month && day > now.day) age--;
return age >= 12;
} on Exception catch (e) {
print("error: $e");
return false;
}
}
void passwordRecovery(context) async {
bool hasValidCpf = false;
if (isSubmitAvailable) hasValidCpf = await _validateCpf(context);
if (hasValidCpf) _validateDateOfBirthAndLogin(context);
}
Future<bool> _validateCpf(context) async {
final usecase = VerifyCpfUsecase(repository);
showDialog(context: context, builder: (context) => LoaderWidget());
final result = await usecase(
VerifyCpfParams(
type: option.value,
cellNumber: cellNumber,
countryCode: countryCode,
email: email,
codeNumber: codeNumber,
cpf: document.value,
),
);
Modular.to.pop();
return result.fold(
(failure) {
showDialog(
context: context,
builder: (context) => ErrorAlertBody(message: failure.message));
return false;
},
(success) => true,
);
}
void _validateDateOfBirthAndLogin(context) {
UsecaseHelper(
usecase: VerifyDateOfBirthUsecase(repository),
onSuccess: (success) => increaseCurrentStep(),
onFailure: (failure) => showDialog(
context: context,
builder: (context) => ErrorAlertBody(message: failure.message),
),
).extractUsecase(
context: context,
params: VerifyDateOfBirthParams(
type: option.value,
cellNumber: cellNumber,
codeNumber: codeNumber,
countryCode: countryCode,
dateOfBirth: date.value.formatScreenStyledDateToApi,
email: email,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment