fix: made nextITStep a completer so it will not be set twice (#1978)

This commit is contained in:
ggurdin 2025-03-05 10:16:03 -05:00 committed by GitHub
parent 7d52f58b4c
commit 71f4652390
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -33,7 +33,7 @@ class ITController {
String? sourceText;
List<ITStep> completedITSteps = [];
CurrentITStep? currentITStep;
CurrentITStep? nextITStep;
Completer<CurrentITStep?>? nextITStep;
GoldRouteTracker goldRouteTracker = GoldRouteTracker.defaultTracker;
List<int> payLoadIds = [];
@ -132,18 +132,11 @@ class ITController {
);
}
currentITStep = null;
// During first IT step, next step will not be set
if (nextITStep == null) {
currentITStep = null;
final ITResponseModel res = await _customInputTranslation(currentText);
// final ITResponseModel res = await (useCustomInput ||
// currentText.isEmpty ||
// translationId == null ||
// completedITSteps.last.chosenContinuance?.indexSavedByServer ==
// null
// ? _customInputTranslation(currentText)
// : _systemChoiceTranslation(translationId));
if (sourceText == null) return;
if (res.goldContinuances != null && res.goldContinuances!.isNotEmpty) {
@ -162,15 +155,17 @@ class ITController {
_addPayloadId(res);
} else {
currentITStep = nextITStep;
nextITStep = null;
currentITStep = await nextITStep!.future;
}
if (isTranslationDone) {
nextITStep = null;
choreographer.altTranslator.setTranslationFeedback();
choreographer.getLanguageHelp(onlyTokensAndLanguageDetection: true);
} else {
getNextTranslationData();
nextITStep = Completer<CurrentITStep?>();
final nextStep = await getNextTranslationData();
nextITStep!.complete(nextStep);
}
} catch (e, s) {
debugger(when: kDebugMode);
@ -193,7 +188,7 @@ class ITController {
}
}
Future<void> getNextTranslationData() async {
Future<CurrentITStep?> getNextTranslationData() async {
if (sourceText == null) {
ErrorHandler.logError(
e: Exception("sourceText is null in getNextTranslationData"),
@ -203,28 +198,28 @@ class ITController {
"continuances": goldRouteTracker.continuances.map((e) => e.toJson()),
},
);
return;
return null;
}
if (completedITSteps.length >= goldRouteTracker.continuances.length) {
return null;
}
try {
if (completedITSteps.length < goldRouteTracker.continuances.length) {
final String currentText = choreographer.currentText;
final String nextText =
goldRouteTracker.continuances[completedITSteps.length].text;
final String currentText = choreographer.currentText;
final String nextText =
goldRouteTracker.continuances[completedITSteps.length].text;
final ITResponseModel res =
await _customInputTranslation(currentText + nextText);
if (sourceText == null) return;
final ITResponseModel res =
await _customInputTranslation(currentText + nextText);
if (sourceText == null) return null;
nextITStep = CurrentITStep(
sourceText: sourceText!,
currentText: nextText,
responseModel: res,
storedGoldContinuances: goldRouteTracker.continuances,
);
} else {
nextITStep = null;
}
return CurrentITStep(
sourceText: sourceText!,
currentText: nextText,
responseModel: res,
storedGoldContinuances: goldRouteTracker.continuances,
);
} catch (e, s) {
debugger(when: kDebugMode);
if (e is! http.Response) {
@ -245,6 +240,7 @@ class ITController {
} finally {
choreographer.stopLoading();
}
return null;
}
Future<void> onEditSourceTextSubmit(String newSourceText) async {