AppFlowy/frontend/appflowy_flutter/lib/user/presentation/router.dart

142 lines
4.2 KiB
Dart
Raw Normal View History

import 'package:appflowy/mobile/presentation/home/mobile_home_page.dart';
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/user/presentation/screens/screens.dart';
import 'package:appflowy/workspace/presentation/home/desktop_home_screen.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
show UserProfilePB;
import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
2022-01-31 00:15:49 +00:00
class AuthRouter {
2022-11-09 03:07:29 +00:00
void pushForgetPasswordScreen(BuildContext context) {}
void pushWorkspaceStartScreen(
BuildContext context,
UserProfilePB userProfile,
) {
getIt<SplashRouter>().pushWorkspaceStartScreen(context, userProfile);
}
2021-09-06 08:18:34 +00:00
void pushSignUpScreen(BuildContext context) {
context.push(SignUpScreen.routeName);
2021-11-08 11:19:02 +00:00
}
/// Navigates to the home screen based on the current workspace and platform.
///
/// This function takes in a [BuildContext] and a [UserProfilePB] object to
/// determine the user's settings and then navigate to the appropriate home screen
/// (`MobileHomeScreen` for mobile platforms, `DesktopHomeScreen` for others).
///
/// It first fetches the current workspace settings using [FolderEventGetCurrentWorkspace].
/// If the workspace settings are successfully fetched, it navigates to the home screen.
/// If there's an error, it defaults to the workspace start screen.
///
/// @param [context] BuildContext for navigating to the appropriate screen.
/// @param [userProfile] UserProfilePB object containing the details of the current user.
///
Future<void> goHomeScreen(
BuildContext context,
UserProfilePB userProfile,
) async {
final result = await FolderEventGetCurrentWorkspaceSetting().send();
result.fold(
(workspaceSetting) {
// Replace SignInScreen or SkipLogInScreen as root page.
// If user click back button, it will exit app rather than go back to SignInScreen or SkipLogInScreen
if (PlatformExtension.isMobile) {
context.go(
MobileHomeScreen.routeName,
);
} else {
context.go(
DesktopHomeScreen.routeName,
);
}
},
(error) => pushWorkspaceStartScreen(context, userProfile),
);
}
void pushEncryptionScreen(
BuildContext context,
UserProfilePB userProfile,
) {
// After log in,push EncryptionScreen on the top SignInScreen
context.push(
EncryptSecretScreen.routeName,
extra: {
EncryptSecretScreen.argUser: userProfile,
EncryptSecretScreen.argKey: ValueKey(userProfile.id),
},
);
}
Future<void> pushWorkspaceErrorScreen(
BuildContext context,
UserFolderPB userFolder,
FlowyError error,
) async {
await context.push(
WorkspaceErrorScreen.routeName,
extra: {
WorkspaceErrorScreen.argUserFolder: userFolder,
WorkspaceErrorScreen.argError: error,
},
);
}
}
2022-01-31 00:24:29 +00:00
class SplashRouter {
// Unused for now, it was planed to be used in SignUpScreen.
// To let user choose workspace than navigate to corresponding home screen.
Future<void> pushWorkspaceStartScreen(
BuildContext context,
UserProfilePB userProfile,
) async {
await context.push(
WorkspaceStartScreen.routeName,
extra: {
WorkspaceStartScreen.argUserProfile: userProfile,
},
2022-01-31 00:24:29 +00:00
);
final result = await FolderEventGetCurrentWorkspaceSetting().send();
result.fold(
(workspaceSettingPB) => pushHomeScreen(context),
(r) => null,
);
2022-01-31 00:24:29 +00:00
}
void pushHomeScreen(
BuildContext context,
) {
if (PlatformExtension.isMobile) {
context.push(
MobileHomeScreen.routeName,
);
} else {
context.push(
DesktopHomeScreen.routeName,
);
}
2022-01-31 00:24:29 +00:00
}
void goHomeScreen(
BuildContext context,
) {
if (PlatformExtension.isMobile) {
context.go(
MobileHomeScreen.routeName,
);
} else {
context.go(
DesktopHomeScreen.routeName,
);
}
2022-01-31 00:24:29 +00:00
}
}