splash_screen.dart 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import 'package:app_flowy/startup/startup.dart';
  2. import 'package:app_flowy/user/application/splash_bloc.dart';
  3. import 'package:app_flowy/user/domain/auth_state.dart';
  4. import 'package:app_flowy/user/presentation/router.dart';
  5. import 'package:flowy_sdk/log.dart';
  6. import 'package:flowy_sdk/dispatch/dispatch.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-error-code/code.pb.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter_bloc/flutter_bloc.dart';
  10. // [[diagram: splash screen]]
  11. // ┌────────────────┐1.get user ┌──────────┐ ┌────────────┐ 2.send UserEventCheckUser
  12. // │ SplashScreen │──────────▶│SplashBloc│────▶│ISplashUser │─────┐
  13. // └────────────────┘ └──────────┘ └────────────┘ │
  14. // │
  15. // ▼
  16. // ┌───────────┐ ┌─────────────┐ ┌────────┐
  17. // │HomeScreen │◀───────────│BlocListener │◀────────────────│RustSDK │
  18. // └───────────┘ └─────────────┘ └────────┘
  19. // 4. Show HomeScreen or SignIn 3.return AuthState
  20. class SplashScreen extends StatelessWidget {
  21. const SplashScreen({Key? key}) : super(key: key);
  22. @override
  23. Widget build(BuildContext context) {
  24. return BlocProvider(
  25. create: (context) {
  26. return getIt<SplashBloc>()..add(const SplashEvent.getUser());
  27. },
  28. child: Scaffold(
  29. body: BlocListener<SplashBloc, SplashState>(
  30. listener: (context, state) {
  31. state.auth.map(
  32. authenticated: (r) => _handleAuthenticated(context, r),
  33. unauthenticated: (r) => _handleUnauthenticated(context, r),
  34. initial: (r) => {},
  35. );
  36. },
  37. child: const Body(),
  38. ),
  39. ),
  40. );
  41. }
  42. void _handleAuthenticated(BuildContext context, Authenticated result) {
  43. final userProfile = result.userProfile;
  44. FolderEventReadCurrentWorkspace().send().then(
  45. (result) {
  46. return result.fold(
  47. (workspaceSetting) => getIt<SplashRoute>()
  48. .pushHomeScreen(context, userProfile, workspaceSetting),
  49. (error) async {
  50. Log.error(error);
  51. assert(error.code == ErrorCode.RecordNotFound.value);
  52. getIt<SplashRoute>().pushWelcomeScreen(context, userProfile);
  53. },
  54. );
  55. },
  56. );
  57. }
  58. void _handleUnauthenticated(BuildContext context, Unauthenticated result) {
  59. // getIt<SplashRoute>().pushSignInScreen(context);
  60. getIt<SplashRoute>().pushSkipLoginScreen(context);
  61. }
  62. }
  63. class Body extends StatelessWidget {
  64. const Body({Key? key}) : super(key: key);
  65. @override
  66. Widget build(BuildContext context) {
  67. var size = MediaQuery.of(context).size;
  68. return Container(
  69. alignment: Alignment.center,
  70. child: SingleChildScrollView(
  71. child: Stack(
  72. alignment: Alignment.center,
  73. children: [
  74. Image(
  75. fit: BoxFit.cover,
  76. width: size.width,
  77. height: size.height,
  78. image: const AssetImage(
  79. 'assets/images/appflowy_launch_splash.jpg')),
  80. const CircularProgressIndicator.adaptive(),
  81. ],
  82. ),
  83. ),
  84. );
  85. }
  86. }