Skip to content

Commit

Permalink
支持根据搜索结果删除歌曲
Browse files Browse the repository at this point in the history
  • Loading branch information
浊酒入清梦 committed Jun 11, 2019
1 parent d6aadc5 commit c031f45
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 16 deletions.
34 changes: 28 additions & 6 deletions app/src/main/java/com/mzz/zmusicplayer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
import com.mzz.zmusicplayer.adapter.MusicPage;
import com.mzz.zmusicplayer.adapter.MusicPagerAdapter;
import com.mzz.zmusicplayer.edit.EditHandler;
import com.mzz.zmusicplayer.play.SongListType;
import com.mzz.zmusicplayer.edit.RemovedSongInfo;
import com.mzz.zmusicplayer.play.PlayList;
import com.mzz.zmusicplayer.play.SongListType;
import com.mzz.zmusicplayer.receiver.HeadsetReceiver;
import com.mzz.zmusicplayer.song.ISongChangeListener;
import com.mzz.zmusicplayer.song.SongInfo;
Expand Down Expand Up @@ -180,6 +181,27 @@ public void updatePlayListSongs(List <SongInfo> songInfos) {
playListFragment.updatePlayListSongs(songInfos);
}

@Subscribe
public void removeSong(RemovedSongInfo removedSongInfo) {
SongInfo removedSong = removedSongInfo.getSongInfo();
switch (removedSongInfo.getSongListType()) {
case PLAYLIST:
playListFragment.remove(removedSong);
break;
case RECENT:
recentFragment.removeSong(removedSong);
break;
case LOCAL:
localSongFragment.removeSong(removedSong);
break;
case FAVORITE:
favoriteFragment.removeSong(removedSong);
break;
default:
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand All @@ -191,21 +213,21 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
data.getParcelableArrayListExtra(SongPickerActivity.EXTRA_ADD_SONG);
localSongFragment.addToLocalSongs(newSongInfos);
} else if (resultCode == SongListType.PLAYLIST.getCode()) {
List <Long> deleteIds = geiDeleteIds(data);
List <Long> deleteIds = getDeleteIds(data);
playListFragment.remove(deleteIds);
} else if (resultCode == SongListType.RECENT.getCode()) {
List <Long> deleteIds = geiDeleteIds(data);
List <Long> deleteIds = getDeleteIds(data);
recentFragment.remove(deleteIds);
} else if (resultCode == SongListType.LOCAL.getCode()) {
List <Long> deleteIds = geiDeleteIds(data);
List <Long> deleteIds = getDeleteIds(data);
localSongFragment.remove(deleteIds);
} else if (resultCode == SongListType.FAVORITE.getCode()) {
List <Long> deleteIds = geiDeleteIds(data);
List <Long> deleteIds = getDeleteIds(data);
favoriteFragment.remove(deleteIds);
}
}

private List <Long> geiDeleteIds(Intent data) {
private List <Long> getDeleteIds(Intent data) {
ArrayList <Integer> deleteIds =
data.getIntegerArrayListExtra(SongEditActivity.EXTRA_DELETE_ID);
if (deleteIds == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import com.chad.library.adapter.base.BaseViewHolder;
import com.mzz.zmusicplayer.common.TextQueryHandler;
import com.mzz.zmusicplayer.edit.RemovedSongInfo;
import com.mzz.zmusicplayer.play.PlayList;
import com.mzz.zmusicplayer.play.SongListType;
import com.mzz.zmusicplayer.song.SongInfo;

import org.greenrobot.eventbus.EventBus;
Expand All @@ -17,6 +19,7 @@
*/
public class MusicSearchAdapter extends PlayListAdapter {

SongListType songListType;
private TextQueryHandler textQueryHandler;

/**
Expand All @@ -29,13 +32,21 @@ public MusicSearchAdapter(PlayList playList, RecyclerView recyclerView) {
super(playList, recyclerView);
textQueryHandler = new TextQueryHandler(this, recyclerView.getContext(),
playList.getPlaySongs());
songListType = playList.getSongListType();
this.setOnItemClickListener((adapter, view, position) -> {
SongInfo song = getItem(position);
updatePlaySongBackgroundColor(song);
EventBus.getDefault().post(song);
});
}

@Override
public void removeSongAt(int position) {
SongInfo song = getItem(position);
EventBus.getDefault().post(new RemovedSongInfo(song, songListType));
super.removeSongAt(position);
}

@Override
protected void convert(BaseViewHolder helper, SongInfo songInfo) {
super.convert(helper, songInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,21 @@ protected SongListAdapter(PlayList playList, RecyclerView recyclerView,
songListHeader = new SongListHeader(activity, this, playList.getSongListType());
}

protected void updateSongCount() {
private void updateSongCount() {
songListHeader.updateSongCount();
}

@Override
public void removeSongAt(int position) {
super.removeSongAt(position);
updateSongCount();
}

public void removeSong(SongInfo song) {
int songIndexById = PlayList.getSongIndexById(getData(), song.getId());
removeSongAt(songIndexById);
}

/**
* Update data.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ interface Presenter {

void remove(List <Long> keys);

void remove(SongInfo song);

}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.mzz.zmusicplayer.edit;

import com.mzz.zmusicplayer.song.SongInfo;
import com.mzz.zmusicplayer.play.SongListType;
import com.mzz.zmusicplayer.song.SongInfo;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* 被移除的歌曲信息
* author : Mzz
* date : 2019 2019/6/11 18:45
* description :
*/
@AllArgsConstructor
public class SongEditInfo {
public class RemovedSongInfo {
@Getter
SongInfo songInfo;
@Getter
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/mzz/zmusicplayer/play/PlayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public PlayList[] newArray(int size) {
return new PlayList[size];
}
};

@Getter
private LocalSong localSongs;
@Getter
Expand Down Expand Up @@ -68,16 +67,18 @@ protected PlayList(Parcel in) {
this.playingIndex = in.readInt();
int tmpPlayMode = in.readInt();
this.playMode = tmpPlayMode == -1 ? null : PlayedMode.values()[tmpPlayMode];
int tmpSongListType = in.readInt();
this.songListType = tmpSongListType == -1 ? null : SongListType.values()[tmpSongListType];
}

/**
* Gets song index by id.
* Gets song index by id.根据ID获取歌曲在列表中的位置
*
* @param songInfos the song infos
* @param songId the song id
* @return the song index by id
*/
static int getSongIndexById(List <SongInfo> songInfos, long songId) {
public static int getSongIndexById(List <SongInfo> songInfos, long songId) {
int songIndex = -1;
//根据ID获取歌曲在列表中的位置
for (int i = 0; i < songInfos.size(); i++) {
Expand Down Expand Up @@ -310,6 +311,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(this.playSongs);
dest.writeInt(this.playingIndex);
dest.writeInt(this.playMode == null ? -1 : this.playMode.ordinal());
dest.writeInt(this.songListType == null ? -1 : this.songListType.ordinal());
}

public interface PlayListObserver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ public void remove(List <Long> keys) {
updatePlayList();
}

@Override
public void remove(SongInfo song) {
int songIndexById = PlayList.getSongIndexById(playList.getPlaySongs(), song.getId());
playListAdapter.removeSongAt(songIndexById);
playList.remove(song);
updatePlayList();
}

@Override
public void updatePlaySongBackgroundColor(SongInfo song) {
playListAdapter.updatePlaySongBackgroundColor(song);
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/mzz/zmusicplayer/ui/FavoriteFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,29 @@ public void removeSongAt(int position) {
songListAdapter.setScrollFirstShowInNeed(fabSongScrollFirst);
}

/**
* Remove.
*
* @param keys the keys
*/
public void remove(List <Long> keys) {
songListAdapter.updateData(favoriteSong.remove(keys));
}

/**
* Remove song.
*
* @param song the song
*/
public void removeSong(SongInfo song) {
if (song == null) {
return;
}
//获取喜欢列表中的对应歌曲,保证删除的是指定对象
int songIndexById = PlayList.getSongIndexById(songListAdapter.getData(), song.getId());
favoriteSong.remove(songListAdapter.getItem(songIndexById));
}

@Override
public void updatePlaySongBackgroundColor(SongInfo song) {
if (!isVisibleToUser) {
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/com/mzz/zmusicplayer/ui/LocalSongFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ private void initAdapter() {
public void removeSongAt(int position) {
localSongs.remove(getItem(position));
super.removeSongAt(position);
updateSongCount();
}
};
songListAdapter.setScrollFirstShowInNeed(fabSongScrollFirst);
Expand All @@ -118,6 +117,19 @@ public void remove(List <Long> keys) {
songListAdapter.updateData(localSongs.remove(keys));
}

/**
* Remove song.
*
* @param song the song
*/
public void removeSong(SongInfo song) {
if (song == null) {
return;
}
localSongs.remove(song);
songListAdapter.removeSong(song);
}

@Override
public void updatePlaySongBackgroundColor(SongInfo song) {
if (!isVisibleToUser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.widget.SearchView;

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/mzz/zmusicplayer/ui/PlayListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,28 @@ private void getListener() {
}
}

/**
* Update play list songs.
*
* @param checkedSongs the checked songs
*/
public void updatePlayListSongs(List <SongInfo> checkedSongs) {
playListPresenter.updatePlayListSongs(checkedSongs);
}

/**
* Remove.
*
* @param keys the keys
*/
public void remove(List <Long> keys) {
playListPresenter.remove(keys);
}

public void remove(SongInfo song) {
playListPresenter.remove(song);
}

@Override
public RecyclerView getRecyclerView() {
return rvSong;
Expand Down
19 changes: 18 additions & 1 deletion app/src/main/java/com/mzz/zmusicplayer/ui/RecentFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,33 @@ private void initAdapter() {
public void removeSongAt(int position) {
recentSong.remove(this.getItem(position));
super.removeSongAt(position);
updateSongCount();
}
};
songListAdapter.setScrollFirstShowInNeed(fabSongScrollFirst);
}

/**
* Remove.
*
* @param keys the keys
*/
public void remove(List <Long> keys) {
songListAdapter.updateData(recentSong.remove(keys));
}

/**
* Remove song.
*
* @param song the song
*/
public void removeSong(SongInfo song) {
if (song == null) {
return;
}
recentSong.remove(song);
songListAdapter.removeSong(song);
}

@Override
public void updatePlaySongBackgroundColor(SongInfo song) {
if (!isVisibleToUser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

private void initAdapter() {
HashSet <Integer> allSongIdInFile = LocalSong.getInstance().getAllSongIdInFile();
songInfos = FileManager.getInstance().getAllSongInfos(allSongIdInFile);
// HashSet <Integer> allSongIdInFile = LocalSong.getInstance().getAllSongIdInFile();
songInfos = FileManager.getInstance().getAllSongInfos(null);
songPickerAdapter = new SongPickerAdapter(songInfos, rvSongFile);
songPickerAdapter.setQueryTextListener(svSongFile);
songPickerAdapter.setScrollFirstShowInNeed(fabSongScrollFirst);
Expand Down

0 comments on commit c031f45

Please sign in to comment.