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:
1 parent
05ebecd977
commit
bd91fb1ff9
3 files changed
+303
-6
No files matched your search
@@ -39,11 +39,14 @@ class TranslationDownloadCancelled implements Exception {
|
||||
class TranslationService extends ChangeNotifier {
|
||||
final AppSettingsService _appSettingsService;
|
||||
final TranslationFileStore _fileStore;
|
||||
final http.Client Function() _newClient;
|
||||
|
||||
TranslationService(
|
||||
this._appSettingsService, {
|
||||
TranslationFileStore? fileStore,
|
||||
}) : _fileStore = fileStore ?? TranslationFileStore() {
|
||||
http.Client Function()? clientFactory,
|
||||
}) : _fileStore = fileStore ?? TranslationFileStore(),
|
||||
_newClient = clientFactory ?? http.Client.new {
|
||||
// Initialize langdetect once at service construction.
|
||||
_langDetectInit = initLangDetect();
|
||||
}
|
||||
@@ -64,6 +67,10 @@ class TranslationService extends ChangeNotifier {
|
||||
String? _downloadTargetFileName;
|
||||
String? _downloadSourceUrl;
|
||||
String? _downloadModelId;
|
||||
String? _failedDownloadError;
|
||||
String? _failedDownloadSourceUrl;
|
||||
String? _failedDownloadFileName;
|
||||
String? _failedDownloadModelId;
|
||||
|
||||
bool get isBusy => _isBusy;
|
||||
bool get isDownloading => _isDownloading;
|
||||
@@ -82,6 +89,18 @@ class TranslationService extends ChangeNotifier {
|
||||
|
||||
/// Preset id when the download was started as a preset, otherwise null.
|
||||
String? get downloadModelId => _downloadModelId;
|
||||
|
||||
/// Error message from the most recent failed download, if any.
|
||||
String? get failedDownloadError => _failedDownloadError;
|
||||
|
||||
/// URL of the most recent failed download, if any.
|
||||
String? get failedDownloadSourceUrl => _failedDownloadSourceUrl;
|
||||
|
||||
/// Resolved filename of the most recent failed download, if any.
|
||||
String? get failedDownloadFileName => _failedDownloadFileName;
|
||||
|
||||
/// Preset id of the most recent failed download, if it was a preset.
|
||||
String? get failedDownloadModelId => _failedDownloadModelId;
|
||||
double? get downloadProgress {
|
||||
final total = _downloadTotalBytes;
|
||||
if (!_isDownloading || total == null || total <= 0) {
|
||||
@@ -210,6 +229,7 @@ class TranslationService extends ChangeNotifier {
|
||||
return _runExclusive(() async {
|
||||
_setBusy(true);
|
||||
_lastError = null;
|
||||
_clearFailedDownload();
|
||||
final resolvedFileName =
|
||||
fileName ??
|
||||
_sanitizeFileName(
|
||||
@@ -227,7 +247,7 @@ class TranslationService extends ChangeNotifier {
|
||||
_setDownloading(true);
|
||||
try {
|
||||
// HEAD request to check size and range support.
|
||||
final headClient = http.Client();
|
||||
final headClient = _newClient();
|
||||
int? totalSize;
|
||||
bool supportsRange = false;
|
||||
try {
|
||||
@@ -240,6 +260,10 @@ class TranslationService extends ChangeNotifier {
|
||||
headClient.close();
|
||||
}
|
||||
|
||||
if (_cancelDownloadRequested) {
|
||||
throw const TranslationDownloadCancelled();
|
||||
}
|
||||
|
||||
_downloadTotalBytes = totalSize;
|
||||
_notify();
|
||||
|
||||
@@ -277,6 +301,15 @@ class TranslationService extends ChangeNotifier {
|
||||
await _appSettingsService.setTranslationModelSourceUrl(sourceUrl);
|
||||
_failedModelPath = null;
|
||||
return record;
|
||||
} on TranslationDownloadCancelled {
|
||||
rethrow;
|
||||
} catch (error) {
|
||||
_lastError = '$error';
|
||||
_failedDownloadError = '$error';
|
||||
_failedDownloadSourceUrl = sourceUrl;
|
||||
_failedDownloadFileName = resolvedFileName;
|
||||
_failedDownloadModelId = id;
|
||||
rethrow;
|
||||
} finally {
|
||||
_setDownloading(false);
|
||||
}
|
||||
@@ -287,7 +320,7 @@ class TranslationService extends ChangeNotifier {
|
||||
required Uri uri,
|
||||
required String fileName,
|
||||
}) async {
|
||||
final client = http.Client();
|
||||
final client = _newClient();
|
||||
try {
|
||||
final response = await client.send(http.Request('GET', uri));
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
@@ -331,7 +364,7 @@ class TranslationService extends ChangeNotifier {
|
||||
if (start >= totalSize) break;
|
||||
final chunkPath = await _fileStore.chunkFilePath(fileName, i);
|
||||
chunkPaths.add(chunkPath);
|
||||
final client = http.Client();
|
||||
final client = _newClient();
|
||||
clients.add(client);
|
||||
futures.add(
|
||||
_downloadRange(
|
||||
@@ -718,6 +751,13 @@ class TranslationService extends ChangeNotifier {
|
||||
_notify();
|
||||
}
|
||||
|
||||
void _clearFailedDownload() {
|
||||
_failedDownloadError = null;
|
||||
_failedDownloadSourceUrl = null;
|
||||
_failedDownloadFileName = null;
|
||||
_failedDownloadModelId = null;
|
||||
}
|
||||
|
||||
/// Puts the service in the same state [downloadModel] publishes while a
|
||||
/// fetch is running, so settings can be rebuilt without a network call.
|
||||
@visibleForTesting
|
||||
@@ -729,12 +769,15 @@ class TranslationService extends ChangeNotifier {
|
||||
int downloadedBytes = 0,
|
||||
int? totalBytes,
|
||||
}) {
|
||||
_lastError = null;
|
||||
_clearFailedDownload();
|
||||
_downloadSourceUrl = sourceUrl;
|
||||
_downloadModelId = modelId;
|
||||
_downloadTargetFileName = fileName;
|
||||
_downloadFileName = displayedFileName ?? fileName;
|
||||
_downloadedBytes = downloadedBytes;
|
||||
_downloadTotalBytes = totalBytes;
|
||||
_isBusy = true;
|
||||
_isDownloading = true;
|
||||
_notify();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user