router.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import 'package:appflowy/mobile/presentation/mobile_home_page.dart';
  2. import 'package:appflowy/startup/startup.dart';
  3. import 'package:appflowy/user/application/auth/auth_service.dart';
  4. import 'package:appflowy/user/presentation/screens/screens.dart';
  5. import 'package:appflowy/user/presentation/screens/workspace_start_screen/workspace_start_screen.dart';
  6. import 'package:appflowy/workspace/presentation/home/home_screen.dart';
  7. import 'package:appflowy_backend/dispatch/dispatch.dart';
  8. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  9. import 'package:flowy_infra/time/duration.dart';
  10. import 'package:flowy_infra_ui/widget/route/animation.dart';
  11. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'
  12. show UserProfilePB;
  13. import 'package:appflowy_backend/protobuf/flowy-folder2/protobuf.dart';
  14. import 'package:flutter/material.dart';
  15. import 'package:appflowy/util/platform_extension.dart';
  16. class AuthRouter {
  17. void pushForgetPasswordScreen(BuildContext context) {}
  18. void pushWorkspaceStartScreen(
  19. BuildContext context,
  20. UserProfilePB userProfile,
  21. ) {
  22. getIt<SplashRouter>().pushWorkspaceStartScreen(context, userProfile);
  23. }
  24. void pushSignUpScreen(BuildContext context) {
  25. Navigator.of(context).push(
  26. PageRoutes.fade(
  27. () => SignUpScreen(router: getIt<AuthRouter>()),
  28. const RouteSettings(name: SignUpScreen.routeName),
  29. ),
  30. );
  31. }
  32. /// Navigates to the home screen based on the current workspace and platform.
  33. ///
  34. /// This function takes in a [BuildContext] and a [UserProfilePB] object to
  35. /// determine the user's settings and then navigate to the appropriate home screen
  36. /// (`MobileHomeScreen` for mobile platforms, `DesktopHomeScreen` for others).
  37. ///
  38. /// It first fetches the current workspace settings using [FolderEventGetCurrentWorkspace].
  39. /// If the workspace settings are successfully fetched, it navigates to the home screen.
  40. /// If there's an error, it defaults to the workspace start screen.
  41. ///
  42. /// @param [context] BuildContext for navigating to the appropriate screen.
  43. /// @param [userProfile] UserProfilePB object containing the details of the current user.
  44. ///
  45. Future<void> pushHomeScreen(
  46. BuildContext context,
  47. UserProfilePB userProfile,
  48. ) async {
  49. final result = await FolderEventGetCurrentWorkspace().send();
  50. result.fold(
  51. (workspaceSetting) {
  52. if (PlatformExtension.isMobile) {
  53. Navigator.of(context).pushAndRemoveUntil(
  54. MaterialPageRoute<void>(
  55. builder: (BuildContext context) => MobileHomeScreen(
  56. key: ValueKey(userProfile.id),
  57. userProfile: userProfile,
  58. workspaceSetting: workspaceSetting,
  59. ),
  60. ),
  61. // pop up all the pages until [SplashScreen]
  62. (route) => route.settings.name == SplashScreen.routeName,
  63. );
  64. } else {
  65. Navigator.push(
  66. context,
  67. PageRoutes.fade(
  68. () => DesktopHomeScreen(
  69. key: ValueKey(userProfile.id),
  70. userProfile: userProfile,
  71. workspaceSetting: workspaceSetting,
  72. ),
  73. const RouteSettings(
  74. name: DesktopHomeScreen.routeName,
  75. ),
  76. RouteDurations.slow.inMilliseconds * .001,
  77. ),
  78. );
  79. }
  80. },
  81. (error) => pushWorkspaceStartScreen(context, userProfile),
  82. );
  83. }
  84. Future<void> pushEncryptionScreen(
  85. BuildContext context,
  86. UserProfilePB userProfile,
  87. ) async {
  88. Navigator.push(
  89. context,
  90. PageRoutes.fade(
  91. () => EncryptSecretScreen(
  92. user: userProfile,
  93. key: ValueKey(userProfile.id),
  94. ),
  95. const RouteSettings(name: EncryptSecretScreen.routeName),
  96. RouteDurations.slow.inMilliseconds * .001,
  97. ),
  98. );
  99. }
  100. Future<void> pushWorkspaceErrorScreen(
  101. BuildContext context,
  102. UserFolderPB userFolder,
  103. FlowyError error,
  104. ) async {
  105. final screen = WorkspaceErrorScreen(
  106. userFolder: userFolder,
  107. error: error,
  108. );
  109. await Navigator.of(context).push(
  110. PageRoutes.fade(
  111. () => screen,
  112. const RouteSettings(name: WorkspaceErrorScreen.routeName),
  113. RouteDurations.slow.inMilliseconds * .001,
  114. ),
  115. );
  116. }
  117. }
  118. class SplashRouter {
  119. Future<void> pushWorkspaceStartScreen(
  120. BuildContext context,
  121. UserProfilePB userProfile,
  122. ) async {
  123. final screen = WorkspaceStartScreen(userProfile: userProfile);
  124. await Navigator.of(context).push(
  125. PageRoutes.fade(
  126. () => screen,
  127. const RouteSettings(name: WorkspaceStartScreen.routeName),
  128. RouteDurations.slow.inMilliseconds * .001,
  129. ),
  130. );
  131. FolderEventGetCurrentWorkspace().send().then((result) {
  132. result.fold(
  133. (workspaceSettingPB) =>
  134. pushHomeScreen(context, userProfile, workspaceSettingPB),
  135. (r) => null,
  136. );
  137. });
  138. }
  139. void pushHomeScreen(
  140. BuildContext context,
  141. UserProfilePB userProfile,
  142. WorkspaceSettingPB workspaceSetting,
  143. ) {
  144. if (PlatformExtension.isMobile) {
  145. Navigator.pushAndRemoveUntil<void>(
  146. context,
  147. MaterialPageRoute<void>(
  148. builder: (BuildContext context) => MobileHomeScreen(
  149. key: ValueKey(userProfile.id),
  150. userProfile: userProfile,
  151. workspaceSetting: workspaceSetting,
  152. ),
  153. ),
  154. // pop up all the pages until [SplashScreen]
  155. (route) => route.settings.name == SplashScreen.routeName,
  156. );
  157. } else {
  158. Navigator.push(
  159. context,
  160. PageRoutes.fade(
  161. () => DesktopHomeScreen(
  162. userProfile: userProfile,
  163. workspaceSetting: workspaceSetting,
  164. key: ValueKey(userProfile.id),
  165. ),
  166. const RouteSettings(
  167. name: DesktopHomeScreen.routeName,
  168. ),
  169. RouteDurations.slow.inMilliseconds * .001,
  170. ),
  171. );
  172. }
  173. }
  174. void pushSignInScreen(BuildContext context) {
  175. Navigator.push(
  176. context,
  177. PageRoutes.fade(
  178. () => SignInScreen(router: getIt<AuthRouter>()),
  179. const RouteSettings(name: SignInScreen.routeName),
  180. RouteDurations.slow.inMilliseconds * .001,
  181. ),
  182. );
  183. }
  184. void pushSkipLoginScreen(BuildContext context) {
  185. Navigator.push(
  186. context,
  187. PageRoutes.fade(
  188. () => SkipLogInScreen(
  189. router: getIt<AuthRouter>(),
  190. authService: getIt<AuthService>(),
  191. ),
  192. const RouteSettings(name: SkipLogInScreen.routeName),
  193. RouteDurations.slow.inMilliseconds * .001,
  194. ),
  195. );
  196. }
  197. }