| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | import 'package:app_flowy/startup/startup.dart'; | 
					
						
							| 
									
										
										
										
											2021-10-09 10:09:31 +08:00
										 |  |  | import 'package:app_flowy/user/application/splash_bloc.dart'; | 
					
						
							|  |  |  | import 'package:app_flowy/user/domain/auth_state.dart'; | 
					
						
							| 
									
										
										
										
											2022-03-01 10:48:34 -05:00
										 |  |  | import 'package:app_flowy/user/presentation/router.dart'; | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  | import 'package:flowy_sdk/log.dart'; | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | import 'package:flowy_sdk/dispatch/dispatch.dart'; | 
					
						
							| 
									
										
										
										
											2022-04-07 15:34:00 +08:00
										 |  |  | import 'package:flowy_sdk/protobuf/error-code/error_code.pbenum.dart'; | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | import 'package:flutter_bloc/flutter_bloc.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 { | 
					
						
							|  |  |  |   const SplashScreen({Key? key}) : super(key: key); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							|  |  |  |     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(), | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void _handleAuthenticated(BuildContext context, Authenticated result) { | 
					
						
							|  |  |  |     final userProfile = result.userProfile; | 
					
						
							| 
									
										
										
										
											2022-01-27 20:39:54 +08:00
										 |  |  |     FolderEventReadCurWorkspace().send().then( | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |       (result) { | 
					
						
							|  |  |  |         return result.fold( | 
					
						
							| 
									
										
										
										
											2022-01-31 08:24:29 +08:00
										 |  |  |           (workspaceSetting) => getIt<SplashRoute>().pushHomeScreen(context, userProfile, workspaceSetting), | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |           (error) async { | 
					
						
							| 
									
										
										
										
											2022-01-23 22:33:47 +08:00
										 |  |  |             Log.error(error); | 
					
						
							| 
									
										
										
										
											2021-11-08 10:25:10 +08:00
										 |  |  |             assert(error.code == ErrorCode.RecordNotFound.value); | 
					
						
							| 
									
										
										
										
											2022-01-31 08:24:29 +08:00
										 |  |  |             getIt<SplashRoute>().pushWelcomeScreen(context, userProfile); | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |           }, | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void _handleUnauthenticated(BuildContext context, Unauthenticated result) { | 
					
						
							| 
									
										
										
										
											2022-01-31 08:24:29 +08:00
										 |  |  |     // getIt<SplashRoute>().pushSignInScreen(context);
 | 
					
						
							|  |  |  |     getIt<SplashRoute>().pushSkipLoginScreen(context); | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Body extends StatelessWidget { | 
					
						
							|  |  |  |   const Body({Key? key}) : super(key: key); | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							|  |  |  |     var size = MediaQuery.of(context).size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Container( | 
					
						
							|  |  |  |       alignment: Alignment.center, | 
					
						
							|  |  |  |       child: SingleChildScrollView( | 
					
						
							|  |  |  |         child: Stack( | 
					
						
							|  |  |  |           alignment: Alignment.center, | 
					
						
							|  |  |  |           children: [ | 
					
						
							|  |  |  |             Image( | 
					
						
							|  |  |  |                 fit: BoxFit.cover, | 
					
						
							|  |  |  |                 width: size.width, | 
					
						
							|  |  |  |                 height: size.height, | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  |                 image: const AssetImage('assets/images/appflowy_launch_splash.jpg')), | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |             const CircularProgressIndicator.adaptive(), | 
					
						
							|  |  |  |           ], | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |