initial
This commit is contained in:
56
lib/core/router/app_router.dart
Normal file
56
lib/core/router/app_router.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hiddify/core/router/routes/routes.dart';
|
||||
import 'package:hiddify/services/deep_link_service.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'app_router.g.dart';
|
||||
|
||||
// TODO: test and improve handling of deep link
|
||||
@riverpod
|
||||
GoRouter router(RouterRef ref) {
|
||||
final deepLink = ref.listen(
|
||||
deepLinkServiceProvider,
|
||||
(_, next) async {
|
||||
if (next case AsyncData(value: final link?)) {
|
||||
await ref.state.push(
|
||||
NewProfileRoute(url: link.url, name: link.name).location,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
final initialLink = deepLink.read();
|
||||
String initialLocation = HomeRoute.path;
|
||||
if (initialLink case AsyncData(value: final link?)) {
|
||||
initialLocation = NewProfileRoute(url: link.url, name: link.name).location;
|
||||
}
|
||||
|
||||
return GoRouter(
|
||||
navigatorKey: rootNavigatorKey,
|
||||
initialLocation: initialLocation,
|
||||
debugLogDiagnostics: true,
|
||||
routes: $routes,
|
||||
);
|
||||
}
|
||||
|
||||
int getCurrentIndex(BuildContext context) {
|
||||
final String location = GoRouterState.of(context).location;
|
||||
if (location == HomeRoute.path) return 0;
|
||||
if (location.startsWith(ProxiesRoute.path)) return 1;
|
||||
if (location.startsWith(LogsRoute.path)) return 2;
|
||||
if (location.startsWith(SettingsRoute.path)) return 3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void switchTab(int index, BuildContext context) {
|
||||
switch (index) {
|
||||
case 0:
|
||||
const HomeRoute().go(context);
|
||||
case 1:
|
||||
const ProxiesRoute().go(context);
|
||||
case 2:
|
||||
const LogsRoute().go(context);
|
||||
case 3:
|
||||
const SettingsRoute().go(context);
|
||||
}
|
||||
}
|
||||
2
lib/core/router/router.dart
Normal file
2
lib/core/router/router.dart
Normal file
@@ -0,0 +1,2 @@
|
||||
export 'app_router.dart';
|
||||
export 'routes/routes.dart';
|
||||
52
lib/core/router/routes/desktop_routes.dart
Normal file
52
lib/core/router/routes/desktop_routes.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hiddify/core/router/routes/shared_routes.dart';
|
||||
import 'package:hiddify/features/logs/view/view.dart';
|
||||
import 'package:hiddify/features/settings/view/view.dart';
|
||||
import 'package:hiddify/features/wrapper/wrapper.dart';
|
||||
|
||||
part 'desktop_routes.g.dart';
|
||||
|
||||
@TypedShellRoute<DesktopWrapperRoute>(
|
||||
routes: [
|
||||
TypedGoRoute<HomeRoute>(
|
||||
path: HomeRoute.path,
|
||||
routes: [
|
||||
TypedGoRoute<ProfilesRoute>(path: ProfilesRoute.path),
|
||||
TypedGoRoute<NewProfileRoute>(path: NewProfileRoute.path),
|
||||
TypedGoRoute<ProfileDetailsRoute>(path: ProfileDetailsRoute.path),
|
||||
],
|
||||
),
|
||||
TypedGoRoute<ProxiesRoute>(path: ProxiesRoute.path),
|
||||
TypedGoRoute<LogsRoute>(path: LogsRoute.path),
|
||||
TypedGoRoute<SettingsRoute>(path: SettingsRoute.path),
|
||||
],
|
||||
)
|
||||
class DesktopWrapperRoute extends ShellRouteData {
|
||||
const DesktopWrapperRoute();
|
||||
|
||||
@override
|
||||
Widget builder(BuildContext context, GoRouterState state, Widget navigator) {
|
||||
return DesktopWrapper(navigator);
|
||||
}
|
||||
}
|
||||
|
||||
class LogsRoute extends GoRouteData {
|
||||
const LogsRoute();
|
||||
static const path = '/logs';
|
||||
|
||||
@override
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return const NoTransitionPage(child: LogsPage());
|
||||
}
|
||||
}
|
||||
|
||||
class SettingsRoute extends GoRouteData {
|
||||
const SettingsRoute();
|
||||
static const path = '/settings';
|
||||
|
||||
@override
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return const NoTransitionPage(child: SettingsPage());
|
||||
}
|
||||
}
|
||||
62
lib/core/router/routes/mobile_routes.dart
Normal file
62
lib/core/router/routes/mobile_routes.dart
Normal file
@@ -0,0 +1,62 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hiddify/core/router/routes/shared_routes.dart';
|
||||
import 'package:hiddify/features/logs/view/view.dart';
|
||||
import 'package:hiddify/features/settings/view/view.dart';
|
||||
import 'package:hiddify/features/wrapper/wrapper.dart';
|
||||
|
||||
part 'mobile_routes.g.dart';
|
||||
|
||||
@TypedShellRoute<MobileWrapperRoute>(
|
||||
routes: [
|
||||
TypedGoRoute<HomeRoute>(
|
||||
path: HomeRoute.path,
|
||||
routes: [
|
||||
TypedGoRoute<ProfilesRoute>(path: ProfilesRoute.path),
|
||||
TypedGoRoute<NewProfileRoute>(path: NewProfileRoute.path),
|
||||
TypedGoRoute<ProfileDetailsRoute>(path: ProfileDetailsRoute.path),
|
||||
],
|
||||
),
|
||||
TypedGoRoute<ProxiesRoute>(path: ProxiesRoute.path),
|
||||
],
|
||||
)
|
||||
class MobileWrapperRoute extends ShellRouteData {
|
||||
const MobileWrapperRoute();
|
||||
|
||||
@override
|
||||
Widget builder(BuildContext context, GoRouterState state, Widget navigator) {
|
||||
return MobileWrapper(navigator);
|
||||
}
|
||||
}
|
||||
|
||||
@TypedGoRoute<LogsRoute>(path: LogsRoute.path)
|
||||
class LogsRoute extends GoRouteData {
|
||||
const LogsRoute();
|
||||
static const path = '/logs';
|
||||
|
||||
static final GlobalKey<NavigatorState> $parentNavigatorKey = rootNavigatorKey;
|
||||
|
||||
@override
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return const MaterialPage(
|
||||
fullscreenDialog: true,
|
||||
child: LogsPage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@TypedGoRoute<SettingsRoute>(path: SettingsRoute.path)
|
||||
class SettingsRoute extends GoRouteData {
|
||||
const SettingsRoute();
|
||||
static const path = '/settings';
|
||||
|
||||
static final GlobalKey<NavigatorState> $parentNavigatorKey = rootNavigatorKey;
|
||||
|
||||
@override
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return const MaterialPage(
|
||||
fullscreenDialog: true,
|
||||
child: SettingsPage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
16
lib/core/router/routes/routes.dart
Normal file
16
lib/core/router/routes/routes.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hiddify/core/router/routes/desktop_routes.dart' as desktop;
|
||||
import 'package:hiddify/core/router/routes/mobile_routes.dart' as mobile;
|
||||
import 'package:hiddify/core/router/routes/shared_routes.dart' as shared;
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
|
||||
export 'mobile_routes.dart';
|
||||
export 'shared_routes.dart' hide $appRoutes;
|
||||
|
||||
List<RouteBase> get $routes => [
|
||||
if (PlatformUtils.isDesktop)
|
||||
...desktop.$appRoutes
|
||||
else
|
||||
...mobile.$appRoutes,
|
||||
...shared.$appRoutes,
|
||||
];
|
||||
101
lib/core/router/routes/shared_routes.dart
Normal file
101
lib/core/router/routes/shared_routes.dart
Normal file
@@ -0,0 +1,101 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hiddify/features/common/common.dart';
|
||||
import 'package:hiddify/features/home/view/view.dart';
|
||||
import 'package:hiddify/features/profile_detail/view/view.dart';
|
||||
import 'package:hiddify/features/profiles/view/view.dart';
|
||||
import 'package:hiddify/features/proxies/view/view.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
|
||||
part 'shared_routes.g.dart';
|
||||
|
||||
List<RouteBase> get $sharedRoutes => $appRoutes;
|
||||
|
||||
final GlobalKey<NavigatorState> rootNavigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
class HomeRoute extends GoRouteData {
|
||||
const HomeRoute();
|
||||
static const path = '/';
|
||||
|
||||
@override
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return const NoTransitionPage(child: HomePage());
|
||||
}
|
||||
}
|
||||
|
||||
class ProxiesRoute extends GoRouteData {
|
||||
const ProxiesRoute();
|
||||
static const path = '/proxies';
|
||||
|
||||
@override
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return const NoTransitionPage(child: ProxiesPage());
|
||||
}
|
||||
}
|
||||
|
||||
@TypedGoRoute<AddProfileRoute>(path: AddProfileRoute.path)
|
||||
class AddProfileRoute extends GoRouteData {
|
||||
const AddProfileRoute();
|
||||
static const path = '/add';
|
||||
|
||||
static final GlobalKey<NavigatorState> $parentNavigatorKey = rootNavigatorKey;
|
||||
|
||||
@override
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return BottomSheetPage(
|
||||
fixed: true,
|
||||
builder: (controller) => AddProfileModal(scrollController: controller),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ProfilesRoute extends GoRouteData {
|
||||
const ProfilesRoute();
|
||||
static const path = 'profiles';
|
||||
|
||||
static final GlobalKey<NavigatorState> $parentNavigatorKey = rootNavigatorKey;
|
||||
|
||||
@override
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return BottomSheetPage(
|
||||
builder: (controller) => ProfilesModal(scrollController: controller),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class NewProfileRoute extends GoRouteData {
|
||||
const NewProfileRoute({this.url, this.name});
|
||||
static const path = 'profiles/new';
|
||||
final String? url;
|
||||
final String? name;
|
||||
|
||||
static final GlobalKey<NavigatorState> $parentNavigatorKey = rootNavigatorKey;
|
||||
|
||||
@override
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return MaterialPage(
|
||||
fullscreenDialog: true,
|
||||
child: ProfileDetailPage(
|
||||
"new",
|
||||
url: url,
|
||||
name: name,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ProfileDetailsRoute extends GoRouteData {
|
||||
const ProfileDetailsRoute(this.id);
|
||||
final String id;
|
||||
static const path = 'profiles/:id';
|
||||
|
||||
static final GlobalKey<NavigatorState> $parentNavigatorKey = rootNavigatorKey;
|
||||
|
||||
@override
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return MaterialPage(
|
||||
fullscreenDialog: true,
|
||||
child: ProfileDetailPage(id),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user