Skip to content

Commit

Permalink
feat: Adding rename selection feature (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzanardo authored Oct 22, 2024
1 parent 776fd17 commit 29db7f7
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import 'package:fire_atlas_editor/store/actions/atlas_actions.dart';
import 'package:fire_atlas_editor/store/actions/editor_actions.dart';
import 'package:fire_atlas_editor/store/store.dart';
import 'package:fire_atlas_editor/widgets/button.dart';
import 'package:fire_atlas_editor/widgets/input_text_row.dart';
import 'package:fire_atlas_editor/widgets/text.dart';
import 'package:flutter/widgets.dart';
import 'package:slices/slices.dart';

class RenameSelectionModal extends StatefulWidget {
final String currentName;

const RenameSelectionModal({required this.currentName, super.key});

@override
State createState() => _RenameSelectionModalState();
}

class _RenameSelectionModalState extends State<RenameSelectionModal> {
final newNameController = TextEditingController();

@override
void initState() {
super.initState();
newNameController.text = widget.currentName;
}

@override
Widget build(BuildContext ctx) {
return Container(
padding: const EdgeInsets.only(left: 20, right: 20),
child: Column(
children: [
const FSubtitleTitle(title: 'Rename selection'),
InputTextRow(
label: 'New name:',
inputController: newNameController,
),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FButton(
label: 'Cancel',
onSelect: () => _closeModal(ctx),
),
const SizedBox(width: 20),
FButton(
label: 'Rename',
onSelect: () => _renameSelection(ctx),
),
],
),
const SizedBox(height: 20),
],
),
);
}

Future<void> _renameSelection(BuildContext ctx) async {
final store = SlicesProvider.of<FireAtlasState>(ctx);
final newName = newNameController.text;
if (newName.isEmpty) {
store.dispatch(
CreateMessageAction(
type: MessageType.ERROR,
message: 'The new name is required.',
),
);
} else {
final action = RenameSelectionAction(
oldName: widget.currentName,
newName: newName,
);
await store.dispatchAsync(action);
await _closeModal(ctx);
}
}

Future<void> _closeModal(BuildContext ctx) async {
final store = SlicesProvider.of<FireAtlasState>(ctx);
store.dispatch(CloseEditorModal());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:equatable/equatable.dart';
import 'package:fire_atlas_editor/screens/editor_screen/widgets/delete_selection_modal.dart';
import 'package:fire_atlas_editor/screens/editor_screen/widgets/edit_selection_folder_modal.dart';
import 'package:fire_atlas_editor/screens/editor_screen/widgets/rename_selection_modal.dart';
import 'package:fire_atlas_editor/screens/editor_screen/widgets/selection_canvas/selection_form.dart';
import 'package:fire_atlas_editor/store/actions/atlas_actions.dart';
import 'package:fire_atlas_editor/store/actions/editor_actions.dart';
Expand All @@ -16,18 +17,19 @@ class _SelectionListSlice extends Equatable {
final FireAtlas? currentAtlas;
final BaseSelection? selectedSelection;

final List<String?> _groups;
final List<(String, String?)> _selections;

_SelectionListSlice(this.currentAtlas, this.selectedSelection)
: _groups =
currentAtlas?.selections.values.map((e) => e.group).toList() ??
const [];
: _selections = currentAtlas?.selections.values
.map((e) => (e.id, e.group))
.toList() ??
const [];

@override
List<Object?> get props => [
currentAtlas?.id,
selectedSelection?.id,
_groups,
_selections,
];
}

Expand Down Expand Up @@ -233,6 +235,22 @@ class _SelectionEntry extends StatelessWidget {
},
tooltip: 'Move into Folder',
),
FIconButton(
iconData: Icons.drive_file_rename_outline_sharp,
onPress: () {
select(selection);
store.dispatch(
OpenEditorModal(
RenameSelectionModal(
currentName: selection.id,
),
300,
300,
),
);
},
tooltip: 'Rename selection',
),
FIconButton(
iconData: Icons.cancel,
onPress: () {
Expand Down
43 changes: 43 additions & 0 deletions fire_atlas_editor/lib/store/actions/atlas_actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,49 @@ class RenameAtlasAction extends AsyncSlicesAction<FireAtlasState> {
}
}

class RenameSelectionAction extends AsyncSlicesAction<FireAtlasState> {
final String oldName;
final String newName;

RenameSelectionAction({
required this.newName,
required this.oldName,
});

@override
Future<FireAtlasState> perform(
SlicesStore<FireAtlasState> store,
FireAtlasState state,
) async {
final atlas = state.currentAtlas;
if (atlas == null) {
return state;
}

final selection = atlas.selections[oldName];
if (selection != null) {
final newSelection = selection.copyWithInfo(
selection.selection.copyWith(id: newName),
);

atlas.selections.remove(oldName);

atlas.selections[newName] = newSelection;

return state.copyWith(
hasChanges: true,
loadedProject: Nullable(
state.loadedProject.value?.copyWith(
project: atlas,
),
),
);
}

return state;
}
}

class LoadAtlasAction extends AsyncSlicesAction<FireAtlasState> {
final String path;

Expand Down
19 changes: 10 additions & 9 deletions fire_atlas_editor/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,19 @@ packages:
dependency: "direct main"
description:
name: flame
sha256: "79133dc46a3ff870950f41d0dc1598414e7bd7ae2c29bd9f0a9de208d9a70cb7"
sha256: baafe5c38075b60577db05e0421c12941695b246547d9aca95166def3f29e57f
url: "https://pub.dev"
source: hosted
version: "1.18.0"
version: "1.21.0"
flame_fire_atlas:
dependency: "direct main"
description:
name: flame_fire_atlas
sha256: "27ad3bd6c37723d41a6079ea675d77e5ca52670f5a073ba12e0ee5b7b575082f"
url: "https://pub.dev"
source: hosted
version: "1.5.3"
path: "packages/flame_fire_atlas"
ref: HEAD
resolved-ref: "291af57deb7d742a73438b026ca2f4fd1c6a3454"
url: "https://github.com/flame-engine/flame.git"
source: git
version: "1.6.0"
flame_lint:
dependency: "direct main"
description:
Expand Down Expand Up @@ -284,10 +285,10 @@ packages:
dependency: transitive
description:
name: ordered_set
sha256: "1bfaaaee0419e43ecc9eaebd410eb4bd5039657b72011de75ff3e2915c9aac60"
sha256: fc861bb51fc863cd3e0718e21768af9586e0d5022b91a0fd4437636456cdb7d0
url: "https://pub.dev"
source: hosted
version: "5.0.3"
version: "6.1.1"
path:
dependency: transitive
description:
Expand Down
5 changes: 4 additions & 1 deletion fire_atlas_editor/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ dependencies:
file_selector_macos: ^0.9.0+4
file_selector_windows: ^0.9.1+4
flame: ^1.18.0
flame_fire_atlas: ^1.5.3
flame_fire_atlas:
git:
url: https://github.com/flame-engine/flame.git
path: packages/flame_fire_atlas
flame_lint: ^1.2.1
flutter:
sdk: flutter
Expand Down

0 comments on commit 29db7f7

Please sign in to comment.