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/user/presentation/sign_in_screen.dart';
  4. import 'package:appflowy_backend/dispatch/dispatch.dart';
  5. import 'package:appflowy_backend/log.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter_bloc/flutter_bloc.dart';
  8. import '../../startup/startup.dart';
  9. import '../application/splash_bloc.dart';
  10. import '../domain/auth_state.dart';
  11. import 'router.dart';
  12. class SplashScreen extends StatelessWidget {
  13. const SplashScreen({
  14. Key? key,
  15. required this.autoRegister,
  16. }) : super(key: key);
  17. final bool autoRegister;
  18. @override
  19. Widget build(BuildContext context) {
  20. if (!autoRegister) {
  21. return _buildChild(context);
  22. } else {
  23. return FutureBuilder<void>(
  24. future: _registerIfNeeded(),
  25. builder: (context, snapshot) {
  26. if (snapshot.connectionState != ConnectionState.done) {
  27. return Container();
  28. }
  29. return _buildChild(context);
  30. },
  31. );
  32. }
  33. }
  34. BlocProvider<SplashBloc> _buildChild(BuildContext context) {
  35. return BlocProvider(
  36. create: (context) {
  37. return getIt<SplashBloc>()..add(const SplashEvent.getUser());
  38. },
  39. child: Scaffold(
  40. body: BlocListener<SplashBloc, SplashState>(
  41. listener: (context, state) {
  42. state.auth.map(
  43. authenticated: (r) => _handleAuthenticated(context, r),
  44. unauthenticated: (r) => _handleUnauthenticated(context, r),
  45. initial: (r) => {},
  46. );
  47. },
  48. child: const Body(),
  49. ),
  50. ),
  51. );
  52. }
  53. /// Handles the authentication flow once a user is authenticated.
  54. Future<void> _handleAuthenticated(
  55. BuildContext context,
  56. Authenticated authenticated,
  57. ) async {
  58. final userProfile = authenticated.userProfile;
  59. /// After a user is authenticated, this function checks if encryption is required.
  60. final result = await UserEventCheckEncryptionSign().send();
  61. result.fold(
  62. (check) async {
  63. /// If encryption is needed, the user is navigated to the encryption screen.
  64. /// Otherwise, it fetches the current workspace for the user and navigates them
  65. if (check.isNeedSecret) {
  66. getIt<AuthRouter>().pushEncryptionScreen(context, userProfile);
  67. } else {
  68. final result = await FolderEventGetCurrentWorkspace().send();
  69. result.fold(
  70. (workspaceSetting) {
  71. getIt<SplashRoute>().pushHomeScreen(
  72. context,
  73. userProfile,
  74. workspaceSetting,
  75. );
  76. },
  77. (error) {
  78. handleOpenWorkspaceError(context, error);
  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. }