| 
									
										
										
										
											2024-06-11 22:17:29 +02:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-25 01:18:31 -08:00
										 |  |  | import 'package:appflowy/env/cloud_env.dart'; | 
					
						
							| 
									
										
										
										
											2023-09-11 21:32:26 -05:00
										 |  |  | import 'package:appflowy/generated/flowy_svgs.g.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/startup/startup.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  | import 'package:appflowy/user/application/auth/auth_service.dart'; | 
					
						
							| 
									
										
										
										
											2023-09-11 21:32:26 -05:00
										 |  |  | import 'package:appflowy/user/application/splash_bloc.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/user/domain/auth_state.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/user/presentation/helpers/helpers.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/user/presentation/router.dart'; | 
					
						
							| 
									
										
										
										
											2023-09-28 01:17:38 -05:00
										 |  |  | import 'package:appflowy/user/presentation/screens/screens.dart'; | 
					
						
							| 
									
										
										
										
											2023-01-08 12:10:53 +08:00
										 |  |  | import 'package:appflowy_backend/dispatch/dispatch.dart'; | 
					
						
							|  |  |  | import 'package:appflowy_backend/log.dart'; | 
					
						
							| 
									
										
										
										
											2023-12-15 18:20:44 +08:00
										 |  |  | import 'package:appflowy_editor/appflowy_editor.dart' hide Log; | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | import 'package:flutter_bloc/flutter_bloc.dart'; | 
					
						
							| 
									
										
										
										
											2023-09-28 01:17:38 -05:00
										 |  |  | import 'package:go_router/go_router.dart'; | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class SplashScreen extends StatelessWidget { | 
					
						
							| 
									
										
										
										
											2023-09-28 01:17:38 -05:00
										 |  |  |   /// Root Page of the app.
 | 
					
						
							| 
									
										
										
										
											2024-05-12 22:09:55 +02:00
										 |  |  |   const SplashScreen({super.key, required this.isAnon}); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |   final bool isAnon; | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |     if (isAnon) { | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |       return FutureBuilder<void>( | 
					
						
							|  |  |  |         future: _registerIfNeeded(), | 
					
						
							|  |  |  |         builder: (context, snapshot) { | 
					
						
							|  |  |  |           if (snapshot.connectionState != ConnectionState.done) { | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |             return const SizedBox.shrink(); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |           } | 
					
						
							|  |  |  |           return _buildChild(context); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-11-27 18:54:31 -08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       return _buildChild(context); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   BlocProvider<SplashBloc> _buildChild(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |     return BlocProvider( | 
					
						
							| 
									
										
										
										
											2023-09-28 01:17:38 -05:00
										 |  |  |       create: (context) => | 
					
						
							|  |  |  |           getIt<SplashBloc>()..add(const SplashEvent.getUser()), | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |       child: Scaffold( | 
					
						
							|  |  |  |         body: BlocListener<SplashBloc, SplashState>( | 
					
						
							|  |  |  |           listener: (context, state) { | 
					
						
							|  |  |  |             state.auth.map( | 
					
						
							|  |  |  |               authenticated: (r) => _handleAuthenticated(context, r), | 
					
						
							|  |  |  |               unauthenticated: (r) => _handleUnauthenticated(context, r), | 
					
						
							|  |  |  |               initial: (r) => {}, | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           child: const Body(), | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |   /// Handles the authentication flow once a user is authenticated.
 | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  |   Future<void> _handleAuthenticated( | 
					
						
							|  |  |  |     BuildContext context, | 
					
						
							|  |  |  |     Authenticated authenticated, | 
					
						
							|  |  |  |   ) async { | 
					
						
							|  |  |  |     final userProfile = authenticated.userProfile; | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /// After a user is authenticated, this function checks if encryption is required.
 | 
					
						
							|  |  |  |     final result = await UserEventCheckEncryptionSign().send(); | 
					
						
							| 
									
										
										
										
											2024-01-29 10:26:45 +08:00
										 |  |  |     await result.fold( | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |       (check) async { | 
					
						
							|  |  |  |         /// If encryption is needed, the user is navigated to the encryption screen.
 | 
					
						
							|  |  |  |         /// Otherwise, it fetches the current workspace for the user and navigates them
 | 
					
						
							| 
									
										
										
										
											2023-10-24 20:11:06 +08:00
										 |  |  |         if (check.requireSecret) { | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |           getIt<AuthRouter>().pushEncryptionScreen(context, userProfile); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2023-11-01 11:45:35 +08:00
										 |  |  |           final result = await FolderEventGetCurrentWorkspaceSetting().send(); | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |           result.fold( | 
					
						
							|  |  |  |             (workspaceSetting) { | 
					
						
							| 
									
										
										
										
											2023-09-28 01:17:38 -05:00
										 |  |  |               // After login, replace Splash screen by corresponding home screen
 | 
					
						
							|  |  |  |               getIt<SplashRouter>().goHomeScreen( | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |                 context, | 
					
						
							|  |  |  |               ); | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2023-09-11 21:32:26 -05:00
										 |  |  |             (error) => handleOpenWorkspaceError(context, error), | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |           ); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |       (err) { | 
					
						
							|  |  |  |         Log.error(err); | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void _handleUnauthenticated(BuildContext context, Unauthenticated result) { | 
					
						
							| 
									
										
										
										
											2023-09-28 01:17:38 -05:00
										 |  |  |     // replace Splash screen as root page
 | 
					
						
							| 
									
										
										
										
											2023-12-14 20:33:00 +08:00
										 |  |  |     if (isAuthEnabled || PlatformExtension.isMobile) { | 
					
						
							| 
									
										
										
										
											2023-09-28 01:17:38 -05:00
										 |  |  |       context.go(SignInScreen.routeName); | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2023-09-28 01:17:38 -05:00
										 |  |  |       // if the env is not configured, we will skip to the 'skip login screen'.
 | 
					
						
							|  |  |  |       context.go(SkipLogInScreen.routeName); | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Future<void> _registerIfNeeded() async { | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  |     final result = await UserEventGetUserProfile().send(); | 
					
						
							| 
									
										
										
										
											2024-03-22 16:15:18 +07:00
										 |  |  |     if (result.isFailure) { | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  |       await getIt<AuthService>().signUpAsGuest(); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Body extends StatelessWidget { | 
					
						
							| 
									
										
										
										
											2023-09-11 21:32:26 -05:00
										 |  |  |   const Body({super.key}); | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							|  |  |  |     return Container( | 
					
						
							|  |  |  |       alignment: Alignment.center, | 
					
						
							| 
									
										
										
										
											2023-09-11 21:32:26 -05:00
										 |  |  |       child: PlatformExtension.isMobile | 
					
						
							| 
									
										
										
										
											2024-06-11 22:17:29 +02:00
										 |  |  |           ? const FlowySvg(FlowySvgs.flowy_logo_xl, blendMode: null) | 
					
						
							| 
									
										
										
										
											2023-09-11 21:32:26 -05:00
										 |  |  |           : const _DesktopSplashBody(), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _DesktopSplashBody extends StatelessWidget { | 
					
						
							|  |  |  |   const _DesktopSplashBody(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							|  |  |  |     final size = MediaQuery.of(context).size; | 
					
						
							|  |  |  |     return SingleChildScrollView( | 
					
						
							|  |  |  |       child: Stack( | 
					
						
							|  |  |  |         alignment: Alignment.center, | 
					
						
							|  |  |  |         children: [ | 
					
						
							|  |  |  |           Image( | 
					
						
							|  |  |  |             fit: BoxFit.cover, | 
					
						
							|  |  |  |             width: size.width, | 
					
						
							|  |  |  |             height: size.height, | 
					
						
							|  |  |  |             image: const AssetImage( | 
					
						
							|  |  |  |               'assets/images/appflowy_launch_splash.jpg', | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |             ), | 
					
						
							| 
									
										
										
										
											2023-09-11 21:32:26 -05:00
										 |  |  |           ), | 
					
						
							|  |  |  |           const CircularProgressIndicator.adaptive(), | 
					
						
							|  |  |  |         ], | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |