Repository navigation
Expand file tree
/
Copy pathrouter.dart
More file actions
247 lines (236 loc) · 7.52 KB
/
Copy pathrouter.dart
File metadata and controls
247 lines (236 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:open_earable_flutter/open_earable_flutter.dart';
import 'package:open_wearable/models/app_upgrade_registry.dart';
import 'package:open_wearable/widgets/devices/connect_devices_page.dart';
import 'package:open_wearable/widgets/devices/device_detail/device_detail_page.dart';
import 'package:open_wearable/widgets/fota/firmware_update.dart';
import 'package:open_wearable/widgets/fota/fota_slots_page.dart';
import 'package:open_wearable/widgets/fota/fota_warning_page.dart';
import 'package:open_wearable/widgets/home_page.dart';
import 'package:open_wearable/widgets/logging/log_files_screen.dart';
import 'package:open_wearable/widgets/sensors/local_recorder/local_recorder_all_recordings_page.dart';
import 'package:open_wearable/widgets/settings/connectors_page.dart';
import 'package:open_wearable/widgets/settings/general_settings_page.dart';
import 'package:open_wearable/widgets/startup/startup_loading_page.dart';
import 'package:open_wearable/widgets/updates/app_upgrade_history_page.dart';
import 'dart:io' show Platform;
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:open_wearable/widgets/updates/app_upgrade_page.dart';
/// Global navigator key for go_router
final GlobalKey<NavigatorState> rootNavigatorKey = GlobalKey<NavigatorState>();
VoidCallback? startupRouteReadyCallback;
bool _unsupportedFotaDialogVisible = false;
void _showUnsupportedFotaDialog() {
if (_unsupportedFotaDialogVisible) {
return;
}
_unsupportedFotaDialogVisible = true;
WidgetsBinding.instance.addPostFrameCallback((_) async {
final ctx = rootNavigatorKey.currentContext;
if (ctx == null) {
_unsupportedFotaDialogVisible = false;
return;
}
try {
await showPlatformDialog<void>(
context: ctx,
builder: (_) => PlatformAlertDialog(
title: PlatformText('Firmware Update'),
content: PlatformText(
'Firmware update is not supported on this platform. '
'Please use an Android device or J-Link to update the firmware.',
),
actions: <Widget>[
PlatformDialogAction(
cupertino: (_, __) =>
CupertinoDialogActionData(isDefaultAction: true),
child: PlatformText('OK'),
onPressed: () => Navigator.of(ctx).pop(),
),
],
),
);
} finally {
_unsupportedFotaDialogVisible = false;
}
});
}
int _parseHomeSectionIndex(String? tabParam) {
if (tabParam == null || tabParam.isEmpty) {
return 0;
}
switch (tabParam.toLowerCase()) {
case 'overview':
return 0;
case 'devices':
return 1;
case 'sensors':
return 2;
case 'apps':
return 3;
case 'settings':
return 4;
default:
final parsed = int.tryParse(tabParam);
if (parsed == null || parsed < 0 || parsed > 4) {
return 0;
}
return parsed;
}
}
/// Router configuration for the app
final GoRouter router = GoRouter(
navigatorKey: rootNavigatorKey,
initialLocation: '/startup',
routes: [
GoRoute(
path: '/startup',
name: 'startup',
builder: (context, state) => StartupLoadingPage(
onReady: startupRouteReadyCallback,
),
),
GoRoute(
path: '/',
name: 'home',
builder: (context, state) {
final initialSection = _parseHomeSectionIndex(
state.uri.queryParameters['tab'],
);
return HeroMode(
enabled: false,
child: HomePage(initialSectionIndex: initialSection),
);
},
),
GoRoute(
path: '/connect-devices',
name: 'connect-devices',
builder: (context, state) => const ConnectDevicesPage(),
),
GoRoute(
path: '/device-detail',
name: 'device-detail',
builder: (context, state) {
if (state.extra == null || state.extra is! Wearable) {
// Fallback to home if device is not provided
return const HomePage();
}
final device = state.extra as Wearable;
return DeviceDetailPage(device: device);
},
),
GoRoute(
path: '/log-files',
name: 'log-files',
builder: (context, state) => const LogFilesScreen(),
),
GoRoute(
path: '/recordings',
name: 'recordings',
builder: (context, state) {
final isRecording = state.extra is bool ? state.extra as bool : false;
return LocalRecorderAllRecordingsPage(isRecording: isRecording);
},
),
GoRoute(
path: '/settings/general',
name: 'settings/general',
builder: (context, state) => const GeneralSettingsPage(),
),
GoRoute(
path: '/settings/connectors',
name: 'settings/connectors',
redirect: (_, __) => kIsWeb ? '/settings/general' : null,
builder: (context, state) => const ConnectorsPage(),
),
GoRoute(
path: '/whats-new',
name: 'whats-new',
builder: (context, state) {
final String? version = state.uri.queryParameters['version'];
if (version == null || version.isEmpty) {
return const AppUpgradeHistoryPage();
}
final highlight = AppUpgradeRegistry.forVersion(version);
if (highlight == null) {
return const AppUpgradeHistoryPage();
}
return AppUpgradePage(highlight: highlight);
},
),
GoRoute(
path: '/settings/app-close',
redirect: (_, __) => '/settings/general',
),
GoRoute(
path: '/connectors',
redirect: (_, __) =>
kIsWeb ? '/settings/general' : '/settings/connectors',
),
GoRoute(
path: '/fota',
name: 'fota',
redirect: (context, state) {
final bool isAndroid = !kIsWeb && Platform.isAndroid;
final bool isIOS = !kIsWeb && Platform.isIOS;
if (!isAndroid && !isIOS) {
_showUnsupportedFotaDialog();
return '/?tab=devices';
}
return null;
},
builder: (context, state) => const FotaWarningPage(),
),
GoRoute(
path: '/fota/update',
name: 'fota/update',
pageBuilder: (context, state) => CustomTransitionPage<void>(
key: state.pageKey,
transitionDuration: const Duration(milliseconds: 220),
reverseTransitionDuration: const Duration(milliseconds: 180),
child: const FirmwareUpdateWidget(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
final curved = CurvedAnimation(
parent: animation,
curve: Curves.easeOutCubic,
reverseCurve: Curves.easeInCubic,
);
final slideAnimation = Tween<Offset>(
begin: const Offset(0.06, 0),
end: Offset.zero,
).animate(curved);
return FadeTransition(
opacity: curved,
child: SlideTransition(position: slideAnimation, child: child),
);
},
),
),
GoRoute(
path: '/fota/slots',
name: 'fota/slots',
builder: (context, state) {
final device = state.extra;
if (device is! Wearable) {
return const HomePage();
}
return FotaSlotsPage(device: device);
},
),
GoRoute(
path: '/view',
name: 'view',
builder: (context, state) {
final extra = state.extra;
if (extra is Widget) {
return extra;
}
return const HomePage();
},
),
],
);