From db618099398ebc2dfd005ecba8f20a13b15fc9d3 Mon Sep 17 00:00:00 2001 From: marco Date: Mon, 3 Nov 2025 13:29:51 +0100 Subject: [PATCH] cleaned up sloppy ai code --- lib/main.dart | 25 ++---- lib/model/maps_link_metadata.dart | 26 +++++++ lib/pages/collections_page.dart | 10 --- lib/service/share_intent_service.dart | 107 ++------------------------ 4 files changed, 39 insertions(+), 129 deletions(-) create mode 100644 lib/model/maps_link_metadata.dart diff --git a/lib/main.dart b/lib/main.dart index ad995de..5286f6b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -40,36 +40,21 @@ class _MapsBookmarksState extends State @override void didChangeAppLifecycleState(AppLifecycleState state) { - // Check for shared content when app resumes if (state == AppLifecycleState.resumed) { _checkForSharedContent(); } } Future _checkForSharedContent() async { - final sharedText = await _shareIntentService.getSharedText(); + final sharedText = await _shareIntentService.getSharedMapsLink(); - if (sharedText != null && mounted) { - // Validate it's a Google Maps link - if (_shareIntentService.isGoogleMapsLink(sharedText)) { - final metadata = _shareIntentService.extractMetadata(sharedText); - _handleSharedMapsLink(metadata); - } + if (sharedText.isNotEmpty && mounted) { + _handleSharedMapsLink(sharedText); } } - void _handleSharedMapsLink(MapsLinkMetadata metadata) { - // Navigate to the appropriate page or show a dialog - // You can customize this based on your app's flow - - // Option 1: Navigate to collections page with data - _navigatorKey.currentState?.pushNamed( - CollectionsPage.routeName, - arguments: metadata, - ); - - // Option 2: Show a dialog to select collection and save - // This would be implemented based on your UI requirements + void _handleSharedMapsLink(String sharedText) { + //TODO: implement } @override diff --git a/lib/model/maps_link_metadata.dart b/lib/model/maps_link_metadata.dart new file mode 100644 index 0000000..a255851 --- /dev/null +++ b/lib/model/maps_link_metadata.dart @@ -0,0 +1,26 @@ +class MapsLinkMetadata { + final String url; + final String placeName; + final String latitude; + final String longitude; + final String address; + final String description; + + const MapsLinkMetadata({ + required this.url, + this.placeName = '', + this.latitude = '', + this.longitude = '', + this.address = '', + this.description = '', + }); + + String get coordinates { + if (hasCoordinates) { + return '$latitude, $longitude'; + } + return ''; + } + + bool get hasCoordinates => latitude.isNotEmpty && longitude.isNotEmpty; +} diff --git a/lib/pages/collections_page.dart b/lib/pages/collections_page.dart index 403b7b5..b90c0ab 100644 --- a/lib/pages/collections_page.dart +++ b/lib/pages/collections_page.dart @@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; import '../model/collection.dart'; import '../service/bookmarks_provider.dart'; -import '../service/share_intent_service.dart'; import '../service/storage.dart'; import '../widgets/create_bookmark_collection_dialog.dart'; import 'bookmarks_page.dart'; @@ -59,14 +58,5 @@ class _CollectionsPageState extends State { @override void didChangeDependencies() { super.didChangeDependencies(); - // Check if we received shared data - final metadata = - ModalRoute.of(context)?.settings.arguments as MapsLinkMetadata?; - if (metadata != null) { - // Handle the shared link - WidgetsBinding.instance.addPostFrameCallback((_) { - // _showSaveBookmarkDialog(metadata); - }); - } } } diff --git a/lib/service/share_intent_service.dart b/lib/service/share_intent_service.dart index 9e26a17..0e6acdc 100644 --- a/lib/service/share_intent_service.dart +++ b/lib/service/share_intent_service.dart @@ -1,10 +1,6 @@ import 'package:flutter/services.dart'; class ShareIntentService { - static const _platform = MethodChannel('app.channel.shared.data'); - static ShareIntentService? _instance; - - // Singleton pattern factory ShareIntentService() { _instance ??= ShareIntentService._internal(); return _instance!; @@ -12,105 +8,18 @@ class ShareIntentService { ShareIntentService._internal(); - /// Retrieves shared text from the platform channel - /// Returns null if no shared text is available - Future getSharedText() async { - try { - final String? sharedText = await _platform.invokeMethod('getSharedText'); - return sharedText; - } on PlatformException catch (e) { - // Log error in production app - print('Failed to get shared text: ${e.message}'); - return null; - } + static ShareIntentService? _instance; + static const _platform = MethodChannel('app.channel.shared.data'); + + Future getSharedMapsLink() async { + final String? sharedText = await _platform.invokeMethod('getSharedText'); + if (sharedText != null && _isGoogleMapsLink(sharedText)) return sharedText; + return ''; } - /// Checks if the text is a Google Maps link - bool isGoogleMapsLink(String text) { + bool _isGoogleMapsLink(String text) { return text.contains('maps.google.com') || text.contains('maps.app.goo.gl') || text.contains('goo.gl/maps'); } - - /// Extracts metadata from a Google Maps link - MapsLinkMetadata extractMetadata(String mapsLink) { - String? placeName; - String? latitude; - String? longitude; - String? address; - - // Extract place name from URL - final placeMatch = RegExp(r'/place/([^/]+)').firstMatch(mapsLink); - if (placeMatch != null) { - placeName = Uri.decodeComponent( - placeMatch.group(1)!, - ).replaceAll('+', ' '); - } - - // Extract coordinates - final coordMatch = RegExp( - r'@(-?\d+\.\d+),(-?\d+\.\d+)', - ).firstMatch(mapsLink); - if (coordMatch != null) { - latitude = coordMatch.group(1); - longitude = coordMatch.group(2); - } - - // Extract search query/address - final searchMatch = RegExp(r'/search/([^/?]+)').firstMatch(mapsLink); - if (searchMatch != null) { - address = Uri.decodeComponent(searchMatch.group(1)!).replaceAll('+', ' '); - // Use address as place name if name not found - placeName ??= address; - } - - // Extract from data parameter (alternative format) - final dataMatch = RegExp(r'[?&]q=([^&]+)').firstMatch(mapsLink); - if (dataMatch != null && placeName == null) { - final query = Uri.decodeComponent(dataMatch.group(1)!); - // Check if it's coordinates - final coordPattern = RegExp(r'^(-?\d+\.\d+),(-?\d+\.\d+)$'); - final coords = coordPattern.firstMatch(query); - if (coords != null) { - latitude ??= coords.group(1); - longitude ??= coords.group(2); - } else { - placeName = query.replaceAll('+', ' '); - } - } - - return MapsLinkMetadata( - url: mapsLink, - placeName: placeName ?? 'Unknown Location', - latitude: latitude, - longitude: longitude, - address: address, - ); - } -} - -/// Data class for Google Maps link metadata -class MapsLinkMetadata { - final String url; - final String placeName; - final String? latitude; - final String? longitude; - final String? address; - - const MapsLinkMetadata({ - required this.url, - required this.placeName, - this.latitude, - this.longitude, - this.address, - }); - - String get coordinates { - if (latitude != null && longitude != null) { - return '$latitude, $longitude'; - } - return ''; - } - - bool get hasCoordinates => latitude != null && longitude != null; }