Skip to content

Commit

Permalink
feat(mobile/library): Duplicate workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Nov 5, 2023
1 parent b7b8d27 commit ce57791
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 10 deletions.
23 changes: 20 additions & 3 deletions frontend/mobile/lib/components/library/workflow_tile.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:area_mobile/components/editor/modals/name_modal.dart';
import 'package:area_mobile/services/dio.dart';
import 'package:area_mobile/types/services.dart';
import 'package:area_mobile/types/workflows/editor.dart';
import 'package:area_mobile/types/workflows/workflows.dart';
import 'package:area_mobile/utils/workflows.dart';
import 'package:flutter/material.dart';
Expand All @@ -10,7 +11,7 @@ import 'package:flutter_svg/svg.dart';
class WorkflowTile extends StatefulWidget {
final Workflow workflow;
final Function onUpdate;
final Function(Workflow workflow) onOpenEditor;
final Function(EditorWorkflow workflow) onOpenEditor;

const WorkflowTile({
required this.workflow,
Expand Down Expand Up @@ -64,9 +65,25 @@ class _WorkflowTileState extends State<WorkflowTile> {
child: ListTile(
leading: const Icon(Icons.edit),
title: Text(AppLocalizations.of(context)!.edit),
onTap: () {
onTap: () async {
Navigator.pop(context);
final editorWorkflow =
await convertWorkflowToEditorWorkflow(widget.workflow);
widget.onOpenEditor(editorWorkflow);
}),
),
PopupMenuItem(
value: "duplicate",
child: ListTile(
leading: const Icon(Icons.content_copy),
title: Text(AppLocalizations.of(context)!.duplicate),
onTap: () async {
Navigator.pop(context);
widget.onOpenEditor(widget.workflow);
final duplicatedWorkflow =
await convertWorkflowToDuplicateEditorWorkflow(
widget.workflow,
AppLocalizations.of(context)!.copyWorkflow);
widget.onOpenEditor(duplicatedWorkflow);
}),
),
PopupMenuItem(
Expand Down
2 changes: 2 additions & 0 deletions frontend/mobile/lib/locales/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"back": "Back",
"rename": "Rename",
"edit": "Edit",
"duplicate": "Duplicate",
"copyWorkflow": "Copy",
"delete": "Delete",
"editorActionDescription": "An event that starts your workflow",
"editorReactionDescription": "An event a workflow performs after it start",
Expand Down
2 changes: 2 additions & 0 deletions frontend/mobile/lib/locales/app_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"back": "Précédent",
"rename": "Renommer",
"edit": "Modifier",
"duplicate": "Dupliquer",
"copyWorkflow": "Copie",
"delete": "Supprimer",
"editorActionDescription": "Un événement qui lance votre workflow",
"editorReactionDescription": "Un événement qu'un workflow exécutes une fois lancé",
Expand Down
2 changes: 2 additions & 0 deletions frontend/mobile/lib/locales/app_is.arb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"back": "Fyrri",
"rename": "Endurnefna",
"edit": "Til að breyta",
"duplicate": "Afrit",
"copyWorkflow": "Afrita",
"delete": "EYÐA",
"editorActionDescription": "Atburður sem ræsir vinnuflæðið þitt",
"editorReactionDescription": "Atburður sem verkflæði keyrir þegar það er ræst",
Expand Down
9 changes: 3 additions & 6 deletions frontend/mobile/lib/pages/landing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:area_mobile/pages/services.dart';
import 'package:area_mobile/pages/user.dart';
import 'package:area_mobile/storage/index.dart';
import 'package:area_mobile/types/workflows/editor.dart';
import 'package:area_mobile/types/workflows/workflows.dart';
import 'package:area_mobile/utils/workflows.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand Down Expand Up @@ -141,11 +140,9 @@ class _LandingPageState extends State<LandingPage> {
Center(
child: Library(
key: ValueKey(libraryKey),
onOpenEditor: (Workflow workflowToOpen) async {
final newEditorWorkflow =
await convertWorkflowToEditorWorkflow(workflowToOpen);
onOpenEditor: (EditorWorkflow workflowToOpen) async {
setState(() {
editorWorkflow = newEditorWorkflow;
editorWorkflow = workflowToOpen;
editorKey++;
_selectedIndex = 2;
});
Expand All @@ -157,7 +154,7 @@ class _LandingPageState extends State<LandingPage> {
),
Center(
child: Editor(
key: ValueKey(editorWorkflow.id),
key: ValueKey(editorKey),
workflow: editorWorkflow,
onSave: () {
setState(() {
Expand Down
3 changes: 2 additions & 1 deletion frontend/mobile/lib/pages/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import 'package:area_mobile/components/empty_notice.dart';
import 'package:area_mobile/components/library/workflow_tile.dart';
import 'package:area_mobile/services/dio.dart';
import 'package:area_mobile/types/services.dart';
import 'package:area_mobile/types/workflows/editor.dart';
import 'package:area_mobile/types/workflows/workflows.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class Library extends StatefulWidget {
final Function(Workflow workflow) onOpenEditor;
final Function(EditorWorkflow workflow) onOpenEditor;

const Library({required this.onOpenEditor, Key? key}) : super(key: key);

Expand Down
34 changes: 34 additions & 0 deletions frontend/mobile/lib/utils/workflows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,40 @@ Future<EditorWorkflow> convertWorkflowToEditorWorkflow(
active: workflow.active);
}

Future<EditorWorkflow> convertWorkflowToDuplicateEditorWorkflow(
Workflow workflow, String copyText) async {
var editorWorkflow = await convertWorkflowToEditorWorkflow(workflow);

Map<String, String> idMapping = {};
for (var id in [
editorWorkflow.action.id,
...editorWorkflow.reactions.map((e) => e.id)
]) {
idMapping[id] = const UuidV4().generate().toString();
}

final newAction = EditorWorkflowAction(
id: idMapping[editorWorkflow.action.id]!,
area: editorWorkflow.action.area,
areaService: editorWorkflow.action.areaService);

final newReactions = editorWorkflow.reactions
.map((reaction) => EditorWorkflowReaction(
id: idMapping[reaction.id]!,
area: reaction.area,
areaService: reaction.areaService,
previousAreaId: idMapping[editorWorkflow.reactions
.firstWhere((e) => e.id == reaction.id)
.previousAreaId]!))
.toList();

return EditorWorkflow(
name: "${editorWorkflow.name} ($copyText)",
action: newAction,
reactions: newReactions,
active: editorWorkflow.active);
}

List<T> getSortedReactions<T>(List<dynamic> reactions, String basePreviousId) {
final sortedReactions = [];
var previousId = basePreviousId;
Expand Down

0 comments on commit ce57791

Please sign in to comment.