Merge pull request #682 from zjs81/cursor/consistent-model-download-ui-092a

Use one model row for image and translation downloads
This commit is contained in:
zjs81 authored and GitHub committed 2026-11-21 07:37:03 -07:00
commit 031844788a
53 files changed
+3975 -916

No files matched your search

+100 -16
View File
@@ -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();
}
@@ -61,6 +64,13 @@ class TranslationService extends ChangeNotifier {
int _downloadedBytes = 0;
int? _downloadTotalBytes;
String? _downloadFileName;
String? _downloadTargetFileName;
String? _downloadSourceUrl;
String? _downloadModelId;
String? _failedDownloadError;
String? _failedDownloadSourceUrl;
String? _failedDownloadFileName;
String? _failedDownloadModelId;
bool get isBusy => _isBusy;
bool get isDownloading => _isDownloading;
@@ -68,6 +78,29 @@ 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;
/// 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) {
@@ -195,22 +228,26 @@ class TranslationService extends ChangeNotifier {
}
return _runExclusive(() async {
_setBusy(true);
_setDownloading(true);
_lastError = null;
_clearFailedDownload();
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();
final headClient = _newClient();
int? totalSize;
bool supportsRange = false;
try {
@@ -223,6 +260,10 @@ class TranslationService extends ChangeNotifier {
headClient.close();
}
if (_cancelDownloadRequested) {
throw const TranslationDownloadCancelled();
}
_downloadTotalBytes = totalSize;
_notify();
@@ -260,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);
}
@@ -270,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) {
@@ -314,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(
@@ -701,6 +751,37 @@ 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
void debugSetActiveDownload({
required String sourceUrl,
String? fileName,
String? modelId,
String? displayedFileName,
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();
}
void _setDownloading(bool value) {
_isDownloading = value;
if (!value) {
@@ -708,6 +789,9 @@ class TranslationService extends ChangeNotifier {
_downloadedBytes = 0;
_downloadTotalBytes = null;
_downloadFileName = null;
_downloadTargetFileName = null;
_downloadSourceUrl = null;
_downloadModelId = null;
}
_notify();
}