splash_screen.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'package:appflowy/env/env.dart';
  2. import 'package:appflowy/user/application/auth/auth_service.dart';
  3. import 'package:appflowy_backend/dispatch/dispatch.dart';
  4. import 'package:appflowy_backend/log.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. import '../../startup/startup.dart';
  8. import '../application/splash_bloc.dart';
  9. import '../domain/auth_state.dart';
  10. import 'router.dart';
  11. // [[diagram: splash screen]]
  12. // ┌────────────────┐1.get user ┌──────────┐ ┌────────────┐ 2.send UserEventCheckUser
  13. // │ SplashScreen │──────────▶│SplashBloc│────▶│ISplashUser │─────┐
  14. // └────────────────┘ └──────────┘ └────────────┘ │
  15. // │
  16. // ▼
  17. // ┌───────────┐ ┌─────────────┐ ┌────────┐
  18. // │HomeScreen │◀───────────│BlocListener │◀────────────────│RustSDK │
  19. // └───────────┘ └─────────────┘ └────────┘
  20. // 4. Show HomeScreen or SignIn 3.return AuthState
  21. class SplashScreen extends StatelessWidget {
  22. const SplashScreen({
  23. Key? key,
  24. required this.autoRegister,
  25. }) : super(key: key);
  26. final bool autoRegister;
  27. @override
  28. Widget build(BuildContext context) {
  29. if (!autoRegister) {
  30. return _buildChild(context);
  31. } else {
  32. return FutureBuilder<void>(
  33. future: _registerIfNeeded(),
  34. builder: (context, snapshot) {
  35. if (snapshot.connectionState != ConnectionState.done) {
  36. return Container();
  37. }
  38. return _buildChild(context);
  39. },
  40. );
  41. }
  42. }
  43. BlocProvider<SplashBloc> _buildChild(BuildContext context) {
  44. return BlocProvider(
  45. create: (context) {
  46. return getIt<SplashBloc>()..add(const SplashEvent.getUser());
  47. },
  48. child: Scaffold(
  49. body: BlocListener<SplashBloc, SplashState>(
  50. listener: (context, state) {
  51. state.auth.map(
  52. authenticated: (r) => _handleAuthenticated(context, r),
  53. unauthenticated: (r) => _handleUnauthenticated(context, r),
  54. initial: (r) => {},
  55. );
  56. },
  57. child: const Body(),
  58. ),
  59. ),
  60. );
  61. }
  62. Future<void> _handleAuthenticated(
  63. BuildContext context,
  64. Authenticated authenticated,
  65. ) async {
  66. final userProfile = authenticated.userProfile;
  67. final result = await FolderEventGetCurrentWorkspace().send();
  68. result.fold(
  69. (workspaceSetting) {
  70. getIt<SplashRoute>().pushHomeScreen(
  71. context,
  72. userProfile,
  73. workspaceSetting,
  74. );
  75. },
  76. (error) async {
  77. Log.error(error);
  78. getIt<SplashRoute>().pushWelcomeScreen(context, userProfile);
  79. },
  80. );
  81. }
  82. void _handleUnauthenticated(BuildContext context, Unauthenticated result) {
  83. // if the env is not configured, we will skip to the 'skip login screen'.
  84. if (isSupabaseEnable) {
  85. getIt<SplashRoute>().pushSignInScreen(context);
  86. } else {
  87. getIt<SplashRoute>().pushSkipLoginScreen(context);
  88. }
  89. }
  90. Future<void> _registerIfNeeded() async {
  91. final result = await UserEventGetUserProfile().send();
  92. if (!result.isLeft()) {
  93. await getIt<AuthService>().signUpAsGuest();
  94. }
  95. }
  96. }
  97. class Body extends StatelessWidget {
  98. const Body({Key? key}) : super(key: key);
  99. @override
  100. Widget build(BuildContext context) {
  101. final size = MediaQuery.of(context).size;
  102. return Container(
  103. alignment: Alignment.center,
  104. child: SingleChildScrollView(
  105. child: Stack(
  106. alignment: Alignment.center,
  107. children: [
  108. Image(
  109. fit: BoxFit.cover,
  110. width: size.width,
  111. height: size.height,
  112. image:
  113. const AssetImage('assets/images/appflowy_launch_splash.jpg'),
  114. ),
  115. const CircularProgressIndicator.adaptive(),
  116. ],
  117. ),
  118. ),
  119. );
  120. }
  121. }