Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a favorites feature to remember and easily access favorite albums. #799

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.anpmech.mpd.subsystem.status.IdleSubsystemMonitor;
import com.anpmech.mpd.subsystem.status.StatusChangeListener;
import com.anpmech.mpd.subsystem.status.TrackPositionListener;
import com.namelessdev.mpdroid.favorites.Favorites;
import com.namelessdev.mpdroid.helpers.CachedMPD;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test a modifier

import com.namelessdev.mpdroid.helpers.MPDAsyncHelper;
import com.namelessdev.mpdroid.helpers.UpdateTrackInfo;
Expand Down Expand Up @@ -82,6 +83,8 @@ class MPDApplicationBase extends Application implements

private MPD mMPD;

private Favorites mFavorites;

private MPDAsyncHelper mMPDAsyncHelper;

private ServiceBinder mServiceBinder;
Expand Down Expand Up @@ -232,6 +235,10 @@ public MPD getMPD() {
return mMPD;
}

public Favorites getFavorites(){
return mFavorites;
}

/**
* Called upon receiving messages from any handler, in this case most often the Service.
*
Expand Down Expand Up @@ -395,6 +402,8 @@ public void onCreate() {
mMPD = new MPD();
}

mFavorites = new Favorites(this);

mIdleSubsystemMonitor = new IdleSubsystemMonitor(mMPD);
}

Expand Down
16 changes: 14 additions & 2 deletions MPDroid/src/main/java/com/namelessdev/mpdroid/SearchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.anpmech.mpd.item.Item;
import com.anpmech.mpd.item.Music;
import com.namelessdev.mpdroid.adapters.SeparatedListAdapter;
import com.namelessdev.mpdroid.favorites.Favorites;
import com.namelessdev.mpdroid.helpers.MPDAsyncHelper.AsyncExecListener;
import com.namelessdev.mpdroid.library.SimpleLibraryActivity;
import com.namelessdev.mpdroid.tools.Tools;
Expand Down Expand Up @@ -71,6 +72,8 @@ public class SearchActivity extends MPDActivity implements OnMenuItemClickListen

public static final int GOTO_ALBUM = 4;

public static final int ADD_TO_FAVORITES = 5;

public static final int MAIN = 0;

public static final int PLAYLIST = 3;
Expand Down Expand Up @@ -374,6 +377,8 @@ public void onCreateContextMenu(final ContextMenu menu, final View v,

switch (mPager.getCurrentItem()) {
case RESULT_ALBUM:
final MenuItem addToFavoritesIcon = menu.add(Menu.NONE, ADD_TO_FAVORITES, 0, R.string.addToFavorites);
addToFavoritesIcon.setOnMenuItemClickListener(this);
final Album album = mAlbumResults.get((int) info.id);
menu.setHeaderTitle(album.toString());
setContextForObject(album);
Expand Down Expand Up @@ -428,7 +433,7 @@ public void onItemClick(final AdapterView<?> parent, final View view, final int
intent.putExtra(Artist.EXTRA, (Parcelable) selectedItem);
startActivityForResult(intent, -1);
} else if (selectedItem instanceof Album) {
final Intent intent = new Intent(this, SimpleLibraryActivity.class);
final Intent intent = new Intent(this, SimpleLibraryActivity.class);
intent.putExtra(Album.EXTRA, (Parcelable) selectedItem);
startActivityForResult(intent, -1);
}
Expand Down Expand Up @@ -471,7 +476,14 @@ public boolean onMenuItemClick(final MenuItem item) {
intent.putExtra(Album.EXTRA, music.getAlbum());
startActivityForResult(intent, -1);
}
} else {
}
else if (item.getItemId() == ADD_TO_FAVORITES) {
if (selectedItem instanceof Album){
Favorites favorites = new Favorites(this);
favorites.addAlbum((Album)selectedItem);
}
}
else{
mApp.getAsyncHelper().execAsync(new Runnable() {
@Override
public void run() {
Expand Down
135 changes: 135 additions & 0 deletions MPDroid/src/main/java/com/namelessdev/mpdroid/favorites/Favorites.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (C) 2010-2016 The MPDroid Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.namelessdev.mpdroid.favorites;

import com.anpmech.mpd.item.Album;
import com.anpmech.mpd.item.AlbumBuilder;
import com.anpmech.mpd.item.Artist;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* Created by thaag on 2/14/2016.
*/
public class Favorites {

private final Context mContext;
private Set<Album> mAlbums = new HashSet<>();

public Favorites(Context context){
this.mContext = context;
loadFavorites();
}

public void addAlbum(final Album newAlbum) {
boolean alreadyInList = false;
for (Album album : mAlbums){
if (album.equals(newAlbum)){
alreadyInList = true;
break;
}
}

if (!alreadyInList){
mAlbums.add(newAlbum);
}

saveFavorites();
}

public void removeAlbum(final Album remAlbum){
Iterator<Album> iterator = mAlbums.iterator();
while (iterator.hasNext()){
Album album = iterator.next();
if (album.equals(remAlbum)){
iterator.remove();
break;
}
}

saveFavorites();
}

public Collection<Album> getAlbums() {
loadFavorites();
return mAlbums;
}

public void loadFavorites() {
mAlbums.clear();

SharedPreferences prefs = mContext.getSharedPreferences("favorites",0);
JSONArray jsonFavorites = null;
try {
jsonFavorites = new JSONArray(prefs.getString("favorites", "{}"));

} catch (JSONException e) {
e.printStackTrace();
}

if (jsonFavorites != null){
final AlbumBuilder albumBuilder = new AlbumBuilder();


for (int i=0; i<jsonFavorites.length(); i++){
try {
JSONObject jsonAlbum = jsonFavorites.getJSONObject(i);
albumBuilder.setBase(jsonAlbum.getString("albumName"), jsonAlbum.getString("albumArtist"), true);
mAlbums.add(albumBuilder.build());
} catch (JSONException e) {
e.printStackTrace();
}

}

}
}

public void saveFavorites(){
JSONArray jsonFavorites = new JSONArray();
for (Album album : mAlbums){
JSONObject jsonAlbum = new JSONObject();
try {
jsonAlbum.put("albumName", album.getName());
jsonAlbum.put("albumArtist", album.getArtist().getName());
} catch (JSONException e) {
e.printStackTrace();
}

jsonFavorites.put(jsonAlbum);
}

SharedPreferences prefs = mContext.getSharedPreferences("favorites",0);
SharedPreferences.Editor editor = prefs.edit();

editor.putString("favorites", jsonFavorites.toString());
editor.apply();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.namelessdev.mpdroid.adapters.ArrayIndexerAdapter;
import com.namelessdev.mpdroid.cover.CoverAsyncHelper;
import com.namelessdev.mpdroid.cover.CoverManager;
import com.namelessdev.mpdroid.favorites.Favorites;
import com.namelessdev.mpdroid.helpers.AlbumInfo;
import com.namelessdev.mpdroid.library.ILibraryFragmentActivity;
import com.namelessdev.mpdroid.tools.Tools;
Expand Down Expand Up @@ -152,6 +153,12 @@ private void cleanupCover(final MenuItem item, final boolean isWrongCover) {
updateNowPlayingSmallFragment(albumInfo);
}

private void addToFavorites(final MenuItem item){
final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Favorites favorites = new Favorites(getActivity());
favorites.addAlbum(mItems.get((int) info.id));
}

@Override
protected Artist getArtist(final Album item) {
return item.getArtist();
Expand Down Expand Up @@ -220,6 +227,8 @@ public void onCreateContextMenu(final ContextMenu menu, final View v,
final MenuItem resetCoverItem = menu.add(POPUP_COVER_SELECTIVE_CLEAN,
POPUP_COVER_SELECTIVE_CLEAN, 0, R.string.resetCover);
resetCoverItem.setOnMenuItemClickListener(this);
final MenuItem addToFavoritesItem = menu.add(POPUP_ADD_TO_FAVORITES, POPUP_ADD_TO_FAVORITES , 0, R.string.addToFavorites);
addToFavoritesItem.setOnMenuItemClickListener(this);
}

@Override
Expand Down Expand Up @@ -275,6 +284,9 @@ public boolean onMenuItemClick(final MenuItem item) {
case POPUP_COVER_SELECTIVE_CLEAN:
cleanupCover(item, false);
break;
case POPUP_ADD_TO_FAVORITES:
addToFavorites(item);
break;
default:
result = super.onMenuItemClick(item);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ abstract class BrowseFragmentBase<T extends Item<T>> extends Fragment implements

public static final int POPUP_COVER_SELECTIVE_CLEAN = 11;

public static final int POPUP_ADD_TO_FAVORITES = 12;

public static final int POPUP_REMOVE_FROM_FAVORITES = 24;

/**
* This is the group number used to enable or disable the playlist add group.
*/
Expand Down Expand Up @@ -495,11 +499,11 @@ public void onCreateContextMenu(final ContextMenu menu, final View v,
}
}

if (getArtist(item) != null) {
final MenuItem gotoArtistItem =
menu.add(GOTO_ARTIST, GOTO_ARTIST, 0, R.string.goToArtist);
gotoArtistItem.setOnMenuItemClickListener(this);
}
//if (getArtist(item) != null) {
// final MenuItem gotoArtistItem =
// menu.add(GOTO_ARTIST, GOTO_ARTIST, 0, R.string.goToArtist);
// gotoArtistItem.setOnMenuItemClickListener(this);
//}
}
}

Expand Down
Loading