addNote method
- {required CreatorFieldValues creatorFieldValues,
- required AnkiMapping mapping,
- required String deck,
- required dynamic onSuccess(
Add a note with certain creatorFieldValues
and a mapping
of fields to
a model to a given deck
.
Implementation
Future<void> addNote({
required CreatorFieldValues creatorFieldValues,
required AnkiMapping mapping,
required String deck,
required Function() onSuccess,
}) async {
if (mapping.isExportFieldsEmpty) {
Fluttertoast.showToast(
msg: t.export_profile_empty,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
);
return;
}
Map<Field, String> exportedImages = {};
Map<Field, String> exportedAudio = {};
for (MapEntry<Field, File> entry
in creatorFieldValues.imagesToExport.entries) {
Field field = entry.key;
File exportFile = entry.value;
String timestamp =
intl.DateFormat('yyyyMMddTkkmmss').format(DateTime.now());
String preferredName = 'jidoujisho-$timestamp';
String? imageFileName;
if (exportFile.existsSync()) {
imageFileName = await addFileToMedia(
exportFile: exportFile,
preferredName: preferredName,
mimeType: 'image',
);
exportedImages[field] = imageFileName;
}
}
for (MapEntry<Field, File> entry
in creatorFieldValues.audioToExport.entries) {
Field field = entry.key;
File exportFile = entry.value;
String timestamp =
intl.DateFormat('yyyyMMddTkkmmss').format(DateTime.now());
String preferredName = 'jidoujisho-$timestamp';
String? audioFileName;
if (exportFile.existsSync()) {
audioFileName = await addFileToMedia(
exportFile: exportFile,
preferredName: preferredName,
mimeType: 'audio',
);
exportedAudio[field] = audioFileName;
}
}
String model = mapping.model;
List<String> fields = getCardFields(
creatorFieldValues: creatorFieldValues,
mapping: mapping,
exportedImages: exportedImages,
exportedAudio: exportedAudio,
);
List<String> tags =
creatorFieldValues.textValues[TagsField.instance]?.split(' ') ?? [];
try {
await methodChannel.invokeMethod(
'addNote',
<String, dynamic>{
'deck': deck,
'model': model,
'fields': fields,
'tags': tags,
},
);
Fluttertoast.showToast(
msg: t.card_exported(deck: deck),
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
);
onSuccess.call();
} on PlatformException {
debugPrint('Failed to add note');
Fluttertoast.showToast(
msg: t.error_add_note,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
);
rethrow;
} finally {
debugPrint('Added note to Anki media');
}
}