main.dart (4508B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:receive_sharing_intent/receive_sharing_intent.dart'; import 'package:creekui/services/theme_service.dart'; import 'package:creekui/ui/pages/share_handler_page.dart'; import 'package:creekui/ui/pages/home_page.dart'; import 'package:creekui/services/analysis_queue_manager.dart'; import 'package:flutter/services.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await dotenv.load(fileName: ".env"); AnalysisQueueManager().processQueue(); runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { late StreamSubscription _intentStreamSubscription; final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>(); bool _isReady = false; @override void initState() { super.initState(); _initApp(); } Future<void> _initApp() async { // 1. Load Theme try { await themeService.loadTheme(); } catch (e) { debugPrint("Theme load error (ignoring): $e"); } // 2. Setup Share Listeneer try { _intentStreamSubscription = ReceiveSharingIntent.instance .getMediaStream() .listen((List<SharedMediaFile> value) { if (value.isNotEmpty) { _handleShare(value.first.path); } }, onError: (err) => debugPrint("Share error: $err")); // 3. Cold Start ReceiveSharingIntent.instance.getInitialMedia().then(( List<SharedMediaFile> value, ) { if (value.isNotEmpty) { _handleShare(value.first.path); ReceiveSharingIntent.instance.reset(); } }); } catch (e) { debugPrint("Share intent error: $e"); } themeService.addListener(_updateTheme); if (mounted) setState(() => _isReady = true); } Future<void> _handleShare(String content) async { final context = _navigatorKey.currentState?.overlay?.context; if (context == null) return; // 1. Determine destination (Files vs Moodboards) String shareDestination = 'moodboards'; // Default try { const platform = MethodChannel('com.creek.ui/methods'); final result = await platform.invokeMethod<String>('getShareSource'); if (result != null) shareDestination = result; } catch (e) { debugPrint("Error getting share source: $e"); } // 2. Pass destination to the Handler Page _navigatorKey.currentState?.push( MaterialPageRoute( builder: (_) => ShareHandlerPage( sharedText: content, destination: shareDestination, // Pass the destination here ), ), ); } void _updateTheme() { if (mounted) setState(() {}); } @override void dispose() { _intentStreamSubscription.cancel(); themeService.removeListener(_updateTheme); super.dispose(); } @override Widget build(BuildContext context) { if (!_isReady) { return const MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( backgroundColor: Colors.white, body: Center(child: CircularProgressIndicator()), ), ); } return MaterialApp( navigatorKey: _navigatorKey, debugShowCheckedModeBanner: false, // Theme Configuration // themeMode: themeService.mode, themeMode: ThemeMode.light, theme: ThemeData( fontFamily: 'GeneralSans', colorScheme: ColorScheme.fromSeed( seedColor: Colors.blue, brightness: Brightness.light, ), useMaterial3: true, scaffoldBackgroundColor: Colors.white, appBarTheme: const AppBarTheme( backgroundColor: Colors.white, foregroundColor: Colors.black, surfaceTintColor: Colors.transparent, ), ), darkTheme: ThemeData( fontFamily: 'GeneralSans', colorScheme: ColorScheme.fromSeed( seedColor: Colors.blue, brightness: Brightness.dark, ), useMaterial3: true, scaffoldBackgroundColor: Colors.grey[900], appBarTheme: AppBarTheme( backgroundColor: Colors.grey[900], foregroundColor: Colors.white, surfaceTintColor: Colors.transparent, ), ), home: const HomePage(), ); } } |