Skip to content

Commit a064d86

Browse files
committed
feat: minor fixes
1 parent d662666 commit a064d86

6 files changed

Lines changed: 169 additions & 166 deletions

File tree

lib/core/services/supabase_service.dart

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import 'dart:io';
2-
32
import 'package:cookethflow/core/utils/enums.dart';
43
import 'package:cookethflow/core/utils/state_handler.dart';
54
import 'package:cookethflow/core/utils/ui_helper.dart';
65
import 'package:cookethflow/models/flow_manager.dart';
76
import 'package:cookethflow/models/flow_node.dart';
87
import 'package:file_selector/file_selector.dart';
8+
import 'package:flutter/foundation.dart'; // For kIsWeb
99
import 'package:flutter/material.dart';
1010
import 'package:http/http.dart' as http;
1111
import 'package:path_provider/path_provider.dart';
@@ -19,7 +19,7 @@ class SupabaseService extends StateHandler {
1919

2020
late AuthResponse _userData;
2121
bool _userDataSet = false;
22-
XFile? _userPfp = XFile('assets/Frame 271.png');
22+
XFile? _userPfp;
2323
String? _userName;
2424
String? _email;
2525

@@ -38,7 +38,7 @@ class SupabaseService extends StateHandler {
3838

3939
void setUserPfp(XFile? val) {
4040
_userPfp = val;
41-
print('Updated userPfp: ${val?.name}');
41+
print('Updated userPfp: ${val?.path}');
4242
notifyListeners();
4343
}
4444

@@ -56,7 +56,6 @@ class SupabaseService extends StateHandler {
5656
return text.length > 12 ? '${text.substring(0, 12)}...' : text;
5757
}
5858

59-
// Initialize user data on service creation
6059
Future<void> _initializeUserData() async {
6160
final user = supabase.auth.currentUser;
6261
if (user != null) {
@@ -67,9 +66,7 @@ class SupabaseService extends StateHandler {
6766

6867
Future<Map<String, dynamic>?> fetchCurrentUserName() async {
6968
final user = supabase.auth.currentUser;
70-
if (user == null) {
71-
return null;
72-
}
69+
if (user == null) return null;
7370

7471
try {
7572
final response = await supabase
@@ -87,9 +84,7 @@ class SupabaseService extends StateHandler {
8784

8885
Future<Map<String, dynamic>?> fetchCurrentUserDetails() async {
8986
final user = supabase.auth.currentUser;
90-
if (user == null) {
91-
return null;
92-
}
87+
if (user == null) return null;
9388

9489
try {
9590
final response =
@@ -272,7 +267,7 @@ class SupabaseService extends StateHandler {
272267
await supabase.auth.signOut();
273268
_userName = null;
274269
_email = null;
275-
_userPfp = XFile('assets/Frame 271.png');
270+
_userPfp = null; // Set to null to use asset in UI
276271
_userDataSet = false;
277272
notifyListeners();
278273
} catch (e) {
@@ -289,7 +284,7 @@ class SupabaseService extends StateHandler {
289284
await supabase.auth.signOut();
290285
_userName = null;
291286
_email = null;
292-
_userPfp = XFile('assets/Frame 271.png');
287+
_userPfp = null; // Set to null to use asset in UI
293288
_userDataSet = false;
294289
notifyListeners();
295290
} catch (e) {
@@ -383,12 +378,10 @@ class SupabaseService extends StateHandler {
383378
final user = supabase.auth.currentUser;
384379
if (user == null) throw Exception('User not authenticated');
385380

386-
// Get file extension and MIME type
387381
final extension = imageFile.name.split('.').last.toLowerCase();
388382
final mimeType = _getMimeTypeFromExtension(extension);
389383
final storagePath = '${user.id}/pfp.$extension';
390384

391-
// Remove existing profile picture if it exists
392385
try {
393386
await supabase.storage
394387
.from(_profileBucketName)
@@ -397,28 +390,22 @@ class SupabaseService extends StateHandler {
397390
print('No existing profile picture to remove: $e');
398391
}
399392

400-
// Read file bytes for web compatibility
401393
final bytes = await imageFile.readAsBytes();
402394

403-
// Upload to Supabase
404395
await supabase.storage.from(_profileBucketName).uploadBinary(
405396
storagePath,
406397
bytes,
407398
fileOptions: FileOptions(contentType: mimeType, upsert: true),
408399
);
409400

410-
// Get public URL
411401
final String publicUrl =
412402
supabase.storage.from(_profileBucketName).getPublicUrl(storagePath);
413403

414-
// Update user table with new URL
415404
await supabase
416405
.from('User')
417406
.update({'profile_picture_url': publicUrl}).eq('id', user.id);
418407

419-
// Update local state
420-
setUserPfp(imageFile);
421-
notifyListeners();
408+
setUserPfp(imageFile); // Use the uploaded file directly
422409
return publicUrl;
423410
} catch (e) {
424411
print('Error uploading profile picture: $e');
@@ -446,22 +433,32 @@ class SupabaseService extends StateHandler {
446433
const supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
447434

448435
if (supportedExtensions.contains(fileExtension)) {
449-
// For web, store the URL directly instead of downloading
450-
setUserPfp(XFile.fromData(
451-
await http.get(Uri.parse(pfpUrl)).then((res) => res.bodyBytes),
452-
name: 'pfp.$fileExtension',
453-
mimeType: _getMimeTypeFromExtension(fileExtension),
454-
));
436+
if (kIsWeb) {
437+
// Web: Use XFile.fromData for in-memory bytes
438+
final bytes = await http.get(uri).then((res) => res.bodyBytes);
439+
setUserPfp(XFile.fromData(
440+
bytes,
441+
name: 'pfp.$fileExtension',
442+
mimeType: _getMimeTypeFromExtension(fileExtension),
443+
));
444+
} else {
445+
// Desktop: Save to temporary file
446+
final tempDir = await getTemporaryDirectory();
447+
final tempFile = File('${tempDir.path}/pfp.$fileExtension');
448+
final bytes = await http.get(uri).then((res) => res.bodyBytes);
449+
await tempFile.writeAsBytes(bytes);
450+
setUserPfp(XFile(tempFile.path));
451+
}
455452
notifyListeners();
456453
return;
457454
}
458455
}
459-
// Fallback to default image
460-
setUserPfp(XFile(_defaultPfpPath));
456+
// Fallback to null (UI should use Image.asset for default)
457+
setUserPfp(null);
461458
notifyListeners();
462459
} catch (e) {
463460
print('Error fetching profile picture: $e');
464-
setUserPfp(XFile(_defaultPfpPath));
461+
setUserPfp(null);
465462
notifyListeners();
466463
}
467464
}

lib/core/widgets/alert_dialogues/delete_workspace.dart

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,45 @@ class DeleteWorkspace extends StatelessWidget {
99
@override
1010
Widget build(BuildContext context) {
1111
return Consumer<FlowmanageProvider>(
12-
builder: (context, flowProvider,child) {
13-
return AlertDialog(
14-
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
15-
title: Text(
16-
'Delete Workspace',
17-
style: TextStyle(fontFamily: 'Frederik', fontWeight: FontWeight.bold),
12+
builder: (context, flowProvider, child) {
13+
return AlertDialog(
14+
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
15+
title: Text(
16+
'Delete Workspace',
17+
style: TextStyle(fontFamily: 'Frederik', fontWeight: FontWeight.bold),
18+
),
19+
content: Text(
20+
'Are you sure you want to delete this workspace? This action cannot be undone.',
21+
style: TextStyle(fontFamily: 'Frederik'),
22+
),
23+
actions: [
24+
TextButton(
25+
onPressed: () {
26+
Navigator.pop(context);
27+
},
28+
child: Text('Cancel'),
1829
),
19-
content: Text(
20-
'Are you sure you want to delete this workspace? This action cannot be undone.',
21-
style: TextStyle(fontFamily: 'Frederik'),
30+
TextButton(
31+
onPressed: () async {
32+
try {
33+
await flowProvider.deleteWorkspace(flowId);
34+
35+
ScaffoldMessenger.of(context).showSnackBar(
36+
SnackBar(content: Text('Workspace deleted successfully')));
37+
38+
Navigator.pop(context); // Return to dashboard
39+
} catch (e) {
40+
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
41+
content: Text('Error deleting workspace: ${e.toString()}'),
42+
backgroundColor: Colors.red,
43+
));
44+
}
45+
Navigator.pop(context);
46+
},
47+
child: Text('Delete', style: TextStyle(color: Colors.red)),
2248
),
23-
actions: [
24-
TextButton(
25-
onPressed: () {
26-
Navigator.pop(context);
27-
},
28-
child: Text('Cancel'),
29-
),
30-
TextButton(
31-
onPressed: () async {
32-
Navigator.pop(context);
33-
34-
try {
35-
36-
await flowProvider.deleteWorkspace(flowId);
37-
38-
ScaffoldMessenger.of(context).showSnackBar(
39-
SnackBar(content: Text('Workspace deleted successfully')));
40-
41-
Navigator.of(context).pop(); // Return to dashboard
42-
} catch (e) {
43-
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
44-
content: Text('Error deleting workspace: ${e.toString()}'),
45-
backgroundColor: Colors.red,
46-
));
47-
}
48-
},
49-
child: Text('Delete', style: TextStyle(color: Colors.red)),
50-
),
51-
],
52-
);
53-
}
54-
);
49+
],
50+
);
51+
});
5552
}
5653
}

0 commit comments

Comments
 (0)