share_handler_page.dart (5475B)
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 162 163 164 165 166 167 168 169 170 | import 'dart:io'; import 'package:flutter/material.dart'; import 'package:creekui/services/download_service.dart'; import 'package:creekui/services/instagram_download_service.dart'; import 'package:creekui/services/image_service.dart'; import 'package:creekui/ui/styles/variables.dart'; import 'package:creekui/ui/widgets/primary_button.dart'; import 'package:creekui/ui/widgets/app_bar.dart'; import 'share_to_moodboard_page.dart'; import 'share_to_file_page.dart'; class ShareHandlerPage extends StatefulWidget { final String sharedText; final String destination; const ShareHandlerPage({ super.key, required this.sharedText, required this.destination, }); @override State<ShareHandlerPage> createState() => _ShareHandlerPageState(); } class _ShareHandlerPageState extends State<ShareHandlerPage> { final _downloadService = DownloadService(); final _instagramService = InstagramDownloadService(); final _imageService = ImageService(); bool _hasError = false; String? _errorMessage; @override void initState() { super.initState(); _processSharedContent(); } Future<void> _processSharedContent() async { final sharedContent = widget.sharedText.trim(); List<File> tempFiles = []; try { // CASE A: Local File Path if (await File(sharedContent).exists()) { tempFiles.add(File(sharedContent)); } // CASE B: URL else { final urlRegExp = RegExp(r'(https?://\S+)'); final match = urlRegExp.firstMatch(sharedContent); if (match != null) { final url = match.group(0)!; if (url.contains('instagram.com')) { // Instagram Logic final downloadedPaths = await _instagramService .downloadInstagramImage(url); if (downloadedPaths != null && downloadedPaths.isNotEmpty) { tempFiles.addAll(downloadedPaths.map((path) => File(path))); } } else { // Generic Download Logic final savedPath = await _downloadService.downloadAndSaveImage(url); if (savedPath != null) { tempFiles.add(File(savedPath)); } } } else { throw Exception("Invalid content: Not a valid file path or URL."); } } // Success: Route based on Destination if (tempFiles.isNotEmpty) { if (!mounted) return; if (widget.destination == 'files') { // Files Navigator.pushReplacement( context, MaterialPageRoute( builder: (_) => ShareToFilePage(sharedImage: tempFiles.first), ), ); } else { // Moodboard List<File> permanentFiles = []; for (var file in tempFiles) { final id = await _imageService.saveImage(file, 0, tags: []); final savedImage = await _imageService.getImage(id); if (savedImage != null) { permanentFiles.add(File(savedImage.filePath)); } } if (mounted) { Navigator.pushReplacement( context, MaterialPageRoute( builder: (_) => ShareToMoodboardPage(imageFiles: permanentFiles), ), ); } } } else { throw Exception("Could not retrieve any media files."); } } catch (e) { if (mounted) { setState(() { _hasError = true; _errorMessage = e.toString(); }); } } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Variables.background, appBar: const CommonAppBar(title: "Processing", showBack: false), body: Center( child: _hasError ? Padding( padding: const EdgeInsets.all(24.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon( Icons.error_outline, color: Colors.red, size: 50, ), const SizedBox(height: 16), Text( "Error processing media", style: Variables.headerStyle.copyWith(fontSize: 18), ), const SizedBox(height: 8), Text( _errorMessage ?? "Unknown Error", textAlign: TextAlign.center, style: const TextStyle(color: Colors.grey), ), const SizedBox(height: 24), SizedBox( width: 200, child: PrimaryButton( text: "Close", onPressed: () => Navigator.pop(context), ), ), ], ), ) : const Column( mainAxisAlignment: MainAxisAlignment.center, children: [ CircularProgressIndicator(color: Variables.textPrimary), SizedBox(height: 20), Text("Downloading media..."), ], ), ), ); } } |