| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  | import 'dart:io'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | import 'package:app_flowy/plugin/plugin.dart'; | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  | import 'package:app_flowy/startup/tasks/prelude.dart'; | 
					
						
							| 
									
										
										
										
											2022-03-25 15:02:43 +08:00
										 |  |  | import 'package:app_flowy/startup/deps_resolver.dart'; | 
					
						
							| 
									
										
										
										
											2022-02-20 09:58:25 +08:00
										 |  |  | import 'package:flutter/foundation.dart'; | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | import 'package:get_it/get_it.dart'; | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  | import 'package:flowy_sdk/flowy_sdk.dart'; | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  | // [[diagram: flowy startup flow]]
 | 
					
						
							|  |  |  | //                   ┌──────────┐
 | 
					
						
							|  |  |  | //                   │ FlowyApp │
 | 
					
						
							|  |  |  | //                   └──────────┘
 | 
					
						
							|  |  |  | //                         │  impl
 | 
					
						
							|  |  |  | //                         ▼
 | 
					
						
							|  |  |  | // ┌────────┐  1.run ┌──────────┐
 | 
					
						
							|  |  |  | // │ System │───┬───▶│EntryPoint│
 | 
					
						
							|  |  |  | // └────────┘   │    └──────────┘         ┌─────────────────┐
 | 
					
						
							|  |  |  | //              │                    ┌──▶ │ RustSDKInitTask │
 | 
					
						
							|  |  |  | //              │    ┌───────────┐   │    └─────────────────┘
 | 
					
						
							|  |  |  | //              └──▶ │AppLauncher│───┤
 | 
					
						
							|  |  |  | //        2.launch   └───────────┘   │    ┌─────────────┐         ┌──────────────────┐      ┌───────────────┐
 | 
					
						
							|  |  |  | //                                   └───▶│AppWidgetTask│────────▶│ApplicationWidget │─────▶│ SplashScreen  │
 | 
					
						
							|  |  |  | //                                        └─────────────┘         └──────────────────┘      └───────────────┘
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //                                                 3.build MeterialApp
 | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  | final getIt = GetIt.instance; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-12 22:19:59 +08:00
										 |  |  | abstract class EntryPoint { | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  |   Widget create(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 16:10:50 +08:00
										 |  |  | class FlowyRunner { | 
					
						
							| 
									
										
										
										
											2022-02-19 17:12:44 +08:00
										 |  |  |   static Future<void> run(EntryPoint f) async { | 
					
						
							| 
									
										
										
										
											2021-11-19 10:23:58 +03:00
										 |  |  |     // Specify the env
 | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  |     final env = integrationEnv(); | 
					
						
							| 
									
										
										
										
											2022-02-20 08:35:52 +08:00
										 |  |  |     initGetIt(getIt, env, f); | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // add task
 | 
					
						
							| 
									
										
										
										
											2021-12-04 22:24:32 +08:00
										 |  |  |     getIt<AppLauncher>().addTask(InitRustSDKTask()); | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!env.isTest()) { | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  |       getIt<AppLauncher>().addTask(PluginLoadTask()); | 
					
						
							| 
									
										
										
										
											2022-02-20 16:10:50 +08:00
										 |  |  |       getIt<AppLauncher>().addTask(InitAppWidgetTask()); | 
					
						
							|  |  |  |       getIt<AppLauncher>().addTask(InitPlatformServiceTask()); | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // execute the tasks
 | 
					
						
							|  |  |  |     getIt<AppLauncher>().launch(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Future<void> initGetIt( | 
					
						
							|  |  |  |   GetIt getIt, | 
					
						
							| 
									
										
										
										
											2022-02-20 16:34:15 +08:00
										 |  |  |   IntegrationMode env, | 
					
						
							| 
									
										
										
										
											2022-02-20 08:35:52 +08:00
										 |  |  |   EntryPoint f, | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  | ) async { | 
					
						
							| 
									
										
										
										
											2022-02-20 08:35:52 +08:00
										 |  |  |   getIt.registerFactory<EntryPoint>(() => f); | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  |   getIt.registerLazySingleton<FlowySDK>(() => const FlowySDK()); | 
					
						
							|  |  |  |   getIt.registerLazySingleton<AppLauncher>(() => AppLauncher(env, getIt)); | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  |   getIt.registerSingleton<PluginSandbox>(PluginSandbox()); | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:02:43 +08:00
										 |  |  |   await DependencyResolver.resolve(getIt); | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class LaunchContext { | 
					
						
							|  |  |  |   GetIt getIt; | 
					
						
							| 
									
										
										
										
											2022-02-20 16:34:15 +08:00
										 |  |  |   IntegrationMode env; | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  |   LaunchContext(this.getIt, this.env); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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-02-20 16:34:15 +08:00
										 |  |  |   IntegrationMode env; | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  |   GetIt getIt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   AppLauncher(this.env, this.getIt) : tasks = List.from([]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void addTask(LaunchTask task) { | 
					
						
							|  |  |  |     tasks.add(task); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-19 17:12:44 +08:00
										 |  |  |   Future<void> launch() async { | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  |     final context = LaunchContext(getIt, env); | 
					
						
							|  |  |  |     for (var task in tasks) { | 
					
						
							|  |  |  |       await task.initialize(context); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 16:34:15 +08:00
										 |  |  | enum IntegrationMode { | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  |   develop, | 
					
						
							|  |  |  |   release, | 
					
						
							|  |  |  |   test, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 16:34:15 +08:00
										 |  |  | extension IntegrationEnvExt on IntegrationMode { | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  |   bool isTest() { | 
					
						
							| 
									
										
										
										
											2022-02-20 16:34:15 +08:00
										 |  |  |     return this == IntegrationMode.test; | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 16:34:15 +08:00
										 |  |  | IntegrationMode integrationEnv() { | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  |   if (Platform.environment.containsKey('FLUTTER_TEST')) { | 
					
						
							| 
									
										
										
										
											2022-02-20 16:34:15 +08:00
										 |  |  |     return IntegrationMode.test; | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-02-20 09:58:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (kReleaseMode) { | 
					
						
							| 
									
										
										
										
											2022-02-20 16:34:15 +08:00
										 |  |  |     return IntegrationMode.release; | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 16:34:15 +08:00
										 |  |  |   return IntegrationMode.develop; | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  | } |