splash_screen.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'package:appflowy_backend/dispatch/dispatch.dart';
  2. import 'package:appflowy_backend/log.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_bloc/flutter_bloc.dart';
  5. import '../../startup/startup.dart';
  6. import '../application/auth_service.dart';
  7. import '../application/splash_bloc.dart';
  8. import '../domain/auth_state.dart';
  9. import 'router.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({
  22. Key? key,
  23. required this.autoRegister,
  24. }) : super(key: key);
  25. final bool autoRegister;
  26. @override
  27. Widget build(BuildContext context) {
  28. if (!autoRegister) {
  29. return _buildChild(context);
  30. } else {
  31. return FutureBuilder<void>(
  32. future: _registerIfNeeded(),
  33. builder: (context, snapshot) {
  34. if (snapshot.connectionState != ConnectionState.done) {
  35. return Container();
  36. }
  37. return _buildChild(context);
  38. },
  39. );
  40. }
  41. }
  42. BlocProvider<SplashBloc> _buildChild(BuildContext context) {
  43. return BlocProvider(
  44. create: (context) {
  45. return getIt<SplashBloc>()..add(const SplashEvent.getUser());
  46. },
  47. child: Scaffold(
  48. body: BlocListener<SplashBloc, SplashState>(
  49. listener: (context, state) {
  50. state.auth.map(
  51. authenticated: (r) => _handleAuthenticated(context, r),
  52. unauthenticated: (r) => _handleUnauthenticated(context, r),
  53. initial: (r) => {},
  54. );
  55. },
  56. child: const Body(),
  57. ),
  58. ),
  59. );
  60. }
  61. void _handleAuthenticated(BuildContext context, Authenticated result) {
  62. final userProfile = result.userProfile;
  63. FolderEventReadCurrentWorkspace().send().then(
  64. (result) {
  65. return result.fold(
  66. (workspaceSetting) {
  67. getIt<SplashRoute>()
  68. .pushHomeScreen(context, userProfile, workspaceSetting);
  69. },
  70. (error) async {
  71. Log.error(error);
  72. getIt<SplashRoute>().pushWelcomeScreen(context, userProfile);
  73. },
  74. );
  75. },
  76. );
  77. }
  78. void _handleUnauthenticated(BuildContext context, Unauthenticated result) {
  79. // getIt<SplashRoute>().pushSignInScreen(context);
  80. getIt<SplashRoute>().pushSkipLoginScreen(context);
  81. }
  82. Future<void> _registerIfNeeded() async {
  83. final result = await UserEventCheckUser().send();
  84. if (!result.isLeft()) {
  85. await getIt<AuthService>().autoSignUp();
  86. }
  87. }
  88. }
  89. class Body extends StatelessWidget {
  90. const Body({Key? key}) : super(key: key);
  91. @override
  92. Widget build(BuildContext context) {
  93. var size = MediaQuery.of(context).size;
  94. return Container(
  95. alignment: Alignment.center,
  96. child: SingleChildScrollView(
  97. child: Stack(
  98. alignment: Alignment.center,
  99. children: [
  100. Image(
  101. fit: BoxFit.cover,
  102. width: size.width,
  103. height: size.height,
  104. image:
  105. const AssetImage('assets/images/appflowy_launch_splash.jpg'),
  106. ),
  107. const CircularProgressIndicator.adaptive(),
  108. ],
  109. ),
  110. ),
  111. );
  112. }
  113. }