router.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import 'package:appflowy/mobile/presentation/home/mobile_home_page.dart';
  2. import 'package:appflowy/startup/startup.dart';
  3. import 'package:appflowy/user/presentation/screens/screens.dart';
  4. import 'package:appflowy/workspace/presentation/home/desktop_home_screen.dart';
  5. import 'package:appflowy_backend/dispatch/dispatch.dart';
  6. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  7. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
  8. show UserProfilePB;
  9. import 'package:appflowy_backend/protobuf/flowy-folder2/protobuf.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:appflowy/util/platform_extension.dart';
  12. import 'package:go_router/go_router.dart';
  13. class AuthRouter {
  14. void pushForgetPasswordScreen(BuildContext context) {}
  15. void pushWorkspaceStartScreen(
  16. BuildContext context,
  17. UserProfilePB userProfile,
  18. ) {
  19. getIt<SplashRouter>().pushWorkspaceStartScreen(context, userProfile);
  20. }
  21. void pushSignUpScreen(BuildContext context) {
  22. context.push(SignUpScreen.routeName);
  23. }
  24. /// Navigates to the home screen based on the current workspace and platform.
  25. ///
  26. /// This function takes in a [BuildContext] and a [UserProfilePB] object to
  27. /// determine the user's settings and then navigate to the appropriate home screen
  28. /// (`MobileHomeScreen` for mobile platforms, `DesktopHomeScreen` for others).
  29. ///
  30. /// It first fetches the current workspace settings using [FolderEventGetCurrentWorkspace].
  31. /// If the workspace settings are successfully fetched, it navigates to the home screen.
  32. /// If there's an error, it defaults to the workspace start screen.
  33. ///
  34. /// @param [context] BuildContext for navigating to the appropriate screen.
  35. /// @param [userProfile] UserProfilePB object containing the details of the current user.
  36. ///
  37. Future<void> goHomeScreen(
  38. BuildContext context,
  39. UserProfilePB userProfile,
  40. ) async {
  41. final result = await FolderEventGetCurrentWorkspace().send();
  42. result.fold(
  43. (workspaceSetting) {
  44. // Replace SignInScreen or SkipLogInScreen as root page.
  45. // If user click back button, it will exit app rather than go back to SignInScreen or SkipLogInScreen
  46. if (PlatformExtension.isMobile) {
  47. context.go(
  48. MobileHomeScreen.routeName,
  49. );
  50. } else {
  51. context.go(
  52. DesktopHomeScreen.routeName,
  53. );
  54. }
  55. },
  56. (error) => pushWorkspaceStartScreen(context, userProfile),
  57. );
  58. }
  59. Future<void> pushEncryptionScreen(
  60. BuildContext context,
  61. UserProfilePB userProfile,
  62. ) async {
  63. // After log in,push EncryptionScreen on the top SignInScreen
  64. context.push(
  65. EncryptSecretScreen.routeName,
  66. extra: {
  67. EncryptSecretScreen.argUser: userProfile,
  68. EncryptSecretScreen.argKey: ValueKey(userProfile.id),
  69. },
  70. );
  71. }
  72. Future<void> pushWorkspaceErrorScreen(
  73. BuildContext context,
  74. UserFolderPB userFolder,
  75. FlowyError error,
  76. ) async {
  77. await context.push(
  78. WorkspaceErrorScreen.routeName,
  79. extra: {
  80. WorkspaceErrorScreen.argUserFolder: userFolder,
  81. WorkspaceErrorScreen.argError: error,
  82. },
  83. );
  84. }
  85. }
  86. class SplashRouter {
  87. // Unused for now, it was planed to be used in SignUpScreen.
  88. // To let user choose workspace than navigate to corresponding home screen.
  89. Future<void> pushWorkspaceStartScreen(
  90. BuildContext context,
  91. UserProfilePB userProfile,
  92. ) async {
  93. await context.push(
  94. WorkspaceStartScreen.routeName,
  95. extra: {
  96. WorkspaceStartScreen.argUserProfile: userProfile,
  97. },
  98. );
  99. FolderEventGetCurrentWorkspace().send().then((result) {
  100. result.fold(
  101. (workspaceSettingPB) => pushHomeScreen(context),
  102. (r) => null,
  103. );
  104. });
  105. }
  106. void pushHomeScreen(
  107. BuildContext context,
  108. ) {
  109. if (PlatformExtension.isMobile) {
  110. context.push(
  111. MobileHomeScreen.routeName,
  112. );
  113. } else {
  114. context.push(
  115. DesktopHomeScreen.routeName,
  116. );
  117. }
  118. }
  119. void goHomeScreen(
  120. BuildContext context,
  121. ) {
  122. if (PlatformExtension.isMobile) {
  123. context.go(
  124. MobileHomeScreen.routeName,
  125. );
  126. } else {
  127. context.go(
  128. DesktopHomeScreen.routeName,
  129. );
  130. }
  131. }
  132. }