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, {
|
2023-05-17 03:03:33 +00:00
|
|
|
LaunchConfiguration config = const LaunchConfiguration(
|
|
|
|
autoRegistrationSupported: false,
|
|
|
|
),
|
2022-12-20 03:14:42 +00:00
|
|
|
}) 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
|
2023-05-17 03:03:33 +00:00
|
|
|
final launcher = getIt<AppLauncher>();
|
|
|
|
launcher.addTasks(
|
|
|
|
[
|
|
|
|
// handle platform errors.
|
|
|
|
const PlatformErrorCatcherTask(),
|
|
|
|
// localization
|
|
|
|
const InitLocalizationTask(),
|
|
|
|
// init the app window
|
|
|
|
const InitAppWindowTask(),
|
|
|
|
// Init Rust SDK
|
|
|
|
InitRustSDKTask(directory: directory),
|
|
|
|
// Load Plugins, like document, grid ...
|
|
|
|
const PluginLoadTask(),
|
|
|
|
|
|
|
|
// init the app widget
|
|
|
|
// ignore in test mode
|
|
|
|
if (!env.isTest()) ...[
|
|
|
|
const HotKeyTask(),
|
|
|
|
const InitAppWidgetTask(),
|
|
|
|
const InitPlatformServiceTask()
|
|
|
|
],
|
|
|
|
],
|
|
|
|
);
|
|
|
|
await launcher.launch(); // execute the tasks
|
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 {
|
2023-05-17 03:03:33 +00:00
|
|
|
const LaunchTask();
|
|
|
|
|
2022-02-19 05:52:52 +00:00
|
|
|
LaunchTaskType get type => LaunchTaskType.dataProcessing;
|
2023-05-17 03:03:33 +00:00
|
|
|
|
2022-02-19 05:52:52 +00:00
|
|
|
Future<void> initialize(LaunchContext context);
|
|
|
|
}
|
|
|
|
|
|
|
|
class AppLauncher {
|
2023-05-17 03:03:33 +00:00
|
|
|
AppLauncher({
|
|
|
|
required this.context,
|
|
|
|
});
|
2022-02-19 05:52:52 +00:00
|
|
|
|
2022-12-20 03:14:42 +00:00
|
|
|
final LaunchContext context;
|
2023-05-17 03:03:33 +00:00
|
|
|
final List<LaunchTask> tasks = [];
|
2022-02-19 05:52:52 +00:00
|
|
|
|
|
|
|
void addTask(LaunchTask task) {
|
|
|
|
tasks.add(task);
|
|
|
|
}
|
|
|
|
|
2023-05-17 03:03:33 +00:00
|
|
|
void addTasks(Iterable<LaunchTask> tasks) {
|
|
|
|
this.tasks.addAll(tasks);
|
|
|
|
}
|
|
|
|
|
2022-02-19 09:12:44 +00:00
|
|
|
Future<void> launch() async {
|
2023-05-17 03:03:33 +00:00
|
|
|
for (final task in tasks) {
|
2022-02-19 05:52:52 +00:00
|
|
|
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
|
|
|
}
|