From e4cc9e6939c19d66a54f93029a3ced30e378c008 Mon Sep 17 00:00:00 2001 From: KunMinX Date: Wed, 20 Sep 2023 09:50:48 +0800 Subject: [PATCH] update diff logic --- .../puremusic/ui/page/PlayerFragment.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/kunminx/puremusic/ui/page/PlayerFragment.java b/app/src/main/java/com/kunminx/puremusic/ui/page/PlayerFragment.java index a99783bd..4ad632b5 100755 --- a/app/src/main/java/com/kunminx/puremusic/ui/page/PlayerFragment.java +++ b/app/src/main/java/com/kunminx/puremusic/ui/page/PlayerFragment.java @@ -31,6 +31,7 @@ import com.kunminx.architecture.utils.Res; import com.kunminx.architecture.utils.ToastUtils; import com.kunminx.architecture.utils.Utils; +import com.kunminx.player.domain.PlayingInfoManager; import com.kunminx.puremusic.BR; import com.kunminx.puremusic.R; import com.kunminx.puremusic.databinding.FragmentPlayerBinding; @@ -149,21 +150,26 @@ public void onPanelStateChanged( } }); - // TODO tip 6:所有播放状态的改变,皆来自 "可信源" PlayerInfoDispatcher 统一分发, - // 确保 "消息分发可靠一致",避免不可预期推送和错误。 + // TODO tip 6:所有播放状态的改变,皆来自 getUiStates() 统一分发, + // 确保 "消息分发可靠一致",避免不可预期推送和错误, - // 如这么说无体会,详见 https://xiaozhuanlan.com/topic/6017825943 & https://juejin.cn/post/7117498113983512589 + // 细节 1: uiStates 回调只读,此处只可通过 getter 获取只读数据,避免数据被篡改, + // 细节 2: uiStates 每次都是整个推送,progress 等属性会造成 uiStates 的高频回推, + // 故此宜对低频变化属性做防抖处理,仅当属性值变化时,通知相关控件完成一次重绘, PlayerManager.getInstance().getUiStates().observe(getViewLifecycleOwner(), uiStates -> { - mStates.title.set(uiStates.getTitle()); - mStates.artist.set(uiStates.getSummary()); - mStates.coverImg.set(uiStates.getImg(), changed -> { + mStates.musicId.set(uiStates.getMusicId(), changed -> { + mStates.title.set(uiStates.getTitle()); + mStates.artist.set(uiStates.getSummary()); + mStates.coverImg.set(uiStates.getImg()); if (mListener != null) view.post(mListener::calculateTitleAndArtist); + mStates.maxSeekDuration.set(uiStates.getDuration()); }); - mStates.isPlaying.set(!uiStates.isPaused()); - mStates.playModeIcon.set(PlayerManager.getInstance().getModeIcon(uiStates.getRepeatMode())); - mStates.maxSeekDuration.set(uiStates.getDuration()); mStates.currentSeekPosition.set(uiStates.getProgress()); + mStates.isPlaying.set(!uiStates.isPaused()); + mStates.repeatMode.set(uiStates.getRepeatMode(), changed -> { + mStates.playModeIcon.set(PlayerManager.getInstance().getModeIcon(uiStates.getRepeatMode())); + }); }); } @@ -218,6 +224,8 @@ public void onStopTrackingTouch(SeekBar seekBar) { //如这么说无体会,详见 https://xiaozhuanlan.com/topic/6741932805 public static class PlayerStates extends StateHolder { + public final State musicId = new State<>("", true); + public final State> repeatMode = new State<>(PlayingInfoManager.RepeatMode.LIST_CYCLE, true); public final State title = new State<>(Utils.getApp().getString(R.string.app_name), true); public final State artist = new State<>(Utils.getApp().getString(R.string.app_name), true); public final State coverImg = new State<>("", true);