Skip to content

Commit

Permalink
feat: add piped search mode
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Jun 28, 2023
1 parent a9b5a71 commit 17a25a5
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/collections/spotube_icons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ abstract class SpotubeIcons {
static const timer = FeatherIcons.clock;
static const logs = FeatherIcons.fileText;
static const clipboard = FeatherIcons.clipboard;
static const youtube = FeatherIcons.youtube;
static const skip = FeatherIcons.fastForward;
}
3 changes: 2 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,6 @@
"custom_hours": "Custom Hours",
"logs": "Logs",
"developers": "Developers",
"not_logged_in": "You're not logged in"
"not_logged_in": "You're not logged in",
"search_mode": "Search Mode"
}
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:spotube/hooks/use_disable_battery_optimizations.dart';
import 'package:spotube/l10n/l10n.dart';
import 'package:spotube/models/logger.dart';
import 'package:spotube/models/matched_track.dart';
import 'package:spotube/models/skip_segment.dart';
import 'package:spotube/provider/palette_provider.dart';
import 'package:spotube/provider/user_preferences_provider.dart';
import 'package:spotube/services/audio_player/audio_player.dart';
Expand Down Expand Up @@ -101,6 +102,7 @@ Future<void> main(List<String> rawArgs) async {
connectivity: FlQueryConnectivityPlusAdapter(),
);
Hive.registerAdapter(MatchedTrackAdapter());
Hive.registerAdapter(SkipSegmentAdapter());

await Hive.openLazyBox<MatchedTrack>(
MatchedTrack.boxName,
Expand Down
24 changes: 24 additions & 0 deletions lib/models/skip_segment.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:hive/hive.dart';

part 'skip_segment.g.dart';

@HiveType(typeId: 2)
class SkipSegment {
@HiveField(0)
final int start;
@HiveField(1)
final int end;
SkipSegment(this.start, this.end);

static const boxName = "oss.krtirtho.spotube.skip_segments";
static LazyBox<SkipSegment> get box => Hive.lazyBox<SkipSegment>(boxName);

SkipSegment.fromJson(Map<String, dynamic> json)
: start = json['start'],
end = json['end'];

Map<String, dynamic> toJson() => {
'start': start,
'end': end,
};
}
44 changes: 44 additions & 0 deletions lib/models/skip_segment.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 25 additions & 13 deletions lib/models/spotube_track.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import 'dart:async';
import 'dart:convert';

import 'package:catcher/catcher.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:http/http.dart';
import 'package:piped_client/piped_client.dart';
import 'package:spotify/spotify.dart';
import 'package:spotube/extensions/album_simple.dart';
import 'package:spotube/extensions/artist_simple.dart';
import 'package:spotube/models/logger.dart';
import 'package:spotube/models/matched_track.dart';
import 'package:spotube/provider/user_preferences_provider.dart';
import 'package:spotube/utils/platform.dart';
import 'package:spotube/utils/service_utils.dart';
import 'package:collection/collection.dart';

typedef SkipSegment = ({int start, int end});

class SpotubeTrack extends Track {
final PipedStreamResponse ytTrack;
final String ytUri;
Expand Down Expand Up @@ -68,8 +69,9 @@ class SpotubeTrack extends Track {

static Future<List<PipedSearchItemStream>> fetchSiblings(
Track track,
PipedClient client,
) async {
PipedClient client, [
PipedFilter filter = PipedFilter.musicSongs,
]) async {
final artists = (track.artists ?? [])
.map((ar) => ar.name)
.toList()
Expand All @@ -82,12 +84,8 @@ class SpotubeTrack extends Track {
onlyCleanArtist: true,
).trim();

final List<PipedSearchItemStream> siblings = await client
.search(
"$title - ${artists.join(", ")}",
PipedFilter.musicSongs,
)
.then(
final List<PipedSearchItemStream> siblings =
await client.search("$title - ${artists.join(", ")}", filter).then(
(res) {
final siblings = res.items
.whereType<PipedSearchItemStream>()
Expand Down Expand Up @@ -122,7 +120,14 @@ class SpotubeTrack extends Track {
if (matchedCachedTrack != null) {
ytVideo = await client.streams(matchedCachedTrack.youtubeId);
} else {
siblings = await fetchSiblings(track, client);
siblings = await fetchSiblings(
track,
client,
switch (preferences.searchMode) {
SearchMode.youtube => PipedFilter.video,
SearchMode.youtubeMusic => PipedFilter.musicSongs,
},
);
if (siblings.isEmpty) {
throw Exception("Failed to find any results for ${track.name}");
}
Expand Down Expand Up @@ -229,10 +234,17 @@ class SpotubeTrack extends Track {
);
}

Future<SpotubeTrack> populatedCopy(PipedClient client) async {
Future<SpotubeTrack> populatedCopy(
PipedClient client,
PipedFilter filter,
) async {
if (this.siblings.isNotEmpty) return this;

final siblings = await fetchSiblings(this, client);
final siblings = await fetchSiblings(
this,
client,
filter,
);

return SpotubeTrack.fromTrack(
track: this,
Expand Down
39 changes: 39 additions & 0 deletions lib/pages/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,45 @@ class SettingsPage extends HookConsumerWidget {
Text(error.toString()),
);
}),
AdaptiveSelectTile<SearchMode>(
secondary: const Icon(SpotubeIcons.youtube),
title: Text(context.l10n.search_mode),
value: preferences.searchMode,
options: SearchMode.values
.map((e) => DropdownMenuItem(
value: e,
child: Text(e.label),
))
.toList(),
onChanged: (value) {
if (value == null) return;
preferences.setSearchMode(value);
},
),
AnimatedOpacity(
duration: const Duration(milliseconds: 200),
opacity:
preferences.searchMode == SearchMode.youtubeMusic
? 0
: 1,
child: AnimatedSize(
duration: const Duration(milliseconds: 200),
child: SizedBox(
height: preferences.searchMode ==
SearchMode.youtubeMusic
? 0
: 50,
child: SwitchListTile(
secondary: const Icon(SpotubeIcons.skip),
title: Text(context.l10n.skip_non_music),
value: preferences.skipNonMusic,
onChanged: (state) {
preferences.setSkipNonMusic(state);
},
),
),
),
),
SwitchListTile(
secondary: const Icon(SpotubeIcons.download),
title: Text(context.l10n.pre_download_play),
Expand Down
Loading

0 comments on commit 17a25a5

Please sign in to comment.