Keep a custom model download visible after leaving settings

The in-progress URL lived only in the settings page, so reopening App Settings hid the progress and Cancel control while the download continued. The service now keeps the URL and file name, and the list rebuilds that row from it.

Co-authored-by: zjs81 <zjs81@users.noreply.github.com>
This commit is contained in:
Cursor Agentandzjs81 committed 2026-11-21 00:14:51 +00:00
1 parent 9ffdbaae09
commit 05ebecd977
3 files changed
+163 -38

No files matched your search

+53 -12
View File
@@ -61,6 +61,9 @@ class TranslationService extends ChangeNotifier {
int _downloadedBytes = 0;
int? _downloadTotalBytes;
String? _downloadFileName;
String? _downloadTargetFileName;
String? _downloadSourceUrl;
String? _downloadModelId;
bool get isBusy => _isBusy;
bool get isDownloading => _isDownloading;
@@ -68,6 +71,17 @@ class TranslationService extends ChangeNotifier {
int get downloadedBytes => _downloadedBytes;
int? get downloadTotalBytes => _downloadTotalBytes;
String? get downloadFileName => _downloadFileName;
/// File being fetched. Stays set while chunks are merged, when
/// [downloadFileName] is replaced with the merge status.
String? get downloadTargetFileName => _downloadTargetFileName;
/// URL of the download in progress. Settings can rebuild the row after
/// the page that started the download has been disposed.
String? get downloadSourceUrl => _downloadSourceUrl;
/// Preset id when the download was started as a preset, otherwise null.
String? get downloadModelId => _downloadModelId;
double? get downloadProgress {
final total = _downloadTotalBytes;
if (!_isDownloading || total == null || total <= 0) {
@@ -195,20 +209,23 @@ class TranslationService extends ChangeNotifier {
}
return _runExclusive(() async {
_setBusy(true);
_setDownloading(true);
_lastError = null;
final resolvedFileName =
fileName ??
_sanitizeFileName(
uri.pathSegments.isNotEmpty
? uri.pathSegments.last
: 'translation-model.gguf',
);
_downloadSourceUrl = sourceUrl;
_downloadModelId = id;
_downloadTargetFileName = resolvedFileName;
_downloadFileName = resolvedFileName;
_downloadedBytes = 0;
_downloadTotalBytes = null;
_cancelDownloadRequested = false;
_setDownloading(true);
try {
final resolvedFileName =
fileName ??
_sanitizeFileName(
uri.pathSegments.isNotEmpty
? uri.pathSegments.last
: 'translation-model.gguf',
);
_downloadFileName = resolvedFileName;
_downloadedBytes = 0;
_cancelDownloadRequested = false;
// HEAD request to check size and range support.
final headClient = http.Client();
int? totalSize;
@@ -701,6 +718,27 @@ class TranslationService extends ChangeNotifier {
_notify();
}
/// Puts the service in the same state [downloadModel] publishes while a
/// fetch is running, so settings can be rebuilt without a network call.
@visibleForTesting
void debugSetActiveDownload({
required String sourceUrl,
String? fileName,
String? modelId,
String? displayedFileName,
int downloadedBytes = 0,
int? totalBytes,
}) {
_downloadSourceUrl = sourceUrl;
_downloadModelId = modelId;
_downloadTargetFileName = fileName;
_downloadFileName = displayedFileName ?? fileName;
_downloadedBytes = downloadedBytes;
_downloadTotalBytes = totalBytes;
_isDownloading = true;
_notify();
}
void _setDownloading(bool value) {
_isDownloading = value;
if (!value) {
@@ -708,6 +746,9 @@ class TranslationService extends ChangeNotifier {
_downloadedBytes = 0;
_downloadTotalBytes = null;
_downloadFileName = null;
_downloadTargetFileName = null;
_downloadSourceUrl = null;
_downloadModelId = null;
}
_notify();
}
+55 -26
View File
@@ -236,14 +236,10 @@ class _TranslationModelsState extends State<_TranslationModels> {
required TranslationModelRecord? installed,
}) {
if (installed != null) used.add(installed);
final currentFile = service.downloadFileName;
final fileMatches =
currentFile != null &&
currentFile != _mergingChunksFileName &&
(currentFile == fileName || currentFile == installed?.name);
final downloading =
service.isDownloading &&
(targets(id, sourceUrl, name) || fileMatches);
(targets(id, sourceUrl, name) ||
_fileNamesThisDownload(service, fileName, installed?.name));
final failed = !downloading && _failedId == id;
if (downloading || failed) pendingShown = true;
final merging = downloading && _isMerging(service);
@@ -335,34 +331,22 @@ class _TranslationModelsState extends State<_TranslationModels> {
);
}
if (pending != null &&
!pendingShown &&
(service.isDownloading || _failedId == pending.id)) {
final downloading = service.isDownloading;
final merging = downloading && _isMerging(service);
if (!pendingShown && service.isDownloading) {
items.add(_inProgressItem(l10n, service));
} else if (pending != null && !pendingShown && _failedId == pending.id) {
items.add(
ModelDownloadItem(
id: pending.id,
name: pending.name,
status: downloading
? ModelDownloadStatus.downloading
: ModelDownloadStatus.error,
detail: merging
? l10n.translation_mergingChunks
: downloading
status: ModelDownloadStatus.error,
detail: _failedDetail,
onDownload: service.isBusy
? null
: _failedDetail,
progress: downloading && !merging ? service.downloadProgress : null,
downloadedBytes: downloading ? service.downloadedBytes : 0,
totalBytes: downloading ? service.downloadTotalBytes : null,
onCancel: downloading ? service.cancelDownload : null,
onDownload: !service.isBusy && !downloading
? () => _download(
: () => _download(
sourceUrl: pending.sourceUrl,
fileName: pending.fileName,
id: pending.modelId,
)
: null,
),
),
);
}
@@ -370,6 +354,51 @@ class _TranslationModelsState extends State<_TranslationModels> {
return items;
}
/// True when this row is the file [service] is fetching, including while
/// its displayed filename is the merge status.
bool _fileNamesThisDownload(
TranslationService service,
String? fileName,
String? installedName,
) {
bool matches(String? candidate) {
if (candidate == null || candidate.isEmpty) return false;
if (candidate == _mergingChunksFileName) return false;
return candidate == fileName || candidate == installedName;
}
return matches(service.downloadFileName) ||
matches(service.downloadTargetFileName);
}
/// Row for a download whose page state was discarded, or that has no
/// preset or installed row yet. The service keeps the URL and file name.
ModelDownloadItem _inProgressItem(
AppLocalizations l10n,
TranslationService service,
) {
final sourceUrl = service.downloadSourceUrl ?? '';
final rawName = service.downloadTargetFileName ?? service.downloadFileName;
final fileName = rawName == _mergingChunksFileName ? null : rawName;
final modelId = service.downloadModelId;
final id = (modelId != null && modelId.isNotEmpty)
? modelId
: sourceUrl.isNotEmpty
? sourceUrl
: (fileName ?? 'download');
final merging = _isMerging(service);
return ModelDownloadItem(
id: id,
name: _displayName(sourceUrl, fileName, modelId),
status: ModelDownloadStatus.downloading,
detail: merging ? l10n.translation_mergingChunks : null,
progress: merging ? null : service.downloadProgress,
downloadedBytes: service.downloadedBytes,
totalBytes: service.downloadTotalBytes,
onCancel: service.cancelDownload,
);
}
bool _isMerging(TranslationService service) =>
service.downloadFileName == _mergingChunksFileName;