diff --git a/lib/pages/project/project_page.dart b/lib/pages/project/project_page.dart index 54c5c6eb..a149e66b 100644 --- a/lib/pages/project/project_page.dart +++ b/lib/pages/project/project_page.dart @@ -908,9 +908,17 @@ class _ProjectPageState extends State { for (PathPlannerPath path in _paths) { pathNames.add(path.name); } - String pathName = 'Copy of ${_paths[i].name}'; + String pathName = _paths[i].name; + RegExp exp = RegExp(r'\(\d+\)'); + String source = pathName.substring(pathName.length - 3); while (pathNames.contains(pathName)) { - pathName = 'Copy of $pathName'; + if (exp.hasMatch(source)) { + RegExpMatch? match = exp.firstMatch(source); + int index = int.parse(match![0]!.substring(1, 2)) + 1; + pathName = '${pathName.substring(0, pathName.length - 3)}($index)'; + } else { + pathName = '$pathName (1)'; + } } setState(() { @@ -1414,11 +1422,18 @@ class _ProjectPageState extends State { for (PathPlannerAuto auto in _autos) { autoNames.add(auto.name); } - String autoName = 'Copy of ${_autos[i].name}'; + String autoName = _autos[i].name; + RegExp exp = RegExp(r'\(\d+\)'); + String source = autoName.substring(autoName.length - 3); while (autoNames.contains(autoName)) { - autoName = 'Copy of $autoName'; + if (exp.hasMatch(source)) { + RegExpMatch? match = exp.firstMatch(source); + int index = int.parse(match![0]!.substring(1, 2)) + 1; + autoName = '${autoName.substring(0, autoName.length - 3)}($index)'; + } else { + autoName = '$autoName (1)'; + } } - setState(() { _autos.add(_autos[i].duplicate(autoName)); _sortAutos(_autoSortValue); diff --git a/test/pages/project/project_page_test.dart b/test/pages/project/project_page_test.dart index 0bcecfb8..1cb6e45e 100644 --- a/test/pages/project/project_page_test.dart +++ b/test/pages/project/project_page_test.dart @@ -475,7 +475,7 @@ void main() { await widgetTester.tap(find.text('Duplicate')); await widgetTester.pumpAndSettle(); - expect(find.widgetWithText(ProjectItemCard, 'Copy of Example Path'), + expect(find.widgetWithText(ProjectItemCard, 'Example Path (1)'), findsOneWidget); await widgetTester.tap(menuButton); @@ -484,7 +484,7 @@ void main() { await widgetTester.tap(find.text('Duplicate')); await widgetTester.pumpAndSettle(); - expect(find.widgetWithText(ProjectItemCard, 'Copy of Copy of Example Path'), + expect(find.widgetWithText(ProjectItemCard, 'Example Path (2)'), findsOneWidget); }); @@ -538,8 +538,7 @@ void main() { await widgetTester.tap(find.text('Duplicate')); await widgetTester.pumpAndSettle(); - expect( - find.widgetWithText(ProjectItemCard, 'Copy of auto1'), findsOneWidget); + expect(find.widgetWithText(ProjectItemCard, 'auto1 (1)'), findsOneWidget); await widgetTester.tap(menuButton); await widgetTester.pumpAndSettle(); @@ -547,8 +546,7 @@ void main() { await widgetTester.tap(find.text('Duplicate')); await widgetTester.pumpAndSettle(); - expect(find.widgetWithText(ProjectItemCard, 'Copy of Copy of auto1'), - findsOneWidget); + expect(find.widgetWithText(ProjectItemCard, 'auto1 (2)'), findsOneWidget); }); testWidgets('delete path', (widgetTester) async {