launchFilePicker method
- {required BuildContext context,
- required WidgetRef ref,
- required AppModel appModel}
Launches a file picker and opens a media
Implementation
void launchFilePicker(
{required BuildContext context,
required WidgetRef ref,
required AppModel appModel}) async {
List<Directory> rootDirectories =
await appModel.getFilePickerDirectoriesForMediaType(mediaType);
List<String> usedFiles = appModel
.getMediaSourceHistory(mediaSource: this)
.map((item) => item.mediaIdentifier)
.map((e) => e.replaceAll('file://', ''))
.toList();
Iterable<String>? filePaths = await FilesystemPicker.open(
allowedExtensions: ['.html'],
context: context,
rootDirectories: rootDirectories,
fsType: FilesystemType.file,
title: '',
pickText: t.dialog_select,
cancelText: t.dialog_cancel,
themeData: Theme.of(context),
folderIconColor: Theme.of(context).colorScheme.primary,
usedFiles: usedFiles,
currentActiveFile: appModel.currentMediaItem?.mediaIdentifier,
);
if (filePaths == null || filePaths.isEmpty) {
return;
}
String filePath = filePaths.first;
String fileBasename = path.basename(filePath);
String directory = path.dirname(filePath);
String directoryBasename = path.basename(directory);
String parentDirectory = path.dirname(directory);
appModel.setLastPickedDirectory(
type: ReaderMediaType.instance,
directory: Directory(path.dirname(filePath)),
);
HeadlessInAppWebView webView = HeadlessInAppWebView(
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
allowFileAccessFromFileURLs: true,
allowUniversalAccessFromFileURLs: true,
),
android: AndroidInAppWebViewOptions(),
),
initialUrlRequest: URLRequest(
url: Uri.parse(
'file://$parentDirectory/${Uri.encodeComponent(directoryBasename)}/${Uri.encodeComponent(fileBasename)}'),
),
onLoadStop: (controller, url) async {
MediaItem? item = await generateMediaItemFromWebView(
appModel: appModel,
controller: controller,
);
if (item == null) {
Fluttertoast.showToast(msg: t.invalid_mokuro_file);
} else {
appModel.openMedia(
context: context,
item: item,
ref: ref,
mediaSource: this,
);
}
},
);
await webView.run();
}