enhanceCreatorParams method

  1. @override
Future<void> enhanceCreatorParams(
  1. {required BuildContext context,
  2. required WidgetRef ref,
  3. required AppModel appModel,
  4. required CreatorModel creatorModel,
  5. required EnhancementTriggerCause cause}
)
override

Perform a change to the CreatorModel, executing the functionality of this enhancement. An EnhancementTriggerCause may be used to modify the behavior of the enhancement's function depending on whether the enhancement is being executed on auto, manual or cascade modes.

Implementation

@override
Future<void> enhanceCreatorParams({
  required BuildContext context,
  required WidgetRef ref,
  required AppModel appModel,
  required CreatorModel creatorModel,
  required EnhancementTriggerCause cause,
}) async {
  Directory appDirDoc = await getApplicationSupportDirectory();
  String immersionKitImagePath = '${appDirDoc.path}/immersion_kit';
  Directory immersionKitImageDir = Directory(immersionKitImagePath);
  if (immersionKitImageDir.existsSync()) {
    immersionKitImageDir.deleteSync(recursive: true);
  }
  immersionKitImageDir.createSync(recursive: true);

  String timestamp = DateFormat('yyyyMMddTkkmmss').format(DateTime.now());
  Directory directory = Directory('${immersionKitImageDir.path}/$timestamp');
  directory.createSync();

  String searchTerm = creatorModel.getFieldController(field).text;
  List<ImmersionKitResult> exampleSentences = await searchForSentences(
    context: context,
    appModel: appModel,
    searchTerm: searchTerm,
  );

  appModel.openImmersionKitSentenceDialog(
    exampleSentences: exampleSentences,
    onSelect: (selection) async {
      if (selection.isEmpty) {
        return;
      }

      ImmersionKitResult firstResult = selection.removeAt(0);
      creatorModel.setSentenceAndCloze(firstResult.selection);
      creatorModel.getFieldController(ContextField.instance).text =
          firstResult.source;
      for (ImmersionKitResult result in selection) {
        creatorModel.appendSentenceAndCloze(result.text);
      }

      if (firstResult.imageUrl.isNotEmpty) {
        await ImageField.instance.setImages(
          cause: cause,
          appModel: appModel,
          creatorModel: creatorModel,
          newAutoCannotOverride: false,
          generateImages: () async {
            String imagePath = '${directory.path}/image';
            File imageFile = File(imagePath);
            File networkFile = await DefaultCacheManager()
                .getSingleFile(firstResult.imageUrl);
            networkFile.copySync(imageFile.path);

            return [NetworkToFileImage(file: imageFile)];
          },
        );
      }

      if (firstResult.audioUrl.isNotEmpty) {
        await AudioSentenceField.instance.setAudio(
          appModel: appModel,
          creatorModel: creatorModel,
          searchTerm: searchTerm,
          newAutoCannotOverride: false,
          cause: cause,
          generateAudio: () async {
            String audioPath = '${directory.path}/audio.mp3';
            File audioFile = File(audioPath);
            File networkFile = await DefaultCacheManager()
                .getSingleFile(firstResult.audioUrl);
            networkFile.copySync(audioFile.path);

            return audioFile;
          },
        );
      }
    },
    onAppend: (selection) async {
      if (selection.isEmpty) {
        return;
      }

      for (ImmersionKitResult result in selection) {
        creatorModel.appendSentenceAndCloze(result.text);
      }
    },
  );
}