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 {
  AudioExportField audioField = field as AudioExportField;
  String? searchTerm;

  if (cause != EnhancementTriggerCause.auto) {
    searchTerm = audioField.getSearchTermWithFallback(
      appModel: appModel,
      creatorModel: creatorModel,
      fallbackSearchTerms: [
        TermField.instance,
        ReadingField.instance,
      ],
    );
  } else {
    searchTerm = creatorModel.getFieldController(TermField.instance).text;

    if (searchTerm.trim().isEmpty) {
      return;
    }
  }

  List<ForvoResult> results = await getForvoResults(
    appModel: appModel,
    searchTerm: searchTerm!,
  );

  if (cause != EnhancementTriggerCause.manual) {
    if (results.isNotEmpty) {
      int index = 0;
      ForvoResult result = results[index];

      Directory appDirDoc = await getApplicationDocumentsDirectory();
      String forvoAudioPath = '${appDirDoc.path}/forvoAudio';
      Directory forvoAudioDir = Directory(forvoAudioPath);
      if (forvoAudioDir.existsSync()) {
        forvoAudioDir.deleteSync(recursive: true);
      }
      forvoAudioDir.createSync();

      File file = File('$forvoAudioPath/$index.mp3');
      File networkFile =
          await DefaultCacheManager().getSingleFile(result.audioUrl);
      networkFile.copySync(file.path);

      await audioField.setAudio(
        appModel: appModel,
        creatorModel: creatorModel,
        searchTerm: searchTerm,
        newAutoCannotOverride: false,
        cause: cause,
        generateAudio: () async {
          return file;
        },
      );
    }
  } else {
    await showDialog(
      context: context,
      builder: (context) => ForvoAudioDialogPage(
        results: results,
        onSelect: (index) async {
          Directory appDirDoc = await getApplicationDocumentsDirectory();
          String forvoAudioPath = '${appDirDoc.path}/forvoAudio';
          Directory forvoAudioDir = Directory(forvoAudioPath);
          if (forvoAudioDir.existsSync()) {
            forvoAudioDir.deleteSync(recursive: true);
          }
          forvoAudioDir.createSync();

          File file = File('$forvoAudioPath/$index.mp3');
          File networkFile = await DefaultCacheManager()
              .getSingleFile(results[index].audioUrl);
          networkFile.copySync(file.path);

          await audioField.setAudio(
            appModel: appModel,
            creatorModel: creatorModel,
            searchTerm: searchTerm,
            newAutoCannotOverride: false,
            cause: cause,
            generateAudio: () async {
              return file;
            },
          );
        },
      ),
    );
  }
}