Skip to content

Commit

Permalink
Merge pull request #36 from flame-engine/luan.rename
Browse files Browse the repository at this point in the history
Add rename feature to fire-atlas
  • Loading branch information
luanpotter authored Feb 6, 2023
2 parents cf3c8e3 + d01b0e6 commit e088c32
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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 RenameAtlasModal extends StatefulWidget {
final String? currentName;

const RenameAtlasModal({Key? key, required this.currentName})
: super(key: key);

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

class _RenameAtlasModalState extends State<RenameAtlasModal> {
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 atlas'),
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: () => _renameAtlas(ctx),
),
],
),
const SizedBox(height: 20),
],
),
);
}

Future<void> _renameAtlas(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 = RenameAtlasAction(newAtlasId: newName);
await store.dispatchAsync(action);
await _closeModal(ctx);
}
}

Future<void> _closeModal(BuildContext ctx) async {
final store = SlicesProvider.of<FireAtlasState>(ctx);
store.dispatch(CloseEditorModal());
}
}
16 changes: 16 additions & 0 deletions fire_atlas_editor/lib/screens/editor_screen/widgets/toolbar.dart
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/change_image_modal.dart';
import 'package:fire_atlas_editor/screens/editor_screen/widgets/concat_image_modal.dart';
import 'package:fire_atlas_editor/screens/editor_screen/widgets/rename_atlas_modal.dart';
import 'package:fire_atlas_editor/screens/widgets/toggle_theme_button.dart';
import 'package:fire_atlas_editor/services/storage/storage.dart';
import 'package:fire_atlas_editor/store/actions/atlas_actions.dart';
Expand Down Expand Up @@ -62,6 +63,21 @@ class Toolbar extends StatelessWidget {
},
tooltip: 'Save project',
),
FIconButton(
iconData: Icons.edit,
onPress: () {
store.dispatch(
OpenEditorModal(
RenameAtlasModal(
currentName: store.state.currentAtlas?.id,
),
400,
500,
),
);
},
tooltip: 'Rename project',
),
FIconButton(
iconData: Icons.image,
onPress: () {
Expand Down
27 changes: 27 additions & 0 deletions fire_atlas_editor/lib/store/actions/atlas_actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,33 @@ class SaveAction extends AsyncSlicesAction<FireAtlasState> {
}
}

class RenameAtlasAction extends AsyncSlicesAction<FireAtlasState> {
final String newAtlasId;

RenameAtlasAction({required this.newAtlasId});

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

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

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

Expand Down

0 comments on commit e088c32

Please sign in to comment.