collectTranscriptionData method
Implementation
void collectTranscriptionData(RemoteActivityData remoteData) {
// Check if the incoming data is a final transcription
if (remoteData.finalTranscription?.isNotEmpty == true) {
// If there's an existing partial transcription, finalize it
if (particalTranscription != null) {
particalTranscription = particalTranscription!.copyWith(
name: getParticipantNameByIdentity(remoteData.participantIdentity),
transcription: Utils.decodeUnicode(remoteData.finalTranscription),
isFinal: true,
sourceLang: transcriptionLanguageData?.sourceLang,
targetLang: translationLanguage?.code ??
transcriptionLanguageData?.sourceLang);
// Replace the existing transcription in the list with the finalized one
_updateTranscriptionInList(particalTranscription!);
// Trigger translation if source and target languages differ
if (particalTranscription?.sourceLang !=
particalTranscription?.targetLang) {
translateText(particalTranscription!);
}
} else {
// Create and add a new finalized transcription
final newTranscription = TranscriptionModel(
id: const Uuid().v4(),
name: getParticipantNameByIdentity(remoteData.participantIdentity),
transcription: Utils.decodeUnicode(remoteData.finalTranscription),
timestamp: Utils.formatTimestampToTime(
DateTime.now().millisecondsSinceEpoch),
isFinal: true,
sourceLang: transcriptionLanguageData?.sourceLang ?? "",
targetLang: translationLanguage?.code ??
(transcriptionLanguageData?.sourceLang ?? ""),
);
addTranscription(newTranscription);
// Trigger translation if source and target languages differ
if (newTranscription.sourceLang != newTranscription.targetLang) {
translateText(newTranscription);
}
}
// Reset the partial transcription
particalTranscription = null;
} else if (remoteData.partialTranscription?.isNotEmpty == true) {
// Handle partial transcription updates
if (particalTranscription != null) {
// Update the existing partial transcription
particalTranscription = particalTranscription!.copyWith(
name: getParticipantNameByIdentity(remoteData.participantIdentity),
transcription: remoteData.partialTranscription ?? "",
isFinal: false,
sourceLang: transcriptionLanguageData?.sourceLang,
targetLang: translationLanguage?.code ??
transcriptionLanguageData?.sourceLang);
// Update the transcription in the list
_updateTranscriptionInList(particalTranscription!);
} else {
// Create and add a new partial transcription
particalTranscription = TranscriptionModel(
id: const Uuid().v4(),
name: getParticipantNameByIdentity(remoteData.participantIdentity),
transcription: remoteData.partialTranscription ?? "",
timestamp: Utils.formatTimestampToTime(
DateTime.now().millisecondsSinceEpoch),
isFinal: false,
sourceLang: transcriptionLanguageData?.sourceLang ?? "",
targetLang: translationLanguage?.code ??
(transcriptionLanguageData?.sourceLang ?? ""),
);
addTranscription(particalTranscription!);
}
}
}