insertDrop method

Future<void> insertDrop(
  1. BuildContext context,
  2. List<DropItem> files
)

Implementation

Future<void> insertDrop(BuildContext context, List<DropItem> files) async {
  List<String> conflicts = [];
  Map<String, String> renames = {};

  for (DropItem i in files) {
    String p = VPaths.join(workingDirectory, i.name);
    if (await exists(p)) {
      conflicts.add(i.name);
    }
  }

  if (conflicts.isNotEmpty) {
    bool abort = false;
    await DialogConfirm(
      title: "Overwrite Files?",
      description:
          "The following files already exist in this directory:\n\n${conflicts.join("\n")}",
      onConfirm: () => conflicts.clear(),
      cancelText: "Skip Duplicates",
      confirmText: "Overwrite",
      actions: [
        OutlineButton(
            onPressed: () {
              abort = true;
              Navigator.pop(context);
            },
            child: Text("Stop")),
        OutlineButton(
            onPressed: () async {
              for (DropItem i in files) {
                String p = VPaths.join(workingDirectory, i.name);
                if (await exists(p)) {
                  renames[p] = await findUnallocatedName(p);
                }
              }

              conflicts.clear();
              Navigator.pop(context);
            },
            child: Text("Keep Both")),
        OutlineButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text("Skip")),
        DestructiveButton(
            onPressed: () {
              conflicts.clear();
              Navigator.pop(context);
            },
            child: Text("Overwrite"))
      ],
    ).open(context);

    if (abort) {
      return;
    }
  }

  List<Future> work = [];

  for (DropItem i in files) {
    String p = VPaths.join(workingDirectory, i.name);
    p = renames[p] ?? p;

    if (conflicts.contains(i.name)) {
      continue;
    }

    work.add(writeFileStream(p, i.openRead()));
  }

  await Future.wait(work);
  update();
}