:sparklesS: Able to search siblings tracks

This commit is contained in:
LittleSheep 2024-08-29 17:55:35 +08:00
parent a063d19952
commit 7e95c167ef

View File

@ -1,15 +1,24 @@
import 'dart:async';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
import 'package:rhythm_box/providers/audio_player.dart'; import 'package:rhythm_box/providers/audio_player.dart';
import 'package:rhythm_box/providers/user_preferences.dart';
import 'package:rhythm_box/services/database/database.dart';
import 'package:rhythm_box/services/duration.dart'; import 'package:rhythm_box/services/duration.dart';
import 'package:rhythm_box/services/server/active_sourced_track.dart'; import 'package:rhythm_box/services/server/active_sourced_track.dart';
import 'package:rhythm_box/services/sourced_track/models/source_info.dart'; import 'package:rhythm_box/services/sourced_track/models/source_info.dart';
import 'package:rhythm_box/services/sourced_track/models/video_info.dart';
import 'package:rhythm_box/services/sourced_track/sourced_track.dart'; import 'package:rhythm_box/services/sourced_track/sourced_track.dart';
import 'package:rhythm_box/services/sourced_track/sources/piped.dart'; import 'package:rhythm_box/services/sourced_track/sources/piped.dart';
import 'package:rhythm_box/services/sourced_track/sources/youtube.dart'; import 'package:rhythm_box/services/sourced_track/sources/youtube.dart';
import 'package:rhythm_box/services/artist.dart';
import 'package:rhythm_box/services/utils.dart';
import 'package:rhythm_box/widgets/auto_cache_image.dart'; import 'package:rhythm_box/widgets/auto_cache_image.dart';
import 'package:rhythm_box/widgets/tracks/querying_track_info.dart'; import 'package:rhythm_box/widgets/tracks/querying_track_info.dart';
import 'package:spotify/spotify.dart';
class SiblingTracks extends StatefulWidget { class SiblingTracks extends StatefulWidget {
const SiblingTracks({super.key}); const SiblingTracks({super.key});
@ -23,77 +32,189 @@ class _SiblingTracksState extends State<SiblingTracks> {
late final ActiveSourcedTrackProvider _activeSource = Get.find(); late final ActiveSourcedTrackProvider _activeSource = Get.find();
late final AudioPlayerProvider _playback = Get.find(); late final AudioPlayerProvider _playback = Get.find();
get _activeTrack => final TextEditingController _searchTermController = TextEditingController();
Track? get _activeTrack =>
_activeSource.state.value ?? _playback.state.value.activeTrack; _activeSource.state.value ?? _playback.state.value.activeTrack;
List<SourceInfo> get _siblings => !_query.isQueryingTrackInfo.value List<SourceInfo> _siblings = List.empty(growable: true);
? [
(_activeTrack as SourcedTrack).sourceInfo,
..._activeSource.state.value!.siblings,
]
: [];
final sourceInfoToLabelMap = { final sourceInfoToLabelMap = {
YoutubeSourceInfo: 'YouTube', YoutubeSourceInfo: 'YouTube',
PipedSourceInfo: 'Piped', PipedSourceInfo: 'Piped',
}; };
List<StreamSubscription>? _subscriptions;
String? _lastActiveTrackId;
void _updateSiblings() {
_siblings = List.from(
!_query.isQueryingTrackInfo.value
? [
(_activeTrack as SourcedTrack).sourceInfo,
..._activeSource.state.value!.siblings,
]
: [],
growable: true,
);
}
void _updateSearchTerm() {
if (_lastActiveTrackId == _activeTrack?.id) return;
final title = ServiceUtils.getTitle(
_activeTrack?.name ?? '',
artists: _activeTrack?.artists?.map((e) => e.name!).toList() ?? [],
onlyCleanArtist: true,
).trim();
final defaultSearchTerm =
'$title - ${_activeTrack?.artists?.asString() ?? ''}';
_searchTermController.text = defaultSearchTerm;
}
bool _isSearching = false;
Future<void> _searchSiblings() async {
if (_isSearching) return;
if (_searchTermController.text.trim().isEmpty) return;
_siblings.clear();
setState(() => _isSearching = true);
final preferences = Get.find<UserPreferencesProvider>().state.value;
final searchTerm = _searchTermController.text.trim();
if (preferences.audioSource == AudioSource.youtube ||
preferences.audioSource == AudioSource.piped) {
final resultsYt = await youtubeClient.search.search(searchTerm.trim());
final searchResults = await Future.wait(
resultsYt.map(YoutubeVideoInfo.fromVideo).mapIndexed((i, video) async {
final siblingType = await YoutubeSourcedTrack.toSiblingType(i, video);
return siblingType.info;
}),
);
final activeSourceInfo = (_activeTrack! as SourcedTrack).sourceInfo;
_siblings = List.from(
searchResults
..removeWhere((element) => element.id == activeSourceInfo.id)
..insert(
0,
activeSourceInfo,
),
growable: true,
);
}
setState(() => _isSearching = false);
}
@override
void initState() {
super.initState();
_updateSearchTerm();
_updateSiblings();
_subscriptions = [
_playback.state.listen((value) async {
if (value.activeTrack != null) {
_updateSearchTerm();
_updateSiblings();
setState(() {});
}
}),
];
}
@override
void dispose() {
_searchTermController.dispose();
if (_subscriptions != null) {
for (final subscription in _subscriptions!) {
subscription.cancel();
}
}
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Obx( return Column(
() => ListView.builder( children: [
itemCount: _siblings.length, Container(
itemBuilder: (context, idx) { color: Theme.of(context).colorScheme.surfaceContainerHigh,
final item = _siblings[idx]; padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
final src = sourceInfoToLabelMap[item.runtimeType]; child: TextField(
return ListTile( controller: _searchTermController,
title: Text( decoration: InputDecoration(
item.title, isCollapsed: true,
overflow: TextOverflow.ellipsis, border: InputBorder.none,
maxLines: 1, hintText: 'search'.tr,
), ),
leading: Padding( onSubmitted: (_) {
padding: const EdgeInsets.all(8.0), _searchSiblings();
child: AutoCacheImage( },
item.thumbnail, ),
height: 64, ),
width: 64, if (_isSearching) const LinearProgressIndicator(minHeight: 3),
), Expanded(
), child: ListView.builder(
shape: RoundedRectangleBorder( itemCount: _siblings.length,
borderRadius: BorderRadius.circular(5), itemBuilder: (context, idx) {
), final item = _siblings[idx];
trailing: Text( final src = sourceInfoToLabelMap[item.runtimeType];
item.duration.toHumanReadableString(), return ListTile(
style: GoogleFonts.robotoMono(), title: Text(
), item.title,
subtitle: Row( overflow: TextOverflow.ellipsis,
children: [ maxLines: 1,
if (src != null) Text(src), ),
Expanded( leading: Padding(
child: Text( padding: const EdgeInsets.all(8.0),
' · ${item.artist}', child: AutoCacheImage(
maxLines: 1, item.thumbnail,
overflow: TextOverflow.ellipsis, height: 64,
width: 64,
), ),
), ),
], shape: RoundedRectangleBorder(
), borderRadius: BorderRadius.circular(5),
enabled: !_query.isQueryingTrackInfo.value, ),
tileColor: !_query.isQueryingTrackInfo.value && trailing: Text(
item.id == (_activeTrack as SourcedTrack).sourceInfo.id item.duration.toHumanReadableString(),
? Theme.of(context).colorScheme.secondaryContainer style: GoogleFonts.robotoMono(),
: null, ),
onTap: () { subtitle: Row(
if (!_query.isQueryingTrackInfo.value && children: [
item.id != (_activeTrack as SourcedTrack).sourceInfo.id) { if (src != null) Text(src),
_activeSource.swapSibling(item); Expanded(
Navigator.of(context).pop(); child: Text(
} ' · ${item.artist}',
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
),
enabled: !_query.isQueryingTrackInfo.value,
tileColor: !_query.isQueryingTrackInfo.value &&
item.id == (_activeTrack as SourcedTrack).sourceInfo.id
? Theme.of(context).colorScheme.secondaryContainer
: null,
onTap: () {
if (!_query.isQueryingTrackInfo.value &&
item.id != (_activeTrack as SourcedTrack).sourceInfo.id) {
_activeSource.swapSibling(item);
Navigator.of(context).pop();
}
},
);
}, },
); ),
}, ),
), ],
); );
} }
} }