executeAction method

  1. @override
Future<void> executeAction(
  1. {required BuildContext context,
  2. required WidgetRef ref,
  3. required AppModel appModel,
  4. required CreatorModel creatorModel,
  5. required DictionaryHeading heading,
  6. required String? dictionaryName}
)
override

Execute the functionality of this action.

Implementation

@override
Future<void> executeAction({
  required BuildContext context,
  required WidgetRef ref,
  required AppModel appModel,
  required CreatorModel creatorModel,
  required DictionaryHeading heading,
  required String? dictionaryName,
}) async {
  CreatorModel creatorModel = ref.read(instantExportProvider);

  Map<Field, String> newTextFields = {};
  for (Field field in appModel.activeFields) {
    String? newTextField = field.onCreatorOpenAction(
      context: context,
      ref: ref,
      appModel: appModel,
      creatorModel: creatorModel,
      heading: heading,
      creatorJustLaunched: true,
      dictionaryName: dictionaryName,
    );

    if (newTextField != null) {
      newTextFields[field] = newTextField;
    }
  }

  creatorModel.copyContext(
    CreatorFieldValues(textValues: newTextFields),
  );

  for (Field field in appModel.activeFields) {
    /// If a media source has a generate images or audio function, then use that
    /// over any set auto enhancement.
    if (appModel.isMediaOpen && appModel.getCurrentMediaItem() != null) {
      MediaSource mediaSource =
          appModel.getCurrentMediaItem()!.getMediaSource(appModel: appModel);
      if (field is ImageField && mediaSource.overridesAutoImage) {
        await field.setImages(
          appModel: appModel,
          creatorModel: creatorModel,
          searchTerm: '',
          newAutoCannotOverride: true,
          cause: EnhancementTriggerCause.manual,
          generateImages: () async {
            return mediaSource.generateImages(
              appModel: appModel,
              item: appModel.getCurrentMediaItem()!,
              options: appModel.currentSubtitleOptions!.value,
              data: mediaSource.currentExtraData,
            );
          },
        );
        continue;
      }
      if (field is AudioSentenceField && mediaSource.overridesAutoAudio) {
        await field.setAudio(
          appModel: appModel,
          creatorModel: creatorModel,
          searchTerm: '',
          newAutoCannotOverride: true,
          cause: EnhancementTriggerCause.manual,
          generateAudio: () async {
            return mediaSource.generateAudio(
              appModel: appModel,
              item: appModel.getCurrentMediaItem()!,
              options: appModel.currentSubtitleOptions!.value,
            );
          },
        );
        continue;
      }
    }

    Enhancement? enhancement = appModel.lastSelectedMapping
        .getAutoFieldEnhancement(appModel: appModel, field: field);

    if (enhancement != null) {
      await enhancement.enhanceCreatorParams(
        context: context,
        ref: ref,
        appModel: appModel,
        creatorModel: creatorModel,
        cause: EnhancementTriggerCause.auto,
      );
    }
  }

  await appModel.addNote(
    creatorFieldValues: creatorModel.getExportDetails(ref),
    mapping: appModel.lastSelectedMapping,
    deck: appModel.lastSelectedDeckName,
    onSuccess: () {
      creatorModel.clearAll(
        overrideLocks: true,
        savedTags: appModel.savedTags,
      );

      /// Need to clear media as these conflict.
      CreatorModel cardCreatorModel = ref.read(creatorProvider);
      cardCreatorModel.clearField(
        ImageField.instance,
        savedTags: appModel.savedTags,
      );
      cardCreatorModel.clearField(
        AudioField.instance,
        savedTags: appModel.savedTags,
      );
      cardCreatorModel.clearField(
        SentenceField.instance,
        savedTags: appModel.savedTags,
      );
    },
  );
}