Skip to content

Commit

Permalink
[Refactor] LazyLoad Music.
Browse files Browse the repository at this point in the history
  • Loading branch information
canxin121 committed May 15, 2024
1 parent 5e49309 commit ec82d12
Show file tree
Hide file tree
Showing 17 changed files with 486 additions and 412 deletions.
7 changes: 4 additions & 3 deletions lib/comp/play_page_comp/lyric.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LyricDisplayState extends State<LyricDisplay> {
var lyricModel =
LyricsModelBuilder.create().bindLyricToMain("[00:00.00]无歌词").getModel();
var lyricUI = UINetease(lyricAlign: LyricAlign.CENTER, highlight: true);
late StreamSubscription<PlayMusic?> stream;
late StreamSubscription<Music?> stream;
@override
void initState() {
super.initState();
Expand Down Expand Up @@ -70,7 +70,7 @@ class LyricDisplayState extends State<LyricDisplay> {
globalAudioHandler.seek(toSeek).then((value) {
confirm.call();
// 这里是考虑到在暂停状态下。需要开启播放
if (!globalAudioHandler.isPlaying()) {
if (!globalAudioHandler.isPlaying) {
globalAudioHandler.play();
}
});
Expand All @@ -87,7 +87,8 @@ class LyricDisplayState extends State<LyricDisplay> {
),
Text(
formatDuration(Duration(milliseconds: progress).inSeconds),
style: const TextStyle(color: CupertinoColors.white).useSystemChineseFont(),
style: const TextStyle(color: CupertinoColors.white)
.useSystemChineseFont(),
)
],
);
Expand Down
22 changes: 9 additions & 13 deletions lib/comp/play_page_comp/music_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_down_button/pull_down_button.dart';

// PlayMusicList组件
class PlayMusicList extends StatelessWidget {
// MusicList组件
class MusicListComp extends StatelessWidget {
final double maxHeight;
final EdgeInsets picPadding;
final double itemHeight;
const PlayMusicList(
const MusicListComp(
{super.key,
required this.maxHeight,
required this.picPadding,
Expand All @@ -29,7 +29,7 @@ class PlayMusicList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Obx(() {
var musics = globalAudioHandler.playMusicList;
var musics = globalAudioHandler.musicList;

return Container(
constraints: BoxConstraints(maxHeight: maxHeight),
Expand Down Expand Up @@ -77,7 +77,7 @@ class PlayMusicList extends StatelessWidget {
// 播放展示界面的列表中的音乐卡片的长按触发操作
List<PullDownMenuEntry> displayListMusicCardPullDown(
BuildContext context,
PlayMusic music,
Music music,
Future<void> Function() onDelete,
Rect position,
) =>
Expand Down Expand Up @@ -143,11 +143,8 @@ List<PullDownMenuEntry> displayListMusicCardPullDown(
if (context.mounted) {
await showPullDownMenu(
context: context,
items: addToMusicListPullDown(
context,
musicLists,
Future.value([DisplayMusic(music.ref, info_: music.info)]),
position),
items: addToMusicListPullDown(context, musicLists,
Future.value([Music(music.ref)]), position),
position: position);
}
},
Expand Down Expand Up @@ -181,9 +178,8 @@ List<PullDownMenuEntry> displayListMusicCardPullDown(
),
PullDownMenuItem(
onTap: () async {
globalTopUiController.updateWidget(InMusicAlbumListPage(
key: UniqueKey(),
music: DisplayMusic(music.ref, info_: music.info)));
globalTopUiController.updateWidget(
InMusicAlbumListPage(key: UniqueKey(), music: Music(music.ref)));
},
title: "查看专辑",
icon: CupertinoIcons.music_albums,
Expand Down
13 changes: 2 additions & 11 deletions lib/comp/play_page_comp/quality_time.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:io';

import 'package:app_rhyme/src/rust/api/mirror.dart';
import 'package:app_rhyme/types/music.dart';
import 'package:app_rhyme/util/helper.dart';
import 'package:app_rhyme/util/audio_controller.dart';
import 'package:app_rhyme/util/time_parse.dart';
Expand Down Expand Up @@ -49,16 +48,8 @@ class QualityTimeState extends State<QualityTime> {
context: context,
items: qualitySelectPullDown(context, qualityOptions,
(selectQuality) async {
var playingMusic = globalAudioHandler.playingMusic.value;
if (playingMusic != null) {
var displayData = DisplayMusic(playingMusic.ref,
info_: playingMusic.info);
var newPlayMusic =
await display2PlayMusic(displayData, selectQuality);
if (newPlayMusic == null) return;
await globalAudioHandler
.replacePlayingMusic(newPlayMusic);
}
await globalAudioHandler
.replacePlayingMusic(selectQuality);
}),
position: details.globalPosition & Size.zero);
}
Expand Down
20 changes: 10 additions & 10 deletions lib/page/in_music_album.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import 'package:pull_down_button/pull_down_button.dart';
import 'package:toastification/toastification.dart';

class InMusicAlbumListPage extends StatefulWidget {
final DisplayMusic music;
final Music music;
const InMusicAlbumListPage({
super.key,
required this.music,
Expand All @@ -35,7 +35,7 @@ class InMusicAlbumListPage extends StatefulWidget {
class InMusicAlbumListPageState extends State<InMusicAlbumListPage> {
MusicList musicList = const MusicList(name: "Album", artPic: "", desc: "");
var allowEmptyTime = 3;
var pagingController = PagingController<int, DisplayMusic>(firstPageKey: 1);
var pagingController = PagingController<int, Music>(firstPageKey: 1);

@override
void initState() {
Expand Down Expand Up @@ -64,13 +64,13 @@ class InMusicAlbumListPageState extends State<InMusicAlbumListPage> {
musicList = musicList_;
});
}
var newMusics = newMusicsRefs.map((e) => DisplayMusic(e));
List<DisplayMusic> uniqueItems = [];
var newMusics = newMusicsRefs.map((e) => Music(e));
List<Music> uniqueItems = [];

if (pagingController.value.itemList != null) {
for (var newMusic in newMusics) {
bool exist = false;
for (DisplayMusic existItem in pagingController.value.itemList!) {
for (Music existItem in pagingController.value.itemList!) {
if (existItem.info.name == newMusic.info.name &&
existItem.info.artist.join(",") ==
newMusic.info.artist.join(",")) {
Expand Down Expand Up @@ -198,15 +198,15 @@ class InMusicAlbumListPageState extends State<InMusicAlbumListPage> {
music: displayMusic,
onClick: () {
globalAudioHandler.addMusicPlay(
displayMusic as DisplayMusic,
displayMusic as Music,
);
},
onPress: (details) async {
var position = details.globalPosition & Size.zero;
await showPullDownMenu(
context: context,
items: inMusicAlbumMusicCardPullDown(
context, displayMusic as DisplayMusic, position),
context, displayMusic as Music, position),
position: position);
},
),
Expand Down Expand Up @@ -253,7 +253,7 @@ Widget _buildButton(BuildContext context,
// 专辑界面的音乐卡片的长按触发操作
List<PullDownMenuEntry> inMusicAlbumMusicCardPullDown(
BuildContext context,
DisplayMusic music,
Music music,
Rect position,
) =>
[
Expand Down Expand Up @@ -349,7 +349,7 @@ List<PullDownMenuEntry> musicAlbumActionPullDown(
BuildContext context,
Future<void> Function() fetchAllMusic,
MusicList musiclist,
PagingController<int, DisplayMusic> pagingController,
PagingController<int, Music> pagingController,
Rect position,
) =>
[
Expand Down Expand Up @@ -448,7 +448,7 @@ List<PullDownMenuEntry> musicAlbumActionPullDown(
if (pagingController.itemList != null) {
return pagingController.itemList!;
}
}(), // 这里传递Future<List<DisplayMusic>>
}(), // 这里传递Future<List<Music>>
position),
position: position);
}
Expand Down
52 changes: 22 additions & 30 deletions lib/page/in_music_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'package:pull_down_button/pull_down_button.dart';

class InMusicListPage extends StatefulWidget {
final MusicList musicList;
final Future<List<DisplayMusic>>? musicsFuture;
final Future<List<Music>>? musicsFuture;

const InMusicListPage({
super.key,
Expand All @@ -34,7 +34,7 @@ class InMusicListPage extends StatefulWidget {

class InMusicListPageState extends State<InMusicListPage> {
late MusicList musicList;
late Future<List<DisplayMusic>> musicsFuture;
late Future<List<Music>> musicsFuture;

@override
void initState() {
Expand All @@ -43,15 +43,15 @@ class InMusicListPageState extends State<InMusicListPage> {
musicsFuture = widget.musicsFuture ?? getMusicsFromSQL();
}

Future<List<DisplayMusic>> getMusicsFromSQL() async {
Future<List<Music>> getMusicsFromSQL() async {
var results = await globalSqlMusicFactory.readMusic(musicList: musicList);
return results.map((m) => DisplayMusic(m)).toList();
return results.map((m) => Music(m)).toList();
}

@override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
List<DisplayMusic> musics = [];
List<Music> musics = [];
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
padding: const EdgeInsetsDirectional.only(end: 16),
Expand Down Expand Up @@ -132,7 +132,7 @@ class InMusicListPageState extends State<InMusicListPage> {
height: 1,
),
),
FutureBuilder<List<DisplayMusic>>(
FutureBuilder<List<Music>>(
future: musicsFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
Expand Down Expand Up @@ -164,27 +164,24 @@ class InMusicListPageState extends State<InMusicListPage> {
items: inListMusicCardPullDown(context, music,
() async {
// 缓存
var playMusic = await display2PlayMusic(music);
if (playMusic == null) return;
var playInfo = await music.getPlayInfo();
if (playInfo == null) return;
cacheFile(
file: playMusic.playInfo.file,
file: playInfo.file,
cachePath: musicCachePath,
filename: playMusic.toCacheFileName())
filename: music.toCacheFileName())
.then((file) {
// 下载完成之后设置本地路径为新的播放文件
playMusic.playInfo.file = file;
playInfo.file = file;
// 如果这首歌正在播放列表中,替换他,防止继续在线播放
globalAudioHandler.replaceMusic(playMusic);
globalAudioHandler.replaceMusic(music);
// 在这里需要重新判断是否 hasCache,所以直接setState解决
setState(() {});
});
}, () async {
// 删除缓存
if (music.info.defaultQuality == null) return;
var result = music.toCacheFileNameAndExtra(
music.info.defaultQuality!);
if (result == null) return;
var (cacheFileName, _) = result;
var cacheFileName = music.toCacheFileName();
deleteCacheFile(
file: "",
cachePath: musicCachePath,
Expand All @@ -195,10 +192,8 @@ class InMusicListPageState extends State<InMusicListPage> {
if (kDebugMode) {
print("成功删除缓存:${music.info.name}");
}
display2PlayMusic(music).then((value) {
if (value == null) return;
globalAudioHandler.replaceMusic(value);
});

globalAudioHandler.replaceMusic(music);
});
}, () async {
// 删除音乐
Expand Down Expand Up @@ -289,7 +284,7 @@ Widget _buildButton(BuildContext context,
// 在自定义歌单内的音乐卡片的长按触发操作
List<PullDownMenuEntry> inListMusicCardPullDown(
BuildContext context,
DisplayMusic music,
Music music,
Future<void> Function() onSave,
Future<void> Function() onUnSave,
Future<void> Function() onDelete,
Expand Down Expand Up @@ -396,7 +391,7 @@ List<PullDownMenuEntry> inListMusicCardPullDown(
// 资料库界面自定义歌单内部右上角的编辑触发的的长按触发操作
List<PullDownMenuEntry> musicListActionPullDown(
BuildContext context,
List<DisplayMusic> displayMusics,
List<Music> displayMusics,
void Function() refresh,
) =>
[
Expand All @@ -405,12 +400,12 @@ List<PullDownMenuEntry> musicListActionPullDown(
onTap: () async {
for (var music in displayMusics) {
if (await music.hasCache()) continue;
var playMusic = await display2PlayMusic(music);
if (playMusic != null) {
var playInfo = await music.getPlayInfo();
if (playInfo != null) {
await cacheFile(
file: playMusic.playInfo.file,
file: playInfo.file,
cachePath: musicCachePath,
filename: playMusic.toCacheFileName());
filename: music.toCacheFileName(quality_: playInfo.quality));
}
refresh();
}
Expand All @@ -422,10 +417,7 @@ List<PullDownMenuEntry> musicListActionPullDown(
onTap: () async {
for (var music in displayMusics) {
if (music.info.defaultQuality != null) {
var result =
music.toCacheFileNameAndExtra(music.info.defaultQuality!);
if (result == null) return;
var (cacheFileName, _) = result;
var cacheFileName = music.toCacheFileName();
await deleteCacheFile(
file: "", cachePath: musicCachePath, filename: cacheFileName);
refresh();
Expand Down
14 changes: 7 additions & 7 deletions lib/page/in_search_music_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class InSearchMusicListPage extends StatefulWidget {
class InSearchMusicListPageState extends State<InSearchMusicListPage> {
late MusicList musicList;
var allowEmptyTime = 3;
var pagingController = PagingController<int, DisplayMusic>(firstPageKey: 1);
var pagingController = PagingController<int, Music>(firstPageKey: 1);

@override
void initState() {
Expand All @@ -57,14 +57,14 @@ class InSearchMusicListPageState extends State<InSearchMusicListPage> {
source: 'KuWo',
payload: widget.payload,
))
.map((i) => DisplayMusic(i));
.map((i) => Music(i));

List<DisplayMusic> uniqueItems = [];
List<Music> uniqueItems = [];

if (pagingController.value.itemList != null) {
for (var newItem in newItems) {
bool exist = false;
for (DisplayMusic existItem in pagingController.value.itemList!) {
for (Music existItem in pagingController.value.itemList!) {
if (existItem.info.name == newItem.info.name &&
existItem.info.artist.join(",") ==
newItem.info.artist.join(",")) {
Expand Down Expand Up @@ -192,15 +192,15 @@ class InSearchMusicListPageState extends State<InSearchMusicListPage> {
music: displayMusic,
onClick: () {
globalAudioHandler.addMusicPlay(
displayMusic as DisplayMusic,
displayMusic as Music,
);
},
onPress: (details) async {
var position = details.globalPosition & Size.zero;
await showPullDownMenu(
context: context,
items: searchMusicCardPullDown(
context, displayMusic as DisplayMusic, position),
context, displayMusic as Music, position),
position: position);
},
),
Expand Down Expand Up @@ -247,7 +247,7 @@ Widget _buildButton(BuildContext context,
// 搜索界面的音乐卡片的长按触发操作
List<PullDownMenuEntry> searchMusicCardPullDown(
BuildContext context,
DisplayMusic music,
Music music,
Rect position,
) =>
[
Expand Down
2 changes: 1 addition & 1 deletion lib/page/playing_music_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class SongDisplayPageState extends State<SongDisplayPage> {
),
),
// 应当占据剩下的所有高度
PlayMusicList(
MusicListComp(
maxHeight: Platform.isIOS
? screenHeight * 0.87 - 350
: screenHeight * 0.87 - 300,
Expand Down
Loading

0 comments on commit ec82d12

Please sign in to comment.