splash_screen.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. class SplashScreen extends StatelessWidget {
  12. const SplashScreen({
  13. Key? key,
  14. required this.autoRegister,
  15. }) : super(key: key);
  16. final bool autoRegister;
  17. @override
  18. Widget build(BuildContext context) {
  19. if (!autoRegister) {
  20. return _buildChild(context);
  21. } else {
  22. return FutureBuilder<void>(
  23. future: _registerIfNeeded(),
  24. builder: (context, snapshot) {
  25. if (snapshot.connectionState != ConnectionState.done) {
  26. return Container();
  27. }
  28. return _buildChild(context);
  29. },
  30. );
  31. }
  32. }
  33. BlocProvider<SplashBloc> _buildChild(BuildContext context) {
  34. return BlocProvider(
  35. create: (context) {
  36. return getIt<SplashBloc>()..add(const SplashEvent.getUser());
  37. },
  38. child: Scaffold(
  39. body: BlocListener<SplashBloc, SplashState>(
  40. listener: (context, state) {
  41. state.auth.map(
  42. authenticated: (r) => _handleAuthenticated(context, r),
  43. unauthenticated: (r) => _handleUnauthenticated(context, r),
  44. initial: (r) => {},
  45. );
  46. },
  47. child: const Body(),
  48. ),
  49. ),
  50. );
  51. }
  52. /// Handles the authentication flow once a user is authenticated.
  53. Future<void> _handleAuthenticated(
  54. BuildContext context,
  55. Authenticated authenticated,
  56. ) async {
  57. final userProfile = authenticated.userProfile;
  58. /// After a user is authenticated, this function checks if encryption is required.
  59. final result = await UserEventCheckEncryptionSign().send();
  60. result.fold(
  61. (check) async {
  62. /// If encryption is needed, the user is navigated to the encryption screen.
  63. /// Otherwise, it fetches the current workspace for the user and navigates them
  64. if (check.isNeedSecret) {
  65. getIt<AuthRouter>().pushEncryptionScreen(context, userProfile);
  66. } else {
  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. },
  83. (err) {
  84. Log.error(err);
  85. },
  86. );
  87. }
  88. void _handleUnauthenticated(BuildContext context, Unauthenticated result) {
  89. // if the env is not configured, we will skip to the 'skip login screen'.
  90. if (isSupabaseEnabled) {
  91. getIt<SplashRoute>().pushSignInScreen(context);
  92. } else {
  93. getIt<SplashRoute>().pushSkipLoginScreen(context);
  94. }
  95. }
  96. Future<void> _registerIfNeeded() async {
  97. final result = await UserEventGetUserProfile().send();
  98. if (!result.isLeft()) {
  99. await getIt<AuthService>().signUpAsGuest();
  100. }
  101. }
  102. }
  103. class Body extends StatelessWidget {
  104. const Body({Key? key}) : super(key: key);
  105. @override
  106. Widget build(BuildContext context) {
  107. final size = MediaQuery.of(context).size;
  108. return Container(
  109. alignment: Alignment.center,
  110. child: SingleChildScrollView(
  111. child: Stack(
  112. alignment: Alignment.center,
  113. children: [
  114. Image(
  115. fit: BoxFit.cover,
  116. width: size.width,
  117. height: size.height,
  118. image:
  119. const AssetImage('assets/images/appflowy_launch_splash.jpg'),
  120. ),
  121. const CircularProgressIndicator.adaptive(),
  122. ],
  123. ),
  124. ),
  125. );
  126. }
  127. }