Skip to content

Commit

Permalink
refactor: Use UniLinksHandler.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 11, 2023
1 parent e4a0d13 commit e7cd821
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 58 deletions.
119 changes: 119 additions & 0 deletions lib/app_wrappers/UniLinksHandler.dart
Original file line number Diff line number Diff line change
@@ -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<UniLinksHandler> createState() => _UniLinksHandlerState();
}

class _UniLinksHandlerState extends State<UniLinksHandler> {
late final StreamSubscription<String?> _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<void> _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();
}
}
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -98,6 +99,7 @@ void main() async {
child: const Stack(
children: [
LocationHistoryUpdater(),
UniLinksHandler(),
App(),
],
),
Expand Down
58 changes: 0 additions & 58 deletions lib/screens/LocationsOverviewScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -111,7 +109,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
LocationPointService? visibleLocation;

Position? lastPosition;
StreamSubscription<String?>? _uniLinksStream;
Timer? _viewsAlarmCheckerTimer;
LocationStatus locationStatus = LocationStatus.stale;

Expand Down Expand Up @@ -154,7 +151,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
..addPostFrameCallback((_) {
_setLocationFromSettings();
initQuickActions(context);
_initUniLinks();
_updateLocaleToSettings();
_updateBackgroundListeners();
_showUpdateDialogIfRequired();
Expand Down Expand Up @@ -205,7 +201,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
flutterMapController?.dispose();

_viewsAlarmCheckerTimer?.cancel();
_uniLinksStream?.cancel();
mapEventStream.close();

_removeLiveLocationUpdate();
Expand Down Expand Up @@ -425,59 +420,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
_positionStream = null;
}

Future<void> _importUniLink(final String url) => showPlatformModalSheet(
context: context,
material: MaterialModalSheetData(
isScrollControlled: true,
isDismissible: true,
backgroundColor: Colors.transparent,
),
builder: (context) => ImportTaskSheet(initialURL: url),
);

Future<void> _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),
Expand Down

0 comments on commit e7cd821

Please sign in to comment.