| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  | import 'package:appflowy/startup/tasks/supabase_task.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/user/application/auth/auth_service.dart'; | 
					
						
							| 
									
										
										
										
											2023-01-08 12:10:53 +08:00
										 |  |  | import 'package:appflowy_backend/dispatch/dispatch.dart'; | 
					
						
							|  |  |  | import 'package:appflowy_backend/log.dart'; | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | import 'package:flutter_bloc/flutter_bloc.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | import '../../startup/startup.dart'; | 
					
						
							|  |  |  | import '../application/splash_bloc.dart'; | 
					
						
							|  |  |  | import '../domain/auth_state.dart'; | 
					
						
							|  |  |  | import 'router.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  | // [[diagram: splash screen]]
 | 
					
						
							|  |  |  | // ┌────────────────┐1.get user ┌──────────┐     ┌────────────┐ 2.send UserEventCheckUser
 | 
					
						
							|  |  |  | // │  SplashScreen  │──────────▶│SplashBloc│────▶│ISplashUser │─────┐
 | 
					
						
							|  |  |  | // └────────────────┘           └──────────┘     └────────────┘     │
 | 
					
						
							|  |  |  | //                                                                  │
 | 
					
						
							|  |  |  | //                                                                  ▼
 | 
					
						
							|  |  |  | //    ┌───────────┐            ┌─────────────┐                 ┌────────┐
 | 
					
						
							|  |  |  | //    │HomeScreen │◀───────────│BlocListener │◀────────────────│RustSDK │
 | 
					
						
							|  |  |  | //    └───────────┘            └─────────────┘                 └────────┘
 | 
					
						
							|  |  |  | //           4. Show HomeScreen or SignIn      3.return AuthState
 | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | class SplashScreen extends StatelessWidget { | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |   const SplashScreen({ | 
					
						
							|  |  |  |     Key? key, | 
					
						
							|  |  |  |     required this.autoRegister, | 
					
						
							|  |  |  |   }) : super(key: key); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final bool autoRegister; | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |     if (!autoRegister) { | 
					
						
							|  |  |  |       return _buildChild(context); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return FutureBuilder<void>( | 
					
						
							|  |  |  |         future: _registerIfNeeded(), | 
					
						
							|  |  |  |         builder: (context, snapshot) { | 
					
						
							|  |  |  |           if (snapshot.connectionState != ConnectionState.done) { | 
					
						
							|  |  |  |             return Container(); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           return _buildChild(context); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   BlocProvider<SplashBloc> _buildChild(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |     return BlocProvider( | 
					
						
							|  |  |  |       create: (context) { | 
					
						
							|  |  |  |         return getIt<SplashBloc>()..add(const SplashEvent.getUser()); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       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-05-21 18:53:59 +08:00
										 |  |  |   Future<void> _handleAuthenticated( | 
					
						
							|  |  |  |     BuildContext context, | 
					
						
							|  |  |  |     Authenticated authenticated, | 
					
						
							|  |  |  |   ) async { | 
					
						
							|  |  |  |     final userProfile = authenticated.userProfile; | 
					
						
							| 
									
										
										
										
											2023-06-05 13:10:14 +08:00
										 |  |  |     final result = await FolderEventGetCurrentWorkspace().send(); | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  |     result.fold( | 
					
						
							|  |  |  |       (workspaceSetting) { | 
					
						
							|  |  |  |         getIt<SplashRoute>().pushHomeScreen( | 
					
						
							|  |  |  |           context, | 
					
						
							|  |  |  |           userProfile, | 
					
						
							|  |  |  |           workspaceSetting, | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |         ); | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  |       (error) async { | 
					
						
							|  |  |  |         Log.error(error); | 
					
						
							|  |  |  |         getIt<SplashRoute>().pushWelcomeScreen(context, userProfile); | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void _handleUnauthenticated(BuildContext context, Unauthenticated result) { | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  |     // if the env is not configured, we will skip to the 'skip login screen'.
 | 
					
						
							|  |  |  |     if (isSupabaseEnable) { | 
					
						
							|  |  |  |       getIt<SplashRoute>().pushSignInScreen(context); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       getIt<SplashRoute>().pushSkipLoginScreen(context); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Future<void> _registerIfNeeded() async { | 
					
						
							|  |  |  |     final result = await UserEventCheckUser().send(); | 
					
						
							|  |  |  |     if (!result.isLeft()) { | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							|  |  |  |   const Body({Key? key}) : super(key: key); | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2023-06-07 13:55:37 +05:30
										 |  |  |     final size = MediaQuery.of(context).size; | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return Container( | 
					
						
							|  |  |  |       alignment: Alignment.center, | 
					
						
							|  |  |  |       child: SingleChildScrollView( | 
					
						
							|  |  |  |         child: Stack( | 
					
						
							|  |  |  |           alignment: Alignment.center, | 
					
						
							|  |  |  |           children: [ | 
					
						
							|  |  |  |             Image( | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |               fit: BoxFit.cover, | 
					
						
							|  |  |  |               width: size.width, | 
					
						
							|  |  |  |               height: size.height, | 
					
						
							|  |  |  |               image: | 
					
						
							|  |  |  |                   const AssetImage('assets/images/appflowy_launch_splash.jpg'), | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |             const CircularProgressIndicator.adaptive(), | 
					
						
							|  |  |  |           ], | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |