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;

  bool permissionGranted = await Permission.microphone.isGranted;
  if (!permissionGranted) {
    await Permission.microphone.request();
  } else {
    Directory appDirDoc = await getApplicationSupportDirectory();
    String tempAudioPath =
        '${appDirDoc.path}/${field.uniqueKey}/audioRecorderTemp';
    Directory tempAudioDirectory = Directory(tempAudioPath);

    String tempTimestamp =
        DateFormat('yyyyMMddTkkmmss').format(DateTime.now());

    Directory tempTimestampDirectory =
        Directory('$tempAudioPath/$tempTimestamp');
    tempTimestampDirectory.createSync(recursive: true);
    String tempFilePath = '${tempTimestampDirectory.path}/audio.mp3';

    await showDialog<File?>(
      context: context,
      builder: (context) => AudioRecorderDialogPage(
        filePath: tempFilePath,
        onSave: (tempFile) {
          String audioRecorderPath =
              '${appDirDoc.path}/${field.uniqueKey}/audioRecorder';
          Directory audioRecorderDirectory = Directory(audioRecorderPath);
          if (audioRecorderDirectory.existsSync()) {
            audioRecorderDirectory.deleteSync(recursive: true);
          }
          audioRecorderDirectory.createSync(recursive: true);

          String finalTimestamp =
              DateFormat('yyyyMMddTkkmmss').format(DateTime.now());
          Directory finalTimestampDirectory =
              Directory('$audioRecorderPath/$finalTimestamp');
          String finalFilePath = '${finalTimestampDirectory.path}/audio.mp3';

          finalTimestampDirectory.createSync(recursive: true);
          tempFile.copySync(finalFilePath);

          tempAudioDirectory.deleteSync(recursive: true);

          audioField.setAudio(
            cause: cause,
            appModel: appModel,
            creatorModel: creatorModel,
            newAutoCannotOverride: false,
            generateAudio: () async {
              return File(finalFilePath);
            },
          );
        },
      ),
    );
  }
}