2020-12-30 14:13:25 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:cubit_form/cubit_form.dart';
|
2022-02-16 07:28:29 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2022-05-17 13:31:34 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
2022-05-02 11:56:46 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/forms/validations/validations.dart';
|
2020-12-30 14:13:25 +00:00
|
|
|
|
2022-07-12 12:54:16 +00:00
|
|
|
class ProviderFormCubit extends FormCubit {
|
|
|
|
ProviderFormCubit(this.serverInstallationCubit) {
|
2022-10-26 16:07:35 +00:00
|
|
|
//final int tokenLength =
|
|
|
|
// serverInstallationCubit.serverProviderApiTokenValidation().length;
|
2020-12-30 14:13:25 +00:00
|
|
|
apiKey = FieldCubit(
|
|
|
|
initalValue: '',
|
|
|
|
validations: [
|
2021-06-20 21:08:52 +00:00
|
|
|
RequiredStringValidation('validations.required'.tr()),
|
2022-10-26 16:07:35 +00:00
|
|
|
//LengthStringNotEqualValidation(tokenLength),
|
2020-12-30 14:13:25 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2021-03-23 19:21:42 +00:00
|
|
|
super.addFields([apiKey]);
|
2020-12-30 14:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
FutureOr<void> onSubmit() async {
|
2022-10-11 20:11:13 +00:00
|
|
|
serverInstallationCubit.setServerProviderKey(apiKey.state.value);
|
2020-12-30 14:13:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 17:43:25 +00:00
|
|
|
final ServerInstallationCubit serverInstallationCubit;
|
2021-03-15 15:39:44 +00:00
|
|
|
late final FieldCubit<String> apiKey;
|
2020-12-30 14:13:25 +00:00
|
|
|
|
2021-01-06 17:35:57 +00:00
|
|
|
@override
|
|
|
|
FutureOr<bool> asyncValidation() async {
|
2021-03-15 15:39:44 +00:00
|
|
|
late bool isKeyValid;
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2021-01-13 16:45:46 +00:00
|
|
|
try {
|
2022-07-12 12:54:16 +00:00
|
|
|
isKeyValid = await serverInstallationCubit
|
2022-07-14 13:34:08 +00:00
|
|
|
.isServerProviderApiTokenValid(apiKey.state.value);
|
2021-01-13 16:45:46 +00:00
|
|
|
} catch (e) {
|
|
|
|
addError(e);
|
2022-06-15 01:55:45 +00:00
|
|
|
isKeyValid = false;
|
2021-01-13 16:45:46 +00:00
|
|
|
}
|
2021-01-06 17:35:57 +00:00
|
|
|
|
|
|
|
if (!isKeyValid) {
|
2022-10-11 20:11:13 +00:00
|
|
|
apiKey.setError('initializing.provider_bad_key_error'.tr());
|
2021-01-06 17:35:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-15 01:55:45 +00:00
|
|
|
|
2021-01-06 17:35:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-12-30 14:13:25 +00:00
|
|
|
}
|