2022-11-09 02:28:48 +00:00
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
|
2022-11-10 02:02:26 +00:00
|
|
|
import 'globals.dart';
|
2022-11-17 16:04:14 +00:00
|
|
|
import 'screens/editor.dart';
|
2022-11-09 02:28:48 +00:00
|
|
|
import 'screens/home.dart';
|
2022-11-19 03:49:11 +00:00
|
|
|
import 'screens/notifications_screen.dart';
|
2022-11-23 20:48:09 +00:00
|
|
|
import 'screens/post_screen.dart';
|
2022-11-19 03:49:11 +00:00
|
|
|
import 'screens/profile_screen.dart';
|
2022-11-09 02:28:48 +00:00
|
|
|
import 'screens/sign_in.dart';
|
2022-11-10 02:02:26 +00:00
|
|
|
import 'screens/splash.dart';
|
2022-11-29 15:33:16 +00:00
|
|
|
import 'screens/user_posts_screen.dart';
|
|
|
|
import 'screens/user_profile_screen.dart';
|
2022-11-10 02:02:26 +00:00
|
|
|
import 'services/auth_service.dart';
|
2022-11-09 02:28:48 +00:00
|
|
|
|
|
|
|
class ScreenPaths {
|
|
|
|
static String splash = '/splash';
|
|
|
|
static String home = '/';
|
2022-11-19 03:49:11 +00:00
|
|
|
static String profile = '/profile';
|
|
|
|
static String notifications = '/notifications';
|
2022-11-09 02:28:48 +00:00
|
|
|
static String signin = '/signin';
|
|
|
|
static String signup = '/signup';
|
|
|
|
static String settings = '/settings';
|
2022-11-29 15:33:16 +00:00
|
|
|
static String userProfile = '/user_profile';
|
|
|
|
static String userPosts = '/user_posts';
|
2022-11-09 02:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool needAuthChangeInitialized = true;
|
2022-11-10 02:02:26 +00:00
|
|
|
final _authService = getIt<AuthService>();
|
|
|
|
final allowedLoggedOut = [
|
|
|
|
ScreenPaths.splash,
|
|
|
|
ScreenPaths.signin,
|
|
|
|
ScreenPaths.signup
|
|
|
|
];
|
|
|
|
|
2022-11-09 02:28:48 +00:00
|
|
|
final appRouter = GoRouter(
|
2022-11-10 02:02:26 +00:00
|
|
|
initialLocation: ScreenPaths.home,
|
2022-11-09 02:28:48 +00:00
|
|
|
debugLogDiagnostics: true,
|
2022-11-10 02:02:26 +00:00
|
|
|
refreshListenable: _authService,
|
|
|
|
redirect: (context, state) async {
|
|
|
|
print('redirect handler');
|
|
|
|
final loggedIn = _authService.loggedIn;
|
|
|
|
print('$loggedIn ${state.location}');
|
|
|
|
if (!loggedIn && !allowedLoggedOut.contains(state.location)) {
|
|
|
|
print('Redirecting to sign in');
|
|
|
|
return ScreenPaths.signin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loggedIn && allowedLoggedOut.contains(state.location)) {
|
|
|
|
print('Redirecting to home');
|
|
|
|
return ScreenPaths.home;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
2022-11-09 02:28:48 +00:00
|
|
|
routes: [
|
|
|
|
GoRoute(
|
|
|
|
path: ScreenPaths.signin,
|
|
|
|
name: ScreenPaths.signin,
|
|
|
|
builder: (context, state) => SignInScreen(),
|
|
|
|
),
|
|
|
|
GoRoute(
|
|
|
|
path: ScreenPaths.home,
|
|
|
|
name: ScreenPaths.home,
|
2022-11-19 03:49:11 +00:00
|
|
|
pageBuilder: (context, state) => NoTransitionPage(
|
|
|
|
name: ScreenPaths.home,
|
|
|
|
child: HomeScreen(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
GoRoute(
|
|
|
|
path: ScreenPaths.profile,
|
|
|
|
name: ScreenPaths.profile,
|
|
|
|
pageBuilder: (context, state) => NoTransitionPage(
|
|
|
|
child: ProfileScreen(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
GoRoute(
|
|
|
|
path: ScreenPaths.notifications,
|
|
|
|
name: ScreenPaths.notifications,
|
|
|
|
pageBuilder: (context, state) => NoTransitionPage(
|
|
|
|
child: NotificationsScreen(),
|
|
|
|
),
|
2022-11-09 02:28:48 +00:00
|
|
|
),
|
2022-11-10 02:02:26 +00:00
|
|
|
GoRoute(
|
|
|
|
path: ScreenPaths.splash,
|
|
|
|
name: ScreenPaths.splash,
|
|
|
|
builder: (context, state) => SplashScreen(),
|
|
|
|
),
|
2022-11-17 16:04:14 +00:00
|
|
|
GoRoute(
|
|
|
|
path: '/post',
|
|
|
|
redirect: (context, state) {
|
|
|
|
print('post state redirect');
|
|
|
|
if (state.location == '/post') {
|
|
|
|
return '/post/new';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
routes: [
|
|
|
|
GoRoute(
|
|
|
|
path: 'new',
|
|
|
|
builder: (context, state) => EditorScreen(),
|
|
|
|
),
|
|
|
|
GoRoute(
|
|
|
|
path: 'edit/:id',
|
|
|
|
builder: (context, state) =>
|
|
|
|
EditorScreen(id: state.params['id'] ?? 'Not Found'),
|
|
|
|
),
|
2022-11-23 20:48:09 +00:00
|
|
|
GoRoute(
|
|
|
|
path: 'view/:id',
|
|
|
|
builder: (context, state) =>
|
|
|
|
PostScreen(id: state.params['id'] ?? 'Not Found'),
|
|
|
|
),
|
2022-11-23 02:59:08 +00:00
|
|
|
]),
|
|
|
|
GoRoute(
|
|
|
|
path: '/comment',
|
|
|
|
redirect: (context, state) {
|
|
|
|
print('post state redirect');
|
|
|
|
if (state.location == '/comment') {
|
|
|
|
return '/comment/new';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
routes: [
|
|
|
|
GoRoute(
|
|
|
|
path: 'new',
|
|
|
|
builder: (context, state) => EditorScreen(
|
|
|
|
parentId: state.queryParams['parent_id'] ?? '',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
GoRoute(
|
|
|
|
path: 'edit/:id',
|
|
|
|
builder: (context, state) =>
|
|
|
|
EditorScreen(id: state.params['id'] ?? 'Not Found'),
|
|
|
|
),
|
|
|
|
]),
|
2022-11-29 15:33:16 +00:00
|
|
|
GoRoute(
|
|
|
|
path: '/user_posts/:id',
|
|
|
|
name: ScreenPaths.userPosts,
|
|
|
|
builder: (context, state) =>
|
|
|
|
UserPostsScreen(userId: state.params['id']!),
|
|
|
|
),
|
|
|
|
GoRoute(
|
|
|
|
path: '/user_profile/:id',
|
|
|
|
name: ScreenPaths.userProfile,
|
|
|
|
builder: (context, state) =>
|
|
|
|
UserProfileScreen(userId: state.params['id']!),
|
|
|
|
),
|
2022-11-09 02:28:48 +00:00
|
|
|
]);
|