mirror of
https://codeberg.org/shroff/phylum.git
synced 2025-12-31 16:30:28 -06:00
[client] Rename resources
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import 'package:phylum/libphylum/actions/resource_mkdir_action.dart';
|
||||
import 'package:phylum/libphylum/actions/resource_rename_action.dart';
|
||||
import 'package:phylum/libphylum/actions/resource_upload_action.dart';
|
||||
|
||||
const actionDeserializers = {
|
||||
ResourceMkdirAction.actionName: ResourceMkdirAction.fromMap,
|
||||
ResourceUploadAction.actionName: ResourceUploadAction.fromMap,
|
||||
ResourceRenameAction.actionName: ResourceRenameAction.fromMap,
|
||||
};
|
||||
|
||||
70
client/lib/libphylum/actions/resource_rename_action.dart
Normal file
70
client/lib/libphylum/actions/resource_rename_action.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:offtheline/offtheline.dart';
|
||||
import 'package:phylum/libphylum/db/db.dart';
|
||||
import 'package:phylum/libphylum/phylum_account.dart';
|
||||
|
||||
class ResourceRenameAction extends ApiAction<PhylumAccount> with JsonApiAction {
|
||||
static const actionName = 'resourceRename';
|
||||
@override
|
||||
String get name => actionName;
|
||||
|
||||
@override
|
||||
String get method => 'POST';
|
||||
|
||||
@override
|
||||
String get endpoint => '/v1/resources/rename/$id';
|
||||
|
||||
final String id;
|
||||
final String newName;
|
||||
final String oldName;
|
||||
|
||||
ResourceRenameAction._({
|
||||
required this.id,
|
||||
required this.newName,
|
||||
required this.oldName,
|
||||
});
|
||||
|
||||
ResourceRenameAction({
|
||||
required Resource r,
|
||||
required String name,
|
||||
}) : this._(id: r.id, newName: name, oldName: r.name);
|
||||
|
||||
static ResourceRenameAction fromMap(Map<String, dynamic> map, dynamic data) {
|
||||
return ResourceRenameAction._(
|
||||
id: map['id'],
|
||||
newName: map['newName'],
|
||||
oldName: map['oldName'],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String generateDescription(PhylumAccount account) {
|
||||
return "Renaming $oldName to $newName";
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, dynamic>? generateRequestBody() {
|
||||
return {
|
||||
'name': newName,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
FutureOr<void> applyOptimisticUpdate(PhylumAccount account) {
|
||||
account.datastore.db.managers.resources.update((o) => o(name: Value(newName)));
|
||||
}
|
||||
|
||||
@override
|
||||
FutureOr<void> revertOptimisticUpdate(PhylumAccount account) {
|
||||
account.datastore.db.managers.resources.update((o) => o(name: Value(oldName)));
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toMap() => {
|
||||
'id': id,
|
||||
'newName': newName,
|
||||
'oldName': oldName,
|
||||
};
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:phylum/libphylum/actions/resource_rename_action.dart';
|
||||
import 'package:phylum/libphylum/db/db.dart';
|
||||
import 'package:phylum/libphylum/phylum_account.dart';
|
||||
import 'package:phylum/libphylum/requests/resource_detail_request.dart';
|
||||
import 'package:phylum/ui/folder/resource_options_dialog.dart';
|
||||
import 'package:phylum/util/dialogs.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'folder_navigator_stack.dart';
|
||||
@@ -42,14 +45,31 @@ class _FolderContentsViewState extends State<FolderContentsView> {
|
||||
ListTile(
|
||||
leading: r.getIcon(),
|
||||
title: Text(r.name),
|
||||
trailing: r.dir ? null : Text(r.getSizeString()),
|
||||
onTap: () => context.read<FolderNavigatorStack>().push(r.id),
|
||||
subtitle: r.dir ? const Text('Folder') : Text(r.getSizeString()),
|
||||
trailing: IconButton(icon: Icon(Icons.adaptive.more), onPressed: () => showResourceOptions(account, r)),
|
||||
onTap: r.dir ? () => context.read<FolderNavigatorStack>().push(r.id) : null,
|
||||
onLongPress: () => showResourceOptions(account, r),
|
||||
),
|
||||
],
|
||||
)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void showResourceOptions(PhylumAccount account, Resource r) async {
|
||||
final context = this.context;
|
||||
final option = await showModalBottomSheet(context: context, builder: (context) => const ResourceOptionsList());
|
||||
if (!context.mounted) {
|
||||
return;
|
||||
}
|
||||
switch (option) {
|
||||
case ResourceOption.rename:
|
||||
final name = await showInputDialog(context, title: 'Rename', preset: r.name);
|
||||
if (name != null) {
|
||||
account.api.sendRequest(ResourceRenameAction(r: r, name: name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ResourceViewExtensions on Resource {
|
||||
|
||||
30
client/lib/ui/folder/resource_options_dialog.dart
Normal file
30
client/lib/ui/folder/resource_options_dialog.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
enum ResourceOption {
|
||||
rename(Icons.edit, 'Rename'),
|
||||
delete(Icons.delete, 'Delete');
|
||||
|
||||
const ResourceOption(this.icon, this.text);
|
||||
|
||||
final IconData icon;
|
||||
final String text;
|
||||
}
|
||||
|
||||
class ResourceOptionsList extends StatelessWidget {
|
||||
const ResourceOptionsList({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
shrinkWrap: true,
|
||||
children: [
|
||||
for (final o in ResourceOption.values)
|
||||
ListTile(
|
||||
leading: Icon(o.icon),
|
||||
title: Text(o.text),
|
||||
onTap: () => Navigator.of(context).pop(o),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user