@@ -5,10 +5,13 @@ import 'package:flutter/material.dart';
55import 'package:phosphor_flutter/phosphor_flutter.dart' ;
66import 'package:supabase_flutter/supabase_flutter.dart' ;
77import 'package:uuid/uuid.dart' ;
8+ import 'package:cookethflow/core/helpers/file_helper.dart' ;
89
910class DashboardProvider extends StateHandler {
1011 late SupabaseClient ? supabase;
1112 late SupabaseService supabaseService;
13+ final FileServices _fileServices = FileServices ();
14+
1215 DashboardProvider (this .supabase, this .supabaseService) : super () {
1316 initialize ();
1417 }
@@ -151,7 +154,7 @@ class DashboardProvider extends StateHandler {
151154 }
152155 }
153156
154- Future <String > createNewProject (BuildContext context ) async {
157+ Future <String > createNewProject () async {
155158 _isLoading = true ;
156159 try {
157160 var res = supabase? .auth.currentUser;
@@ -188,7 +191,87 @@ class DashboardProvider extends StateHandler {
188191 }
189192 }
190193
191- void importExistingProject (BuildContext context) {}
194+ Future <String > importExistingProject () async {
195+ _isLoading = true ;
196+ notifyListeners ();
197+ try {
198+ final jsonContent = await _fileServices.importJsonFileFromUser ();
199+ if (jsonContent == null ) {
200+ return 'Import operation cancelled or failed.' ;
201+ }
202+
203+ if (jsonContent['workspace' ] == null || jsonContent['canvasObjects' ] == null ) {
204+ return 'Invalid file format. Missing "workspace" or "canvasObjects" data.' ;
205+ }
206+
207+ final currentUser = supabase? .auth.currentUser;
208+ if (currentUser == null ) {
209+ return 'User not authenticated.' ;
210+ }
211+
212+ final oldWorkspaceId = jsonContent['workspace' ]['id' ];
213+ final newWorkspaceId = Uuid ().v4 ();
214+
215+ final newWorkspace = Map <String , dynamic >.from (jsonContent['workspace' ]);
216+ newWorkspace['id' ] = newWorkspaceId;
217+ newWorkspace['owner' ] = currentUser.id;
218+ newWorkspace['name' ] = '${newWorkspace ['name' ]} (Imported)' ;
219+ newWorkspace.remove ('created_at' );
220+ newWorkspace.remove ('lastEdited' );
221+
222+ final oldToNewIdMap = < String , String > {};
223+ final newCanvasObjects = < Map <String , dynamic >> [];
224+
225+ for (var obj in (jsonContent['canvasObjects' ] as List )) {
226+ final newId = Uuid ().v4 ();
227+ final oldId = obj['id' ];
228+ oldToNewIdMap[oldId] = newId;
229+
230+ final newObj = Map <String , dynamic >.from (obj);
231+ newObj['id' ] = newId;
232+ newCanvasObjects.add (newObj);
233+ }
234+
235+ for (var obj in newCanvasObjects) {
236+ if (obj['object_type' ] == 'connector' ) {
237+ final sourceId = obj['source_id' ];
238+ final targetId = obj['target_id' ];
239+ if (sourceId != null && oldToNewIdMap.containsKey (sourceId)) {
240+ obj['source_id' ] = oldToNewIdMap[sourceId];
241+ }
242+ if (targetId != null && oldToNewIdMap.containsKey (targetId)) {
243+ obj['target_id' ] = oldToNewIdMap[targetId];
244+ }
245+ }
246+ }
247+
248+ await supabase! .from ('workspace' ).insert (newWorkspace);
249+
250+ if (newCanvasObjects.isNotEmpty) {
251+ final objectsToInsert = newCanvasObjects.map ((obj) {
252+ return {
253+ 'id' : obj['id' ],
254+ 'object' : obj,
255+ 'workspace_id' : newWorkspaceId,
256+ };
257+ }).toList ();
258+ await supabase! .from ('canvas_objects' ).insert (objectsToInsert);
259+ }
260+
261+ await refreshDashboard ();
262+ return 'Workspace imported successfully!' ;
263+ } catch (e) {
264+ print ("Error importing project: $e " );
265+ String output = 'An error occurred during import.' ;
266+ if (e.toString ().contains ('maximum limit of 10 workspaces' )) {
267+ output = 'Maximum limit of workspaces reached. Upgrade your plan for more!' ;
268+ }
269+ return output;
270+ } finally {
271+ _isLoading = false ;
272+ notifyListeners ();
273+ }
274+ }
192275
193276 Future <void > syncWithDb () async {
194277 if (supabase == null ) {
@@ -249,4 +332,4 @@ class DashboardProvider extends StateHandler {
249332 await refreshDashboard ();
250333 }
251334 }
252- }
335+ }
0 commit comments