Skip to content

Commit 77848bb

Browse files
committed
🦄 refactor: 优化封装
1 parent 4c337fa commit 77848bb

2 files changed

Lines changed: 28 additions & 25 deletions

File tree

src/core/player/PlayModeManager.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ export class PlayModeManager {
7878
/**
7979
* 计算下一个随机模式
8080
*/
81-
private calculateNextShuffleMode(
82-
currentMode: ShuffleModeType,
83-
targetMode?: ShuffleModeType,
84-
): ShuffleModeType {
85-
if (targetMode) return targetMode;
81+
public calculateNextShuffleMode(currentMode: ShuffleModeType): ShuffleModeType {
8682
if (currentMode === "off") return "on";
8783
if (currentMode === "on") return "heartbeat";
8884
return "off";
@@ -114,11 +110,7 @@ export class PlayModeManager {
114110
/**
115111
* 执行开启心动模式的操作
116112
*/
117-
private async applyHeartbeatMode(
118-
signal: AbortSignal,
119-
previousMode: ShuffleModeType,
120-
playAction?: () => Promise<void>,
121-
) {
113+
private async applyHeartbeatMode(signal: AbortSignal, previousMode: ShuffleModeType) {
122114
const statusStore = useStatusStore();
123115
const musicStore = useMusicStore();
124116
const dataStore = useDataStore();
@@ -134,12 +126,6 @@ export class PlayModeManager {
134126
return;
135127
}
136128

137-
if (previousMode === "heartbeat") {
138-
if (playAction) await playAction();
139-
statusStore.showFullPlayer = true;
140-
return;
141-
}
142-
143129
this.loadingMessage = window.$message.loading("心动模式开启中...", {
144130
duration: 0, // 不自动关闭,必须手动 destroy
145131
});
@@ -194,15 +180,14 @@ export class PlayModeManager {
194180

195181
/**
196182
* 切换随机模式
197-
* @param mode 可选,直接设置目标模式。如果不传则按 Off -> On -> Heartbeat -> Off 顺序轮转
198-
* @param playAction 回调函数,用于在心动模式下触发播放。通常你应该传入 PlayerController.play,或者其他类似的方法
183+
* @param mode 要切换到的随机模式
199184
*/
200-
public async toggleShuffle(mode?: ShuffleModeType, playAction?: () => Promise<void>) {
185+
public async toggleShuffle(mode: ShuffleModeType) {
201186
const statusStore = useStatusStore();
202187
const signal = this.resetCurrentTask();
203188

189+
const nextMode = mode;
204190
const currentMode = statusStore.shuffleMode;
205-
const nextMode = this.calculateNextShuffleMode(currentMode, mode);
206191

207192
if (nextMode === currentMode) return;
208193

@@ -220,7 +205,7 @@ export class PlayModeManager {
220205
await this.applyShuffleOn(signal);
221206
break;
222207
case "heartbeat":
223-
await this.applyHeartbeatMode(signal, previousMode, playAction);
208+
await this.applyHeartbeatMode(signal, previousMode);
224209
break;
225210
default:
226211
await this.applyShuffleOff();
@@ -263,10 +248,10 @@ export class PlayModeManager {
263248
* 专门处理 SMTC 的随机按钮事件
264249
* @param playAction 播放回调
265250
*/
266-
public handleSmtcShuffle(playAction?: () => Promise<void>) {
251+
public handleSmtcShuffle() {
267252
const statusStore = useStatusStore();
268253
const nextMode = statusStore.shuffleMode === "off" ? "on" : "off";
269-
this.toggleShuffle(nextMode, playAction);
254+
this.toggleShuffle(nextMode);
270255
}
271256

272257
/**

src/core/player/PlayerController.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ class PlayerController {
825825
* 专门处理 SMTC 的随机按钮事件
826826
*/
827827
public handleSmtcShuffle() {
828-
this.playModeManager.handleSmtcShuffle(this.play.bind(this));
828+
this.playModeManager.handleSmtcShuffle();
829829
}
830830

831831
/**
@@ -953,7 +953,25 @@ class PlayerController {
953953
* @param mode 可选,直接设置目标模式。如果不传则按 Off -> On -> Heartbeat -> Off 顺序轮转
954954
*/
955955
public async toggleShuffle(mode?: ShuffleModeType) {
956-
await this.playModeManager.toggleShuffle(mode, this.play.bind(this));
956+
const statusStore = useStatusStore();
957+
const currentMode = statusStore.shuffleMode;
958+
959+
// 预判下一个模式
960+
const nextMode = mode ?? this.playModeManager.calculateNextShuffleMode(currentMode);
961+
962+
// 已经是心动模式,再次触发心动模式并播放
963+
if (currentMode === "heartbeat" && nextMode === "heartbeat") {
964+
if (!statusStore.playStatus) {
965+
await this.play();
966+
}
967+
statusStore.showFullPlayer = true;
968+
return;
969+
}
970+
971+
// 如果模式确实改变了,才让 Manager 进行繁重的数据处理
972+
if (currentMode !== nextMode) {
973+
await this.playModeManager.toggleShuffle(nextMode);
974+
}
957975
}
958976

959977
/**

0 commit comments

Comments
 (0)