Skip to content

Commit

Permalink
Podcasts: catch and display search limit errors (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier authored Aug 1, 2023
1 parent 7974118 commit 66cec66
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/app/podcasts/podcast_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PodcastModel extends SafeChangeNotifier {

StreamSubscription<bool>? _searchChangedSub;

List<Item>? get searchResult => _podcastService.searchResult;
SearchResult? get searchResult => _podcastService.searchResult;

bool _searchActive = false;
bool get searchActive => _searchActive;
Expand Down Expand Up @@ -107,13 +107,13 @@ class PodcastModel extends SafeChangeNotifier {
_searchChangedSub = _podcastService.searchChanged.listen((_) {
notifyListeners();
});
if (_podcastService.searchResult?.isNotEmpty == true) return;
if (_podcastService.searchResult?.items.isNotEmpty == true) return;
final c = Country.values.firstWhereOrNull((c) => c.code == countryCode);
if (c != null) {
_country = c;
}
if (_podcastService.searchResult == null ||
_podcastService.searchResult?.isEmpty == true) {
_podcastService.searchResult?.items.isEmpty == true) {
search();
}

Expand Down
17 changes: 11 additions & 6 deletions lib/app/podcasts/podcasts_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:musicpod/data/audio.dart';
import 'package:musicpod/data/podcast_genre.dart';
import 'package:musicpod/l10n/l10n.dart';
import 'package:musicpod/service/podcast_service.dart';
import 'package:musicpod/string_x.dart';
import 'package:podcast_search/podcast_search.dart';
import 'package:provider/provider.dart';
import 'package:yaru_icons/yaru_icons.dart';
Expand Down Expand Up @@ -87,7 +88,7 @@ class _PodcastsPageState extends State<PodcastsPage> {
final setPodcastGenre = model.setPodcastGenre;
final searchResult = context.select((PodcastModel m) => m.searchResult);
final searchResultCount =
context.select((PodcastModel m) => m.searchResult?.length);
context.select((PodcastModel m) => m.searchResult?.resultCount);

final showWindowControls =
context.select((AppModel a) => a.showWindowControls);
Expand All @@ -106,17 +107,21 @@ class _PodcastsPageState extends State<PodcastsPage> {
.map((e) => const AudioCard())
.toList(),
);
} else if (searchResult.isEmpty == true) {
grid = NoSearchResultPage(
message: context.l10n.noPodcastChartsFound,
);
} else if (searchResult.items.isEmpty == true) {
final noResultMessage = searchResult.lastError.isEmpty
? context.l10n.noPodcastChartsFound
: (searchResult.lastError.contains('RangeError')
? '${context.l10n.decreaseSearchLimit} ${country?.name.capitalize()}'
: searchResult.lastError);

grid = NoSearchResultPage(message: noResultMessage);
} else {
grid = GridView.builder(
padding: kPodcastGridPadding,
itemCount: searchResultCount,
gridDelegate: kImageGridDelegate,
itemBuilder: (context, index) {
final podcast = searchResult.elementAt(index);
final podcast = searchResult.items.elementAt(index);

final image = SafeNetworkImage(
url: podcast.artworkUrl600,
Expand Down
3 changes: 2 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
"unknown": "Unknown",
"volume": "Volume",
"queue": "Queue",
"limit": "Limit"
"limit": "Limit",
"decreaseSearchLimit": "Please decrease the search limit for"
}
44 changes: 25 additions & 19 deletions lib/service/podcast_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class PodcastService {

final _searchChangedController = StreamController<bool>.broadcast();
Stream<bool> get searchChanged => _searchChangedController.stream;
List<Item>? _searchResult;
List<Item>? get searchResult => _searchResult;
SearchResult? _searchResult;
SearchResult? get searchResult => _searchResult;
final Search _search;

Future<void> dispose() async {
Expand All @@ -28,26 +28,32 @@ class PodcastService {
}) async {
_searchResult = null;

SearchResult results;
if (searchQuery == null || searchQuery.isEmpty == true) {
results = await _search.charts(
genre: podcastGenre == PodcastGenre.all ? '' : podcastGenre.id,
limit: limit,
country: country ?? Country.none,
);
} else {
results = await _search.search(
searchQuery,
country: country ?? Country.none,
language: Language.none,
limit: limit,
);
SearchResult? result;
String? error;
try {
if (searchQuery == null || searchQuery.isEmpty == true) {
result = await _search.charts(
genre: podcastGenre == PodcastGenre.all ? '' : podcastGenre.id,
limit: limit,
country: country ?? Country.none,
);
} else {
result = await _search.search(
searchQuery,
country: country ?? Country.none,
language: Language.none,
limit: limit,
);
}
} catch (e) {
error = e.toString();
}

if (results.successful && results.items.isNotEmpty) {
_searchResult = results.items;
if (result != null && result.successful) {
_searchResult = result;
} else {
_searchResult = [];
_searchResult =
SearchResult.fromError(lastError: error ?? 'Something went wrong');
}
_searchChangedController.add(true);
}
Expand Down

0 comments on commit 66cec66

Please sign in to comment.