diff --git a/client/lib/libphylum/actions/action_resource_mkdir.dart b/client/lib/libphylum/actions/action_resource_mkdir.dart index 6564de53..b97df63e 100644 --- a/client/lib/libphylum/actions/action_resource_mkdir.dart +++ b/client/lib/libphylum/actions/action_resource_mkdir.dart @@ -72,13 +72,16 @@ class ResourceMkdirAction extends ResourceCreateAction with JsonApiAction { Future applyOptimisticUpdate() async { await account.db.createResource(resourceId, parent, localName, true, timestamp); if (deletedId != null) { - account.db.updateResource(deletedId!, (u) => u(parent: Value(null))); + await account.db.updateResource(deletedId!, (u) => u(parent: Value(null))); } } @override - Future revertOptimisticUpdate() { - return account.db.deleteResource(resourceId); + Future revertOptimisticUpdate() async { + await account.db.deleteResource(resourceId); + if (deletedId != null) { + await account.db.updateResource(deletedId!, (u) => u(parent: Value(parent))); + } } @override diff --git a/client/lib/util/upload_utils.dart b/client/lib/util/upload_utils.dart index 2d347111..67aa49cb 100644 --- a/client/lib/util/upload_utils.dart +++ b/client/lib/util/upload_utils.dart @@ -19,7 +19,7 @@ final illegalChars = { bool validateName(String name) => !name.startsWith(' ') && name != '.' && name != '..' && name.trim().isNotEmpty && !name.runes.any((c) => c < 31 || illegalChars.contains(c)); -void createDirectory(BuildContext context, String folderId) async { +Future createDirectory(BuildContext context, String folderId) async { final account = context.read(); final dirName = await showInputDialog( context, @@ -28,7 +28,44 @@ void createDirectory(BuildContext context, String folderId) async { validate: validateName, ); if (dirName == null || dirName == "") return; - account.resourceRepository.mkdir(parent: folderId, name: dirName); + try { + await account.resourceRepository.mkdir(parent: folderId, name: dirName); + } on NameConflictException { + if (!context.mounted) return; + final conflictResolution = await showDialog( + context: context, + builder: (context) => AlertDialog( + title: const Text('Name Conflict'), + content: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 360), + child: Text( + 'There is already another item with the name \'$dirName\' in this folder.', + softWrap: true, + ), + ), + actions: [ + TextButton( + child: Text('Delete'), + onPressed: () { + Navigator.of(context).pop(nameConflictDelete); + }, + ), + ElevatedButton( + autofocus: true, + onPressed: () { + Navigator.of(context).pop(nameConflictRename); + }, + child: Text('Rename'), + ), + ], + ), + ); + if (conflictResolution == null) return; + await account.resourceRepository.mkdir(parent: folderId, name: dirName, conflictResolution: conflictResolution); + } catch (e) { + if (!context.mounted) return; + await showAlertDialog(context, title: 'Error while creating directory', message: e.toString()); + } } void pickAndUploadFiles(BuildContext context, String folderId) async {