initialise method

Future<void> initialise()

Prepare application data and state to be ready of use upon starting up the application. AppModel is initialised in the main function before runApp is executed.

Implementation

Future<void> initialise() async {
  /// Prepare entities that may be repeatedly used at runtime.
  _packageInfo = await PackageInfo.fromPlatform();
  _androidDeviceInfo = await DeviceInfoPlugin().androidInfo;

  /// Initialise persistent key-value store.
  await Hive.initFlutter();
  _preferences = await Hive.openBox('appModel');
  _dictionaryHistory = await Hive.openBox('dictionaryHistory');

  /// Perform startup activities unnecessary to further initialisation here.
  await requestExternalStoragePermissions();
  await requestAnkidroidPermissions();

  /// These directories will commonly be accessed.
  _temporaryDirectory = await getTemporaryDirectory();
  _appDirectory = await getApplicationDocumentsDirectory();
  _databaseDirectory = await getApplicationSupportDirectory();
  _browserDirectory = Directory(path.join(appDirectory.path, 'browser'));
  _thumbnailsDirectory =
      Directory(path.join(appDirectory.path, 'thumbnails'));
  _hiveDirectory = Directory(path.join(appDirectory.path, 'hive'));

  _dictionaryImportWorkingDirectory = Directory(
      path.join(appDirectory.path, 'dictionaryImportWorkingDirectory'));
  _exportDirectory = await prepareJidoujishoDirectory();
  _alternateExportDirectory = await prepareFallbackJidoujishoDirectory();

  thumbnailsDirectory.createSync();
  hiveDirectory.createSync();
  dictionaryImportWorkingDirectory.createSync();

  /// Inject open source licenses for non-Flutter dependencies that are
  /// included as assets.
  await injectAssetLicenses();

  /// Populate entities with key-value maps for constant time performance.
  /// This is not the initialisation step, which occurs below.
  populateLanguages();
  populateLocales();
  populateMediaTypes();
  populateMediaSources();
  populateDictionaryFormats();
  populateEnhancements();
  populateQuickActions();

  /// Get the current target language and prepare its resources for use. This
  /// will not re-run if the target language is already initialised, as
  /// a [Language] should always have a singleton instance and will not
  /// re-prepare its resources if already initialised. See
  /// [Language.initialise] for more details.
  await targetLanguage.initialise();

  /// Ready all enhancements sources for use.
  for (Field field in globalFields) {
    for (Enhancement enhancement in enhancements[field]!.values) {
      await enhancement.initialise();
    }
  }

  /// Ready all quick actions for use.
  for (QuickAction action in quickActions.values) {
    await action.initialise();
  }

  /// Ready all media sources for use.
  for (MediaType type in mediaTypes.values) {
    for (MediaSource source in mediaSources[type]!.values) {
      await source.initialise();
    }
  }

  /// Initialise persistent database.
  _database = await Isar.open(
    globalSchemas,
    directory: _databaseDirectory.path,
    maxSizeMiB: 8192,
  );

  /// Preloads the search database in memory.
  searchDictionary(
    searchTerm: targetLanguage.helloWorld,
    searchWithWildcards: false,
    useCache: false,
  ).then((_) {
    /// Preloads for wildcard searches.
    searchDictionary(
      searchTerm: '${targetLanguage.helloWorld.substring(0, 1)}?',
      searchWithWildcards: true,
      useCache: false,
    ).then((_) {
      searchDictionary(
        searchTerm: '${targetLanguage.helloWorld.substring(0, 1)}*',
        searchWithWildcards: true,
        useCache: false,
      );
    });
  });
}