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

Refactor Large Class Abstract Manager #115

Open
wants to merge 4 commits into
base: master
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/bin
*.apk
local.properties
.idea/
android-xbmcremote.iml
7 changes: 7 additions & 0 deletions patches/0000-Refactor AbstractManager.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
From: Corey Lee <[email protected]>
Date: Wed, 26 Nov 2014 21:57:27 -0500
Subject: [PATCH 0/3]
AbstractManager is a Large Class (Code Smell) and it has more than one responsibility.
Therefore, we extracted Sort attributes and methods to a new class called SortPreferenceMedia.


24 changes: 24 additions & 0 deletions patches/0001-Added-Preferences-For-IDE-to-Ignore-File.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
From 1c5a8cad50e88000cc247cc8981c3c7eb298896c Mon Sep 17 00:00:00 2001
From: Corey Lee <[email protected]>
Date: Wed, 26 Nov 2014 21:57:27 -0500
Subject: [PATCH 1/3] Added Preferences For IDE to Ignore File

Added files to the .gitignore from the preferences that were created by
Android Studio
---
.gitignore | 2 ++
1 file changed, 2 insertions(+)

diff --git a/.gitignore b/.gitignore
index d84c129..a60a685 100755
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,5 @@
/bin
*.apk
local.properties
+.idea/
+android-xbmcremote.iml
--
1.9.3 (Apple Git-50)

977 changes: 977 additions & 0 deletions patches/0002-Refactored-AbstractManager.patch

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions patches/0003-Removed-Sort-Methods-From-AbstractManager.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
From 9e86e636cf41432feb3b61a15821b4c7f4f2bbea Mon Sep 17 00:00:00 2001
From: Corey Lee <[email protected]>
Date: Wed, 26 Nov 2014 23:42:11 -0500
Subject: [PATCH 3/3] Removed Sort Methods From AbstractManager

- Removed Unnecessary method getSortOrder from AbstractManager
---
src/org/xbmc/android/remote/business/AbstractManager.java | 9 ---------
1 file changed, 9 deletions(-)

diff --git a/src/org/xbmc/android/remote/business/AbstractManager.java b/src/org/xbmc/android/remote/business/AbstractManager.java
index a4da556..ee036c5 100644
--- a/src/org/xbmc/android/remote/business/AbstractManager.java
+++ b/src/org/xbmc/android/remote/business/AbstractManager.java
@@ -317,15 +317,6 @@ public abstract class AbstractManager implements INotifiableManager {
{
mHandler.post(runnable);
}
-
- /**
- * Returns currently saved "sort by" value. If the preference was not set yet, or
- * if the current sort key is not set, return "ASC".
- * @return Sort order
- */
- protected String getSortOrder() {
- return sortPreferenceMedia.getSortOrder();
- }

protected boolean getHideWatched(Context context) {
return context.getSharedPreferences("global", Context.MODE_PRIVATE).getBoolean("HideWatched", false);
--
1.9.3 (Apple Git-50)

75 changes: 7 additions & 68 deletions src/org/xbmc/android/remote/business/AbstractManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@
import org.xbmc.api.object.ICoverArt;
import org.xbmc.api.presentation.INotifiableController;
import org.xbmc.api.type.CacheType;
import org.xbmc.api.type.SortType;
import org.xbmc.api.type.ThumbSize;
import org.xbmc.httpapi.WifiStateException;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;
Expand All @@ -56,31 +54,14 @@ public abstract class AbstractManager implements INotifiableManager {
public static final Boolean DEBUG = false;

protected static final String TAG = "AbstractManager";

public static final String PREF_SORT_BY_PREFIX = "sort_by_";
public static final String PREF_SORT_ORDER_PREFIX = "sort_order_";

/* The idea of the sort keys is to remember different sort settings for
* each type. In your controller, make sure you run setSortKey() in the
* onCreate() method.
*/
public static final int PREF_SORT_KEY_ALBUM = 1;
public static final int PREF_SORT_KEY_ARTIST = 2;
public static final int PREF_SORT_KEY_SONG = 3;
public static final int PREF_SORT_KEY_GENRE = 4;
public static final int PREF_SORT_KEY_FILEMODE = 5;
public static final int PREF_SORT_KEY_SHOW = 6;
public static final int PREF_SORT_KEY_MOVIE = 7;
public static final int PREF_SORT_KEY_EPISODE = 8;

protected INotifiableController mController = null;

protected final SortPreferenceMedia sortPreferenceMedia = new SortPreferenceMedia();

protected INotifiableController mController = null;

protected Handler mHandler;

protected SharedPreferences mPref;
protected int mCurrentSortKey;

protected List<Runnable> failedRequests = new ArrayList<Runnable>();

protected List<Runnable> failedRequests = new ArrayList<Runnable>();
/**
* Sets the handler used in the looping thread
* @param handler
Expand Down Expand Up @@ -331,54 +312,12 @@ public void run() {
}
});
}

/**
* Sets the static reference to the preferences object. Used to obtain
* current sort values.
* @param pref
*/
public void setPreferences(SharedPreferences pref) {
mPref = pref;
}

/**
* Sets which kind of view is currently active.
* @param sortKey
*/
public void setSortKey(int sortKey) {
mCurrentSortKey = sortKey;
}

public void post(Runnable runnable)
public void post(Runnable runnable)
{
mHandler.post(runnable);
}

/**
* Returns currently saved "sort by" value. If the preference was not set yet, or
* if the current sort key is not set, return default value.
* @param type Default value
* @return Sort by field
*/
protected int getSortBy(int type) {
if (mPref != null) {
return mPref.getInt(PREF_SORT_BY_PREFIX + mCurrentSortKey, type);
}
return type;
}

/**
* Returns currently saved "sort by" value. If the preference was not set yet, or
* if the current sort key is not set, return "ASC".
* @return Sort order
*/
protected String getSortOrder() {
if (mPref != null) {
return mPref.getString(PREF_SORT_ORDER_PREFIX + mCurrentSortKey, SortType.ORDER_ASC);
}
return SortType.ORDER_ASC;
}

protected boolean getHideWatched(Context context) {
return context.getSharedPreferences("global", Context.MODE_PRIVATE).getBoolean("HideWatched", false);
}
Expand Down
38 changes: 19 additions & 19 deletions src/org/xbmc/android/remote/business/MusicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
*
* @author Team XBMC
*/
public class MusicManager extends AbstractManager implements IMusicManager, ISortableManager, INotifiableManager {
public class MusicManager extends AbstractManager implements IMusicManager, INotifiableManager {

/**
* Gets all albums from database
Expand All @@ -71,7 +71,7 @@ public void getAlbums(final DataResponse<ArrayList<Album>> response, final Conte
mHandler.post(new Command<ArrayList<Album>>(response, this) {
@Override
public void doRun() throws Exception {
response.value = music(context).getAlbums(MusicManager.this, getSortBy(SortType.ALBUM), getSortOrder());
response.value = music(context).getAlbums(MusicManager.this, sortPreferenceMedia.getSortBy(SortType.ALBUM), sortPreferenceMedia.getSortOrder());
}
});
}
Expand All @@ -82,7 +82,7 @@ public void doRun() throws Exception {
*/
public ArrayList<Album> getAlbums(final Context context) {
try { //TODO fix this to throw
return music(context).getAlbums(MusicManager.this, getSortBy(SortType.ALBUM), getSortOrder());
return music(context).getAlbums(MusicManager.this, sortPreferenceMedia.getSortBy(SortType.ALBUM), sortPreferenceMedia.getSortOrder());
} catch (WifiStateException e) {
e.printStackTrace();
}
Expand All @@ -104,7 +104,7 @@ public void getAlbums(final DataResponse<ArrayList<Album>> response, final Artis
mHandler.post(new Command<ArrayList<Album>>(response, this){
@Override
public void doRun() throws Exception {
response.value = music(context).getAlbums(MusicManager.this, artist, getSortBy(SortType.ALBUM), getSortOrder());
response.value = music(context).getAlbums(MusicManager.this, artist, sortPreferenceMedia.getSortBy(SortType.ALBUM), sortPreferenceMedia.getSortOrder());
}
});
}
Expand All @@ -123,7 +123,7 @@ public void getAlbums(final DataResponse<ArrayList<Album>> response, final Genre
// });
mHandler.post(new Command<ArrayList<Album>>(response, this) {
public void doRun() throws Exception{
response.value = music(context).getAlbums(MusicManager.this, genre, getSortBy(SortType.ALBUM), getSortOrder());
response.value = music(context).getAlbums(MusicManager.this, genre, sortPreferenceMedia.getSortBy(SortType.ALBUM), sortPreferenceMedia.getSortOrder());
}
});
}
Expand All @@ -142,7 +142,7 @@ public void getSongs(final DataResponse<ArrayList<Song>> response, final Album a
// });
mHandler.post(new Command<ArrayList<Song>>(response, this) {
public void doRun() throws Exception{
response.value = music(context).getSongs(MusicManager.this, album, getSortBy(SortType.TRACK), getSortOrder());
response.value = music(context).getSongs(MusicManager.this, album, sortPreferenceMedia.getSortBy(SortType.TRACK), sortPreferenceMedia.getSortOrder());
}
});
}
Expand All @@ -161,7 +161,7 @@ public void getSongs(final DataResponse<ArrayList<Song>> response, final Artist
// });
mHandler.post(new Command<ArrayList<Song>>(response, this) {
public void doRun() throws Exception{
response.value = music(context).getSongs(MusicManager.this, artist, getSortBy(SortType.ARTIST), getSortOrder());
response.value = music(context).getSongs(MusicManager.this, artist, sortPreferenceMedia.getSortBy(SortType.ARTIST), sortPreferenceMedia.getSortOrder());
}
});
}
Expand All @@ -180,7 +180,7 @@ public void getSongs(final DataResponse<ArrayList<Song>> response, final Genre g
// });
mHandler.post(new Command<ArrayList<Song>>(response, this) {
public void doRun() throws Exception{
response.value = music(context).getSongs(MusicManager.this, genre, getSortBy(SortType.ARTIST), getSortOrder());
response.value = music(context).getSongs(MusicManager.this, genre, sortPreferenceMedia.getSortBy(SortType.ARTIST), sortPreferenceMedia.getSortOrder());
}
});
}
Expand Down Expand Up @@ -278,7 +278,7 @@ public void doRun() throws Exception{
final IMusicClient mc = music(context);
final IControlClient cc = control(context);
final int numAlreadyQueued = mc.getPlaylistSize(MusicManager.this);
response.value = mc.addToPlaylist(MusicManager.this, album, getSortBy(SortType.TRACK), getSortOrder());
response.value = mc.addToPlaylist(MusicManager.this, album, sortPreferenceMedia.getSortBy(SortType.TRACK), sortPreferenceMedia.getSortOrder());
checkForPlayAfterQueue(mc, cc, numAlreadyQueued);
}
});
Expand All @@ -296,7 +296,7 @@ public void doRun() throws Exception{
final IMusicClient mc = music(context);
final IControlClient cc = control(context);
final int numAlreadyQueued = mc.getPlaylistSize(MusicManager.this);
response.value = mc.addToPlaylist(MusicManager.this, genre, getSortBy(SortType.ARTIST), getSortOrder());
response.value = mc.addToPlaylist(MusicManager.this, genre, sortPreferenceMedia.getSortBy(SortType.ARTIST), sortPreferenceMedia.getSortOrder());
checkForPlayAfterQueue(mc, cc, numAlreadyQueued);
}
});
Expand Down Expand Up @@ -338,14 +338,14 @@ public void doRun() throws Exception{
int playPos = -1;
if (playlistSize == 0) { // if playlist is empty, add the whole album
int n = 0;
for (Song albumSong : mc.getSongs(MusicManager.this, album, getSortBy(PREF_SORT_KEY_ALBUM), getSortOrder())) {
for (Song albumSong : mc.getSongs(MusicManager.this, album, sortPreferenceMedia.getSortBy(SortPreferenceMedia.PREF_SORT_KEY_ALBUM), sortPreferenceMedia.getSortOrder())) {
if (albumSong.id == song.id) {
playPos = n;
break;
}
n++;
}
mc.addToPlaylist(MusicManager.this, album, getSortBy(PREF_SORT_KEY_ALBUM), getSortOrder());
mc.addToPlaylist(MusicManager.this, album, sortPreferenceMedia.getSortBy(SortPreferenceMedia.PREF_SORT_KEY_ALBUM), sortPreferenceMedia.getSortOrder());
response.value = true;
} else { // otherwise, only add the song
mc.addToPlaylist(MusicManager.this, song);
Expand Down Expand Up @@ -381,7 +381,7 @@ public void doRun() throws Exception{
final IMusicClient mc = music(context);
final IControlClient cc = control(context);
final int numAlreadyQueued = mc.getPlaylistSize(MusicManager.this);
response.value = mc.addToPlaylist(MusicManager.this, artist, getSortBy(SortType.ALBUM), getSortOrder());
response.value = mc.addToPlaylist(MusicManager.this, artist, sortPreferenceMedia.getSortBy(SortType.ALBUM), sortPreferenceMedia.getSortOrder());
checkForPlayAfterQueue(mc, cc, numAlreadyQueued);
}
});
Expand All @@ -400,7 +400,7 @@ public void doRun() throws Exception{
final IMusicClient mc = music(context);
final IControlClient cc = control(context);
final int numAlreadyQueued = mc.getPlaylistSize(MusicManager.this);
response.value = mc.addToPlaylist(MusicManager.this, artist, genre, getSortBy(SortType.ARTIST), getSortOrder());
response.value = mc.addToPlaylist(MusicManager.this, artist, genre, sortPreferenceMedia.getSortBy(SortType.ARTIST), sortPreferenceMedia.getSortOrder());
checkForPlayAfterQueue(mc, cc, numAlreadyQueued);
}
});
Expand Down Expand Up @@ -454,7 +454,7 @@ public void play(final DataResponse<Boolean> response, final Album album, final
mHandler.post(new Command<Boolean>(response, this) {
public void doRun() throws Exception{
control(context).stop(MusicManager.this);
response.value = music(context).play(MusicManager.this, album, getSortBy(SortType.TRACK), getSortOrder());
response.value = music(context).play(MusicManager.this, album, sortPreferenceMedia.getSortBy(SortType.TRACK), sortPreferenceMedia.getSortOrder());
}
});
}
Expand All @@ -468,7 +468,7 @@ public void play(final DataResponse<Boolean> response, final Genre genre, final
mHandler.post(new Command<Boolean>(response, this) {
public void doRun() throws Exception{
control(context).stop(MusicManager.this);
response.value = music(context).play(MusicManager.this, genre, getSortBy(SortType.ARTIST), getSortOrder());
response.value = music(context).play(MusicManager.this, genre, sortPreferenceMedia.getSortBy(SortType.ARTIST), sortPreferenceMedia.getSortOrder());
}
});
}
Expand Down Expand Up @@ -501,15 +501,15 @@ public void doRun() throws Exception{
int n = 0;
int playPos = 0;
mc.clearPlaylist(MusicManager.this);
for (Song albumSong : mc.getSongs(MusicManager.this, album, getSortBy(SortType.TRACK), getSortOrder())) {
for (Song albumSong : mc.getSongs(MusicManager.this, album, sortPreferenceMedia.getSortBy(SortType.TRACK), sortPreferenceMedia.getSortOrder())) {
if (albumSong.id == song.id) {
playPos = n;
break;
}
n++;
}
cc.stop(MusicManager.this);
mc.addToPlaylist(MusicManager.this, album, getSortBy(SortType.TRACK), getSortOrder());
mc.addToPlaylist(MusicManager.this, album, sortPreferenceMedia.getSortBy(SortType.TRACK), sortPreferenceMedia.getSortOrder());
if (playPos > 0) {
response.value = mc.playlistSetSong(MusicManager.this, playPos);
}
Expand All @@ -529,7 +529,7 @@ public void play(final DataResponse<Boolean> response, final Artist artist, fina
mHandler.post(new Command<Boolean>(response, this) {
public void doRun() throws Exception{
control(context).stop(MusicManager.this);
response.value = music(context).play(MusicManager.this, artist, getSortBy(SortType.ALBUM), getSortOrder());
response.value = music(context).play(MusicManager.this, artist, sortPreferenceMedia.getSortBy(SortType.ALBUM), sortPreferenceMedia.getSortOrder());
}
});
}
Expand Down
Loading