diff --git a/lib/features/detail/domain/services/catalogue_resolver.dart b/lib/features/detail/domain/services/catalogue_resolver.dart index ca52d9be..7235c6bf 100644 --- a/lib/features/detail/domain/services/catalogue_resolver.dart +++ b/lib/features/detail/domain/services/catalogue_resolver.dart @@ -44,6 +44,16 @@ class CatalogueLink { /// found source is the one thing this flag exists to prevent. final bool approximate; + /// The same link under the name and icon the source has now. + CatalogueLink withProvider(String name, String image) => CatalogueLink( + providerId: providerId, + providerName: name.trim().isEmpty ? providerName : name, + contentUrl: contentUrl, + catalogueId: catalogueId, + providerImage: image.trim().isEmpty ? providerImage : image, + approximate: approximate, + ); + CatalogueLink withCatalogue(String id) => CatalogueLink( providerId: providerId, providerName: providerName, @@ -446,6 +456,23 @@ class CatalogueResolver { return null; } + /// [link] under its source's current name and icon. + /// + /// A stored link keeps the name the source had when it was saved, and a + /// source can be renamed on the server since: VidAPI became VixSrc in + /// November 2026, and pages linked before that still said VidAPI. The + /// installed entry wins whenever there is one; a source that is gone, or a + /// list that does not come in time, leaves the saved name. + Future _current(CatalogueLink link) async { + try { + final list = await _providers().timeout(const Duration(seconds: 2)); + for (final p in list) { + if (p.id == link.providerId) return link.withProvider(p.name, p.image); + } + } catch (_) {} + return link; + } + /// The source to open [hint] on, or which of the four ways it was missing. Future locate({ required String catalogueId, @@ -455,7 +482,10 @@ class CatalogueResolver { final catalogue = Catalogue.fromId(catalogueId); final known = remembered(catalogueId, contentUrl); if (known != null) { - return CatalogueResolution(catalogue: catalogue, link: known); + return CatalogueResolution( + catalogue: catalogue, + link: await _current(known), + ); } if (catalogue == null || hint == null || hint.title.trim().isEmpty) { return CatalogueResolution( diff --git a/lib/features/download/presentation/pages/downloads_page.dart b/lib/features/download/presentation/pages/downloads_page.dart index 7ba6a38c..0aa09244 100644 --- a/lib/features/download/presentation/pages/downloads_page.dart +++ b/lib/features/download/presentation/pages/downloads_page.dart @@ -33,6 +33,7 @@ import 'package:soplay/features/download/presentation/widgets/relink_source_shee import 'package:soplay/features/history/data/history_service.dart'; import 'package:soplay/features/home/domain/entities/movie.dart'; import 'package:soplay/features/manga/domain/entities/reader_args.dart'; +import 'package:soplay/features/profile/domain/entities/provider_entity.dart'; import 'package:soplay/features/profile/presentation/bloc/provider_bloc.dart'; import 'package:soplay/features/profile/presentation/bloc/provider_state.dart'; import 'package:soplay/features/profile/presentation/widgets/settings_tiles.dart'; @@ -390,19 +391,12 @@ class _DownloadsViewState extends State<_DownloadsView> { } String _providerName(BuildContext context, DownloadedTitle title) { - final saved = title.snapshot?.providerName; - if (saved != null && saved.isNotEmpty) return saved; + var live = const []; try { final state = context.read().state; - if (state is ProviderLoaded) { - for (final p in state.providers) { - if (p.id == title.provider) return p.name; - } - } + if (state is ProviderLoaded) live = state.providers; } catch (_) {} - final id = title.provider; - final colon = id.indexOf(':'); - return colon >= 0 ? id.substring(colon + 1) : id; + return downloadSourceName(title, live); } void _openTitle(BuildContext context, DownloadedTitle title) { @@ -873,3 +867,22 @@ class _CircleBackButton extends StatelessWidget { ); } } + +/// The source name a download is shown under. +/// +/// The installed source's current name first: the one saved with the download +/// is the name it had that day, and a source can be renamed since (VidAPI +/// became VixSrc in November 2026). The saved name is for a source that is no +/// longer installed, and the bare id for a download from before names were +/// saved. +@visibleForTesting +String downloadSourceName(DownloadedTitle title, List live) { + for (final p in live) { + if (p.id == title.provider && p.name.trim().isNotEmpty) return p.name; + } + final saved = title.snapshot?.providerName; + if (saved != null && saved.isNotEmpty) return saved; + final id = title.provider; + final colon = id.indexOf(':'); + return colon >= 0 ? id.substring(colon + 1) : id; +} diff --git a/test/features/detail/catalogue_resolver_test.dart b/test/features/detail/catalogue_resolver_test.dart index 98cf8a35..5fbc9b85 100644 --- a/test/features/detail/catalogue_resolver_test.dart +++ b/test/features/detail/catalogue_resolver_test.dart @@ -145,6 +145,71 @@ void main() { expect(searched, isFalse); }); + test('a remembered link shows its source under its current name', () async { + final hive = _Hive(); + final resolver = CatalogueResolver( + hive: hive, + providers: () async => [ + ProviderEntity( + id: 'vidapi', + name: 'VixSrc', + image: 'https://vixsrc.to/favicon.ico', + url: '', + description: '', + domains: const [], + category: 'tmdb', + ), + ], + finder: ({required title, required candidates, onOutcome}) => + const Stream.empty(), + ); + await hive.setCatalogueLink( + 'cat:tmdb|u', + const CatalogueLink( + providerId: 'vidapi', + providerName: 'VidAPI', + providerImage: 'https://vidapi.ru/favicon.ico', + contentUrl: 'x', + ).encode(), + ); + final link = await resolver.resolve( + catalogueId: 'cat:tmdb', + contentUrl: 'u', + hint: _movie('Anything', category: 'movie'), + ); + expect(link?.providerName, 'VixSrc'); + expect(link?.providerImage, 'https://vixsrc.to/favicon.ico'); + expect(link?.contentUrl, 'x'); + expect(link?.catalogueId, 'cat:tmdb'); + }); + + test( + 'a remembered link to a source that is gone keeps its saved name', + () async { + final hive = _Hive(); + final resolver = CatalogueResolver( + hive: hive, + providers: () async => const [], + finder: ({required title, required candidates, onOutcome}) => + const Stream.empty(), + ); + await hive.setCatalogueLink( + 'cat:tmdb|u', + const CatalogueLink( + providerId: 'vidapi', + providerName: 'VidAPI', + contentUrl: 'x', + ).encode(), + ); + final link = await resolver.resolve( + catalogueId: 'cat:tmdb', + contentUrl: 'u', + hint: _movie('Anything', category: 'movie'), + ); + expect(link?.providerName, 'VidAPI'); + }, + ); + test('the year breaks a tie between a title and its remake', () async { final resolver = CatalogueResolver( hive: _Hive(), @@ -439,15 +504,16 @@ void main() { test('with only manga readers the novel shelf asks nobody', () async { List? asked; - final out = await build( - installed: [_provider('mn:comics')], - answers: [_found('mn:comics', 'Spice and Wolf', 1.0)], - onAsk: (c) => asked = c, - ).locate( - catalogueId: 'cat:anilist-novel', - contentUrl: 'u', - hint: _movie('Spice and Wolf', category: 'novel'), - ); + final out = + await build( + installed: [_provider('mn:comics')], + answers: [_found('mn:comics', 'Spice and Wolf', 1.0)], + onAsk: (c) => asked = c, + ).locate( + catalogueId: 'cat:anilist-novel', + contentUrl: 'u', + hint: _movie('Spice and Wolf', category: 'novel'), + ); expect(asked, isNull); expect(out.found, isFalse); expect(out.miss, CatalogueMiss.noSourcesOfKind); diff --git a/test/features/download/download_source_name_test.dart b/test/features/download/download_source_name_test.dart new file mode 100644 index 00000000..dfc55e8c --- /dev/null +++ b/test/features/download/download_source_name_test.dart @@ -0,0 +1,63 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:soplay/features/download/domain/entities/download_item.dart'; +import 'package:soplay/features/download/domain/entities/downloaded_title.dart'; +import 'package:soplay/features/download/domain/entities/offline_title.dart'; +import 'package:soplay/features/download/presentation/pages/downloads_page.dart'; +import 'package:soplay/features/profile/domain/entities/provider_entity.dart'; + +DownloadedTitle _title(String provider, {String? savedName}) => DownloadedTitle( + key: 'k', + lead: DownloadItem( + id: 'a', + contentUrl: 'page', + provider: provider, + title: 'Film', + sourceUrl: 'video', + relativePath: 'downloads/a/video.mp4', + createdAt: 0, + ), + completed: 1, + total: 1, + sizeBytes: 1, + lastAt: 0, + snapshot: savedName == null + ? null + : OfflineTitle( + contentUrl: 'page', + provider: provider, + providerName: savedName, + title: 'Film', + savedAt: 1, + ), +); + +ProviderEntity _live(String id, String name) => ProviderEntity( + id: id, + name: name, + image: '', + url: '', + description: '', + domains: const [], +); + +void main() { + test('a renamed source shows its current name, not the saved one', () { + expect( + downloadSourceName(_title('vidapi', savedName: 'VidAPI'), [ + _live('vidapi', 'VixSrc'), + ]), + 'VixSrc', + ); + }); + + test('a source no longer installed keeps the name it was saved under', () { + expect( + downloadSourceName(_title('vidapi', savedName: 'VidAPI'), const []), + 'VidAPI', + ); + }); + + test('with no saved name and no source, the bare id is shown', () { + expect(downloadSourceName(_title('cs:moviebox'), const []), 'moviebox'); + }); +}