splash_screen.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import 'package:appflowy/env/env.dart';
  2. import 'package:appflowy/generated/flowy_svgs.g.dart';
  3. import 'package:appflowy/startup/startup.dart';
  4. import 'package:appflowy/user/application/auth/auth_service.dart';
  5. import 'package:appflowy/user/application/splash_bloc.dart';
  6. import 'package:appflowy/user/domain/auth_state.dart';
  7. import 'package:appflowy/user/presentation/helpers/helpers.dart';
  8. import 'package:appflowy/user/presentation/router.dart';
  9. import 'package:appflowy/user/presentation/screens/screens.dart';
  10. import 'package:appflowy/util/platform_extension.dart';
  11. import 'package:appflowy_backend/dispatch/dispatch.dart';
  12. import 'package:appflowy_backend/log.dart';
  13. import 'package:flutter/material.dart';
  14. import 'package:flutter_bloc/flutter_bloc.dart';
  15. import 'package:go_router/go_router.dart';
  16. // [[diagram: splash screen]]
  17. // ┌────────────────┐1.get user ┌──────────┐ ┌────────────┐ 2.send UserEventCheckUser
  18. // │ SplashScreen │──────────▶│SplashBloc│────▶│ISplashUser │─────┐
  19. // └────────────────┘ └──────────┘ └────────────┘ │
  20. // │
  21. // ▼
  22. // ┌───────────┐ ┌─────────────┐ ┌────────┐
  23. // │HomeScreen │◀───────────│BlocListener │◀────────────────│RustSDK │
  24. // └───────────┘ └─────────────┘ └────────┘
  25. // 4. Show HomeScreen or SignIn 3.return AuthState
  26. class SplashScreen extends StatelessWidget {
  27. /// Root Page of the app.
  28. const SplashScreen({
  29. super.key,
  30. required this.autoRegister,
  31. });
  32. final bool autoRegister;
  33. @override
  34. Widget build(BuildContext context) {
  35. if (!autoRegister) {
  36. return _buildChild(context);
  37. } else {
  38. return FutureBuilder<void>(
  39. future: _registerIfNeeded(),
  40. builder: (context, snapshot) {
  41. if (snapshot.connectionState != ConnectionState.done) {
  42. return Container();
  43. }
  44. return _buildChild(context);
  45. },
  46. );
  47. }
  48. }
  49. BlocProvider<SplashBloc> _buildChild(BuildContext context) {
  50. return BlocProvider(
  51. create: (context) =>
  52. getIt<SplashBloc>()..add(const SplashEvent.getUser()),
  53. child: Scaffold(
  54. body: BlocListener<SplashBloc, SplashState>(
  55. listener: (context, state) {
  56. state.auth.map(
  57. authenticated: (r) => _handleAuthenticated(context, r),
  58. unauthenticated: (r) => _handleUnauthenticated(context, r),
  59. initial: (r) => {},
  60. );
  61. },
  62. child: const Body(),
  63. ),
  64. ),
  65. );
  66. }
  67. /// Handles the authentication flow once a user is authenticated.
  68. Future<void> _handleAuthenticated(
  69. BuildContext context,
  70. Authenticated authenticated,
  71. ) async {
  72. final userProfile = authenticated.userProfile;
  73. /// After a user is authenticated, this function checks if encryption is required.
  74. final result = await UserEventCheckEncryptionSign().send();
  75. result.fold(
  76. (check) async {
  77. /// If encryption is needed, the user is navigated to the encryption screen.
  78. /// Otherwise, it fetches the current workspace for the user and navigates them
  79. if (check.isNeedSecret) {
  80. getIt<AuthRouter>().pushEncryptionScreen(context, userProfile);
  81. } else {
  82. final result = await FolderEventGetCurrentWorkspace().send();
  83. result.fold(
  84. (workspaceSetting) {
  85. // After login, replace Splash screen by corresponding home screen
  86. getIt<SplashRouter>().goHomeScreen(
  87. context,
  88. );
  89. },
  90. (error) => handleOpenWorkspaceError(context, error),
  91. );
  92. }
  93. },
  94. (err) {
  95. Log.error(err);
  96. },
  97. );
  98. }
  99. void _handleUnauthenticated(BuildContext context, Unauthenticated result) {
  100. Log.trace(
  101. '_handleUnauthenticated -> cloud is enabled: $isCloudEnabled',
  102. );
  103. // replace Splash screen as root page
  104. if (isCloudEnabled) {
  105. context.go(SignInScreen.routeName);
  106. } else {
  107. // if the env is not configured, we will skip to the 'skip login screen'.
  108. context.go(SkipLogInScreen.routeName);
  109. }
  110. }
  111. Future<void> _registerIfNeeded() async {
  112. final result = await UserEventGetUserProfile().send();
  113. if (!result.isLeft()) {
  114. await getIt<AuthService>().signUpAsGuest();
  115. }
  116. }
  117. }
  118. class Body extends StatelessWidget {
  119. const Body({super.key});
  120. @override
  121. Widget build(BuildContext context) {
  122. return Container(
  123. alignment: Alignment.center,
  124. child: PlatformExtension.isMobile
  125. ? const FlowySvg(
  126. FlowySvgs.flowy_logo_xl,
  127. blendMode: null,
  128. )
  129. : const _DesktopSplashBody(),
  130. );
  131. }
  132. }
  133. class _DesktopSplashBody extends StatelessWidget {
  134. const _DesktopSplashBody();
  135. @override
  136. Widget build(BuildContext context) {
  137. final size = MediaQuery.of(context).size;
  138. return SingleChildScrollView(
  139. child: Stack(
  140. alignment: Alignment.center,
  141. children: [
  142. Image(
  143. fit: BoxFit.cover,
  144. width: size.width,
  145. height: size.height,
  146. image: const AssetImage(
  147. 'assets/images/appflowy_launch_splash.jpg',
  148. ),
  149. ),
  150. const CircularProgressIndicator.adaptive(),
  151. ],
  152. ),
  153. );
  154. }
  155. }