-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_screen.dart
383 lines (329 loc) · 10.6 KB
/
main_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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import 'dart:async';
import 'dart:developer' as developer;
import 'dart:io' show File;
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart' show SvgPicture;
import 'package:logging/logging.dart';
import 'package:widgetbook/widgetbook.dart';
import 'package:widgetbook_annotation/widgetbook_annotation.dart' as widgetbook;
import '../../app_service.dart';
import '../../bloc/app_bloc.dart';
import '../../deep_links.dart';
import '../../di.dart';
import '../../file_extensions.dart';
import '../../services/encryption_key_registry.dart';
import '../../strings_context.dart';
import '../app_theme.dart';
import '../onboarding.dart';
import '../remote_document_signing.dart';
import '../widgets/autogram_logo.dart';
import 'main_menu_screen.dart';
import 'open_document_screen.dart';
import 'preview_document_screen.dart';
import 'start_remote_document_signing_screen.dart';
/// Main app screen that presents app features.
///
/// Has ability to:
/// - open new file and navigate next to [OpenDocumentScreen]
/// - show [MainMenuScreen]
/// - navigate to [StartRemoteDocumentSigningScreen]
/// - start Onboarding by navigating to [OnboardingScreen]
class MainScreen extends StatefulWidget {
final Uri? incomingUri;
const MainScreen({super.key, required this.incomingUri});
@override
State<MainScreen> createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
static final _logger = Logger((MainScreen).toString());
@override
void initState() {
super.initState();
_handleNewIncomingUri();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
Onboarding.refreshOnboardingRequired(context);
}
@override
void didUpdateWidget(covariant MainScreen oldWidget) {
super.didUpdateWidget(oldWidget);
// Using !identical instead of "!=" for case when sharing same file URI
if (!identical(oldWidget.incomingUri, widget.incomingUri)) {
_handleNewIncomingUri();
}
}
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<bool?>(
valueListenable: Onboarding.onboardingRequired,
builder: (context, onboardingRequired, _) {
return BlocListener<AppBloc, AppEvent?>(
listener: (_, state) {
if (state is RequestOpenFileEvent) {
_onOpenFileRequested();
}
},
child: Scaffold(
appBar: _MainAppBar(
context: context,
showQrCodeScannerIcon: (onboardingRequired == false),
onMenuPressed: _showMenu,
onQrCodeScannerPressed: _showQrCodeScanner,
),
body: _Body(
onboardingRequired: onboardingRequired,
onStartOnboardingRequested: _onStartOnboardingRequested,
onOpenFileRequested: _onOpenFileRequested,
),
),
);
},
);
}
void _handleNewIncomingUri() {
final uri = widget.incomingUri;
if (uri != null && mounted) {
switch (uri.scheme) {
case "file" || "content":
// This is only Future that will (hopefully) return the File
final Future<File> sharedFile = getIt.get<AppService>().getFile(uri);
WidgetsBinding.instance.addPostFrameCallback((_) {
// Directly navigate to next screen, which have progress and error handling
_openNewFile(sharedFile);
});
break;
case "https" || "avm":
WidgetsBinding.instance.addPostFrameCallback((_) {
_handleDeepLink(uri);
});
break;
}
}
}
Future<void> _showMenu() {
const screen = MainMenuScreen.create();
return showGeneralDialog(
context: context,
routeSettings: RouteSettings(
name: screen.runtimeType.toString(),
),
pageBuilder: (context, _, __) => screen,
);
}
Future<void> _showQrCodeScanner() {
return RemoteDocumentSigning.startRemoteDocumentSigning(context);
}
Future<void> _openNewFile(FutureOr<File> file) {
getIt.get<EncryptionKeyRegistry>().newValue();
final screen = OpenDocumentScreen(file: file);
final route = MaterialPageRoute(builder: (_) => screen);
// Removing other routes because might want to open another file from Files;
// in that case we will stop any previous signing flow
return Navigator.of(context).pushAndRemoveUntil(
route,
(final route) => route.settings.name == '/',
);
}
void _handleDeepLink(Uri uri) {
DeepLinkAction action;
try {
action = parseDeepLinkAction(uri);
} catch (error) {
final message = context.strings.deepLinkParseErrorMessage(error);
final snackBar = SnackBar(
content: Text(message),
duration: const Duration(seconds: 5),
);
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(snackBar);
return;
}
ScaffoldMessenger.of(context).hideCurrentSnackBar();
_logger.info("Handling Deep Link action: $action");
if (action is SignRemoteDocumentAction) {
getIt.get<EncryptionKeyRegistry>().value = action.key;
final screen = PreviewDocumentScreen(
documentId: action.guid,
file: null,
);
final route = MaterialPageRoute(builder: (_) => screen);
// Removing other routes because might want to open another file from URL;
// in that case we will stop any previous signing flow
Navigator.of(context).pushAndRemoveUntil(
route,
(final route) => route.settings.name == '/',
);
}
}
Future<void> _onStartOnboardingRequested() {
_logger.fine('Requested to start Onboarding.');
return Onboarding.startOnboarding(context);
}
Future<void> _onOpenFileRequested() async {
_logger.fine('Requested to open file.');
FilePickerResult? result = await FilePicker.platform.pickFiles(
dialogTitle: context.strings.buttonOpenDocumentLabel,
);
final selectedFile = result?.files.singleOrNull;
if (selectedFile != null) {
final file = File(selectedFile.path!);
_logger.fine('File selected: ${file.redactedInfo}');
if (context.mounted) {
_openNewFile(file);
}
}
}
}
// ignore: non_constant_identifier_names
AppBar _MainAppBar({
required BuildContext context,
bool showQrCodeScannerIcon = true,
VoidCallback? onMenuPressed,
VoidCallback? onQrCodeScannerPressed,
}) {
final iconColor = Theme.of(context).colorScheme.onSecondary;
final colorFilter = ColorFilter.mode(iconColor, BlendMode.srcIn);
return AppBar(
foregroundColor: kMainAppBarForegroundColor,
backgroundColor: kMainAppBarBackgroundColor,
leading: Semantics(
button: true,
excludeSemantics: true,
label: context.strings.buttonMenuLabelSemantics,
child: IconButton(
icon: SvgPicture.asset(
'assets/icons/menu.svg',
colorFilter: colorFilter,
),
onPressed: onMenuPressed,
),
),
actions: [
if (showQrCodeScannerIcon)
Semantics(
label: context.strings.qrCodeScannerOpenSemantics,
button: true,
excludeSemantics: true,
child: IconButton(
icon: SvgPicture.asset(
'assets/icons/qr_code_scanner.svg',
colorFilter: colorFilter,
),
onPressed: onQrCodeScannerPressed,
),
),
],
title: Builder(builder: (context) {
return Text(
context.strings.appName,
style: const TextStyle(
color: kMainAppBarForegroundColor,
),
);
}),
);
}
/// [MainScreen] body.
class _Body extends StatelessWidget {
final bool? onboardingRequired;
final VoidCallback? onStartOnboardingRequested;
final VoidCallback? onOpenFileRequested;
const _Body({
required this.onboardingRequired,
required this.onStartOnboardingRequested,
required this.onOpenFileRequested,
});
@override
Widget build(BuildContext context) {
final strings = context.strings;
return Padding(
padding: kScreenMargin,
child: Column(
children: [
const SizedBox(height: 96),
const Padding(
padding: EdgeInsets.all(48),
child: AutogramLogo(),
),
Text(
strings.introHeading,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 24),
Text(
strings.introBody,
style: const TextStyle(height: 1.75),
),
const Spacer(),
// Primary button
_buildPrimaryButton(context),
],
),
);
}
Widget _buildPrimaryButton(BuildContext context) {
VoidCallback? onPressed;
String label;
if (onboardingRequired == false) {
onPressed = onOpenFileRequested;
label = context.strings.buttonOpenDocumentLabel;
} else {
onPressed = onStartOnboardingRequested;
label = context.strings.buttonInitialSetupLabel;
}
return FilledButton(
style: FilledButton.styleFrom(
minimumSize: kPrimaryButtonMinimumSize,
),
onPressed: onPressed,
child: Text(label),
);
}
}
@widgetbook.UseCase(
path: '[AVM]',
name: 'main',
type: AppBar,
)
Widget previewMainAppBar(BuildContext context) {
final showQrCodeScannerIcon = context.knobs.boolean(
label: "Show QR code scanner",
initialValue: true,
);
return SizedBox(
height: kToolbarHeight,
child: _MainAppBar(
context: context,
showQrCodeScannerIcon: showQrCodeScannerIcon,
onMenuPressed: () {
developer.log("onMenuPressed");
},
),
);
}
@widgetbook.UseCase(
path: '[Screens]',
name: '',
type: MainScreen,
)
Widget previewMainScreen(BuildContext context) {
final onboardingRequired = context.knobs.booleanOrNull(
label: "Onboarding is required",
initialValue: false,
);
return _Body(
onboardingRequired: onboardingRequired,
onStartOnboardingRequested: () {
developer.log("onStartOnboardingRequested");
},
onOpenFileRequested: () {
developer.log("onOpenFileRequested");
},
);
}