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();
}