Skip to content

Commit

Permalink
Open geo intents
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna Barsukova authored and Anna Barsukova committed Oct 30, 2024
1 parent 338dd34 commit 2dd5f5c
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 17 deletions.
9 changes: 8 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:fullBackupContent="@xml/backup_rules">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:launchMode="singleTask"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:exported="true"
Expand All @@ -30,6 +30,13 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="geo"/>
</intent-filter>
</activity>
<activity android:name="com.linusu.flutter_web_auth_2.CallbackActivity" android:exported="true">
<intent-filter android:label="flutter_web_auth_2">
Expand Down
11 changes: 11 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
<string>everydoor</string>
</array>
</dict>

<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>geo</string>
<key>CFBundleURLSchemes</key>
<array>
<string>geo</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>$(FLUTTER_BUILD_NUMBER)</string>
Expand Down
11 changes: 11 additions & 0 deletions lib/helpers/navigation_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:every_door/screens/browser.dart';
import 'package:flutter/material.dart';

class NavigationHelper {
static Widget navigateByUri(Uri uri) {
if (uri.scheme == 'geo' && uri.path.isNotEmpty) {
return BrowserPage();
}
return BrowserPage();
}
}
50 changes: 35 additions & 15 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import 'dart:async';
import 'dart:io';

import 'package:every_door/helpers/log_store.dart';
import 'package:every_door/helpers/navigation_helper.dart';
import 'package:every_door/providers/app_links_provider.dart';
import 'package:every_door/providers/language.dart';
import 'package:every_door/providers/navigation_provider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:every_door/constants.dart';
Expand Down Expand Up @@ -44,23 +47,40 @@ class EveryDoorApp extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final navigatorKey = ref.read(navigatonKeyProvider);
final uriAsyncValue = ref.watch(uriLinkStreamProvider);

WidgetsBinding.instance.addPostFrameCallback((_) {
uriAsyncValue.whenData((uri) {
if (uri != null) {
final screen = NavigationHelper.navigateByUri(uri);
if (context.mounted) {
navigatorKey.currentState?.push(
MaterialPageRoute(builder: (context) => screen),
);
}
}
});
});

return Portal(
child: MaterialApp(
title: kAppTitle,
theme: ThemeData(
primarySwatch: Colors.blue,
hintColor: Theme.of(context).colorScheme.onSurface.withOpacity(0.3),
useMaterial3: false,
),
localizationsDelegates: AppLocalizations.localizationsDelegates,
// Adding "en" to the front so it's used by default.
supportedLocales: [Locale('en')] + AppLocalizations.supportedLocales,
locale: ref.watch(languageProvider),
home: LoadingPage(),
builder: (context, child) => Stack(children: [
if (child != null) child,
DropdownAlert(delayDismiss: 5000),
]),
navigatorKey: navigatorKey,
title: kAppTitle,
theme: ThemeData(
primarySwatch: Colors.blue,
hintColor: Theme.of(context).colorScheme.onSurface.withOpacity(0.3),
useMaterial3: false,
),
localizationsDelegates: AppLocalizations.localizationsDelegates,
// Adding "en" to the front so it's used by default.
supportedLocales: [Locale('en')] + AppLocalizations.supportedLocales,
locale: ref.watch(languageProvider),
home: LoadingPage(),
builder: (context, child) => Stack(children: [
if (child != null) child,
DropdownAlert(delayDismiss: 5000),
]),
),
);
}
Expand Down
41 changes: 41 additions & 0 deletions lib/providers/app_links_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:every_door/providers/geolocation.dart';
import 'package:every_door/providers/location.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:app_links/app_links.dart';
import 'package:latlong2/latlong.dart';
import 'package:logging/logging.dart';

final appLinksProvider = Provider((ref) => AppLinks());

final uriLinkStreamProvider = StreamProvider<Uri?>((ref) async* {
final appLinks = ref.watch(appLinksProvider);
await for (final uri in appLinks.uriLinkStream) {
if (uri.scheme == 'geo' && uri.path.isNotEmpty) {
_handleGeoIntent(uri, ref);
}

yield uri;
}
});

void _handleGeoIntent(Uri uri, StreamProviderRef<Uri?> ref) {
final location = _parseLatLngFromGeoUri(uri);
if (location != null) {
ref.read(geolocationProvider.notifier).disableTracking();
ref.read(effectiveLocationProvider.notifier).set(location);
}
}

LatLng? _parseLatLngFromGeoUri(Uri uri) {
try {
final coords = uri.path.split(',');
if (coords.length == 2) {
final lat = double.parse(coords[0]);
final lng = double.parse(coords[1]);
return LatLng(lat, lng);
}
} catch (e) {
Logger('AppLinks').warning('Failed to parse coordinates');
}
return null;
}
6 changes: 6 additions & 0 deletions lib/providers/navigation_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final navigatonKeyProvider = Provider<GlobalKey<NavigatorState>>((ref) {
return GlobalKey<NavigatorState>();
});
2 changes: 1 addition & 1 deletion lib/screens/browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class _BrowserPageState extends ConsumerState<BrowserPage> {
final location = ref.read(effectiveLocationProvider);
final bbox = boundsFromRadius(location, kVisibilityRadius);
final status = await area.getAreaStatus(bbox);
if (status != areaStatus) {
if (status != areaStatus && mounted) {
setState(() {
areaStatus = status;
});
Expand Down
40 changes: 40 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.11"
app_links:
dependency: "direct main"
description:
name: app_links
sha256: ad1a6d598e7e39b46a34f746f9a8b011ee147e4c275d407fa457e7a62f84dd99
url: "https://pub.dev"
source: hosted
version: "6.3.2"
app_links_linux:
dependency: transitive
description:
name: app_links_linux
sha256: f5f7173a78609f3dfd4c2ff2c95bd559ab43c80a87dc6a095921d96c05688c81
url: "https://pub.dev"
source: hosted
version: "1.0.3"
app_links_platform_interface:
dependency: transitive
description:
name: app_links_platform_interface
sha256: "05f5379577c513b534a29ddea68176a4d4802c46180ee8e2e966257158772a3f"
url: "https://pub.dev"
source: hosted
version: "2.0.2"
app_links_web:
dependency: transitive
description:
name: app_links_web
sha256: af060ed76183f9e2b87510a9480e56a5352b6c249778d07bd2c95fc35632a555
url: "https://pub.dev"
source: hosted
version: "1.0.4"
appkit_ui_element_colors:
dependency: transitive
description:
Expand Down Expand Up @@ -499,6 +531,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.1"
gtk:
dependency: transitive
description:
name: gtk
sha256: e8ce9ca4b1df106e4d72dad201d345ea1a036cc12c360f1a7d5a758f78ffa42c
url: "https://pub.dev"
source: hosted
version: "2.1.0"
http:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies:
url_launcher: ^6.1.14
uuid: ^4.3.3
xml: ^6.3.0
app_links: ^6.3.2

dependency_overrides:
flutter_dropdown_alert:
Expand Down

0 comments on commit 2dd5f5c

Please sign in to comment.