2023-07-05 20:57:09 +08:00
|
|
|
import 'package:appflowy/env/env.dart';
|
2023-05-21 18:53:59 +08:00
|
|
|
import 'package:appflowy/user/application/auth/auth_service.dart';
|
2023-08-22 00:19:15 +08:00
|
|
|
import 'package:appflowy/user/presentation/sign_in_screen.dart';
|
2023-01-08 12:10:53 +08:00
|
|
|
import 'package:appflowy_backend/dispatch/dispatch.dart';
|
|
|
|
import 'package:appflowy_backend/log.dart';
|
2021-09-06 16:18:34 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
2022-12-20 11:14:42 +08:00
|
|
|
import '../../startup/startup.dart';
|
|
|
|
import '../application/splash_bloc.dart';
|
|
|
|
import '../domain/auth_state.dart';
|
|
|
|
import 'router.dart';
|
|
|
|
|
2021-09-06 16:18:34 +08:00
|
|
|
class SplashScreen extends StatelessWidget {
|
2022-12-20 11:14:42 +08:00
|
|
|
const SplashScreen({
|
|
|
|
Key? key,
|
|
|
|
required this.autoRegister,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
final bool autoRegister;
|
2021-09-06 16:18:34 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-12-20 11:14:42 +08:00
|
|
|
if (!autoRegister) {
|
|
|
|
return _buildChild(context);
|
|
|
|
} else {
|
|
|
|
return FutureBuilder<void>(
|
|
|
|
future: _registerIfNeeded(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.connectionState != ConnectionState.done) {
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
return _buildChild(context);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BlocProvider<SplashBloc> _buildChild(BuildContext context) {
|
2021-09-06 16:18:34 +08:00
|
|
|
return BlocProvider(
|
|
|
|
create: (context) {
|
|
|
|
return getIt<SplashBloc>()..add(const SplashEvent.getUser());
|
|
|
|
},
|
|
|
|
child: Scaffold(
|
|
|
|
body: BlocListener<SplashBloc, SplashState>(
|
|
|
|
listener: (context, state) {
|
|
|
|
state.auth.map(
|
|
|
|
authenticated: (r) => _handleAuthenticated(context, r),
|
|
|
|
unauthenticated: (r) => _handleUnauthenticated(context, r),
|
|
|
|
initial: (r) => {},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Body(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-17 23:46:39 +08:00
|
|
|
/// Handles the authentication flow once a user is authenticated.
|
2023-05-21 18:53:59 +08:00
|
|
|
Future<void> _handleAuthenticated(
|
|
|
|
BuildContext context,
|
|
|
|
Authenticated authenticated,
|
|
|
|
) async {
|
|
|
|
final userProfile = authenticated.userProfile;
|
2023-08-17 23:46:39 +08:00
|
|
|
|
|
|
|
/// After a user is authenticated, this function checks if encryption is required.
|
|
|
|
final result = await UserEventCheckEncryptionSign().send();
|
2023-05-21 18:53:59 +08:00
|
|
|
result.fold(
|
2023-08-17 23:46:39 +08:00
|
|
|
(check) async {
|
|
|
|
/// If encryption is needed, the user is navigated to the encryption screen.
|
|
|
|
/// Otherwise, it fetches the current workspace for the user and navigates them
|
|
|
|
if (check.isNeedSecret) {
|
|
|
|
getIt<AuthRouter>().pushEncryptionScreen(context, userProfile);
|
|
|
|
} else {
|
|
|
|
final result = await FolderEventGetCurrentWorkspace().send();
|
|
|
|
result.fold(
|
|
|
|
(workspaceSetting) {
|
|
|
|
getIt<SplashRoute>().pushHomeScreen(
|
|
|
|
context,
|
|
|
|
userProfile,
|
|
|
|
workspaceSetting,
|
|
|
|
);
|
|
|
|
},
|
2023-08-22 00:19:15 +08:00
|
|
|
(error) {
|
|
|
|
handleOpenWorkspaceError(context, error);
|
2023-08-17 23:46:39 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2021-09-06 16:18:34 +08:00
|
|
|
},
|
2023-08-17 23:46:39 +08:00
|
|
|
(err) {
|
|
|
|
Log.error(err);
|
2023-05-21 18:53:59 +08:00
|
|
|
},
|
2021-09-06 16:18:34 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleUnauthenticated(BuildContext context, Unauthenticated result) {
|
2023-05-21 18:53:59 +08:00
|
|
|
// if the env is not configured, we will skip to the 'skip login screen'.
|
2023-08-03 08:48:04 +08:00
|
|
|
if (isSupabaseEnabled) {
|
2023-05-21 18:53:59 +08:00
|
|
|
getIt<SplashRoute>().pushSignInScreen(context);
|
|
|
|
} else {
|
|
|
|
getIt<SplashRoute>().pushSkipLoginScreen(context);
|
|
|
|
}
|
2021-09-06 16:18:34 +08:00
|
|
|
}
|
2022-12-20 11:14:42 +08:00
|
|
|
|
|
|
|
Future<void> _registerIfNeeded() async {
|
2023-07-09 10:03:22 +07:00
|
|
|
final result = await UserEventGetUserProfile().send();
|
2022-12-20 11:14:42 +08:00
|
|
|
if (!result.isLeft()) {
|
2023-05-21 18:53:59 +08:00
|
|
|
await getIt<AuthService>().signUpAsGuest();
|
2022-12-20 11:14:42 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-06 16:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class Body extends StatelessWidget {
|
|
|
|
const Body({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-06-07 13:55:37 +05:30
|
|
|
final size = MediaQuery.of(context).size;
|
2021-09-06 16:18:34 +08:00
|
|
|
|
|
|
|
return Container(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Stack(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
children: [
|
|
|
|
Image(
|
2022-12-20 11:14:42 +08:00
|
|
|
fit: BoxFit.cover,
|
|
|
|
width: size.width,
|
|
|
|
height: size.height,
|
|
|
|
image:
|
|
|
|
const AssetImage('assets/images/appflowy_launch_splash.jpg'),
|
|
|
|
),
|
2021-09-06 16:18:34 +08:00
|
|
|
const CircularProgressIndicator.adaptive(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|