-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresent_signed_document_screen.dart
365 lines (327 loc) · 10.6 KB
/
present_signed_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import 'dart:developer' as developer;
import 'dart:io' show File, OSError, PathAccessException;
import 'package:autogram_sign/autogram_sign.dart' show SignDocumentResponseBody;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:share_plus/share_plus.dart';
import 'package:widgetbook/widgetbook.dart';
import 'package:widgetbook_annotation/widgetbook_annotation.dart' as widgetbook;
import '../../bloc/present_signed_document_cubit.dart';
import '../../data/document_signing_type.dart';
import '../../di.dart';
import '../../file_system_entity_extensions.dart';
import '../../strings_context.dart';
import '../../util/errors.dart';
import '../app_theme.dart';
import '../widgets/loading_content.dart';
import '../widgets/markdown_text.dart';
import '../widgets/result_view.dart';
/// Screen for presenting signed document.
///
/// When [signingType] is [DocumentSigningType.local], then document is saved
/// into this device and also "Share" button is visible.
///
/// Uses [PresentSignedDocumentCubit].
class PresentSignedDocumentScreen extends StatelessWidget {
final SignDocumentResponseBody signedDocument;
final DocumentSigningType signingType;
const PresentSignedDocumentScreen({
super.key,
required this.signedDocument,
required this.signingType,
});
@override
Widget build(BuildContext context) {
return BlocProvider<PresentSignedDocumentCubit>(
create: (context) {
final cubit = getIt.get<PresentSignedDocumentCubit>(
param1: signedDocument,
param2: signingType,
);
if (signingType == DocumentSigningType.local) {
cubit.saveDocument();
}
return cubit;
},
child: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
),
body: Builder(
builder: (context) {
// Need outer Context to access Cubit
return BlocConsumer<PresentSignedDocumentCubit,
PresentSignedDocumentState>(
listener: (context, state) {
if (state is PresentSignedDocumentErrorState) {
final error = state.error;
final message = context.strings
.saveSignedDocumentErrorMessage(getErrorMessage(error));
_showError(context, message);
}
},
builder: (context, state) {
return _Body(
state: state,
signingType: signingType,
onShareFileRequested: () => _handleShareFile(context),
onCloseRequested: () => _handleClose(context),
);
},
);
},
),
),
);
}
void _showError(BuildContext context, String message) {
final snackBar = SnackBar(
duration: const Duration(seconds: 10),
content: Text(message),
action: SnackBarAction(
onPressed: () {
// Hides automatically
},
label: context.strings.buttonOKLabel,
),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
/// Handles share file request.
Future<void> _handleShareFile(BuildContext context) async {
final cubit = context.read<PresentSignedDocumentCubit>();
final strings = context.strings;
try {
final file = await cubit.getShareableFile();
await Share.shareXFiles(
[XFile(file.path)],
text: strings.shareSignedDocumentText,
// It would be better to have something meaningful for email clients,
// however this is also used in Google Drive as target file name
subject: file.basename,
);
} catch (error) {
if (context.mounted) {
final message =
strings.shareSignedDocumentErrorMessage(getErrorMessage(error));
_showError(context, message);
}
}
}
/// Handles close request.
Future<void> _handleClose(BuildContext context) {
return Navigator.of(context).maybePop();
}
}
/// [PresentSignedDocumentScreen] body.
class _Body extends StatelessWidget {
final PresentSignedDocumentState state;
final DocumentSigningType signingType;
final VoidCallback? onShareFileRequested;
final VoidCallback? onCloseRequested;
const _Body({
required this.state,
required this.signingType,
this.onShareFileRequested,
this.onCloseRequested,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: kScreenMargin,
child: _getChild(context),
);
}
Widget _getChild(BuildContext context) {
final sharingEnabled = (signingType == DocumentSigningType.local);
final onShareFileRequested =
sharingEnabled ? this.onShareFileRequested : null;
return switch (state) {
PresentSignedDocumentInitialState _ => const LoadingContent(),
PresentSignedDocumentLoadingState _ => const LoadingContent(),
PresentSignedDocumentErrorState _ => _SuccessContent(
file: null,
onShareFileRequested: onShareFileRequested,
onCloseRequested: onCloseRequested,
),
PresentSignedLocalDocumentSuccessState state => _SuccessContent(
file: state.file,
onShareFileRequested: onShareFileRequested,
onCloseRequested: onCloseRequested,
),
PresentSignedRemoteDocumentSuccessState() => _SuccessContent(
file: null,
onShareFileRequested: null,
onCloseRequested: onCloseRequested,
),
};
}
}
/// This presents main content when [File] also cannot be saved for some reason.
/// However, still can request to share it and navigate.
class _SuccessContent extends StatelessWidget {
final File? file;
final VoidCallback? onShareFileRequested;
final VoidCallback? onCloseRequested;
const _SuccessContent({
required this.file,
required this.onShareFileRequested,
required this.onCloseRequested,
});
@override
Widget build(BuildContext context) {
final strings = context.strings;
final file = this.file;
Widget body = const SizedBox(height: 58);
if (file != null) {
final directory = _getParentDirectoryName(file);
final name = file.basename;
final text = strings.saveSignedDocumentSuccessMessage(directory, name);
body = MarkdownText(
text,
onLinkTap: (_, __, ___) {
onShareFileRequested?.call();
},
);
}
return Column(
children: [
Expanded(
child: ResultView.success(
titleText: strings.documentSigningSuccessTitle,
body: body,
),
),
if (onShareFileRequested != null)
// Primary button
FilledButton(
style: FilledButton.styleFrom(
minimumSize: kPrimaryButtonMinimumSize,
),
onPressed: onShareFileRequested,
child: Text(strings.shareSignedDocumentLabel),
),
const SizedBox(height: kButtonSpace),
// Secondary button
TextButton(
style: TextButton.styleFrom(
minimumSize: kPrimaryButtonMinimumSize,
),
onPressed: onCloseRequested,
child: Text(strings.buttonCloseLabel),
),
],
);
}
static String _getParentDirectoryName(File file) {
return kIsWeb
? file.uri
.resolve('.')
.path
.split('/')
.where((e) => e.isNotEmpty)
.lastOrNull ??
' '
: file.parent.basename;
}
}
@widgetbook.UseCase(
path: '[Screens]',
name: 'initial',
type: PresentSignedDocumentScreen,
)
Widget previewInitialPresentSignedDocumentScreen(BuildContext context) {
final signingType = context.knobs.list(
label: "Signing type",
options: DocumentSigningType.values,
initialOption: DocumentSigningType.local,
);
return _Body(
state: const PresentSignedDocumentInitialState(),
signingType: signingType,
onShareFileRequested: () {
developer.log('onShareFileRequested');
},
onCloseRequested: () {
developer.log('onCloseRequested');
},
);
}
@widgetbook.UseCase(
path: '[Screens]',
name: 'loading',
type: PresentSignedDocumentScreen,
)
Widget previewLoadingPresentSignedDocumentScreen(BuildContext context) {
final signingType = context.knobs.list(
label: "Signing type",
options: DocumentSigningType.values,
initialOption: DocumentSigningType.local,
);
return _Body(
state: const PresentSignedDocumentLoadingState(),
signingType: signingType,
onShareFileRequested: () {
developer.log('onShareFileRequested');
},
onCloseRequested: () {
developer.log('onCloseRequested');
},
);
}
@widgetbook.UseCase(
path: '[Screens]',
name: 'error',
type: PresentSignedDocumentScreen,
)
Widget previewErrorPresentSignedDocumentScreen(BuildContext context) {
final signingType = context.knobs.list(
label: "Signing type",
options: DocumentSigningType.values,
initialOption: DocumentSigningType.local,
);
// TODO Should preview whole Screen class also with BlocConsumer.listener to display error in SnackBar
const error = PathAccessException(
"/storage/emulated/0/Download/container-signed-xades-baseline-b.sce",
OSError("Permission denied", 13),
"Cannot open file",
);
return _Body(
state: const PresentSignedDocumentErrorState(error),
signingType: signingType,
onShareFileRequested: () {
developer.log('onShareFileRequested');
},
onCloseRequested: () {
developer.log('onCloseRequested');
},
);
}
@widgetbook.UseCase(
path: '[Screens]',
name: 'success',
type: PresentSignedDocumentScreen,
)
Widget previewSuccessPresentSignedDocumentScreen(BuildContext context) {
final signingType = context.knobs.list(
label: "Signing type",
options: DocumentSigningType.values,
initialOption: DocumentSigningType.local,
);
final path = context.knobs.string(
label: "File path",
initialValue: "Downloads/document_signed.pdf",
);
final file = File(path);
return _Body(
state: PresentSignedLocalDocumentSuccessState(file),
signingType: signingType,
onShareFileRequested: () {
developer.log('onShareFileRequested');
},
onCloseRequested: () {
developer.log('onCloseRequested');
},
);
}