-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_validation_cubit.dart
53 lines (41 loc) · 1.77 KB
/
document_validation_cubit.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import 'dart:async';
import 'package:autogram_sign/autogram_sign.dart';
import 'package:flutter_bloc/flutter_bloc.dart' show Cubit;
import 'package:injectable/injectable.dart';
import 'package:logging/logging.dart';
import '../ui/fragment/document_validation_fragment.dart';
import 'document_validation_state.dart';
import 'preview_document_cubit.dart';
export 'document_validation_state.dart';
/// Cubit for the [DocumentValidationFragment] with [validateDocument] function.
///
/// See also:
/// - [PreviewDocumentCubit]
@injectable
class DocumentValidationCubit extends Cubit<DocumentValidationState> {
static final _log = Logger((DocumentValidationCubit).toString());
final IAutogramService _service;
DocumentValidationCubit({
required IAutogramService service,
}) : _service = service,
super(const DocumentValidationInitialState());
Future<void> validateDocument(String documentId) async {
_log.info("Requesting to validate Document Id: '$documentId'.");
emit(const DocumentValidationLoadingState());
try {
final response = await _service.getDocumentValidation(documentId);
_log.info(
"Got Document validation: (signatureForm: ${response.signatureForm?.value}, signatures: ${response.signatures?.map((e) => e.validationResult.value)}).");
emit(DocumentValidationSuccessState(response));
} catch (error, stackTrace) {
if (error is ServiceException &&
error.errorCode == "DOCUMENT_NOT_SIGNED") {
_log.info("Cannot validate unsigned Document.");
emit(const DocumentValidationNotSignedState());
} else {
_log.severe("Error validating Document.", error, stackTrace);
emit(DocumentValidationErrorState(error));
}
}
}
}