-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_document_screen.dart
174 lines (158 loc) · 4.99 KB
/
sign_document_screen.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import 'dart:developer' as developer;
import 'package:autogram_sign/autogram_sign.dart'
show SignDocumentResponseBody, SignDocumentResponseBodyMimeType;
import 'package:eidmsdk/types.dart' show Certificate;
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'package:widgetbook_annotation/widgetbook_annotation.dart' as widgetbook;
import '../../bloc/sign_document_cubit.dart';
import '../../data/document_signing_type.dart';
import '../../data/signature_type.dart';
import '../../strings_context.dart';
import '../app_theme.dart';
import '../widgets/error_content.dart';
import '../widgets/loading_content.dart';
import '../widgets/retry_view.dart';
import 'present_signed_document_screen.dart';
/// Screen for signing the document by calling eID mSDK.
/// Uses [SignDocumentCubit].
///
/// Navigates next to [PresentSignedDocumentScreen] on success.
class SignDocumentScreen extends StatelessWidget {
final String documentId;
final Certificate certificate;
final SignatureType signatureType;
final DocumentSigningType signingType;
const SignDocumentScreen({
super.key,
required this.documentId,
required this.certificate,
required this.signatureType,
required this.signingType,
});
@override
Widget build(BuildContext context) {
final body = BlocProvider<SignDocumentCubit>(
create: (context) {
return GetIt.instance.get<SignDocumentCubit>(
param1: documentId,
param2: certificate,
)..signDocument(signatureType);
},
child: BlocConsumer<SignDocumentCubit, SignDocumentState>(
listener: (context, state) {
if (state is SignDocumentSuccessState) {
_onSuccess(context, state);
}
},
builder: (context, state) {
return _Body(
state: state,
onRetryRequested: () {
context.read<SignDocumentCubit>().signDocument(signatureType);
},
);
},
),
);
return Scaffold(
appBar: AppBar(
title: Text(context.strings.signDocumentTitle),
),
body: SafeArea(
child: body,
),
);
}
void _onSuccess(BuildContext context, SignDocumentSuccessState state) {
final screen = PresentSignedDocumentScreen(
signedDocument: state.signedDocument,
signingType: signingType,
);
final route = MaterialPageRoute(builder: (_) => screen);
Navigator.of(context).pushAndRemoveUntil(
route,
(route) {
// Remove until MainScreen
return route.settings.name == '/';
},
);
}
}
/// [SignDocumentScreen] body.
class _Body extends StatelessWidget {
final SignDocumentState state;
final VoidCallback? onRetryRequested;
const _Body({required this.state, this.onRetryRequested});
@override
Widget build(BuildContext context) {
return Padding(
padding: kScreenMargin,
child: _getChild(context, state),
// TODO Add primary and secondary buttons
);
}
Widget _getChild(BuildContext context, SignDocumentState state) {
return switch (state) {
SignDocumentInitialState _ => const LoadingContent(),
SignDocumentLoadingState _ => const LoadingContent(),
SignDocumentCanceledState _ => RetryView(
headlineText: context.strings.signDocumentCanceledHeading,
// TODO Use selectSigningCertificateCanceledBody
// + add Primary Button: Skúsiť znovu
// + add secondary button: Zrušiť podpisovanie
onRetryRequested: onRetryRequested,
),
SignDocumentErrorState state => ErrorContent(
title: context.strings.signDocumentErrorHeading,
error: state.error,
),
SignDocumentSuccessState _ => const LoadingContent(), // see listener
};
}
}
@widgetbook.UseCase(
path: '[Screens]',
name: 'loading',
type: SignDocumentScreen,
)
Widget previewLoadingSignDocumentScreen(BuildContext context) {
return const _Body(
state: SignDocumentLoadingState(),
);
}
@widgetbook.UseCase(
path: '[Screens]',
name: 'error',
type: SignDocumentScreen,
)
Widget previewErrorSignDocumentScreen(BuildContext context) {
return _Body(
state: const SignDocumentErrorState("Error message!"),
onRetryRequested: () {
developer.log('onRetryRequested');
},
);
}
@widgetbook.UseCase(
path: '[Screens]',
name: 'success',
type: SignDocumentScreen,
)
Widget previewSuccessSignDocumentScreen(BuildContext context) {
return _Body(
state: const SignDocumentSuccessState(
SignDocumentResponseBody(
filename: "document.pdf",
mimeType: SignDocumentResponseBodyMimeType.applicationPdfBase64,
content: "",
issuedBy: "",
signedBy: "",
),
),
onRetryRequested: () {
developer.log('onRetryRequested');
},
);
}