Fix enqueueing order, prevent NPEs

This commit is contained in:
Martin Fietz 2015-06-28 21:53:40 +02:00
parent 957497d787
commit c7256d8152
3 changed files with 26 additions and 24 deletions

View File

@ -1,6 +1,5 @@
package de.danoeh.antennapod.dialog; package de.danoeh.antennapod.dialog;
import android.app.ProgressDialog;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
@ -26,7 +25,9 @@ import com.joanzapata.android.iconify.Iconify;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator; import de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator;
@ -49,6 +50,7 @@ public class EpisodesApplyActionFragment extends Fragment {
private Button btnDownload; private Button btnDownload;
private Button btnDelete; private Button btnDelete;
private final Map<Long,FeedItem> idMap;
private final List<FeedItem> episodes; private final List<FeedItem> episodes;
private final List<String> titles = new ArrayList(); private final List<String> titles = new ArrayList();
private final LongList checkedIds = new LongList(); private final LongList checkedIds = new LongList();
@ -57,16 +59,17 @@ public class EpisodesApplyActionFragment extends Fragment {
private int textColor; private int textColor;
private ProgressDialog pd;
public EpisodesApplyActionFragment(List<FeedItem> episodes) { public EpisodesApplyActionFragment(List<FeedItem> episodes) {
this.episodes = episodes; this.episodes = episodes;
this.idMap = new HashMap<>(episodes.size());
for(FeedItem episode : episodes) {
this.idMap.put(episode.getId(), episode);
}
} }
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ActionBar bar = ((ActionBarActivity)getActivity()).getSupportActionBar();
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
@ -278,14 +281,21 @@ public class EpisodesApplyActionFragment extends Fragment {
Collections.sort(episodes, new Comparator<FeedItem>() { Collections.sort(episodes, new Comparator<FeedItem>() {
@Override @Override
public int compare(FeedItem lhs, FeedItem rhs) { public int compare(FeedItem lhs, FeedItem rhs) {
int code = lhs.getMedia().getDuration() - rhs.getMedia().getDuration(); int ordering;
if (reverse) { if (false == lhs.hasMedia()) {
return -1 * code; ordering = 1;
} else if (false == rhs.hasMedia()) {
ordering = -1;
} else { } else {
return code; ordering = lhs.getMedia().getDuration() - rhs.getMedia().getDuration();
} }
if(reverse) {
return -1 * ordering;
} else {
return ordering;
} }
}); }
});
refreshTitles(); refreshTitles();
refreshCheckboxes(); refreshCheckboxes();
} }
@ -321,7 +331,7 @@ public class EpisodesApplyActionFragment extends Fragment {
private void checkDownloaded(boolean isDownloaded) { private void checkDownloaded(boolean isDownloaded) {
for (FeedItem episode : episodes) { for (FeedItem episode : episodes) {
if(episode.getMedia().isDownloaded() == isDownloaded) { if(episode.hasMedia() && episode.getMedia().isDownloaded() == isDownloaded) {
if(!checkedIds.contains(episode.getId())) { if(!checkedIds.contains(episode.getId())) {
checkedIds.add(episode.getId()); checkedIds.add(episode.getId());
} }
@ -391,8 +401,10 @@ public class EpisodesApplyActionFragment extends Fragment {
private void deleteChecked() { private void deleteChecked() {
for(long id : checkedIds.toArray()) { for(long id : checkedIds.toArray()) {
FeedItem episode = findById(id); FeedItem episode = idMap.get(id);
DBWriter.deleteFeedMediaOfItem(getActivity(), episode.getMedia().getId()); if(episode.hasMedia()) {
DBWriter.deleteFeedMediaOfItem(getActivity(), episode.getMedia().getId());
}
} }
close(); close();
} }
@ -401,14 +413,4 @@ public class EpisodesApplyActionFragment extends Fragment {
getActivity().getSupportFragmentManager().popBackStack(); getActivity().getSupportFragmentManager().popBackStack();
} }
private FeedItem findById(long id) {
for(int i=0; i < episodes.size(); i++) {
FeedItem episode = episodes.get(i);
if(episode.getId() == id) {
return episode;
}
}
return null;
}
} }

View File

@ -415,7 +415,7 @@ public class DBWriter {
boolean addToFront = UserPreferences.enqueueAtFront(); boolean addToFront = UserPreferences.enqueueAtFront();
if(addToFront){ if(addToFront){
queue.add(0, item); queue.add(0+i, item);
} else { } else {
queue.add(item); queue.add(item);
} }

View File

@ -464,7 +464,7 @@
<string name="selected_unplayed_label">Selected unplayed Episodes</string> <string name="selected_unplayed_label">Selected unplayed Episodes</string>
<string name="downloaded_label">Downloaded</string> <string name="downloaded_label">Downloaded</string>
<string name="selected_downloaded_label">Selected downloaded Episodes</string> <string name="selected_downloaded_label">Selected downloaded Episodes</string>
<string name="not_downloaded_label">Unplayed</string> <string name="not_downloaded_label">Not downloaded</string>
<string name="selected_not_downloaded_label">Selected not downloaded Episodes</string> <string name="selected_not_downloaded_label">Selected not downloaded Episodes</string>
<string name="sort_title"><b>Sort by ...</b></string> <string name="sort_title"><b>Sort by ...</b></string>
<string name="sort_title_a_z">Title (A \u2192 Z)</string> <string name="sort_title_a_z">Title (A \u2192 Z)</string>