Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion lib/features/detail/domain/services/catalogue_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<CatalogueLink> _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<CatalogueResolution> locate({
required String catalogueId,
Expand All @@ -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(
Expand Down
33 changes: 23 additions & 10 deletions lib/features/download/presentation/pages/downloads_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 <ProviderEntity>[];
try {
final state = context.read<ProviderBloc>().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) {
Expand Down Expand Up @@ -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<ProviderEntity> 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;
}
84 changes: 75 additions & 9 deletions test/features/detail/catalogue_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -439,15 +504,16 @@ void main() {

test('with only manga readers the novel shelf asks nobody', () async {
List<ProviderEntity>? 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);
Expand Down
63 changes: 63 additions & 0 deletions test/features/download/download_source_name_test.dart
Original file line number Diff line number Diff line change
@@ -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');
});
}
Loading