pickVideoFile method

Future<void> pickVideoFile(
  1. {required BuildContext context,
  2. required AppModel appModel,
  3. required WidgetRef ref,
  4. required bool pushReplacement,
  5. Directory? directory,
  6. FutureOr onFileSelected(
    1. String
    )?}
)

Pick a video file with a built-in file picker.

Implementation

Future<void> pickVideoFile({
  required BuildContext context,
  required AppModel appModel,
  required WidgetRef ref,
  required bool pushReplacement,
  Directory? directory,
  FutureOr Function(String)? onFileSelected,
}) async {
  List<Directory> rootDirectories =
      await appModel.getFilePickerDirectoriesForMediaType(mediaType);

  if (directory != null) {
    if (rootDirectories.isNotEmpty &&
        rootDirectories.first.path != directory.path) {
      rootDirectories.insert(0, directory);
    }
  }

  List<String> usedFiles = appModel
      .getMediaSourceHistory(mediaSource: this)
      .map((item) => item.mediaIdentifier)
      .toList();

  Iterable<String>? filePaths = await FilesystemPicker.open(
    allowedExtensions: allowedExtensions,
    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;
  await onFileSelected?.call(filePath);
  appModel.setLastPickedDirectory(
    type: PlayerMediaType.instance,
    directory: Directory(path.dirname(filePath)),
  );

  MediaItem? item = appModel
      .getMediaTypeHistory(mediaType: mediaType)
      .firstWhereOrNull((item) => item.mediaIdentifier == filePath);
  if (item == null) {
    item ??= MediaItem(
      canDelete: true,
      canEdit: false,
      mediaTypeIdentifier: mediaType.uniqueKey,
      mediaSourceIdentifier: uniqueKey,
      mediaIdentifier: filePath,
      position: 0,
      duration: 0,
      title: path.basenameWithoutExtension(filePath),
    );

    String thumbnailPath = appModel.getThumbnailFile().path;
    File thumbnailFile = appModel.getThumbnailFile();

    if (thumbnailFile.existsSync()) {
      thumbnailFile.deleteSync();
    }
    thumbnailFile.createSync(recursive: true);

    await generateThumbnail(filePath, thumbnailPath);
    await setOverrideThumbnailFromMediaItem(
      appModel: appModel,
      item: item,
      file: thumbnailFile,
      clearOverrideImage: true,
    );
  }

  await appModel.openMedia(
    pushReplacement: pushReplacement,
    context: context,
    ref: ref,
    mediaSource: this,
    item: item,
  );
}