| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  | import 'dart:io'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  | import 'package:appflowy/workspace/application/settings/prelude.dart'; | 
					
						
							| 
									
										
										
										
											2023-01-08 12:10:53 +08:00
										 |  |  | import 'package:appflowy_backend/appflowy_backend.dart'; | 
					
						
							| 
									
										
										
										
											2023-10-24 23:13:51 +08:00
										 |  |  | import 'package:appflowy_backend/log.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'; | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import 'deps_resolver.dart'; | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  | import 'entry_point.dart'; | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | import 'launch_configuration.dart'; | 
					
						
							|  |  |  | import 'plugin/plugin.dart'; | 
					
						
							| 
									
										
										
										
											2023-10-12 20:25:00 +08:00
										 |  |  | import 'tasks/appflowy_cloud_task.dart'; | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | import 'tasks/prelude.dart'; | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | final getIt = GetIt.instance; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-12 22:19:59 +08:00
										 |  |  | abstract class EntryPoint { | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |   Widget create(LaunchConfiguration config); | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  | class FlowyRunnerContext { | 
					
						
							|  |  |  |   final Directory applicationDataDirectory; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   FlowyRunnerContext({required this.applicationDataDirectory}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  | Future<void> runAppFlowy() async { | 
					
						
							|  |  |  |   await FlowyRunner.run( | 
					
						
							|  |  |  |     FlowyApp(), | 
					
						
							| 
									
										
										
										
											2023-08-22 15:40:22 +08:00
										 |  |  |     integrationMode(), | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |   ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 16:10:50 +08:00
										 |  |  | class FlowyRunner { | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |   static Future<FlowyRunnerContext> run( | 
					
						
							| 
									
										
										
										
											2023-06-15 22:43:07 +08:00
										 |  |  |     EntryPoint f, | 
					
						
							|  |  |  |     IntegrationMode mode, { | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  |     LaunchConfiguration config = const LaunchConfiguration( | 
					
						
							|  |  |  |       autoRegistrationSupported: false, | 
					
						
							|  |  |  |     ), | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |   }) async { | 
					
						
							|  |  |  |     // Clear all the states in case of rebuilding.
 | 
					
						
							|  |  |  |     await getIt.reset(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 10:23:58 +03:00
										 |  |  |     // Specify the env
 | 
					
						
							| 
									
										
										
										
											2023-06-15 22:43:07 +08:00
										 |  |  |     initGetIt(getIt, mode, f, config); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |     final applicationDataDirectory = | 
					
						
							|  |  |  |         await getIt<ApplicationDataStorage>().getPath().then( | 
					
						
							|  |  |  |               (value) => Directory(value), | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  |     // add task
 | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  |     final launcher = getIt<AppLauncher>(); | 
					
						
							|  |  |  |     launcher.addTasks( | 
					
						
							|  |  |  |       [ | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  |         // this task should be first task, for handling platform errors.
 | 
					
						
							|  |  |  |         // don't catch errors in test mode
 | 
					
						
							|  |  |  |         if (!mode.isUnitTest) const PlatformErrorCatcherTask(), | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  |         // localization
 | 
					
						
							|  |  |  |         const InitLocalizationTask(), | 
					
						
							|  |  |  |         // init the app window
 | 
					
						
							|  |  |  |         const InitAppWindowTask(), | 
					
						
							|  |  |  |         // Init Rust SDK
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |         InitRustSDKTask(directory: applicationDataDirectory), | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  |         // Load Plugins, like document, grid ...
 | 
					
						
							|  |  |  |         const PluginLoadTask(), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // init the app widget
 | 
					
						
							|  |  |  |         // ignore in test mode
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |         if (!mode.isUnitTest) ...[ | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  |           const HotKeyTask(), | 
					
						
							| 
									
										
										
										
											2023-07-05 20:57:09 +08:00
										 |  |  |           InitSupabaseTask(), | 
					
						
							| 
									
										
										
										
											2023-10-12 20:25:00 +08:00
										 |  |  |           InitAppFlowyCloudTask(), | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  |           const InitAppWidgetTask(), | 
					
						
							|  |  |  |           const InitPlatformServiceTask() | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     await launcher.launch(); // execute the tasks
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return FlowyRunnerContext( | 
					
						
							|  |  |  |       applicationDataDirectory: applicationDataDirectory, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2021-06-19 23:41:19 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Future<void> initGetIt( | 
					
						
							|  |  |  |   GetIt getIt, | 
					
						
							| 
									
										
										
										
											2023-08-22 15:40:22 +08:00
										 |  |  |   IntegrationMode mode, | 
					
						
							| 
									
										
										
										
											2022-02-20 08:35:52 +08:00
										 |  |  |   EntryPoint f, | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |   LaunchConfiguration config, | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  | ) async { | 
					
						
							| 
									
										
										
										
											2022-02-20 08:35:52 +08:00
										 |  |  |   getIt.registerFactory<EntryPoint>(() => f); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |   getIt.registerLazySingleton<FlowySDK>(() { | 
					
						
							|  |  |  |     return FlowySDK(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   getIt.registerLazySingleton<AppLauncher>( | 
					
						
							|  |  |  |     () => AppLauncher( | 
					
						
							|  |  |  |       context: LaunchContext( | 
					
						
							|  |  |  |         getIt, | 
					
						
							| 
									
										
										
										
											2023-08-22 15:40:22 +08:00
										 |  |  |         mode, | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |         config, | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ), | 
					
						
							| 
									
										
										
										
											2023-10-24 23:13:51 +08:00
										 |  |  |     dispose: (launcher) async { | 
					
						
							|  |  |  |       await launcher.dispose(); | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  |   getIt.registerSingleton<PluginSandbox>(PluginSandbox()); | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 15:40:22 +08:00
										 |  |  |   await DependencyResolver.resolve(getIt, mode); | 
					
						
							| 
									
										
										
										
											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-12-20 11:14:42 +08:00
										 |  |  |   LaunchConfiguration config; | 
					
						
							|  |  |  |   LaunchContext(this.getIt, this.env, this.config); | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08: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 11:03:33 +08:00
										 |  |  |   const LaunchTask(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  |   LaunchTaskType get type => LaunchTaskType.dataProcessing; | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  |   Future<void> initialize(LaunchContext context); | 
					
						
							| 
									
										
										
										
											2023-10-24 23:13:51 +08:00
										 |  |  |   Future<void> dispose(); | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AppLauncher { | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  |   AppLauncher({ | 
					
						
							|  |  |  |     required this.context, | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |   final LaunchContext context; | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  |   final List<LaunchTask> tasks = []; | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   void addTask(LaunchTask task) { | 
					
						
							|  |  |  |     tasks.add(task); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  |   void addTasks(Iterable<LaunchTask> tasks) { | 
					
						
							|  |  |  |     this.tasks.addAll(tasks); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-19 17:12:44 +08:00
										 |  |  |   Future<void> launch() async { | 
					
						
							| 
									
										
										
										
											2023-05-17 11:03:33 +08:00
										 |  |  |     for (final task in tasks) { | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  |       await task.initialize(context); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-10-24 23:13:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Future<void> dispose() async { | 
					
						
							|  |  |  |     Log.info('AppLauncher dispose'); | 
					
						
							|  |  |  |     for (final task in tasks) { | 
					
						
							|  |  |  |       await task.dispose(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     tasks.clear(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-02-19 13:52:52 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |   unitTest, | 
					
						
							|  |  |  |   integrationTest; | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |   // test mode
 | 
					
						
							|  |  |  |   bool get isTest => isUnitTest || isIntegrationTest; | 
					
						
							|  |  |  |   bool get isUnitTest => this == IntegrationMode.unitTest; | 
					
						
							|  |  |  |   bool get isIntegrationTest => this == IntegrationMode.integrationTest; | 
					
						
							| 
									
										
										
										
											2023-06-15 22:43:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |   // release mode
 | 
					
						
							|  |  |  |   bool get isRelease => this == IntegrationMode.release; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // develop mode
 | 
					
						
							|  |  |  |   bool get isDevelop => this == IntegrationMode.develop; | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 15:40:22 +08:00
										 |  |  | IntegrationMode integrationMode() { | 
					
						
							| 
									
										
										
										
											2022-02-19 23:19:33 +08:00
										 |  |  |   if (Platform.environment.containsKey('FLUTTER_TEST')) { | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |     return IntegrationMode.unitTest; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |