🐛 Fix netease switch sibling tracks issue

This commit is contained in:
LittleSheep 2024-09-04 23:40:47 +08:00
parent 19a7fd82df
commit 9012f560b5
3 changed files with 31 additions and 7 deletions

View File

@ -9,6 +9,9 @@ Their original app is good enough. But I just want to redesign the user interfac
- [x] Playing music - [x] Playing music
- [x] Add netease music as source - [x] Add netease music as source
- [ ] Add bilibili as source
- [ ] Add kuwo music as source
- [ ] Add kugo music as source
- [x] Re-design user interface - [x] Re-design user interface
- [x] Simplified UI and UX - [x] Simplified UI and UX
- [x] Support for large screen device - [x] Support for large screen device

View File

@ -32,14 +32,14 @@ class NeteaseSourcedTrack extends SourcedTrack {
required super.track, required super.track,
}); });
static String _getBaseUrl() { static String getBaseUrl() {
final preferences = Get.find<UserPreferencesProvider>().state.value; final preferences = Get.find<UserPreferencesProvider>().state.value;
return preferences.neteaseApiInstance; return preferences.neteaseApiInstance;
} }
static GetConnect _getClient() { static GetConnect getClient() {
final client = GetConnect(); final client = GetConnect();
client.baseUrl = _getBaseUrl(); client.baseUrl = getBaseUrl();
client.timeout = const Duration(seconds: 30); client.timeout = const Duration(seconds: 30);
return client; return client;
} }
@ -91,7 +91,7 @@ class NeteaseSourcedTrack extends SourcedTrack {
); );
} }
final client = _getClient(); final client = getClient();
final resp = await client.get('/song/detail?ids=${cachedSource.sourceId}'); final resp = await client.get('/song/detail?ids=${cachedSource.sourceId}');
final item = resp.body['songs'][0]; final item = resp.body['songs'][0];
@ -113,7 +113,7 @@ class NeteaseSourcedTrack extends SourcedTrack {
} }
static SourceMap toSourceMap(dynamic manifest) { static SourceMap toSourceMap(dynamic manifest) {
final baseUrl = _getBaseUrl(); final baseUrl = getBaseUrl();
// Due to netease may provide m4a, mp3 and others, we cannot decide this so mock this data. // Due to netease may provide m4a, mp3 and others, we cannot decide this so mock this data.
return SourceMap( return SourceMap(
@ -135,7 +135,7 @@ class NeteaseSourcedTrack extends SourcedTrack {
}) async { }) async {
final query = SourcedTrack.getSearchTerm(track); final query = SourcedTrack.getSearchTerm(track);
final client = _getClient(); final client = getClient();
final resp = final resp =
await client.get('/search?keywords=${Uri.encodeComponent(query)}'); await client.get('/search?keywords=${Uri.encodeComponent(query)}');
final results = resp.body['result']['songs']; final results = resp.body['result']['songs'];
@ -181,7 +181,7 @@ class NeteaseSourcedTrack extends SourcedTrack {
final newSiblings = siblings.where((s) => s.id != sibling.id).toList() final newSiblings = siblings.where((s) => s.id != sibling.id).toList()
..insert(0, sourceInfo); ..insert(0, sourceInfo);
final client = _getClient(); final client = getClient();
final resp = await client.get('/song/detail?ids=${newSourceInfo.id}'); final resp = await client.get('/song/detail?ids=${newSourceInfo.id}');
final item = resp.body['songs'][0]; final item = resp.body['songs'][0];

View File

@ -12,6 +12,7 @@ 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/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/netease.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/artist.dart';
@ -42,6 +43,7 @@ class _SiblingTracksState extends State<SiblingTracks> {
final sourceInfoToLabelMap = { final sourceInfoToLabelMap = {
YoutubeSourceInfo: 'YouTube', YoutubeSourceInfo: 'YouTube',
PipedSourceInfo: 'Piped', PipedSourceInfo: 'Piped',
NeteaseSourceInfo: 'Netease',
}; };
List<StreamSubscription>? _subscriptions; List<StreamSubscription>? _subscriptions;
@ -107,6 +109,25 @@ class _SiblingTracksState extends State<SiblingTracks> {
), ),
growable: true, growable: true,
); );
} else if (preferences.audioSource == AudioSource.netease) {
final client = NeteaseSourcedTrack.getClient();
final resp = await client
.get('/search?keywords=${Uri.encodeComponent(searchTerm)}');
final searchResults = resp.body['result']['songs']
.map(NeteaseSourcedTrack.toSiblingType)
.map((x) => x.info)
.toList();
final activeSourceInfo = (_activeTrack! as SourcedTrack).sourceInfo;
_siblings = List.from(
searchResults
..removeWhere((element) => element.id == activeSourceInfo.id)
..insert(
0,
activeSourceInfo,
),
growable: true,
);
} }
setState(() => _isSearching = false); setState(() => _isSearching = false);