onCreatorOpenAction method

  1. @override
String? onCreatorOpenAction(
  1. {required BuildContext context,
  2. required WidgetRef ref,
  3. required AppModel appModel,
  4. required CreatorModel creatorModel,
  5. required DictionaryHeading heading,
  6. required bool creatorJustLaunched,
  7. required String? dictionaryName}
)
override

Return the value that this field must have upon opening the Card Creator. For example, the Sentence field must provide the current context or the sentence that is stored in the app state, while the Image field may be used to return a snapshot of the current app context.

Implementation

@override
String? onCreatorOpenAction({
  required BuildContext context,
  required WidgetRef ref,
  required AppModel appModel,
  required CreatorModel creatorModel,
  required DictionaryHeading heading,
  required bool creatorJustLaunched,
  required String? dictionaryName,
}) {
  List<Dictionary> dictionaries = appModel.dictionaries;
  Map<String, bool> dictionaryNamesByHidden = Map<String, bool>.fromEntries(
      dictionaries
          .map((e) => MapEntry(e.name, e.isHidden(appModel.targetLanguage))));
  Map<String, bool> dictionaryNamesByCollapsed =
      Map<String, bool>.fromEntries(dictionaries.map(
          (e) => MapEntry(e.name, e.isCollapsed(appModel.targetLanguage))));
  Map<String, int> dictionaryNamesByOrder = Map<String, int>.fromEntries(
      dictionaries.map((e) => MapEntry(e.name, e.order)));

  late List<DictionaryEntry> expandedEntries;
  if (dictionaryName != null) {
    expandedEntries = heading.entries
        .where((entry) =>
            !dictionaryNamesByHidden[entry.dictionary.value!.name]! &&
            dictionaryName == entry.dictionary.value!.name)
        .toList();
  } else {
    expandedEntries = heading.entries
        .where((entry) =>
            !dictionaryNamesByHidden[entry.dictionary.value!.name]! &&
            !dictionaryNamesByCollapsed[entry.dictionary.value!.name]!)
        .toList();
  }

  expandedEntries.sort((a, b) =>
      dictionaryNamesByOrder[a.dictionary.value!.name]!
          .compareTo(dictionaryNamesByOrder[b.dictionary.value!.name]!));

  return MeaningField.flattenMeanings(
      entries: expandedEntries,
      prependDictionaryNames:
          appModel.lastSelectedMapping.prependDictionaryNames ?? false);
}