diff --git a/lib/services/translation_service.dart b/lib/services/translation_service.dart index 909d49fd..1688203b 100644 --- a/lib/services/translation_service.dart +++ b/lib/services/translation_service.dart @@ -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(); } diff --git a/lib/widgets/translation_settings_section.dart b/lib/widgets/translation_settings_section.dart index 9d68c26f..fc0ac2cb 100644 --- a/lib/widgets/translation_settings_section.dart +++ b/lib/widgets/translation_settings_section.dart @@ -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; diff --git a/test/widgets/model_download_list_test.dart b/test/widgets/model_download_list_test.dart index def521b1..4734bcad 100644 --- a/test/widgets/model_download_list_test.dart +++ b/test/widgets/model_download_list_test.dart @@ -219,6 +219,61 @@ void main() { expect(find.textContaining('Not downloaded'), findsOneWidget); }); + testWidgets('a custom download stays visible after settings is reopened', ( + tester, + ) async { + final settings = AppSettingsService(); + await settings.loadSettings(); + final translation = TranslationService(settings); + addTearDown(translation.dispose); + // The page that started the download is gone. Only the service still + // knows which file is in flight. + translation.debugSetActiveDownload( + sourceUrl: 'http://127.0.0.1/Tencent-demo.gguf', + fileName: 'Tencent-demo.gguf', + downloadedBytes: 400 * 1024, + totalBytes: 1024 * 1024, + ); + + final providers = [ + ChangeNotifierProvider.value(value: settings), + ChangeNotifierProvider.value(value: translation), + ]; + await pump( + tester, + const TranslationSettingsSection(), + providers: providers, + ); + expect(find.text('Tencent-demo'), findsOneWidget); + expect(find.textContaining('Downloading'), findsOneWidget); + expect(find.textContaining('0.4 / 1.0 MB (39%)'), findsOneWidget); + expect(find.byTooltip('Cancel'), findsOneWidget); + + await pump(tester, const SizedBox.shrink(), providers: providers); + await pump( + tester, + const TranslationSettingsSection(), + providers: providers, + ); + expect(find.text('Tencent-demo'), findsOneWidget); + expect(find.textContaining('Downloading'), findsOneWidget); + expect(find.byTooltip('Cancel'), findsOneWidget); + expect(find.text('Tencent HY-MT 1.5 1.8B Q4_K_M'), findsOneWidget); + + final preset = translationPresetModels.first; + translation.debugSetActiveDownload( + sourceUrl: preset.sourceUrl, + fileName: preset.name, + modelId: preset.id, + displayedFileName: 'Merging chunks...', + ); + await tester.pump(); + expect(find.text('Tencent-demo'), findsNothing); + expect(find.textContaining('Downloading'), findsOneWidget); + expect(find.textContaining('Merging downloaded chunks'), findsOneWidget); + expect(find.byTooltip('Cancel'), findsOneWidget); + }); + testWidgets('an installed image bundle is one ready row', (tester) async { final spec = imageCodecPresetModels.single; final settings = AppSettingsService();