2022-05-24 16:09:47 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
2022-05-26 01:02:06 +00:00
|
|
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
2022-05-24 16:09:47 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/recovery_token_status.dart';
|
|
|
|
|
|
|
|
part 'recovery_key_state.dart';
|
|
|
|
|
|
|
|
class RecoveryKeyCubit
|
|
|
|
extends ServerInstallationDependendCubit<RecoveryKeyState> {
|
|
|
|
RecoveryKeyCubit(ServerInstallationCubit serverInstallationCubit)
|
2022-05-24 18:55:39 +00:00
|
|
|
: super(serverInstallationCubit, const RecoveryKeyState.initial());
|
2022-05-24 16:09:47 +00:00
|
|
|
|
|
|
|
final api = ServerApi();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void load() async {
|
|
|
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
|
|
|
final status = await _getRecoveryKeyStatus();
|
|
|
|
if (status == null) {
|
|
|
|
emit(state.copyWith(loadingStatus: LoadingStatus.error));
|
|
|
|
} else {
|
2022-05-26 01:02:06 +00:00
|
|
|
emit(state.copyWith(
|
|
|
|
status: status, loadingStatus: LoadingStatus.success));
|
2022-05-24 16:09:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
emit(state.copyWith(loadingStatus: LoadingStatus.uninitialized));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<RecoveryKeyStatus?> _getRecoveryKeyStatus() async {
|
2022-05-24 17:45:13 +00:00
|
|
|
final ApiResponse<RecoveryKeyStatus?> response =
|
2022-05-24 16:09:47 +00:00
|
|
|
await api.getRecoveryTokenStatus();
|
|
|
|
if (response.isSuccess) {
|
|
|
|
return response.data;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> refresh() async {
|
|
|
|
emit(state.copyWith(loadingStatus: LoadingStatus.refreshing));
|
|
|
|
final status = await _getRecoveryKeyStatus();
|
|
|
|
if (status == null) {
|
|
|
|
emit(state.copyWith(loadingStatus: LoadingStatus.error));
|
|
|
|
} else {
|
2022-05-26 01:02:06 +00:00
|
|
|
emit(
|
|
|
|
state.copyWith(status: status, loadingStatus: LoadingStatus.success));
|
2022-05-24 16:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> generateRecoveryKey({
|
|
|
|
DateTime? expirationDate,
|
|
|
|
int? numberOfUses,
|
|
|
|
}) async {
|
|
|
|
final ApiResponse<String> response =
|
|
|
|
await api.generateRecoveryToken(expirationDate, numberOfUses);
|
|
|
|
if (response.isSuccess) {
|
|
|
|
refresh();
|
|
|
|
return response.data;
|
|
|
|
} else {
|
|
|
|
throw GenerationError(response.errorMessage ?? 'Unknown error');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() {
|
|
|
|
emit(state.copyWith(loadingStatus: LoadingStatus.uninitialized));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GenerationError extends Error {
|
|
|
|
final String message;
|
|
|
|
|
|
|
|
GenerationError(this.message);
|
|
|
|
}
|