mirror of
https://github.com/krille-chan/fluffychat
synced 2024-09-17 09:35:12 +00:00
refactor: Make router static
This commit is contained in:
parent
d2247018e6
commit
d6b48091c4
3 changed files with 338 additions and 328 deletions
|
@ -1,7 +1,8 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import 'package:fluffychat/config/themes.dart';
|
||||
import 'package:fluffychat/pages/add_story/add_story.dart';
|
||||
|
@ -33,18 +34,28 @@ import 'package:fluffychat/widgets/layouts/empty_page.dart';
|
|||
import 'package:fluffychat/widgets/layouts/side_view_layout.dart';
|
||||
import 'package:fluffychat/widgets/layouts/two_column_layout.dart';
|
||||
import 'package:fluffychat/widgets/log_view.dart';
|
||||
import 'package:fluffychat/widgets/matrix.dart';
|
||||
|
||||
class AppRoutes {
|
||||
final List<Client> clients;
|
||||
abstract class AppRoutes {
|
||||
static FutureOr<String?> loggedInRedirect(
|
||||
BuildContext context,
|
||||
GoRouterState state,
|
||||
) =>
|
||||
Matrix.of(context).client.isLogged() ? '/rooms' : null;
|
||||
|
||||
bool get isLoggedIn => clients.any((client) => client.isLogged());
|
||||
static FutureOr<String?> loggedOutRedirect(
|
||||
BuildContext context,
|
||||
GoRouterState state,
|
||||
) =>
|
||||
Matrix.of(context).client.isLogged() ? null : '/home';
|
||||
|
||||
AppRoutes(this.clients);
|
||||
AppRoutes();
|
||||
|
||||
List<RouteBase> get routes => [
|
||||
static final List<RouteBase> routes = [
|
||||
GoRoute(
|
||||
path: '/',
|
||||
redirect: (context, state) => isLoggedIn ? '/rooms' : '/home',
|
||||
redirect: (context, state) =>
|
||||
Matrix.of(context).client.isLogged() ? '/rooms' : '/home',
|
||||
),
|
||||
GoRoute(
|
||||
path: '/home',
|
||||
|
@ -52,7 +63,7 @@ class AppRoutes {
|
|||
context,
|
||||
const HomeserverPicker(),
|
||||
),
|
||||
redirect: (context, state) => isLoggedIn ? '/rooms' : null,
|
||||
redirect: loggedInRedirect,
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: 'login',
|
||||
|
@ -60,7 +71,7 @@ class AppRoutes {
|
|||
context,
|
||||
const Login(),
|
||||
),
|
||||
redirect: (context, state) => isLoggedIn ? '/rooms' : null,
|
||||
redirect: loggedInRedirect,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
@ -91,7 +102,7 @@ class AppRoutes {
|
|||
routes: [
|
||||
GoRoute(
|
||||
path: '/rooms',
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
pageBuilder: (context, state) => defaultPageBuilder(
|
||||
context,
|
||||
FluffyThemes.isColumnMode(context)
|
||||
|
@ -107,7 +118,7 @@ class AppRoutes {
|
|||
context,
|
||||
const AddStoryPage(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'stories/:roomid',
|
||||
|
@ -115,7 +126,7 @@ class AppRoutes {
|
|||
context,
|
||||
const StoryPage(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: 'share',
|
||||
|
@ -123,8 +134,7 @@ class AppRoutes {
|
|||
context,
|
||||
const AddStoryPage(),
|
||||
),
|
||||
redirect: (context, state) =>
|
||||
!isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
@ -137,7 +147,7 @@ class AppRoutes {
|
|||
),
|
||||
),
|
||||
routes: _chatDetailsRoutes,
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'archive',
|
||||
|
@ -154,11 +164,10 @@ class AppRoutes {
|
|||
roomId: state.pathParameters['roomid']!,
|
||||
),
|
||||
),
|
||||
redirect: (context, state) =>
|
||||
!isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
],
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'newprivatechat',
|
||||
|
@ -166,7 +175,7 @@ class AppRoutes {
|
|||
context,
|
||||
const NewPrivateChat(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'newgroup',
|
||||
|
@ -174,7 +183,7 @@ class AppRoutes {
|
|||
context,
|
||||
const NewGroup(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'newspace',
|
||||
|
@ -182,7 +191,7 @@ class AppRoutes {
|
|||
context,
|
||||
const NewSpace(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
ShellRoute(
|
||||
pageBuilder: (context, state, child) => defaultPageBuilder(
|
||||
|
@ -205,8 +214,7 @@ class AppRoutes {
|
|||
: const Settings(),
|
||||
),
|
||||
routes: _settingsRoutes,
|
||||
redirect: (context, state) =>
|
||||
!isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
@ -217,8 +225,8 @@ class AppRoutes {
|
|||
mainView: ChatPage(
|
||||
roomId: state.pathParameters['roomid']!,
|
||||
),
|
||||
sideView:
|
||||
state.fullPath == '/rooms/:roomid' ? null : child,
|
||||
sideView: child,
|
||||
hideSideView: state.fullPath == '/rooms/:roomid',
|
||||
),
|
||||
),
|
||||
routes: [
|
||||
|
@ -226,10 +234,9 @@ class AppRoutes {
|
|||
path: ':roomid',
|
||||
pageBuilder: (context, state) => defaultPageBuilder(
|
||||
context,
|
||||
const EmptyPage(),
|
||||
const SizedBox.shrink(),
|
||||
),
|
||||
redirect: (context, state) =>
|
||||
!isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: 'encryption',
|
||||
|
@ -237,8 +244,7 @@ class AppRoutes {
|
|||
context,
|
||||
const ChatEncryptionSettings(),
|
||||
),
|
||||
redirect: (context, state) =>
|
||||
!isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'invite',
|
||||
|
@ -246,8 +252,7 @@ class AppRoutes {
|
|||
context,
|
||||
const InvitationSelection(),
|
||||
),
|
||||
redirect: (context, state) =>
|
||||
!isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'details',
|
||||
|
@ -258,8 +263,7 @@ class AppRoutes {
|
|||
),
|
||||
),
|
||||
routes: _chatDetailsRoutes,
|
||||
redirect: (context, state) =>
|
||||
!isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
@ -271,14 +275,14 @@ class AppRoutes {
|
|||
),
|
||||
];
|
||||
|
||||
List<RouteBase> get _chatDetailsRoutes => [
|
||||
static final List<RouteBase> _chatDetailsRoutes = [
|
||||
GoRoute(
|
||||
path: 'permissions',
|
||||
pageBuilder: (context, state) => defaultPageBuilder(
|
||||
context,
|
||||
const ChatPermissionsSettings(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'invite',
|
||||
|
@ -286,7 +290,7 @@ class AppRoutes {
|
|||
context,
|
||||
const InvitationSelection(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'multiple_emotes',
|
||||
|
@ -294,7 +298,7 @@ class AppRoutes {
|
|||
context,
|
||||
const MultipleEmotesSettings(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'emotes',
|
||||
|
@ -302,7 +306,7 @@ class AppRoutes {
|
|||
context,
|
||||
const EmotesSettings(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'emotes/:state_key',
|
||||
|
@ -310,18 +314,18 @@ class AppRoutes {
|
|||
context,
|
||||
const EmotesSettings(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
];
|
||||
|
||||
List<RouteBase> get _settingsRoutes => [
|
||||
static final List<RouteBase> _settingsRoutes = [
|
||||
GoRoute(
|
||||
path: 'notifications',
|
||||
pageBuilder: (context, state) => defaultPageBuilder(
|
||||
context,
|
||||
const SettingsNotifications(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'style',
|
||||
|
@ -329,7 +333,7 @@ class AppRoutes {
|
|||
context,
|
||||
const SettingsStyle(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'devices',
|
||||
|
@ -337,7 +341,7 @@ class AppRoutes {
|
|||
context,
|
||||
const DevicesSettings(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'chat',
|
||||
|
@ -354,11 +358,11 @@ class AppRoutes {
|
|||
),
|
||||
),
|
||||
],
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'addaccount',
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
pageBuilder: (context, state) => defaultPageBuilder(
|
||||
context,
|
||||
const HomeserverPicker(),
|
||||
|
@ -370,13 +374,13 @@ class AppRoutes {
|
|||
context,
|
||||
const Login(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
],
|
||||
),
|
||||
GoRoute(
|
||||
path: 'security',
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
pageBuilder: (context, state) => defaultPageBuilder(
|
||||
context,
|
||||
const SettingsSecurity(),
|
||||
|
@ -388,7 +392,7 @@ class AppRoutes {
|
|||
context,
|
||||
const SettingsStories(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: 'ignorelist',
|
||||
|
@ -396,7 +400,7 @@ class AppRoutes {
|
|||
context,
|
||||
const SettingsIgnoreList(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
GoRoute(
|
||||
path: '3pid',
|
||||
|
@ -404,13 +408,13 @@ class AppRoutes {
|
|||
context,
|
||||
const Settings3Pid(),
|
||||
),
|
||||
redirect: (context, state) => !isLoggedIn ? '/home' : null,
|
||||
redirect: loggedOutRedirect,
|
||||
),
|
||||
],
|
||||
),
|
||||
];
|
||||
|
||||
Page defaultPageBuilder(BuildContext context, Widget child) =>
|
||||
static Page defaultPageBuilder(BuildContext context, Widget child) =>
|
||||
CustomTransitionPage(
|
||||
child: child,
|
||||
transitionsBuilder: (context, animation, secondaryAnimation, child) =>
|
||||
|
|
|
@ -26,6 +26,10 @@ class FluffyChatApp extends StatelessWidget {
|
|||
/// in with qr code or magic link.
|
||||
static bool gotInitialLink = false;
|
||||
|
||||
// Router must be outside of build method so that hot reload does not reset
|
||||
// the current path.
|
||||
static final GoRouter router = GoRouter(routes: AppRoutes.routes);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ThemeBuilder(
|
||||
|
@ -37,11 +41,11 @@ class FluffyChatApp extends StatelessWidget {
|
|||
scrollBehavior: CustomScrollBehavior(),
|
||||
localizationsDelegates: L10n.localizationsDelegates,
|
||||
supportedLocales: L10n.supportedLocales,
|
||||
routerConfig: GoRouter(routes: AppRoutes(clients).routes),
|
||||
routerConfig: router,
|
||||
builder: (context, child) => Matrix(
|
||||
context: context,
|
||||
clients: clients,
|
||||
child: child,
|
||||
child: testWidget ?? child,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -4,16 +4,20 @@ import 'package:fluffychat/config/themes.dart';
|
|||
|
||||
class SideViewLayout extends StatelessWidget {
|
||||
final Widget mainView;
|
||||
final Widget? sideView;
|
||||
final Widget sideView;
|
||||
final bool hideSideView;
|
||||
|
||||
const SideViewLayout({Key? key, required this.mainView, this.sideView})
|
||||
: super(key: key);
|
||||
const SideViewLayout({
|
||||
Key? key,
|
||||
required this.mainView,
|
||||
required this.sideView,
|
||||
required this.hideSideView,
|
||||
}) : super(key: key);
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final sideView = this.sideView;
|
||||
final hideSideView =
|
||||
!FluffyThemes.isThreeColumnMode(context) || sideView == null;
|
||||
const sideViewWidth = 360.0;
|
||||
final threeColumnMode = FluffyThemes.isThreeColumnMode(context);
|
||||
return Stack(
|
||||
children: [
|
||||
AnimatedPositioned(
|
||||
|
@ -22,7 +26,7 @@ class SideViewLayout extends StatelessWidget {
|
|||
top: 0,
|
||||
left: 0,
|
||||
bottom: 0,
|
||||
right: hideSideView ? 0 : sideViewWidth,
|
||||
right: !threeColumnMode || hideSideView ? 0 : sideViewWidth,
|
||||
child: ClipRRect(child: mainView),
|
||||
),
|
||||
AnimatedPositioned(
|
||||
|
@ -31,12 +35,10 @@ class SideViewLayout extends StatelessWidget {
|
|||
bottom: 0,
|
||||
top: 0,
|
||||
right: 0,
|
||||
left: !FluffyThemes.isThreeColumnMode(context) && sideView != null
|
||||
left: !threeColumnMode && !hideSideView ? 0 : null,
|
||||
width: hideSideView
|
||||
? 0
|
||||
: null,
|
||||
width: sideView == null
|
||||
? 0
|
||||
: !FluffyThemes.isThreeColumnMode(context)
|
||||
: !threeColumnMode
|
||||
? null
|
||||
: sideViewWidth,
|
||||
child: Container(
|
||||
|
|
Loading…
Reference in a new issue