Skip to content

Commit

Permalink
update mvi-dispatcher lib
Browse files Browse the repository at this point in the history
  • Loading branch information
KunMinX committed Sep 1, 2022
1 parent 43bc5b1 commit 3d4d1eb
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Jetpack-MusicPlayer 目标是:**一行代码即可接入 音乐播放控制组
1.在 build.gradle 中添加依赖。

```groovy
implementation 'com.kunminx.player:player:4.0.4'
implementation 'com.kunminx.player:player:4.1.0'
```

提示:鉴于 Jcenter 关闭,我们已将仓库迁移至 Maven Central,请自行在根目录 build.gradle 添加 `mavenCentral()`
Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ If you are thinking about [**how to choose the right architecture for your proje
1. Add a dependency on the library in build.gradle.

```groovy
implementation 'com.kunminx.player:player:4.0.4'
implementation 'com.kunminx.player:player:4.1.0'
```

Tip: 鉴于 Jcenter 的关闭,我们已将仓库迁移至 Maven Central,请自行在根目录 build.gradle 添加 `mavenCentral()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ public void onPanelStateChanged(
PlayerManager.getInstance().getDispatcher().output(this, playerEvent -> {
switch (playerEvent.eventId) {
case PlayerEvent.EVENT_CHANGE_MUSIC:
mStates.title.set(playerEvent.param.changeMusic.getTitle());
mStates.artist.set(playerEvent.param.changeMusic.getSummary());
mStates.coverImg.set(playerEvent.param.changeMusic.getImg());
mStates.title.set(playerEvent.changeMusic.getTitle());
mStates.artist.set(playerEvent.changeMusic.getSummary());
mStates.coverImg.set(playerEvent.changeMusic.getImg());
if (mListener != null) view.post(mListener::calculateTitleAndArtist);
break;
case PlayerEvent.EVENT_PROGRESS:
mStates.maxSeekDuration.set(playerEvent.param.playingMusic.getDuration());
mStates.currentSeekPosition.set(playerEvent.param.playingMusic.getPlayerPosition());
mStates.maxSeekDuration.set(playerEvent.playingMusic.getDuration());
mStates.currentSeekPosition.set(playerEvent.playingMusic.getPlayerPosition());
break;
case PlayerEvent.EVENT_PLAY_STATUS:
mStates.isPlaying.set(!playerEvent.param.toPause);
mStates.isPlaying.set(!playerEvent.toPause);
break;
case PlayerEvent.EVENT_REPEAT_MODE:
Enum<PlayingInfoManager.RepeatMode> anEnum = playerEvent.param.repeatMode;
Enum<PlayingInfoManager.RepeatMode> anEnum = playerEvent.repeatMode;
if (anEnum == PlayingInfoManager.RepeatMode.LIST_CYCLE) {
mStates.playModeIcon.set(MaterialDrawableBuilder.IconValue.REPEAT);
} else if (anEnum == PlayingInfoManager.RepeatMode.SINGLE_CYCLE) {
Expand Down Expand Up @@ -176,7 +176,7 @@ public static class PlayerViewModel extends ViewModel {
public final State<Drawable> placeHolder = new State<>(ContextCompat.getDrawable(Utils.getApp(), R.drawable.bg_album_default));
public final State<Integer> maxSeekDuration = new State<>(0);
public final State<Integer> currentSeekPosition = new State<>(0);
public final State<Boolean> isPlaying = new State<>(false, false);
public final State<Boolean> isPlaying = new State<>(false, true);
public final State<MaterialDrawableBuilder.IconValue> playModeIcon = new State<>(MaterialDrawableBuilder.IconValue.REPEAT);

{
Expand Down
2 changes: 1 addition & 1 deletion architecture/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ dependencies {

api "androidx.appcompat:appcompat:1.5.0"
api "androidx.navigation:navigation-runtime:2.5.1"
api "com.github.KunMinX:MVI-Dispatcher:7.0.2-beta"
api "com.github.KunMinX:MVI-Dispatcher:7.4.0-beta"
api 'com.github.KunMinX:Smooth-Navigation:v4.0.0'
api 'com.github.KunMinX.Strict-DataBinding:strict_databinding:5.6.0'
api 'com.github.KunMinX.Strict-DataBinding:binding_state:5.6.0'
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ buildscript {
ext {
appTargetSdk = 32
appMinSdk = 15
appVersionCode = 40004
appVersionName = "4.0.4"
appVersionCode = 40100
appVersionName = "4.1.0"
lifecycleVersion = "2.4.1"
navigationVersion = "2.4.2"
}
Expand Down
2 changes: 1 addition & 1 deletion player/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "androidx.appcompat:appcompat:1.5.0"
implementation "com.github.KunMinX:MVI-Dispatcher:7.0.4-beta"
implementation "com.github.KunMinX:MVI-Dispatcher:7.4.0-beta"
implementation "com.google.android.exoplayer:exoplayer:2.18.1"
}
21 changes: 10 additions & 11 deletions player/src/main/java/com/kunminx/player/PlayerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void updateProgress() {
mCurrentPlay.setAllTime(calculateTime(mPlayer.getDuration() / 1000));
mCurrentPlay.setDuration((int) mPlayer.getDuration());
mCurrentPlay.setPlayerPosition((int) mPlayer.getCurrentPosition());
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PROGRESS).setPlayingMusic(mCurrentPlay));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PROGRESS, mCurrentPlay));
if (mCurrentPlay.getAllTime().equals(mCurrentPlay.getNowTime())) {
if (getRepeatMode() == PlayingInfoManager.RepeatMode.SINGLE_CYCLE) playAgain();
else playNext();
Expand Down Expand Up @@ -112,7 +112,6 @@ public void playAudio(int albumIndex) {
playAudio();
}


public void playAudio() {
if (mIsChangingPlayingMusic) getUrlAndPlay();
else if (isPaused()) resumeAudio();
Expand Down Expand Up @@ -145,14 +144,14 @@ private void getUrlAndPlay() {
private void afterPlay() {
setChangingPlayingMusic(false);
mHandler.post(mProgressAction);
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS).setStatus(false));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, false));
if (mIServiceNotifier != null) mIServiceNotifier.notifyService(true);
}

public void requestLastPlayingInfo() {
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PROGRESS).setPlayingMusic(mCurrentPlay));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_CHANGE_MUSIC).setChangeMusic(mChangeMusic));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS).setStatus(isPaused()));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PROGRESS, mCurrentPlay));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_CHANGE_MUSIC, mChangeMusic));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, isPaused()));
}

public void setSeek(int progress) {
Expand Down Expand Up @@ -198,21 +197,21 @@ public void playAgain() {
public void pauseAudio() {
mPlayer.pause();
mHandler.removeCallbacks(mProgressAction);
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS).setStatus(true));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, true));
if (mIServiceNotifier != null) mIServiceNotifier.notifyService(true);
}

public void resumeAudio() {
mPlayer.play();
mHandler.post(mProgressAction);
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS).setStatus(false));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, false));
if (mIServiceNotifier != null) mIServiceNotifier.notifyService(true);
}

public void clear() {
mPlayer.stop();
mPlayer.clearMediaItems();
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS).setStatus(true));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_PLAY_STATUS, true));
resetIsChangingPlayingChapter();
if (mIServiceNotifier != null) mIServiceNotifier.notifyService(false);
}
Expand All @@ -223,7 +222,7 @@ public void resetIsChangingPlayingChapter() {
}

public void changeMode() {
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_REPEAT_MODE).setRepeatMode(mPlayingInfoManager.changeMode()));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_REPEAT_MODE, mPlayingInfoManager.changeMode()));
}

public B getAlbum() {
Expand All @@ -238,7 +237,7 @@ public void setChangingPlayingMusic(boolean changingPlayingMusic) {
mIsChangingPlayingMusic = changingPlayingMusic;
if (mIsChangingPlayingMusic) {
mChangeMusic.setBaseInfo(mPlayingInfoManager.getMusicAlbum(), getCurrentPlayingMusic());
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_CHANGE_MUSIC).setChangeMusic(mChangeMusic));
mDispatcher.input(new PlayerEvent(PlayerEvent.EVENT_CHANGE_MUSIC, mChangeMusic));
mCurrentPlay.setBaseInfo(mPlayingInfoManager.getMusicAlbum(), getCurrentPlayingMusic());
mCurrentPlay.setNowTime("00:00");
mCurrentPlay.setAllTime("00:00");
Expand Down
67 changes: 40 additions & 27 deletions player/src/main/java/com/kunminx/player/domain/PlayerEvent.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,65 @@
package com.kunminx.player.domain;

import com.kunminx.architecture.domain.event.Event;
import com.kunminx.player.PlayingInfoManager;
import com.kunminx.player.bean.dto.ChangeMusic;
import com.kunminx.player.bean.dto.PlayingMusic;

/**
* Create by KunMinX at 2022/7/4
*/
public class PlayerEvent extends Event<PlayerEvent.Param, PlayerEvent.Result> {
public class PlayerEvent {
public final static int EVENT_CHANGE_MUSIC = 1;
public final static int EVENT_PROGRESS = 2;
public final static int EVENT_PLAY_STATUS = 3;
public final static int EVENT_REPEAT_MODE = 4;

public PlayerEvent(int eventId) {
this.eventId = eventId;
this.param = new Param();
this.result = new Result();
}
public final int eventId;
public final ChangeMusic changeMusic;
public final PlayingMusic playingMusic;
public final boolean toPause;
public final Enum<PlayingInfoManager.RepeatMode> repeatMode;

public static class Param {
public ChangeMusic changeMusic;
public PlayingMusic playingMusic;
public boolean toPause;
public Enum<PlayingInfoManager.RepeatMode> repeatMode;
}

public PlayerEvent setPlayingMusic(PlayingMusic music) {
param.playingMusic = music;
return this;
public PlayerEvent(int eventId,
ChangeMusic changeMusic,
PlayingMusic playingMusic,
boolean toPause,
Enum<PlayingInfoManager.RepeatMode> repeatMode) {
this.eventId = eventId;
this.changeMusic = changeMusic;
this.playingMusic = playingMusic;
this.toPause = toPause;
this.repeatMode = repeatMode;
}

public PlayerEvent setChangeMusic(ChangeMusic music) {
param.changeMusic = music;
return this;
public PlayerEvent(int eventId, ChangeMusic changeMusic) {
this.eventId = eventId;
this.changeMusic = changeMusic;
this.playingMusic = null;
this.toPause = false;
this.repeatMode = null;
}

public PlayerEvent setStatus(boolean toPause) {
param.toPause = toPause;
return this;
public PlayerEvent(int eventId, PlayingMusic playingMusic) {
this.eventId = eventId;
this.changeMusic = null;
this.playingMusic = playingMusic;
this.toPause = false;
this.repeatMode = null;
}

public PlayerEvent setRepeatMode(Enum<PlayingInfoManager.RepeatMode> repeatMode) {
param.repeatMode = repeatMode;
return this;
public PlayerEvent(int eventId, boolean toPause) {
this.eventId = eventId;
this.changeMusic = null;
this.playingMusic = null;
this.toPause = toPause;
this.repeatMode = null;
}

public static class Result {
public PlayerEvent(int eventId, Enum<PlayingInfoManager.RepeatMode> repeatMode) {
this.eventId = eventId;
this.changeMusic = null;
this.playingMusic = null;
this.toPause = false;
this.repeatMode = repeatMode;
}
}

0 comments on commit 3d4d1eb

Please sign in to comment.