Skip to content

Commit

Permalink
[android] fix display of recents, refactor on albumLoader.
Browse files Browse the repository at this point in the history
  • Loading branch information
gubatron committed Feb 8, 2016
1 parent 175e0c4 commit fe5e80f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public int getViewCount() {
}

/**
* @param position - The ACTUAL position in the model container. If you're using an offset based on a list view that has a header element at 0, you must substract to the position you might have.
* @param position - The ACTUAL position in the model container. If you're using an offset based on a list view that has a header element at 0, you must subtract to the position you might have.
* @return The element at the indexed position in the model container (not the view). null if position is out of bounds.
*/
public I getItem(int position) {
Expand All @@ -237,7 +237,7 @@ public I getItem(int position) {
/**
*
* @param position The ACTUAL position in the model container.
* If you have an extra header element, substract the offset before invoking this method.
* If you have an extra header element, subtract the offset before invoking this method.
* @return the object id. if out of bound, returns -1.
*/
@Override
Expand Down
36 changes: 16 additions & 20 deletions android/apollo/src/com/andrew/apollo/loaders/AlbumLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,7 @@ public List<Album> loadInBackground() {
// Gather the data
if (mCursor != null && mCursor.moveToFirst()) {
do {
// Copy the album id
final long id = mCursor.getLong(0);

// Copy the album name
final String albumName = mCursor.getString(1);

// Copy the artist name
final String artist = mCursor.getString(2);

// Copy the number of songs
final int songCount = mCursor.getInt(3);

// Copy the release year
final String year = mCursor.getString(4);

// Create a new album
final Album album = new Album(id, albumName, artist, songCount, year);

// Add everything up
mAlbumsList.add(album);
mAlbumsList.add(getAlbumEntryFromCursor(mCursor));
//String an = mCursor.getString(1) != null ? mCursor.getString(1) : "n/a";
//String art = mCursor.getString(2) != null ? mCursor.getString(2) : "n/a";
//LOGGER.info("Adding id: " + mCursor.getLong(0) + " - albumName: " + an + " - artist: " + art);
Expand All @@ -95,6 +76,21 @@ public List<Album> loadInBackground() {
return mAlbumsList;
}

protected Album getAlbumEntryFromCursor(Cursor cursor) {
// Copy the album id
final long id = cursor.getLong(0);
// Copy the album name
final String albumName = cursor.getString(1);
// Copy the artist name
final String artist = cursor.getString(2);
// Copy the number of songs
final int songCount = cursor.getInt(3);
// Copy the release year
final String year = cursor.getString(4);
// Create a new album
return new Album(id, albumName, artist, songCount, year);
}

/**
* Creates the {@link Cursor} used to run the query.
*
Expand Down
27 changes: 23 additions & 4 deletions android/apollo/src/com/andrew/apollo/loaders/RecentLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import android.content.Context;
import android.database.Cursor;
import com.andrew.apollo.model.Album;
import com.andrew.apollo.provider.RecentStore;
import com.andrew.apollo.provider.RecentStore.RecentStoreColumns;

Expand Down Expand Up @@ -50,10 +51,28 @@ public static Cursor makeRecentCursor(final Context context) {
.getReadableDatabase()
.query(RecentStoreColumns.NAME,
new String[] {
RecentStoreColumns.ID + " as id", RecentStoreColumns.ID,
RecentStoreColumns.ALBUMNAME, RecentStoreColumns.ARTISTNAME,
RecentStoreColumns.ALBUMSONGCOUNT, RecentStoreColumns.ALBUMYEAR,
RecentStoreColumns.TIMEPLAYED
RecentStoreColumns.ID + " as id", /** 0 - id */
RecentStoreColumns.ID, /** 1 - albumid */
RecentStoreColumns.ALBUMNAME, /** 2 - itemname */
RecentStoreColumns.ARTISTNAME, /** 3 - artistname */
RecentStoreColumns.ALBUMSONGCOUNT, /** 4 - albumsongcount */
RecentStoreColumns.ALBUMYEAR, /** 5 - albumyear */
RecentStoreColumns.TIMEPLAYED /** 6 - timeplayed */
}, null, null, null, null, RecentStoreColumns.TIMEPLAYED + " DESC");
}

protected Album getAlbumEntryFromCursor(Cursor cursor) {
// Copy the album id
final long id = cursor.getLong(0);
// Copy the album name
final String albumName = cursor.getString(2);
// Copy the artist name
final String artist = cursor.getString(3);
// Copy the number of songs
final int songCount = cursor.getInt(4);
// Copy the release year
final String year = cursor.getString(5);
// Create a new album
return new Album(id, albumName, artist, songCount, year);
}
}
29 changes: 13 additions & 16 deletions android/apollo/src/com/andrew/apollo/provider/RecentStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ public String getAlbumName(final String key) {
}
final SQLiteDatabase database = getReadableDatabase();
final String[] projection = new String[] {
RecentStoreColumns.ID, RecentStoreColumns.ALBUMNAME, RecentStoreColumns.ARTISTNAME,
RecentStoreColumns.ID,
RecentStoreColumns.ALBUMNAME,
RecentStoreColumns.ARTISTNAME,
RecentStoreColumns.TIMEPLAYED
};
final String selection = RecentStoreColumns.ARTISTNAME + "=?";
Expand All @@ -151,12 +153,10 @@ public String getAlbumName(final String key) {
final String album = cursor.getString(cursor
.getColumnIndexOrThrow(RecentStoreColumns.ALBUMNAME));
cursor.close();
cursor = null;
return album;
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
cursor = null;
}

return null;
Expand All @@ -171,37 +171,34 @@ public void deleteDatabase() {
}

/**
* @param item The album Id to remove.
* @param albumId The album Id to remove.
*/
public void removeItem(final long albumId) {
final SQLiteDatabase database = getReadableDatabase();
database.delete(RecentStoreColumns.NAME, RecentStoreColumns.ID + " = ?", new String[] {
String.valueOf(albumId)
});

database.delete(RecentStoreColumns.NAME, RecentStoreColumns.ID + " = ?",
new String[] { String.valueOf(albumId) });
}

public interface RecentStoreColumns {

/* Table name */
public static final String NAME = "albumhistory";
String NAME = "albumhistory";

/* Album IDs column */
public static final String ID = "albumid";
String ID = "albumid";

/* Album name column */
public static final String ALBUMNAME = "itemname";
String ALBUMNAME = "itemname";

/* Artist name column */
public static final String ARTISTNAME = "artistname";
String ARTISTNAME = "artistname";

/* Album song count column */
public static final String ALBUMSONGCOUNT = "albumsongcount";
String ALBUMSONGCOUNT = "albumsongcount";

/* Album year column. It's okay for this to be null */
public static final String ALBUMYEAR = "albumyear";
String ALBUMYEAR = "albumyear";

/* Time played column */
public static final String TIMEPLAYED = "timeplayed";
String TIMEPLAYED = "timeplayed";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.andrew.apollo.provider.RecentStore;
import com.andrew.apollo.recycler.RecycleHolder;
import com.andrew.apollo.ui.activities.BaseActivity;
import com.andrew.apollo.ui.fragments.Fragments;
import com.andrew.apollo.utils.ApolloUtils;
import com.andrew.apollo.utils.MusicUtils;
import com.andrew.apollo.utils.NavUtils;
Expand Down Expand Up @@ -274,6 +275,7 @@ public boolean onContextItemSelected(final android.view.MenuItem item) {
case FragmentMenuItems.PLAYLIST_SELECTED:
final long playlistId = item.getIntent().getLongExtra("playlist", 0);
MusicUtils.addToPlaylist(getActivity(), songList, playlistId);
refresh();
return true;
case FragmentMenuItems.USE_AS_RINGTONE:
MusicUtils.setRingtone(getActivity(), mSelectedId);
Expand Down
1 change: 1 addition & 0 deletions android/apollo/src/com/andrew/apollo/utils/MusicUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ public static void addToPlaylist(final Context context, final long[] ids, final
final String message = context.getResources().getQuantityString(
R.plurals.NNNtrackstoplaylist, numinserted, numinserted);
AppMsg.makeText(context, message, AppMsg.STYLE_CONFIRM).show();
refresh();
} else {
LOG.warn("Unable to complete addToPlaylist, review the logic");
}
Expand Down

0 comments on commit fe5e80f

Please sign in to comment.