Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Got the playlist working even better.

  • Loading branch information...
commit e01028fb311341a6c9968c37a699a87479e4619a 1 parent ebf5338
@klnusbaum authored
View
2  src/org/klnusbaum/udj/Constants.java
@@ -103,4 +103,6 @@
public static final String ACTION_SET_VOLUME = "org.klnusbaum.udj.SetVolume";
public static final String BROADCAST_VOLUME_CHANGED = "org.klnusbaum.udj.VolumeChanged";
+ public static final String BROADCAST_VOTE_COMPLETED = "org.klnusbaum.udj.VoteCompleted";
+
}
View
62 src/org/klnusbaum/udj/PlaylistAdapter.java
@@ -36,6 +36,8 @@
import android.view.ViewGroup;
import java.util.List;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentHashMap;
public class PlaylistAdapter extends BaseAdapter{
private static final String PLAYLIST_ADAPTER_TAG = "PlaylistAdapter";
@@ -47,6 +49,8 @@
private final PlaylistFragment plFrag;
private Account account;
private User me;
+ private ConcurrentMap<String, Long> idMap;
+ private long currentAvailableMapId;
public PlaylistAdapter(
Context context,
@@ -62,8 +66,17 @@ public PlaylistAdapter(
this.plFrag = plFrag;
this.account = account;
this.me = new User(userId);
+ this.currentAvailableMapId = 0;
+ idMap = new ConcurrentHashMap<String, Long>();
+ if(playlist != null){
+ for(ActivePlaylistEntry ae: playlist){
+ idMap.put(ae.song.getLibId(), currentAvailableMapId);
+ currentAvailableMapId++;
+ }
+ }
}
+
public int getCount(){
if(playlist != null){
return playlist.size();
@@ -79,7 +92,7 @@ public Object getItem(int position){
}
public long getItemId(int position){
- return position;
+ return idMap.get(playlist.get(position).song.getLibId());
}
public int getViewTypeCount(){
@@ -97,14 +110,37 @@ public int getItemViewType(int position){
return REGULAR_SONG_VIEW_TYPE;
}
- public void updatePlaylist(List<ActivePlaylistEntry> newPlaylist){
+ public synchronized void updatePlaylist(List<ActivePlaylistEntry> newPlaylist){
+ for(ActivePlaylistEntry ae: newPlaylist){
+ if (!idMap.keySet().contains(ae.song.getLibId())){
+ idMap.put(ae.song.getLibId(), currentAvailableMapId);
+ currentAvailableMapId++;
+ }
+ }
this.playlist = newPlaylist;
+ /**
+ * This algorithm is n*m complexity, might be able to do better...
+ */
+ for(String s: idMap.keySet()){
+ if(!playlistContainsId(s)){
+ idMap.remove(s);
+ }
+ }
notifyDataSetChanged();
}
+ private synchronized boolean playlistContainsId(String s){
+ for(ActivePlaylistEntry ae: playlist){
+ if(s.equals(ae.song.getLibId())){
+ return true;
+ }
+ }
+ return false;
+ }
+
@Override
public View getView(int position, View convertView, ViewGroup parent) {
- ActivePlaylistEntry currentEntry = (ActivePlaylistEntry)getItem(position);
+ final ActivePlaylistEntry currentEntry = (ActivePlaylistEntry)getItem(position);
final String libId = currentEntry.song.getLibId();
View view;
switch(getItemViewType(position)){
@@ -159,7 +195,7 @@ private View getCurrentSongView(View convertView, ViewGroup parent){
private View getRegularSongView(
- ActivePlaylistEntry currentEntry, View convertView, ViewGroup parent)
+ final ActivePlaylistEntry currentEntry, View convertView, ViewGroup parent)
{
View view = convertView;
if(convertView == null){
@@ -171,26 +207,28 @@ private View getRegularSongView(
final String title = currentEntry.song.getTitle();
final ImageButton upButton = (ImageButton)view.findViewById(R.id.upvote_button);
final ImageButton downButton = (ImageButton)view.findViewById(R.id.downvote_button);
- final Toast upVoteToast = Toast.makeText(context,
- context.getString(R.string.voting_up_message)+ " " + title, Toast.LENGTH_SHORT);
+
upButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
- v.setEnabled(false);
+ currentEntry.downvoters.remove(me);
+ currentEntry.upvoters.add(me);
upVoteSong(libId);
- upVoteToast.show();
+ notifyDataSetChanged();
}
});
- final Toast downVoteToast = Toast.makeText(context,
- context.getString(R.string.voting_down_message)+ " " + title, Toast.LENGTH_SHORT);
downButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
- v.setEnabled(false);
+ currentEntry.downvoters.add(me);
+ currentEntry.upvoters.remove(me);
downVoteSong(libId);
- downVoteToast.show();
+ notifyDataSetChanged();
}
});
+ /* Reset buttons from previous view*/
+ upButton.setEnabled(true);
+ downButton.setEnabled(true);
if(currentEntry.upvoters.contains(me)){
upButton.setEnabled(false);
}
View
32 src/org/klnusbaum/udj/PlaylistFragment.java
@@ -1,18 +1,18 @@
/**
* Copyright 2011 Kurtis L. Nusbaum
- *
+ *
* This file is part of UDJ.
- *
+ *
* UDJ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
- *
+ *
* UDJ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with UDJ. If not, see <http://www.gnu.org/licenses/>.
*/
@@ -22,6 +22,8 @@
import org.klnusbaum.udj.network.PlaylistSyncService;
import org.klnusbaum.udj.containers.ActivePlaylistEntry;
+import android.content.IntentFilter;
+import android.content.BroadcastReceiver;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
@@ -47,6 +49,13 @@
public class PlaylistFragment extends RefreshableListFragment implements
LoaderManager.LoaderCallbacks<PlaylistLoader.PlaylistResult>
{
+
+ private BroadcastReceiver playlistUpdateReceiver = new BroadcastReceiver(){
+ public void onReceive(Context context, Intent intent){
+ updatePlaylist();
+ }
+ };
+
private static final String TAG = "PlaylistFragment";
private static final int PLAYLIST_LOADER_ID = 0;
private Account account;
@@ -83,6 +92,19 @@ public void updatePlaylist() {
public void onResume(){
super.onResume();
getLoaderManager().initLoader(PLAYLIST_LOADER_ID, null, this);
+ getActivity().registerReceiver(
+ playlistUpdateReceiver,
+ new IntentFilter(Constants.BROADCAST_VOTE_COMPLETED));
+ }
+
+ public void onPause(){
+ super.onPause();
+ try{
+ getActivity().unregisterReceiver(playlistUpdateReceiver);
+ }
+ catch(IllegalArgumentException e){
+
+ }
}
@@ -203,11 +225,11 @@ public void onLoadFinished(
Loader<PlaylistLoader.PlaylistResult> loader,
PlaylistLoader.PlaylistResult data)
{
- Log.d(TAG, "A loader returned");
if (loader.getId() == PLAYLIST_LOADER_ID) {
refreshDone();
Log.d(TAG, "Playlist loader returned");
if(data.error == PlaylistLoader.PlaylistLoadError.NO_ERROR){
+ Log.d(TAG, "Setting new playlist data");
playlistAdapter.updatePlaylist(data.playlistEntries);
}
if (isResumed()) {
View
1  src/org/klnusbaum/udj/PlaylistLoader.java
@@ -107,7 +107,6 @@ private PlaylistResult attemptLoad(boolean attemptReauth){
return new PlaylistResult(toReturn);
}
catch(JSONException e){
- Log.d(TAG, "Playlist json exception: " + e.getMessage());
return new PlaylistResult(null, PlaylistLoadError.SERVER_ERROR);
}
catch(ParseException e){
View
2  src/org/klnusbaum/udj/containers/User.java
@@ -51,7 +51,7 @@ public User(String ID, String username, String firstName, String lastName){
}
public boolean equals(Object o){
- return ID==((User)o).ID;
+ return ID.equals(((User)o).ID);
}
public static User valueOf(JSONObject jObj)
View
2  src/org/klnusbaum/udj/network/PlaylistSyncService.java
@@ -387,6 +387,8 @@ private void voteOnSong(Account account, String playerId, String libId, int vote
try{
ServerConnection.voteOnSong(playerId, libId, voteWeight, authToken);
+ Intent voteCompleteBroadcast = new Intent(Constants.BROADCAST_VOTE_COMPLETED);
+ this.sendBroadcast(voteCompleteBroadcast);
}
catch(ParseException e){
Log.e(TAG, "Parse exception when retreiving playist");
View
83 src/org/klnusbaum/udj/network/RESTProcessor.java
@@ -79,92 +79,13 @@ else if(playbackState.equals("paused")){
{
checkPlaybackState(context, am, account, activePlaylist.getString("state"));
checkVolume(context, am, account, activePlaylist.getInt("volume"));
- Log.d(TAG, "Current song object: " + activePlaylist.getJSONObject("current_song"));
- ActivePlaylistEntry currentSong = ActivePlaylistEntry.valueOf(activePlaylist.getJSONObject("current_song"));
+ ActivePlaylistEntry currentSong =
+ ActivePlaylistEntry.valueOf(activePlaylist.getJSONObject("current_song"));
currentSong.isCurrentSong = true;
List<ActivePlaylistEntry> playlist = ActivePlaylistEntry.fromJSONArray(
activePlaylist.getJSONArray("active_playlist"));
playlist.add(0, currentSong);
return playlist;
- /*
- final ContentResolver resolver = context.getContentResolver();
- ArrayList<ContentProviderOperation> batchOps = new ArrayList<ContentProviderOperation>();
- JSONArray playlistEntries = activePlaylist.getJSONArray("active_playlist");
- JSONObject currentSong = activePlaylist.getJSONObject("current_song");
-
- clearPlaylistAndVotesTable(resolver);
-
- if(currentSong.length() != 0){
- batchOps.addAll(getPlaylistInsertOps(currentSong, 0, true));
- }
-
- int priority = 1;
- JSONObject currentEntry;
- for(int i=0; i<playlistEntries.length(); i++){
- currentEntry = playlistEntries.getJSONObject(i);
- batchOps.addAll(getPlaylistInsertOps(currentEntry, priority, false));
- if(batchOps.size() >= 50){
- resolver.applyBatch(Constants.AUTHORITY, batchOps);
- batchOps.clear();
- }
- priority++;
- }
- if(batchOps.size() > 0){
- resolver.applyBatch(Constants.AUTHORITY, batchOps);
- batchOps.clear();
- }
- resolver.notifyChange(UDJPlayerProvider.PLAYLIST_URI, null);
- resolver.notifyChange(UDJPlayerProvider.VOTES_URI, null);
- */
- }
-
-/*
- private static void clearPlaylistAndVotesTable(ContentResolver cr) {
- cr.delete(UDJPlayerProvider.PLAYLIST_URI, null, null);
- cr.delete(UDJPlayerProvider.VOTES_URI, null, null);
- }
-
- private static List<ContentProviderOperation> getPlaylistInsertOps(
- JSONObject entry, int priority, boolean isCurrentSong)
- throws JSONException
- {
- JSONObject song = entry.getJSONObject("song");
- JSONObject adder = entry.getJSONObject("adder");
- final ContentProviderOperation.Builder songInsertOp =
- ContentProviderOperation.newInsert(UDJPlayerProvider.PLAYLIST_URI)
- .withValue(UDJPlayerProvider.LIB_ID_COLUMN, song.getString("id"))
- .withValue(UDJPlayerProvider.TIME_ADDED_COLUMN,
- entry.getString("time_added"))
- .withValue(UDJPlayerProvider.PRIORITY_COLUMN, priority)
- .withValue(UDJPlayerProvider.TITLE_COLUMN, song.getString("title"))
- .withValue(UDJPlayerProvider.ARTIST_COLUMN, song.getString("artist"))
- .withValue(UDJPlayerProvider.ALBUM_COLUMN, song.getString("album"))
- .withValue(UDJPlayerProvider.DURATION_COLUMN, song.getInt("duration"))
- .withValue(UDJPlayerProvider.ADDER_ID_COLUMN, adder.getString("id"))
- .withValue(UDJPlayerProvider.ADDER_USERNAME_COLUMN, adder.getString("username"))
- .withValue(UDJPlayerProvider.IS_CURRENTLY_PLAYING_COLUMN, isCurrentSong ? 1 : 0);
- ArrayList<ContentProviderOperation> toReturn = new ArrayList<ContentProviderOperation>();
- toReturn.add(songInsertOp.build());
- JSONArray upVoters = entry.getJSONArray("upvoters");
- for(int i=0; i<upVoters.length(); ++i){
- final ContentProviderOperation.Builder voteInsertOp =
- ContentProviderOperation.newInsert(UDJPlayerProvider.VOTES_URI)
- .withValue(UDJPlayerProvider.LIB_ID_COLUMN, song.getString("id"))
- .withValue(UDJPlayerProvider.VOTE_WEIGHT_COLUMN, 1)
- .withValue(UDJPlayerProvider.VOTER_ID_COLUMN, upVoters.getJSONObject(i).getString("id"));
- toReturn.add(voteInsertOp.build());
- }
- JSONArray downVoters = entry.getJSONArray("downvoters");
- for(int i=0; i<downVoters.length(); ++i){
- final ContentProviderOperation.Builder voteInsertOp =
- ContentProviderOperation.newInsert(UDJPlayerProvider.VOTES_URI)
- .withValue(UDJPlayerProvider.LIB_ID_COLUMN, song.getString("id"))
- .withValue(UDJPlayerProvider.VOTE_WEIGHT_COLUMN, -1)
- .withValue(UDJPlayerProvider.VOTER_ID_COLUMN, downVoters.getJSONObject(i).getString("id"));
- toReturn.add(voteInsertOp.build());
- }
- return toReturn;
}
- */
}
Please sign in to comment.
Something went wrong with that request. Please try again.