diff --git a/lib/app_wrappers/UniLinksHandler.dart b/lib/app_wrappers/UniLinksHandler.dart new file mode 100644 index 0000000..6f84639 --- /dev/null +++ b/lib/app_wrappers/UniLinksHandler.dart @@ -0,0 +1,119 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_logs/flutter_logs.dart'; +import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'; +import 'package:locus/screens/ImportTaskSheet.dart'; +import 'package:uni_links/uni_links.dart'; + +// l10n +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; + +import '../constants/values.dart'; + +class UniLinksHandler extends StatefulWidget { + const UniLinksHandler({super.key}); + + @override + State createState() => _UniLinksHandlerState(); +} + +class _UniLinksHandlerState extends State { + late final StreamSubscription _stream; + + @override + void initState() { + super.initState(); + + FlutterLogs.logInfo( + LOG_TAG, + "Uni Links", + "Initiating uni links...", + ); + _stream = linkStream.listen((final String? link) { + if (link != null) { + _importLink(link); + } + }); + + _initInitialLink(); + } + + @override + void dispose() { + _stream.cancel(); + super.dispose(); + } + + Future _importLink(final String url) { + FlutterLogs.logInfo( + LOG_TAG, + "Uni Links", + "Importing new uni link", + ); + + return showPlatformModalSheet( + context: context, + material: MaterialModalSheetData( + isScrollControlled: true, + isDismissible: true, + backgroundColor: Colors.transparent, + ), + builder: (context) => ImportTaskSheet(initialURL: url), + ); + } + + void _initInitialLink() async { + final l10n = AppLocalizations.of(context); + + FlutterLogs.logInfo( + LOG_TAG, + "Uni Links", + "Checking initial link", + ); + + try { + // Only fired when the app was in background + final initialLink = await getInitialLink(); + + if (initialLink == null) { + FlutterLogs.logInfo( + LOG_TAG, + "Uni Links", + "----> but it is null, so skipping it.", + ); + return; + } + + await _importLink(initialLink); + } on PlatformException catch (error) { + FlutterLogs.logError( + LOG_TAG, + "Uni Links", + "Error initializing uni links: $error", + ); + + showPlatformDialog( + context: context, + builder: (_) => PlatformAlertDialog( + title: Text(l10n.uniLinksOpenError), + content: Text(error.message ?? l10n.unknownError), + actions: [ + PlatformDialogAction( + child: Text(l10n.closeNeutralAction), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ], + ), + ); + } + } + + @override + Widget build(BuildContext context) { + return const SizedBox.shrink(); + } +} diff --git a/lib/main.dart b/lib/main.dart index 11fbdbc..e0c9690 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -8,6 +8,7 @@ import 'package:flutter_logs/flutter_logs.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:locus/App.dart'; import 'package:locus/app_wrappers/LocationHistoryUpdater.dart'; +import 'package:locus/app_wrappers/UniLinksHandler.dart'; import 'package:locus/screens/locations_overview_screen_widgets/LocationFetchers.dart'; import 'package:locus/services/app_update_service.dart'; import 'package:locus/services/current_location_service.dart'; @@ -98,6 +99,7 @@ void main() async { child: const Stack( children: [ LocationHistoryUpdater(), + UniLinksHandler(), App(), ], ), diff --git a/lib/screens/LocationsOverviewScreen.dart b/lib/screens/LocationsOverviewScreen.dart index d41315a..65963e2 100644 --- a/lib/screens/LocationsOverviewScreen.dart +++ b/lib/screens/LocationsOverviewScreen.dart @@ -19,7 +19,6 @@ import 'package:flutter_map_marker_popup/flutter_map_marker_popup.dart'; import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'; import 'package:geolocator/geolocator.dart'; import 'package:latlong2/latlong.dart'; -import 'package:locus/api/get-relays-meta.dart'; import 'package:locus/constants/spacing.dart'; import 'package:locus/screens/ImportTaskSheet.dart'; import 'package:locus/screens/SettingsScreen.dart'; @@ -54,7 +53,6 @@ import 'package:material_design_icons_flutter/material_design_icons_flutter.dart import 'package:modal_bottom_sheet/modal_bottom_sheet.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; -import 'package:uni_links/uni_links.dart'; import '../constants/notifications.dart'; import '../constants/values.dart'; @@ -111,7 +109,6 @@ class _LocationsOverviewScreenState extends State LocationPointService? visibleLocation; Position? lastPosition; - StreamSubscription? _uniLinksStream; Timer? _viewsAlarmCheckerTimer; LocationStatus locationStatus = LocationStatus.stale; @@ -154,7 +151,6 @@ class _LocationsOverviewScreenState extends State ..addPostFrameCallback((_) { _setLocationFromSettings(); initQuickActions(context); - _initUniLinks(); _updateLocaleToSettings(); _updateBackgroundListeners(); _showUpdateDialogIfRequired(); @@ -205,7 +201,6 @@ class _LocationsOverviewScreenState extends State flutterMapController?.dispose(); _viewsAlarmCheckerTimer?.cancel(); - _uniLinksStream?.cancel(); mapEventStream.close(); _removeLiveLocationUpdate(); @@ -425,59 +420,6 @@ class _LocationsOverviewScreenState extends State _positionStream = null; } - Future _importUniLink(final String url) => showPlatformModalSheet( - context: context, - material: MaterialModalSheetData( - isScrollControlled: true, - isDismissible: true, - backgroundColor: Colors.transparent, - ), - builder: (context) => ImportTaskSheet(initialURL: url), - ); - - Future _initUniLinks() async { - final l10n = AppLocalizations.of(context); - - FlutterLogs.logInfo(LOG_TAG, "Uni Links", "Initiating uni links..."); - - _uniLinksStream = linkStream.listen((final String? link) { - if (link != null) { - _importUniLink(link); - } - }); - - try { - // Only fired when the app was in background - final initialLink = await getInitialLink(); - - if (initialLink != null) { - await _importUniLink(initialLink); - } - } on PlatformException catch (error) { - FlutterLogs.logError( - LOG_TAG, - "Uni Links", - "Error initializing uni links: $error", - ); - - showPlatformDialog( - context: context, - builder: (_) => PlatformAlertDialog( - title: Text(l10n.uniLinksOpenError), - content: Text(error.message ?? l10n.unknownError), - actions: [ - PlatformDialogAction( - child: Text(l10n.closeNeutralAction), - onPressed: () { - Navigator.of(context).pop(); - }, - ), - ], - ), - ); - } - } - void _handleViewAlarmChecker() { _viewsAlarmCheckerTimer = Timer.periodic( const Duration(minutes: 1),