Skip to content

Commit

Permalink
feat: show error dialog on piped API 500 error
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Aug 15, 2023
1 parent c94e5ba commit c69f81e
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 29 deletions.
46 changes: 46 additions & 0 deletions lib/components/shared/dialogs/piped_down_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotube/collections/spotube_icons.dart';
import 'package:spotube/extensions/context.dart';
import 'package:spotube/provider/user_preferences_provider.dart';

class PipedDownDialog extends HookConsumerWidget {
const PipedDownDialog({Key? key}) : super(key: key);

@override
Widget build(BuildContext context, ref) {
final pipedInstance =
ref.watch(userPreferencesProvider.select((s) => s.pipedInstance));
final ThemeData(:colorScheme) = Theme.of(context);

return AlertDialog(
insetPadding: const EdgeInsets.all(6),
contentPadding: const EdgeInsets.all(6),
icon: Icon(
SpotubeIcons.error,
color: colorScheme.error,
),
title: Text(
context.l10n.piped_api_down,
style: TextStyle(color: colorScheme.error),
),
content: Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child:
Text(context.l10n.piped_down_error_instructions(pipedInstance)),
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(context.l10n.ok),
),
FilledButton(
onPressed: () => Navigator.pop(context),
child: Text(context.l10n.settings),
),
],
);
}
}
4 changes: 3 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,7 @@
"ok": "Ok",
"failed_to_encrypt": "Failed to encrypt",
"encryption_failed_warning": "Spotube uses encryption to securely store your data. But failed to do so. So it'll fallback to insecure storage\nIf you're using linux, please make sure you've any secret-service (gnome-keyring, kde-wallet, keepassxc etc) installed",
"querying_info": "Querying info..."
"querying_info": "Querying info...",
"piped_api_down": "Piped API is down",
"piped_down_error_instructions": "The Piped instance {pipedInstance} is currently down\n\nEither change the instance or change the 'API type' to official YouTube API\n\nMake sure to restart the app after change"
}
6 changes: 2 additions & 4 deletions lib/models/spotube_track.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,17 @@ class SpotubeTrack extends Track {
await client.search("$title - ${artists.join(", ")}").then(
(res) {
final siblings = res
.sorted((a, b) => a.views.compareTo(b.views))
.where((item) {
return artists.any(
(artist) =>
client.preferences.searchMode == SearchMode.youtube ||
artist.toLowerCase() == item.channelName.toLowerCase(),
);
})
.take(10)
.toList();

if (siblings.isEmpty) {
return res.take(10).toList();
}

return siblings;
},
);
Expand Down
73 changes: 50 additions & 23 deletions lib/services/youtube/youtube.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:flutter_desktop_tools/flutter_desktop_tools.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:piped_client/piped_client.dart';
import 'package:spotube/collections/routes.dart';
import 'package:spotube/components/shared/dialogs/piped_down_dialog.dart';
import 'package:spotube/models/matched_track.dart';
import 'package:spotube/provider/user_preferences_provider.dart';
import 'package:spotube/utils/primitive_utils.dart';
Expand Down Expand Up @@ -133,6 +136,18 @@ class YoutubeEndpoints {
}
}

Future<void> showPipedErrorDialog(Exception e) async {
if (e is DioException && (e.response?.statusCode ?? 0) >= 500) {
final context = rootNavigatorKey?.currentContext;
if (context != null) {
await showDialog(
context: context,
builder: (context) => const PipedDownDialog(),
);
}
}
}

Future<List<YoutubeVideoInfo>> search(String query) async {
if (youtube != null) {
final res = await youtube!.search(
Expand All @@ -142,22 +157,27 @@ class YoutubeEndpoints {

return res.map(YoutubeVideoInfo.fromVideo).toList();
} else {
final res = await piped!.search(
query,
switch (preferences.searchMode) {
SearchMode.youtube => PipedFilter.video,
SearchMode.youtubeMusic => PipedFilter.musicSongs,
},
);
return res.items
.whereType<PipedSearchItemStream>()
.map(
(e) => YoutubeVideoInfo.fromSearchItemStream(
e,
preferences.searchMode,
),
)
.toList();
try {
final res = await piped!.search(
query,
switch (preferences.searchMode) {
SearchMode.youtube => PipedFilter.video,
SearchMode.youtubeMusic => PipedFilter.musicSongs,
},
);
return res.items
.whereType<PipedSearchItemStream>()
.map(
(e) => YoutubeVideoInfo.fromSearchItemStream(
e,
preferences.searchMode,
),
)
.toList();
} on Exception catch (e) {
await showPipedErrorDialog(e);
rethrow;
}
}
}

Expand Down Expand Up @@ -193,19 +213,26 @@ class YoutubeEndpoints {
}

Future<(YoutubeVideoInfo info, String streamingUrl)> video(
String id, SearchMode searchMode) async {
String id,
SearchMode searchMode,
) async {
if (youtube != null) {
final res = await youtube!.videos.get(id);
return (
YoutubeVideoInfo.fromVideo(res),
await streamingUrl(id),
);
} else {
final res = await piped!.streams(id);
return (
YoutubeVideoInfo.fromStreamResponse(res, searchMode),
_pipedStreamResponseToStreamUrl(res),
);
try {
final res = await piped!.streams(id);
return (
YoutubeVideoInfo.fromStreamResponse(res, searchMode),
_pipedStreamResponseToStreamUrl(res),
);
} on Exception catch (e) {
await showPipedErrorDialog(e);
rethrow;
}
}
}
}
43 changes: 42 additions & 1 deletion untranslated_messages.json
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
{}
{
"bn": [
"piped_api_down",
"piped_down_error_instructions"
],

"ca": [
"querying_info",
"piped_api_down",
"piped_down_error_instructions"
],

"de": [
"piped_api_down",
"piped_down_error_instructions"
],

"es": [
"piped_api_down",
"piped_down_error_instructions"
],

"fr": [
"piped_api_down",
"piped_down_error_instructions"
],

"hi": [
"piped_api_down",
"piped_down_error_instructions"
],

"ja": [
"piped_api_down",
"piped_down_error_instructions"
],

"zh": [
"piped_api_down",
"piped_down_error_instructions"
]
}

0 comments on commit c69f81e

Please sign in to comment.