Keep failed translation downloads visible after leaving settings

Record download failure metadata on TranslationService so reopening App Settings still surfaces the failed row and retry action instead of losing the error when the previous page state was disposed.
This commit is contained in:
zjs81 committed 2026-11-21 02:22:07 -07:00
1 parent 05ebecd977
commit bd91fb1ff9
3 files changed
+303 -6

No files matched your search

+67 -2
View File
@@ -171,6 +171,7 @@ class _TranslationModelsState extends State<_TranslationModels> {
lastError != null &&
lastError.isNotEmpty &&
lastError != 'Download stopped.' &&
lastError != translationService.failedDownloadError &&
lastError != _failedDetail;
return Column(
@@ -219,6 +220,8 @@ class _TranslationModelsState extends State<_TranslationModels> {
final items = <ModelDownloadItem>[];
var pendingShown = false;
final serviceFailedError = service.failedDownloadError;
bool targets(String id, String sourceUrl, String name) {
if (pending == null) return false;
if (pending.id == id || pending.modelId == id) return true;
@@ -227,6 +230,27 @@ class _TranslationModelsState extends State<_TranslationModels> {
return false;
}
bool matchesServiceFailure(
String id,
String sourceUrl,
String? fileName,
String? installedName,
) {
if (serviceFailedError == null) return false;
if (service.failedDownloadModelId == id) return true;
if (sourceUrl.isNotEmpty &&
service.failedDownloadSourceUrl == sourceUrl) {
return true;
}
final failedFile = service.failedDownloadFileName;
if (failedFile != null &&
failedFile.isNotEmpty &&
(failedFile == fileName || failedFile == installedName)) {
return true;
}
return false;
}
ModelDownloadItem buildItem({
required String id,
required String name,
@@ -240,11 +264,25 @@ class _TranslationModelsState extends State<_TranslationModels> {
service.isDownloading &&
(targets(id, sourceUrl, name) ||
_fileNamesThisDownload(service, fileName, installed?.name));
final failed = !downloading && _failedId == id;
final failed =
!downloading &&
(_failedId == id ||
(_failedId == null &&
matchesServiceFailure(
id,
sourceUrl,
fileName,
installed?.name,
)));
if (downloading || failed) pendingShown = true;
final merging = downloading && _isMerging(service);
final busy = service.isBusy;
final installedModel = installed;
final failureDetail = _failedId == id
? _failedDetail
: (serviceFailedError != null
? l10n.translation_downloadFailed(serviceFailedError)
: null);
return ModelDownloadItem(
id: id,
@@ -266,7 +304,7 @@ class _TranslationModelsState extends State<_TranslationModels> {
detail: merging
? l10n.translation_mergingChunks
: failed
? _failedDetail
? failureDetail
: null,
progress: downloading && !merging ? service.downloadProgress : null,
downloadedBytes: downloading ? service.downloadedBytes : 0,
@@ -349,6 +387,33 @@ class _TranslationModelsState extends State<_TranslationModels> {
),
),
);
} else if (!pendingShown &&
!service.isDownloading &&
serviceFailedError != null &&
service.failedDownloadSourceUrl != null) {
final failedUrl = service.failedDownloadSourceUrl!;
final failedFile = service.failedDownloadFileName;
final failedModelId = service.failedDownloadModelId;
final id = (failedModelId != null && failedModelId.isNotEmpty)
? failedModelId
: failedUrl.isNotEmpty
? failedUrl
: (failedFile ?? 'download');
items.add(
ModelDownloadItem(
id: id,
name: _displayName(failedUrl, failedFile, failedModelId),
status: ModelDownloadStatus.error,
detail: l10n.translation_downloadFailed(serviceFailedError),
onDownload: service.isBusy
? null
: () => _download(
sourceUrl: failedUrl,
fileName: failedFile,
id: failedModelId,
),
),
);
}
return items;