Skip to content

Commit 83f15ef

Browse files
committed
fix(android): fix deep-link and link sharing
1 parent 83be483 commit 83f15ef

File tree

6 files changed

+167
-67
lines changed

6 files changed

+167
-67
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ ios/Flutter/flutter_export_environment.sh
1414
ios/Flutter/Generated.xcconfig
1515
lib/Screens/LivetimingArchive/livetiming.md
1616
/android/app/.cxx
17+
adb_test_commands.md

lib/Screens/racehub.dart

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,32 +130,54 @@ class RaceHubScreen extends StatelessWidget {
130130
),
131131
),
132132
),
133-
child: Hover(
134-
isRaceHubSession: true,
135-
builder: (isHovered) => Card(
136-
elevation: 5,
137-
color: isHovered
138-
? Theme.of(context)
139-
.colorScheme
140-
.onSecondary
141-
: null,
142-
child: ListTile(
143-
title: Text(
144-
snapshot.data![index - 1].name,
145-
),
146-
subtitle: Text(
147-
snapshot
148-
.data![index - 1].postedDate,
149-
style: TextStyle(
150-
fontSize: 12,
133+
child: kIsWeb
134+
? Hover(
135+
isRaceHubSession: true,
136+
builder: (isHovered) => Card(
137+
elevation: 5,
138+
color: isHovered
139+
? Theme.of(context)
140+
.colorScheme
141+
.onSecondary
142+
: null,
143+
child: ListTile(
144+
title: Text(
145+
snapshot
146+
.data![index - 1].name,
147+
),
148+
subtitle: Text(
149+
snapshot.data![index - 1]
150+
.postedDate,
151+
style: TextStyle(
152+
fontSize: 12,
153+
),
154+
),
155+
leading: Icon(
156+
Icons
157+
.picture_as_pdf_outlined,
158+
),
159+
),
160+
),
161+
)
162+
: Card(
163+
elevation: 5,
164+
child: ListTile(
165+
title: Text(
166+
snapshot
167+
.data![index - 1].name,
168+
),
169+
subtitle: Text(
170+
snapshot.data![index - 1]
171+
.postedDate,
172+
style: TextStyle(
173+
fontSize: 12,
174+
),
175+
),
176+
leading: Icon(
177+
Icons.picture_as_pdf_outlined,
178+
),
151179
),
152180
),
153-
leading: Icon(
154-
Icons.picture_as_pdf_outlined,
155-
),
156-
),
157-
),
158-
),
159181
),
160182
)
161183
: Padding(

lib/config/router.dart

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,32 @@ import 'package:go_router/go_router.dart';
4545

4646
class RouterLocalConfig {
4747
static final router = GoRouter(
48+
redirect: (context, state) {
49+
String url = state.uri.toString();
50+
if (url.startsWith('/')) {
51+
url = url.replaceFirst('/', '');
52+
}
53+
if (url.startsWith('https://www.formula1.com') ||
54+
url.startsWith('https://formula1.com')) {
55+
url = url
56+
.replaceAll('https://www.formula1.com', '')
57+
.replaceAll('https://formula1.com', '')
58+
.replaceAll('.html', '');
59+
if (url.startsWith('/en/latest/article/') ||
60+
url.startsWith('/en/latest/article.')) {
61+
return '/article/${url.split('.').last}';
62+
} else if (url.startsWith('/en/video/') ||
63+
url.startsWith('/en/latest/video.')) {
64+
return '/video/${url.split('.').last}';
65+
}
66+
}
67+
return null;
68+
},
4869
errorBuilder: (context, state) {
49-
String url = state.uri.toString().replaceFirst('/', '');
70+
String url = state.uri.toString();
71+
if (url.startsWith('/')) {
72+
url = url.replaceFirst('/', '');
73+
}
5074
if (url.startsWith('https://www.formula1.com') ||
5175
url.startsWith('https://formula1.com')) {
5276
return SharedLinkHandler(url);

lib/helpers/handle_native.dart

Lines changed: 0 additions & 29 deletions
This file was deleted.

lib/helpers/route_handler.dart

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,101 @@ import 'package:boxbox/Screens/404.dart';
2121
import 'package:boxbox/Screens/schedule.dart';
2222
import 'package:boxbox/Screens/standings.dart';
2323
import 'package:boxbox/Screens/videos.dart';
24+
import 'package:boxbox/config/router.dart';
2425
import 'package:flutter/material.dart';
2526
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2627
import 'package:go_router/go_router.dart';
2728

2829
class HandleRoute {
29-
static Route? handleRoute(String? url) {
30-
if (url == null) return null;
31-
32-
return PageRouteBuilder(
33-
opaque: false,
34-
pageBuilder: (_, __, ___) => SharedLinkHandler(url),
35-
);
30+
void handleRoute(String? sharedUrl, BuildContext context) {
31+
if (sharedUrl == null) return null;
32+
int year = DateTime.now().year;
33+
String url = sharedUrl
34+
.replaceAll('https://www.formula1.com', '')
35+
.replaceAll('https://formula1.com', '')
36+
.replaceAll('.html', '');
37+
if (url.endsWith('/en') || url == '/en/latest/all') {
38+
return null;
39+
} else if (url == '/en/video') {
40+
Navigator.push(
41+
context,
42+
MaterialPageRoute(
43+
builder: (context) => Scaffold(
44+
appBar: AppBar(
45+
title: Text(
46+
AppLocalizations.of(context)!.videos,
47+
),
48+
backgroundColor: Theme.of(context).colorScheme.onPrimary,
49+
),
50+
body: VideosScreen(ScrollController()),
51+
),
52+
),
53+
);
54+
} else if (url.startsWith('/en/latest/article/') ||
55+
url.startsWith('/en/latest/article.')) {
56+
RouterLocalConfig.router
57+
.goNamed('article', pathParameters: {'id': url.split('.').last});
58+
} else if (url.startsWith('/en/video/') ||
59+
url.startsWith('/en/latest/video.')) {
60+
RouterLocalConfig.router
61+
.goNamed('video', pathParameters: {'id': url.split('.').last});
62+
} else if (url.startsWith('/en/racing/$year')) {
63+
Navigator.push(
64+
context,
65+
MaterialPageRoute(
66+
builder: (context) => Scaffold(
67+
appBar: AppBar(
68+
title: Text(
69+
AppLocalizations.of(context)!.schedule,
70+
),
71+
backgroundColor: Theme.of(context).colorScheme.onPrimary,
72+
),
73+
body: const ScheduleScreen(),
74+
),
75+
),
76+
);
77+
} else if (url == '/en/results/$year/drivers') {
78+
Navigator.push(
79+
context,
80+
MaterialPageRoute(
81+
builder: (context) => Scaffold(
82+
appBar: AppBar(
83+
title: Text(
84+
AppLocalizations.of(context)!.standings,
85+
),
86+
backgroundColor: Theme.of(context).colorScheme.onPrimary,
87+
),
88+
body: const StandingsScreen(),
89+
),
90+
),
91+
);
92+
} else if (url == '/en/results/$year/teams') {
93+
Navigator.push(
94+
context,
95+
MaterialPageRoute(
96+
builder: (context) => Scaffold(
97+
appBar: AppBar(
98+
title: Text(
99+
AppLocalizations.of(context)!.standings,
100+
),
101+
backgroundColor: Theme.of(context).colorScheme.onPrimary,
102+
),
103+
body: const StandingsScreen(
104+
switchToTeamStandings: true,
105+
),
106+
),
107+
),
108+
);
109+
} else {
110+
Navigator.push(
111+
context,
112+
MaterialPageRoute(
113+
builder: (context) => ErrorNotFoundScreen(
114+
route: url,
115+
),
116+
),
117+
);
118+
}
36119
}
37120
}
38121

lib/main.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import 'package:background_downloader/background_downloader.dart';
2626
import 'package:boxbox/config/router.dart';
2727
// import 'package:awesome_notifications/awesome_notifications.dart';
2828
import 'package:boxbox/helpers/constants.dart';
29-
import 'package:boxbox/helpers/handle_native.dart';
29+
import 'package:boxbox/helpers/route_handler.dart';
3030
// import 'package:boxbox/Screens/article.dart';
3131
import 'package:boxbox/helpers/team_background_color.dart';
3232
import 'package:flutter/material.dart';
@@ -294,7 +294,6 @@ class MyApp extends StatefulWidget {
294294

295295
class _MyAppState extends State<MyApp> {
296296
late StreamSubscription _intentDataStreamSubscription;
297-
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
298297

299298
@override
300299
void initState() {
@@ -308,8 +307,8 @@ class _MyAppState extends State<MyApp> {
308307
ReceiveSharingIntent.getMediaStream().listen(
309308
(List<SharedMediaFile>? value) {
310309
if (value?.isNotEmpty ?? false) {
311-
if (value![0].type == SharedMediaType.url) {
312-
handleSharedText(value[0].path, navigatorKey);
310+
if (value?[0] != null) {
311+
HandleRoute().handleRoute(value![0].path, context);
313312
}
314313
}
315314
},
@@ -322,8 +321,8 @@ class _MyAppState extends State<MyApp> {
322321
ReceiveSharingIntent.getInitialMedia().then(
323322
(List<SharedMediaFile>? value) {
324323
if (value?.isNotEmpty ?? false) {
325-
if (value![0].type == SharedMediaType.url) {
326-
handleSharedText(value[0].path, navigatorKey);
324+
if (value?[0] != null) {
325+
HandleRoute().handleRoute(value![0].path, context);
327326
}
328327
}
329328
},

0 commit comments

Comments
 (0)