-
Notifications
You must be signed in to change notification settings - Fork 38
/
file_view.dart
576 lines (487 loc) · 16.7 KB
/
file_view.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import 'dart:async';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_file_view/src/enum/x5_status.dart';
import 'enum/view_type.dart';
import 'file_view_localizations.dart';
import 'file_view_tools.dart';
import 'flutter_file_view.dart';
/// @Describe: The view of file.
///
/// @Author: LiWeNHuI
/// @Date: 2022/9/11
class FileView extends StatefulWidget {
// ignore: public_member_api_docs
const FileView({
Key? key,
required this.controller,
this.placeholder,
this.tipTextStyle,
this.unSupportedPlatform,
this.nonExistent,
this.unSupportedType,
}) : super(key: key);
/// The [FileViewController] responsible for the file being rendered in this
/// widget.
final FileViewController controller;
/// Widget displayed while the target is loading.
final Widget? placeholder;
/// The style of the text for the prompt.
final TextStyle? tipTextStyle;
/// Widget to display on unsupported platforms.
///
/// This prompt is required because it only supports Android and iOS,
/// and has not been adapted to desktop and web for the time being.
final Widget? unSupportedPlatform;
/// Widget displayed while the file does not exist.
final Widget? nonExistent;
/// Widget displayed while the file is of an unsupported file types
final Widget? unSupportedType;
@override
State<FileView> createState() => _FileViewState();
}
class _FileViewState extends State<FileView> {
late FileViewLocalizations local = FileViewLocalizations.of(context);
@override
void initState() {
widget.controller.initialize();
widget.controller.addListener(_listener);
super.initState();
}
@override
void didUpdateWidget(covariant FileView oldWidget) {
super.didUpdateWidget(oldWidget);
oldWidget.controller.removeListener(_listener);
widget.controller.addListener(_listener);
}
@override
void deactivate() {
super.deactivate();
widget.controller.removeListener(_listener);
}
@override
void dispose() {
FlutterFileView.currentAndroidViewNumber = 0;
super.dispose();
}
void _listener() {
setState(() {});
}
@override
Widget build(BuildContext context) {
switch (widget.controller.value.viewType) {
case ViewType.UNSUPPORTED_PLATFORM:
return _buildUnSupportPlatformWidget();
case ViewType.NON_EXISTENT:
return _buildNonExistentWidget();
case ViewType.UNSUPPORTED_FILETYPE:
return _buildUnSupportTypeWidget();
case ViewType.DONE:
return _buildDoneWidget();
// ignore: no_default_cases
default:
return _buildPlaceholderWidget();
}
}
/// The layout to display when loading.
Widget _buildPlaceholderWidget() {
return widget.placeholder ??
Center(
child: CircularProgressIndicator(
key: ValueKey<String>('FileView_${hashCode}_Placeholder'),
value: widget.controller.value.progressValue,
),
);
}
/// The layout to display when the platform is unsupported.
Widget _buildUnSupportPlatformWidget() {
return widget.unSupportedPlatform ??
showTipWidget(local.unSupportedPlatformTip);
}
/// The layout to display when the file does not exist.
Widget _buildNonExistentWidget() {
return widget.nonExistent ?? showTipWidget(local.nonExistentTip);
}
/// The layout to display when the file type is unsupported.
Widget _buildUnSupportTypeWidget() {
return widget.unSupportedType ??
showTipWidget(
sprintf(
local.unSupportedType,
widget.controller.value.fileType ?? '',
),
);
}
/// Widgets for presenting information
Widget showTipWidget(String tip) {
return Center(child: Text(tip, style: widget.tipTextStyle));
}
/// A replacement operation for [stringTf].
String sprintf(String stringTf, String msg) {
return stringTf.replaceAll('%s', msg);
}
/// The layout to display when complete.
Widget _buildDoneWidget() {
if (isAndroid) {
return _createAndroidView();
} else if (isIOS) {
return UiKitView(
viewType: viewName,
creationParams: <String, String>{
'filePath': widget.controller.value.filePath ?? '',
'fileType': widget.controller.value.fileType ?? '',
},
creationParamsCodec: const StandardMessageCodec(),
);
}
return _buildUnSupportPlatformWidget();
}
Widget _createAndroidView() {
switch (widget.controller.value.x5status) {
case X5Status.DONE:
return AndroidView(
viewType: viewName,
creationParams: <String, dynamic>{
'filePath': widget.controller.value.filePath,
'fileType': widget.controller.value.fileType,
'is_bar_show': widget.controller._androidViewConfig.isBarShow,
'into_downloading':
widget.controller._androidViewConfig.intoDownloading,
'is_bar_animating':
widget.controller._androidViewConfig.isBarAnimating,
},
onPlatformViewCreated: (int id) {
MethodChannel('${channelName}_$id').invokeMethod<void>(
'openFile',
FlutterFileView.currentAndroidViewNumber++ == 0,
);
},
creationParamsCodec: const StandardMessageCodec(),
);
case X5Status.ERROR:
return showTipWidget(local.engineFail);
case X5Status.DOWNLOAD_SUCCESS:
return showX5TipWidget(local.engineDownloadSuccess);
case X5Status.DOWNLOAD_FAIL:
return showX5RetryWidget(local.engineDownloadFail);
case X5Status.DOWNLOADING:
return showX5TipWidget(local.engineDownloading);
case X5Status.DOWNLOAD_NON_REQUIRED:
return showTipWidget(local.engineDownloadNonRequired);
case X5Status.DOWNLOAD_OUT_OF_ONE:
return showTipWidget(local.engineDownloadOutOfOne);
case X5Status.INSTALL_SUCCESS:
return showX5TipWidget(local.engineInstallSuccess);
case X5Status.INSTALL_FAIL:
return showX5RetryWidget(local.engineInstallFail);
// ignore: no_default_cases
default:
return showX5TipWidget(local.engineLoading);
}
}
/// Widgets for presenting information of x5Status.
Widget showX5TipWidget(String tip) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
CircularProgressIndicator(
key: ValueKey<String>('FileView_${hashCode}_X5_Placeholder'),
value: widget.controller.value.progressValue,
color: Theme.of(context).primaryColor,
backgroundColor: widget.controller.value.progressValue != null
? Theme.of(context).primaryColorLight
: null,
),
const SizedBox(height: 20),
Text(tip, style: widget.tipTextStyle),
],
),
);
}
Widget showX5RetryWidget(String tip) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(tip, style: widget.tipTextStyle),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () {
FlutterFileView.init();
widget.controller.initializeForAndroid();
},
child: Text(local.retry, style: widget.tipTextStyle),
),
],
),
);
}
}
/// The controller of [FileView].
///
/// The document is displayed in a Flutter app by creating a [FileView] widget.
///
/// To reclaim the resources used by the player call [dispose].
///
/// After [dispose] all further calls are ignored.
class FileViewController extends ValueNotifier<FileViewValue> {
/// Constructs a [FileViewController] preview a document from an asset.
///
/// The name of the asset is given by the [dataSource] argument and must not
/// be null. The [package] argument must be non-null when the asset comes
/// from a package and null otherwise.
FileViewController.asset(
this.dataSource, {
this.package,
this.androidViewConfig,
this.isDelExist = true,
}) : dataSourceType = DataSourceType.asset,
networkConfig = null,
super(FileViewValue.uninitialized());
/// Constructs a [FileViewController] preview a document from obtained from
/// the network.
///
/// The URI for the document is given by the [dataSource] argument and must
/// not be null.
FileViewController.network(
this.dataSource, {
NetworkConfig? config,
this.androidViewConfig,
this.isDelExist = true,
}) : dataSourceType = DataSourceType.network,
package = null,
networkConfig = config ?? NetworkConfig(),
super(FileViewValue.uninitialized());
/// Constructs a [FileViewController] preview a document from a file.
FileViewController.file(
File file, {
this.androidViewConfig,
this.isDelExist = true,
}) : dataSource = file.path,
dataSourceType = DataSourceType.file,
package = null,
networkConfig = null,
super(FileViewValue.uninitialized());
/// The URI to the document file. This will be in different formats depending
/// on the [DataSourceType] of the original document.
final String dataSource;
/// Describes the type of data source this [FileViewController]
/// is constructed with.
final DataSourceType dataSourceType;
/// Only set for [FileViewController.asset] documents. The package that the
/// asset was loaded from.
final String? package;
/// The name used to generate the key to obtain the asset. For local assets
/// this is [dataSource], and for assets from packages the [dataSource] is
/// prefixed 'packages/<package_name>/'.
String get keyName =>
package == null ? dataSource : 'packages/$package/$dataSource';
/// HTTP headers used for the request to the [dataSource].
/// Only for [FileViewController.network].
/// Always empty for other document types.
final NetworkConfig? networkConfig;
/// The relevant parameters of TbsReaderView.
final AndroidViewConfig? androidViewConfig;
/// Whether to delete files with the same path.
final bool isDelExist;
/// Parameters supplied to _createAndroidView to use.
AndroidViewConfig get _androidViewConfig =>
androidViewConfig ?? AndroidViewConfig();
/// Attempts to open the given [dataSource] and load metadata about
/// the document.
Future<void> initialize() async {
if (!(isAndroid || isIOS)) {
value = value.copyWith(viewType: ViewType.UNSUPPORTED_PLATFORM);
return;
}
value = value.copyWith(viewType: ViewType.LOADING);
/// The name of the file.
final String fileName = FileViewTools.getFileSaveKey(dataSource);
/// The storage address of the file.
final String filePath =
'${await FileViewTools.getDirectoryPath()}$fileName';
final String fileType = FileViewTools.getFileType(filePath);
value = value.copyWith(fileType: fileType);
if (FileViewTools.isSupportByType(fileType)) {
value = value.copyWith(filePath: filePath);
/// The file to be used later.
File? file;
/// If the file itself exists, it will be deleted.
if (isDelExist && FileViewTools.fileExists(filePath)) {
await File(filePath).delete();
}
if (dataSourceType == DataSourceType.network) {
final bool flag = await FileViewTools.downloadFile(
dataSource,
filePath,
onReceiveProgress: (int count, int total) {
value = value.copyWith(progressValue: count / total);
},
config: networkConfig,
);
if (flag) {
file = File(filePath);
}
} else {
try {
file = File(filePath)..createSync(recursive: true);
if (dataSourceType == DataSourceType.asset) {
final ByteData bd = await rootBundle.load(keyName);
await file.writeAsBytes(bd.buffer.asUint8List());
} else if (dataSourceType == DataSourceType.file) {
await file.writeAsBytes(File(dataSource).readAsBytesSync());
}
} catch (e) {
file = null;
}
}
if (file != null && FileViewTools.fileExists(filePath)) {
value = value.copyWith(viewType: ViewType.DONE);
if (isAndroid) {
await initializeForAndroid();
}
} else {
value = value.copyWith(viewType: ViewType.NON_EXISTENT);
}
} else {
value = value.copyWith(viewType: ViewType.UNSUPPORTED_FILETYPE);
}
}
/// Monitor the loading status of X5 kernel.
StreamSubscription<X5Status>? x5StatusListener;
/// Monitor the download status of X5 kernel.
StreamSubscription<int>? downloadListener;
/// Because Android uses the X5 engine, it needs to be operated separately.
Future<void> initializeForAndroid() async {
final X5Status x5Status = await FlutterFileView.x5Status();
value = value.copyWith(x5status: x5Status);
if (x5Status != X5Status.DONE) {
x5StatusListener = FlutterFileView.initController.listen((_) {
if (_ == X5Status.DOWNLOADING) {
downloadListener = FlutterFileView.downlodController.listen((__) {
value = value.copyWith(x5status: _, progressValue: __ / 100);
});
} else {
value = value.copyWith(x5status: _);
}
if (_ == X5Status.INSTALL_SUCCESS) {
Future<void>.delayed(const Duration(seconds: 10), () {
value = value.copyWith(x5status: X5Status.ERROR);
});
}
});
}
}
/// Delete Files.
void deleteFile() {
final File file = File(value.filePath ?? '');
if (isDelExist && file.existsSync()) {
file.deleteSync();
}
}
@override
void dispose() {
deleteFile();
x5StatusListener?.cancel();
downloadListener?.cancel();
super.dispose();
}
}
/// The viewType, filePath, fileType, downloadProgress, X5Status of a
/// [FileViewController].
class FileViewValue {
/// Constructs a file with the given values. Only [viewType] is required.
/// The rest will initialize with default values when unset.
FileViewValue({
required this.viewType,
this.x5status = X5Status.NONE,
this.filePath,
this.fileType,
this.progressValue,
});
/// Returns an instance for a file that hasn't been loaded.
FileViewValue.uninitialized() : this(viewType: ViewType.NONE);
/// The loaded state of the view.
final ViewType viewType;
/// The state of X5 kernel.
final X5Status x5status;
/// The path where the file is stored.
final String? filePath;
/// The type of storage of the file
final String? fileType;
/// The progress of the loading of [CircularProgressIndicator].
final double? progressValue;
/// Returns a new instance that has the same values as this current instance,
/// except for any overrides passed in as arguments to [copyWith].
FileViewValue copyWith({
ViewType? viewType,
X5Status? x5status,
String? filePath,
String? fileType,
double? progressValue,
}) {
return FileViewValue(
viewType: viewType ?? this.viewType,
x5status: x5status ?? this.x5status,
filePath: filePath ?? this.filePath,
fileType: fileType ?? this.fileType,
progressValue: progressValue,
);
}
}
/// The way in which the document was originally loaded.
///
/// This has nothing to do with the document's file type. It's just the place
/// from which the document is fetched from.
enum DataSourceType {
/// The document was included in the app's asset files.
asset,
/// The document was downloaded from the internet.
network,
/// The document was loaded off of the local filesystem.
file,
}
/// HTTP headers used for the request to the `dataSource`.
/// Only for [FileViewController.network].
/// Always empty for other document types.
class NetworkConfig {
// ignore: public_member_api_docs
NetworkConfig({
this.queryParameters,
this.cancelToken,
this.deleteOnError,
this.lengthHeader,
this.data,
this.options,
});
/// [Dio.download] `queryParameters`
final Map<String, dynamic>? queryParameters;
/// [Dio.download] `cancelToken`
final CancelToken? cancelToken;
/// [Dio.download] `deleteOnError`
final bool? deleteOnError;
/// [Dio.download] `lengthHeader`
final String? lengthHeader;
/// [Dio.download] `data`
final dynamic data;
/// [Dio.download] `options`
final Options? options;
}
/// The relevant parameters of TbsReaderView.
class AndroidViewConfig {
// ignore: public_member_api_docs
AndroidViewConfig({
this.isBarShow = false,
this.intoDownloading = false,
this.isBarAnimating = false,
});
/// The `is_bar_show` of TbsReaderView
final bool isBarShow;
/// The `into_downloading` of TbsReaderView
final bool intoDownloading;
/// The `is_bar_animating` of TbsReaderView
final bool isBarAnimating;
}