mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2024-09-12 06:47:18 +00:00
78 lines
2.1 KiB
Dart
78 lines
2.1 KiB
Dart
import 'dart:async';
|
|
import 'package:cubit_form/cubit_form.dart';
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/backblaze.dart';
|
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
|
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
class BackblazeFormCubit extends FormCubit {
|
|
BackblazeFormCubit(this.serverInstallationCubit) {
|
|
keyId = FieldCubit(
|
|
initalValue: '',
|
|
validations: [
|
|
RequiredStringValidation('validations.required'.tr()),
|
|
],
|
|
);
|
|
|
|
applicationKey = FieldCubit(
|
|
initalValue: '',
|
|
validations: [
|
|
RequiredStringValidation('required'),
|
|
],
|
|
);
|
|
|
|
super.addFields([keyId, applicationKey]);
|
|
}
|
|
|
|
@override
|
|
FutureOr<void> onSubmit() async {
|
|
serverInstallationCubit.setBackblazeKey(
|
|
keyId.state.value,
|
|
applicationKey.state.value,
|
|
);
|
|
}
|
|
|
|
final ServerInstallationCubit serverInstallationCubit;
|
|
|
|
late final FieldCubit<String> keyId;
|
|
late final FieldCubit<String> applicationKey;
|
|
|
|
@override
|
|
FutureOr<bool> asyncValidation() async {
|
|
late APIGenericResult<bool> backblazeResponse;
|
|
final BackblazeApi apiClient = BackblazeApi(isWithToken: false);
|
|
|
|
try {
|
|
final String encodedApiKey = encodedBackblazeKey(
|
|
keyId.state.value,
|
|
applicationKey.state.value,
|
|
);
|
|
backblazeResponse = await apiClient.isApiTokenValid(encodedApiKey);
|
|
} catch (e) {
|
|
addError(e);
|
|
backblazeResponse = APIGenericResult(
|
|
success: false,
|
|
data: false,
|
|
message: e.toString(),
|
|
);
|
|
}
|
|
|
|
if (!backblazeResponse.success) {
|
|
getIt<NavigationService>().showSnackBar(
|
|
'initializing.could_not_connect'.tr(),
|
|
);
|
|
keyId.setError('');
|
|
applicationKey.setError('');
|
|
return false;
|
|
}
|
|
|
|
if (!backblazeResponse.data) {
|
|
keyId.setError('initializing.backblaze_bad_key_error'.tr());
|
|
applicationKey.setError('initializing.backblaze_bad_key_error'.tr());
|
|
}
|
|
|
|
return backblazeResponse.data;
|
|
}
|
|
}
|