cleaned up sloppy ai code

This commit is contained in:
2025-11-03 13:29:51 +01:00
parent be171aa4bc
commit db61809939
4 changed files with 39 additions and 129 deletions

View File

@@ -40,36 +40,21 @@ class _MapsBookmarksState extends State<MapsBookmarks>
@override @override
void didChangeAppLifecycleState(AppLifecycleState state) { void didChangeAppLifecycleState(AppLifecycleState state) {
// Check for shared content when app resumes
if (state == AppLifecycleState.resumed) { if (state == AppLifecycleState.resumed) {
_checkForSharedContent(); _checkForSharedContent();
} }
} }
Future<void> _checkForSharedContent() async { Future<void> _checkForSharedContent() async {
final sharedText = await _shareIntentService.getSharedText(); final sharedText = await _shareIntentService.getSharedMapsLink();
if (sharedText != null && mounted) { if (sharedText.isNotEmpty && mounted) {
// Validate it's a Google Maps link _handleSharedMapsLink(sharedText);
if (_shareIntentService.isGoogleMapsLink(sharedText)) {
final metadata = _shareIntentService.extractMetadata(sharedText);
_handleSharedMapsLink(metadata);
}
} }
} }
void _handleSharedMapsLink(MapsLinkMetadata metadata) { void _handleSharedMapsLink(String sharedText) {
// Navigate to the appropriate page or show a dialog //TODO: implement
// 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
} }
@override @override

View File

@@ -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;
}

View File

@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import '../model/collection.dart'; import '../model/collection.dart';
import '../service/bookmarks_provider.dart'; import '../service/bookmarks_provider.dart';
import '../service/share_intent_service.dart';
import '../service/storage.dart'; import '../service/storage.dart';
import '../widgets/create_bookmark_collection_dialog.dart'; import '../widgets/create_bookmark_collection_dialog.dart';
import 'bookmarks_page.dart'; import 'bookmarks_page.dart';
@@ -59,14 +58,5 @@ class _CollectionsPageState extends State<CollectionsPage> {
@override @override
void didChangeDependencies() { void didChangeDependencies() {
super.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);
});
}
} }
} }

View File

@@ -1,10 +1,6 @@
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
class ShareIntentService { class ShareIntentService {
static const _platform = MethodChannel('app.channel.shared.data');
static ShareIntentService? _instance;
// Singleton pattern
factory ShareIntentService() { factory ShareIntentService() {
_instance ??= ShareIntentService._internal(); _instance ??= ShareIntentService._internal();
return _instance!; return _instance!;
@@ -12,105 +8,18 @@ class ShareIntentService {
ShareIntentService._internal(); ShareIntentService._internal();
/// Retrieves shared text from the platform channel static ShareIntentService? _instance;
/// Returns null if no shared text is available static const _platform = MethodChannel('app.channel.shared.data');
Future<String?> getSharedText() async {
try { Future<String> getSharedMapsLink() async {
final String? sharedText = await _platform.invokeMethod('getSharedText'); final String? sharedText = await _platform.invokeMethod('getSharedText');
return sharedText; if (sharedText != null && _isGoogleMapsLink(sharedText)) return sharedText;
} on PlatformException catch (e) { return '';
// Log error in production app
print('Failed to get shared text: ${e.message}');
return null;
}
} }
/// Checks if the text is a Google Maps link bool _isGoogleMapsLink(String text) {
bool isGoogleMapsLink(String text) {
return text.contains('maps.google.com') || return text.contains('maps.google.com') ||
text.contains('maps.app.goo.gl') || text.contains('maps.app.goo.gl') ||
text.contains('goo.gl/maps'); 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;
} }