2022-02-19 15:19:33 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-01-08 04:10:53 +00:00
|
|
|
import 'package:appflowy_backend/appflowy_backend.dart';
|
2022-02-20 01:58:25 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2021-06-19 15:41:19 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get_it/get_it.dart';
|
2022-12-20 03:14:42 +00:00
|
|
|
|
|
|
|
import '../workspace/application/settings/settings_location_cubit.dart';
|
|
|
|
import 'deps_resolver.dart';
|
|
|
|
import 'launch_configuration.dart';
|
|
|
|
import 'plugin/plugin.dart';
|
|
|
|
import 'tasks/prelude.dart';
|
2021-06-19 15:41:19 +00:00
|
|
|
|
2021-10-11 05:15:41 +00:00
|
|
|
// [[diagram: flowy startup flow]]
|
|
|
|
// ┌──────────┐
|
|
|
|
// │ FlowyApp │
|
|
|
|
// └──────────┘
|
|
|
|
// │ impl
|
|
|
|
// ▼
|
|
|
|
// ┌────────┐ 1.run ┌──────────┐
|
|
|
|
// │ System │───┬───▶│EntryPoint│
|
|
|
|
// └────────┘ │ └──────────┘ ┌─────────────────┐
|
|
|
|
// │ ┌──▶ │ RustSDKInitTask │
|
|
|
|
// │ ┌───────────┐ │ └─────────────────┘
|
|
|
|
// └──▶ │AppLauncher│───┤
|
|
|
|
// 2.launch └───────────┘ │ ┌─────────────┐ ┌──────────────────┐ ┌───────────────┐
|
|
|
|
// └───▶│AppWidgetTask│────────▶│ApplicationWidget │─────▶│ SplashScreen │
|
|
|
|
// └─────────────┘ └──────────────────┘ └───────────────┘
|
|
|
|
//
|
2023-03-20 13:29:17 +00:00
|
|
|
// 3.build MaterialApp
|
2021-06-19 15:41:19 +00:00
|
|
|
final getIt = GetIt.instance;
|
|
|
|
|
2021-09-12 14:19:59 +00:00
|
|
|
abstract class EntryPoint {
|
2022-12-20 03:14:42 +00:00
|
|
|
Widget create(LaunchConfiguration config);
|
2021-06-19 15:41:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-20 08:10:50 +00:00
|
|
|
class FlowyRunner {
|
2022-12-20 03:14:42 +00:00
|
|
|
static Future<void> run(
|
|
|
|
EntryPoint f, {
|
|
|
|
LaunchConfiguration config =
|
|
|
|
const LaunchConfiguration(autoRegistrationSupported: false),
|
|
|
|
}) async {
|
|
|
|
// Clear all the states in case of rebuilding.
|
|
|
|
await getIt.reset();
|
|
|
|
|
2021-11-19 07:23:58 +00:00
|
|
|
// Specify the env
|
2022-02-19 15:19:33 +00:00
|
|
|
final env = integrationEnv();
|
2022-12-20 03:14:42 +00:00
|
|
|
initGetIt(getIt, env, f, config);
|
|
|
|
|
|
|
|
final directory = getIt<SettingsLocationCubit>()
|
|
|
|
.fetchLocation()
|
|
|
|
.then((value) => Directory(value));
|
2021-06-19 15:41:19 +00:00
|
|
|
|
|
|
|
// add task
|
2022-12-20 03:14:42 +00:00
|
|
|
getIt<AppLauncher>().addTask(InitRustSDKTask(directory: directory));
|
2022-10-25 08:51:51 +00:00
|
|
|
getIt<AppLauncher>().addTask(PluginLoadTask());
|
2022-02-19 15:19:33 +00:00
|
|
|
|
|
|
|
if (!env.isTest()) {
|
2022-02-20 08:10:50 +00:00
|
|
|
getIt<AppLauncher>().addTask(InitAppWidgetTask());
|
|
|
|
getIt<AppLauncher>().addTask(InitPlatformServiceTask());
|
2022-02-19 15:19:33 +00:00
|
|
|
}
|
2021-06-19 15:41:19 +00:00
|
|
|
|
|
|
|
// execute the tasks
|
2022-12-20 03:14:42 +00:00
|
|
|
await getIt<AppLauncher>().launch();
|
2021-06-19 15:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-11 05:15:41 +00:00
|
|
|
|
|
|
|
Future<void> initGetIt(
|
|
|
|
GetIt getIt,
|
2022-02-20 08:34:15 +00:00
|
|
|
IntegrationMode env,
|
2022-02-20 00:35:52 +00:00
|
|
|
EntryPoint f,
|
2022-12-20 03:14:42 +00:00
|
|
|
LaunchConfiguration config,
|
2021-10-11 05:15:41 +00:00
|
|
|
) async {
|
2022-02-20 00:35:52 +00:00
|
|
|
getIt.registerFactory<EntryPoint>(() => f);
|
2022-12-20 03:14:42 +00:00
|
|
|
getIt.registerLazySingleton<FlowySDK>(() {
|
|
|
|
return FlowySDK();
|
|
|
|
});
|
|
|
|
getIt.registerLazySingleton<AppLauncher>(
|
|
|
|
() => AppLauncher(
|
|
|
|
context: LaunchContext(
|
|
|
|
getIt,
|
|
|
|
env,
|
|
|
|
config,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2022-02-28 14:38:53 +00:00
|
|
|
getIt.registerSingleton<PluginSandbox>(PluginSandbox());
|
2021-10-11 05:15:41 +00:00
|
|
|
|
2022-03-25 07:02:43 +00:00
|
|
|
await DependencyResolver.resolve(getIt);
|
2021-10-11 05:15:41 +00:00
|
|
|
}
|
2022-02-19 05:52:52 +00:00
|
|
|
|
|
|
|
class LaunchContext {
|
|
|
|
GetIt getIt;
|
2022-02-20 08:34:15 +00:00
|
|
|
IntegrationMode env;
|
2022-12-20 03:14:42 +00:00
|
|
|
LaunchConfiguration config;
|
|
|
|
LaunchContext(this.getIt, this.env, this.config);
|
2022-02-19 05:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum LaunchTaskType {
|
|
|
|
dataProcessing,
|
|
|
|
appLauncher,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The interface of an app launch task, which will trigger
|
|
|
|
/// some nonresident indispensable task in app launching task.
|
|
|
|
abstract class LaunchTask {
|
|
|
|
LaunchTaskType get type => LaunchTaskType.dataProcessing;
|
|
|
|
Future<void> initialize(LaunchContext context);
|
|
|
|
}
|
|
|
|
|
|
|
|
class AppLauncher {
|
|
|
|
List<LaunchTask> tasks;
|
|
|
|
|
2022-12-20 03:14:42 +00:00
|
|
|
final LaunchContext context;
|
|
|
|
|
|
|
|
AppLauncher({required this.context}) : tasks = List.from([]);
|
2022-02-19 05:52:52 +00:00
|
|
|
|
|
|
|
void addTask(LaunchTask task) {
|
|
|
|
tasks.add(task);
|
|
|
|
}
|
|
|
|
|
2022-02-19 09:12:44 +00:00
|
|
|
Future<void> launch() async {
|
2022-02-19 05:52:52 +00:00
|
|
|
for (var task in tasks) {
|
|
|
|
await task.initialize(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-19 15:19:33 +00:00
|
|
|
|
2022-02-20 08:34:15 +00:00
|
|
|
enum IntegrationMode {
|
2022-02-19 15:19:33 +00:00
|
|
|
develop,
|
|
|
|
release,
|
|
|
|
test,
|
|
|
|
}
|
|
|
|
|
2022-02-20 08:34:15 +00:00
|
|
|
extension IntegrationEnvExt on IntegrationMode {
|
2022-02-19 15:19:33 +00:00
|
|
|
bool isTest() {
|
2022-02-20 08:34:15 +00:00
|
|
|
return this == IntegrationMode.test;
|
2022-02-19 15:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 08:34:15 +00:00
|
|
|
IntegrationMode integrationEnv() {
|
2022-02-19 15:19:33 +00:00
|
|
|
if (Platform.environment.containsKey('FLUTTER_TEST')) {
|
2022-02-20 08:34:15 +00:00
|
|
|
return IntegrationMode.test;
|
2022-02-19 15:19:33 +00:00
|
|
|
}
|
2022-02-20 01:58:25 +00:00
|
|
|
|
|
|
|
if (kReleaseMode) {
|
2022-02-20 08:34:15 +00:00
|
|
|
return IntegrationMode.release;
|
2022-02-19 15:19:33 +00:00
|
|
|
}
|
|
|
|
|
2022-02-20 08:34:15 +00:00
|
|
|
return IntegrationMode.develop;
|
2022-02-19 15:19:33 +00:00
|
|
|
}
|