|
| 1 | +import 'dart:math'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 5 | +import 'package:go_router/go_router.dart'; |
| 6 | + |
| 7 | +import '../../../core/api/bili_dio.dart'; |
| 8 | +import '../../../core/router/app_router.dart'; |
| 9 | +import '../../../core/utils/logger.dart'; |
| 10 | +import '../../auth/application/auth_notifier.dart'; |
| 11 | +import '../../player/application/player_notifier.dart'; |
| 12 | +import '../../player/domain/models/audio_track.dart'; |
| 13 | +import '../../playlist/data/playlist_repository_impl.dart'; |
| 14 | +import '../../search_and_parse/data/parse_repository.dart'; |
| 15 | +import '../../search_and_parse/data/parse_repository_impl.dart'; |
| 16 | +import '../../settings/application/settings_notifier.dart'; |
| 17 | + |
| 18 | +/// 极简模式页面(内部代号:给我一首歌)。 |
| 19 | +/// |
| 20 | +/// 全黑背景,自动随机抽歌并播放。 |
| 21 | +/// 双击屏幕退出极简模式,回到原版主页。 |
| 22 | +/// 退到后台时自动停止播放。 |
| 23 | +class MinimalScreen extends ConsumerStatefulWidget { |
| 24 | + const MinimalScreen({super.key}); |
| 25 | + |
| 26 | + @override |
| 27 | + ConsumerState<MinimalScreen> createState() => _MinimalScreenState(); |
| 28 | +} |
| 29 | + |
| 30 | +class _MinimalScreenState extends ConsumerState<MinimalScreen> |
| 31 | + with WidgetsBindingObserver { |
| 32 | + String _statusText = '🎵 正在为你播放...'; |
| 33 | + bool _isLoading = true; |
| 34 | + |
| 35 | + @override |
| 36 | + void initState() { |
| 37 | + super.initState(); |
| 38 | + // 监听 App 生命周期,用于即完即走 |
| 39 | + WidgetsBinding.instance.addObserver(this); |
| 40 | + // 进入页面后触发异步抽歌 |
| 41 | + WidgetsBinding.instance.addPostFrameCallback((_) { |
| 42 | + _pickAndPlay(); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + @override |
| 47 | + void dispose() { |
| 48 | + WidgetsBinding.instance.removeObserver(this); |
| 49 | + super.dispose(); |
| 50 | + } |
| 51 | + |
| 52 | + /// 监听生命周期:退到后台或划掉应用时停止播放 |
| 53 | + @override |
| 54 | + void didChangeAppLifecycleState(AppLifecycleState state) { |
| 55 | + if (state == AppLifecycleState.paused || |
| 56 | + state == AppLifecycleState.detached) { |
| 57 | + _stopPlayback(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// 停止播放 |
| 62 | + void _stopPlayback() { |
| 63 | + try { |
| 64 | + ref.read(playerNotifierProvider.notifier).pause(); |
| 65 | + } catch (e) { |
| 66 | + AppLogger.error('停止播放失败', tag: 'Minimal', error: e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// 核心逻辑:抽歌并播放 |
| 71 | + Future<void> _pickAndPlay() async { |
| 72 | + final parseRepo = ParseRepositoryImpl(biliDio: BiliDio()); |
| 73 | + |
| 74 | + try { |
| 75 | + final track = await _resolveRandomTrack(parseRepo); |
| 76 | + if (!mounted) return; |
| 77 | + |
| 78 | + // 获取音频流 URL |
| 79 | + final streamInfo = await parseRepo.getAudioStream( |
| 80 | + track.bvid, |
| 81 | + track.cid, |
| 82 | + quality: _preferredQuality, |
| 83 | + ); |
| 84 | + if (!mounted) return; |
| 85 | + |
| 86 | + final playableTrack = track.copyWith( |
| 87 | + streamUrl: streamInfo.url, |
| 88 | + quality: streamInfo.quality, |
| 89 | + ); |
| 90 | + |
| 91 | + // 调用全局播放器播放 |
| 92 | + await ref.read(playerNotifierProvider.notifier).playTrack(playableTrack); |
| 93 | + if (!mounted) return; |
| 94 | + |
| 95 | + setState(() { |
| 96 | + _statusText = '🎵 ${playableTrack.title}\n${playableTrack.artist}'; |
| 97 | + _isLoading = false; |
| 98 | + }); |
| 99 | + } catch (e, st) { |
| 100 | + AppLogger.error('极简模式抽歌失败', tag: 'Minimal', error: e, stackTrace: st); |
| 101 | + if (!mounted) return; |
| 102 | + setState(() { |
| 103 | + _statusText = '😢 加载失败,双击退出'; |
| 104 | + _isLoading = false; |
| 105 | + }); |
| 106 | + ScaffoldMessenger.of(context).showSnackBar( |
| 107 | + const SnackBar(content: Text('网络请求失败,请检查网络后重试')), |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// 获取用户偏好音质 |
| 113 | + int? get _preferredQuality { |
| 114 | + final q = ref.read(settingsNotifierProvider).preferredQuality; |
| 115 | + return q == 0 ? null : q; |
| 116 | + } |
| 117 | + |
| 118 | + /// 根据优先级获取随机曲目的 AudioTrack(不含 streamUrl) |
| 119 | + /// |
| 120 | + /// 优先级:本地歌单 > 搜索音乐热门 |
| 121 | + Future<AudioTrack> _resolveRandomTrack(ParseRepository parseRepo) async { |
| 122 | + final random = Random(); |
| 123 | + |
| 124 | + // ── 优先级 1:从设置中指定的本地歌单随机抽歌 ── |
| 125 | + final minimalPlaylistId = await ref |
| 126 | + .read(settingsNotifierProvider.notifier) |
| 127 | + .getMinimalPlaylistId(); |
| 128 | + if (minimalPlaylistId != null && minimalPlaylistId > 0) { |
| 129 | + try { |
| 130 | + final db = ref.read(databaseProvider); |
| 131 | + final playlistRepo = PlaylistRepositoryImpl(db: db); |
| 132 | + final songs = await playlistRepo.getSongsInPlaylist(minimalPlaylistId); |
| 133 | + if (songs.isNotEmpty) { |
| 134 | + final song = songs[random.nextInt(songs.length)]; |
| 135 | + return AudioTrack( |
| 136 | + songId: song.id, |
| 137 | + bvid: song.bvid, |
| 138 | + cid: song.cid, |
| 139 | + title: song.displayTitle, |
| 140 | + artist: song.displayArtist, |
| 141 | + coverUrl: song.coverUrl, |
| 142 | + localPath: song.localPath, |
| 143 | + duration: Duration(seconds: song.duration), |
| 144 | + quality: song.audioQuality, |
| 145 | + ); |
| 146 | + } |
| 147 | + } catch (e) { |
| 148 | + AppLogger.warning('从本地歌单获取歌曲失败,回退到搜索', tag: 'Minimal'); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // ── 优先级 2:搜索 B站 音乐区热门视频 ── |
| 153 | + final searchResult = await parseRepo.searchVideos('音乐'); |
| 154 | + final videos = searchResult.results; |
| 155 | + if (videos.isEmpty) { |
| 156 | + throw Exception('未找到任何音乐视频'); |
| 157 | + } |
| 158 | + |
| 159 | + // 随机选一个视频 |
| 160 | + final video = videos[random.nextInt(videos.length)]; |
| 161 | + |
| 162 | + // 需要通过 getVideoInfo 获取 cid(搜索结果不包含 pages/cid) |
| 163 | + final videoInfo = await parseRepo.getVideoInfo(video.bvid); |
| 164 | + if (videoInfo.pages.isEmpty) { |
| 165 | + throw Exception('视频 ${video.bvid} 没有可用分P'); |
| 166 | + } |
| 167 | + final page = videoInfo.pages.first; |
| 168 | + |
| 169 | + return AudioTrack( |
| 170 | + songId: 0, |
| 171 | + bvid: videoInfo.bvid, |
| 172 | + cid: page.cid, |
| 173 | + title: videoInfo.title, |
| 174 | + artist: videoInfo.owner, |
| 175 | + coverUrl: videoInfo.coverUrl, |
| 176 | + duration: Duration(seconds: page.duration), |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + /// 双击退出极简模式,回到原版主页 |
| 181 | + void _exitMinimalMode() { |
| 182 | + context.go(AppRoutes.home); |
| 183 | + } |
| 184 | + |
| 185 | + @override |
| 186 | + Widget build(BuildContext context) { |
| 187 | + return Scaffold( |
| 188 | + backgroundColor: Colors.black, |
| 189 | + body: GestureDetector( |
| 190 | + onDoubleTap: _exitMinimalMode, |
| 191 | + behavior: HitTestBehavior.opaque, |
| 192 | + child: SizedBox.expand( |
| 193 | + child: Column( |
| 194 | + mainAxisAlignment: MainAxisAlignment.center, |
| 195 | + children: [ |
| 196 | + if (_isLoading) |
| 197 | + const CircularProgressIndicator(color: Colors.white54), |
| 198 | + const SizedBox(height: 24), |
| 199 | + Padding( |
| 200 | + padding: const EdgeInsets.symmetric(horizontal: 32), |
| 201 | + child: Text( |
| 202 | + _statusText, |
| 203 | + textAlign: TextAlign.center, |
| 204 | + style: const TextStyle( |
| 205 | + color: Colors.white70, |
| 206 | + fontSize: 20, |
| 207 | + height: 1.6, |
| 208 | + ), |
| 209 | + ), |
| 210 | + ), |
| 211 | + const SizedBox(height: 48), |
| 212 | + if (!_isLoading) |
| 213 | + const Text( |
| 214 | + '双击屏幕退出极简模式', |
| 215 | + style: TextStyle(color: Colors.white24, fontSize: 13), |
| 216 | + ), |
| 217 | + ], |
| 218 | + ), |
| 219 | + ), |
| 220 | + ), |
| 221 | + ); |
| 222 | + } |
| 223 | +} |
0 commit comments