Refactor events
This commit is contained in:
parent
6aefd4b4de
commit
383449e007
|
@ -97,7 +97,7 @@ public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
|
|||
};
|
||||
EventDistributor.getInstance().register(queueListener);
|
||||
List<FeedItem> queue = writeTestQueue("a");
|
||||
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.ADDED_ITEMS, queue));
|
||||
EventBus.getDefault().post(QueueEvent.setQueue(queue));
|
||||
countDownLatch.await(5000, TimeUnit.MILLISECONDS);
|
||||
|
||||
assertNotNull(queue);
|
||||
|
|
|
@ -220,7 +220,7 @@ public class UITestUtils {
|
|||
adapter.setQueue(queue);
|
||||
adapter.close();
|
||||
EventDistributor.getInstance().sendFeedUpdateBroadcast();
|
||||
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.ADDED_ITEMS, queue));
|
||||
EventBus.getDefault().post(QueueEvent.setQueue(queue));
|
||||
}
|
||||
|
||||
public PlaybackController getPlaybackController(MainActivity mainActivity) {
|
||||
|
|
|
@ -7,6 +7,8 @@ import android.widget.ImageButton;
|
|||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import de.danoeh.antennapod.R;
|
||||
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||
import de.danoeh.antennapod.core.feed.FeedMedia;
|
||||
|
@ -27,7 +29,7 @@ public class ActionButtonUtils {
|
|||
public ActionButtonUtils(Context context) {
|
||||
Validate.notNull(context);
|
||||
|
||||
this.context = context;
|
||||
this.context = context.getApplicationContext();
|
||||
drawables = context.obtainStyledAttributes(new int[] {
|
||||
R.attr.av_play,
|
||||
R.attr.navigation_cancel,
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package de.danoeh.antennapod.core.event;
|
||||
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||
|
||||
public class FeedItemEvent {
|
||||
|
||||
public enum Action {
|
||||
UPDATE, DELETE_MEDIA
|
||||
}
|
||||
|
||||
@NonNull public final Action action;
|
||||
@NonNull public final List<FeedItem> items;
|
||||
|
||||
private FeedItemEvent(Action action, List<FeedItem> items) {
|
||||
this.action = action;
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public static FeedItemEvent deletedMedia(List<FeedItem> items) {
|
||||
return new FeedItemEvent(Action.DELETE_MEDIA, items);
|
||||
}
|
||||
|
||||
public static FeedItemEvent updated(List<FeedItem> items) {
|
||||
return new FeedItemEvent(Action.UPDATE, items);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
|
||||
.append("action", action)
|
||||
.append("items", items)
|
||||
.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -20,5 +20,4 @@ public class FeedMediaEvent {
|
|||
return new FeedMediaEvent(Action.UPDATE, media);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package de.danoeh.antennapod.core.event;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
|
||||
|
@ -10,7 +12,7 @@ import de.danoeh.antennapod.core.feed.FeedItem;
|
|||
public class QueueEvent {
|
||||
|
||||
public enum Action {
|
||||
ADDED, ADDED_ITEMS, REMOVED, IRREVERSIBLE_REMOVED, CLEARED, DELETED_MEDIA, SORTED, MOVED
|
||||
ADDED, ADDED_ITEMS, SET_QUEUE, REMOVED, IRREVERSIBLE_REMOVED, CLEARED, DELETED_MEDIA, SORTED, MOVED
|
||||
}
|
||||
|
||||
public final Action action;
|
||||
|
@ -18,29 +20,45 @@ public class QueueEvent {
|
|||
public final int position;
|
||||
public final List<FeedItem> items;
|
||||
|
||||
public QueueEvent(Action action) {
|
||||
this(action, null, null, -1);
|
||||
}
|
||||
|
||||
public QueueEvent(Action action, FeedItem item) {
|
||||
this(action, item, null, -1);
|
||||
}
|
||||
|
||||
public QueueEvent(Action action, FeedItem item, int position) {
|
||||
this(action, item, null, position);
|
||||
}
|
||||
|
||||
public QueueEvent(Action action, List<FeedItem> items) {
|
||||
this(action, null, items, -1);
|
||||
}
|
||||
|
||||
private QueueEvent(Action action, FeedItem item, List<FeedItem> items, int position) {
|
||||
private QueueEvent(Action action,
|
||||
@Nullable FeedItem item,
|
||||
@Nullable List<FeedItem> items,
|
||||
int position) {
|
||||
this.action = action;
|
||||
this.item = item;
|
||||
this.items = items;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public static QueueEvent added(FeedItem item, int position) {
|
||||
return new QueueEvent(Action.ADDED, item, null, position);
|
||||
}
|
||||
|
||||
public static QueueEvent setQueue(List<FeedItem> queue) {
|
||||
return new QueueEvent(Action.SET_QUEUE, null, queue, -1);
|
||||
}
|
||||
|
||||
public static QueueEvent removed(FeedItem item) {
|
||||
return new QueueEvent(Action.REMOVED, item, null, -1);
|
||||
}
|
||||
|
||||
public static QueueEvent irreversibleRemoved(FeedItem item) {
|
||||
return new QueueEvent(Action.IRREVERSIBLE_REMOVED, item, null, -1);
|
||||
}
|
||||
|
||||
public static QueueEvent cleared() {
|
||||
return new QueueEvent(Action.CLEARED, null, null, -1);
|
||||
}
|
||||
|
||||
public static QueueEvent sorted(List<FeedItem> sortedQueue) {
|
||||
return new QueueEvent(Action.SORTED, null, sortedQueue, -1);
|
||||
}
|
||||
|
||||
public static QueueEvent moved(FeedItem item, int newPosition) {
|
||||
return new QueueEvent(Action.MOVED, item, null, newPosition);
|
||||
}
|
||||
|
||||
public boolean contains(long id) {
|
||||
if(item != null) {
|
||||
return item.getId() == id;
|
||||
|
|
|
@ -14,6 +14,7 @@ import java.io.File;
|
|||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
|
@ -27,6 +28,8 @@ import de.danoeh.antennapod.core.BuildConfig;
|
|||
import de.danoeh.antennapod.core.ClientConfig;
|
||||
import de.danoeh.antennapod.core.asynctask.FlattrClickWorker;
|
||||
import de.danoeh.antennapod.core.event.FavoritesEvent;
|
||||
import de.danoeh.antennapod.core.event.FeedItemEvent;
|
||||
import de.danoeh.antennapod.core.event.QueueEvent;
|
||||
import de.danoeh.antennapod.core.feed.EventDistributor;
|
||||
import de.danoeh.antennapod.core.feed.Feed;
|
||||
import de.danoeh.antennapod.core.feed.FeedEvent;
|
||||
|
@ -34,7 +37,6 @@ import de.danoeh.antennapod.core.feed.FeedImage;
|
|||
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||
import de.danoeh.antennapod.core.feed.FeedMedia;
|
||||
import de.danoeh.antennapod.core.feed.FeedPreferences;
|
||||
import de.danoeh.antennapod.core.event.QueueEvent;
|
||||
import de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction;
|
||||
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
|
||||
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
|
||||
|
@ -131,7 +133,7 @@ public class DBWriter {
|
|||
}
|
||||
}
|
||||
Log.d(TAG, "Deleting File. Result: " + result);
|
||||
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.DELETED_MEDIA, media.getItem()));
|
||||
EventBus.getDefault().post(FeedItemEvent.deletedMedia(Arrays.asList(media.getItem())));
|
||||
EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast();
|
||||
}
|
||||
});
|
||||
|
@ -210,8 +212,9 @@ public class DBWriter {
|
|||
adapter.open();
|
||||
if (removed.size() > 0) {
|
||||
adapter.setQueue(queue);
|
||||
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.IRREVERSIBLE_REMOVED,
|
||||
removed));
|
||||
for(FeedItem item : removed) {
|
||||
EventBus.getDefault().post(QueueEvent.irreversibleRemoved(item));
|
||||
}
|
||||
}
|
||||
adapter.removeFeed(feed);
|
||||
adapter.close();
|
||||
|
@ -319,7 +322,7 @@ public class DBWriter {
|
|||
queue.add(index, item);
|
||||
adapter.setQueue(queue);
|
||||
item.addTag(FeedItem.TAG_QUEUE);
|
||||
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.ADDED, item, index));
|
||||
EventBus.getDefault().post(QueueEvent.added(item, index));
|
||||
if (item.isNew()) {
|
||||
DBWriter.markItemPlayed(FeedItem.UNPLAYED, item.getId());
|
||||
}
|
||||
|
@ -365,17 +368,21 @@ public class DBWriter {
|
|||
if (queue != null) {
|
||||
boolean queueModified = false;
|
||||
LongList markAsUnplayedIds = new LongList();
|
||||
List<QueueEvent> events = new ArrayList<QueueEvent>();
|
||||
for (int i = 0; i < itemIds.length; i++) {
|
||||
if (!itemListContains(queue, itemIds[i])) {
|
||||
final FeedItem item = DBReader.getFeedItem(itemIds[i]);
|
||||
|
||||
|
||||
if (item != null) {
|
||||
// add item to either front ot back of queue
|
||||
boolean addToFront = UserPreferences.enqueueAtFront();
|
||||
if (addToFront) {
|
||||
queue.add(0 + i, item);
|
||||
events.add(QueueEvent.added(item, 0 + i));
|
||||
} else {
|
||||
queue.add(item);
|
||||
events.add(QueueEvent.added(item, queue.size()-1));
|
||||
}
|
||||
queueModified = true;
|
||||
if (item.isNew()) {
|
||||
|
@ -386,7 +393,9 @@ public class DBWriter {
|
|||
}
|
||||
if (queueModified) {
|
||||
adapter.setQueue(queue);
|
||||
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.ADDED_ITEMS, queue));
|
||||
for(QueueEvent event : events) {
|
||||
EventBus.getDefault().post(event);
|
||||
}
|
||||
if (markAsUnplayedIds.size() > 0) {
|
||||
DBWriter.markItemPlayed(FeedItem.UNPLAYED, markAsUnplayedIds.toArray());
|
||||
}
|
||||
|
@ -411,7 +420,7 @@ public class DBWriter {
|
|||
adapter.clearQueue();
|
||||
adapter.close();
|
||||
|
||||
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.CLEARED));
|
||||
EventBus.getDefault().post(QueueEvent.cleared());
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -435,7 +444,7 @@ public class DBWriter {
|
|||
queue.remove(position);
|
||||
adapter.setQueue(queue);
|
||||
item.removeTag(FeedItem.TAG_QUEUE);
|
||||
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.REMOVED, item, position));
|
||||
EventBus.getDefault().post(QueueEvent.removed(item));
|
||||
} else {
|
||||
Log.w(TAG, "Queue was not modified by call to removeQueueItem");
|
||||
}
|
||||
|
@ -512,9 +521,9 @@ public class DBWriter {
|
|||
return dbExec.submit(() -> {
|
||||
LongList queueIdList = DBReader.getQueueIDList();
|
||||
int index = queueIdList.indexOf(itemId);
|
||||
if(index >= 0) {
|
||||
if (index >= 0) {
|
||||
moveQueueItemHelper(index, queueIdList.size() - 1,
|
||||
broadcastUpdate);
|
||||
broadcastUpdate);
|
||||
} else {
|
||||
Log.e(TAG, "moveQueueItemToBottom: item not found");
|
||||
}
|
||||
|
@ -561,7 +570,7 @@ public class DBWriter {
|
|||
|
||||
adapter.setQueue(queue);
|
||||
if (broadcastUpdate) {
|
||||
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.MOVED, item, to));
|
||||
EventBus.getDefault().post(QueueEvent.moved(item, to));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -955,7 +964,7 @@ public class DBWriter {
|
|||
Collections.sort(queue, comparator);
|
||||
adapter.setQueue(queue);
|
||||
if (broadcastUpdate) {
|
||||
EventBus.getDefault().post(new QueueEvent(QueueEvent.Action.SORTED));
|
||||
EventBus.getDefault().post(QueueEvent.sorted(queue));
|
||||
}
|
||||
} else {
|
||||
Log.e(TAG, "sortQueue: Could not load queue");
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
package de.danoeh.antennapod.core.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||
|
||||
public class FeedItemUtil {
|
||||
|
||||
public static int indexOfItemWithDownloadUrl(List<FeedItem> items, String downloadUrl) {
|
||||
if(items == null) {
|
||||
return -1;
|
||||
}
|
||||
for(int i=0; i < items.size(); i++) {
|
||||
FeedItem item = items.get(i);
|
||||
if(item.hasMedia() && item.getMedia().getDownload_url().equals(downloadUrl)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int indexOfItemWithId(List<FeedItem> items, long id) {
|
||||
for(int i=0; i < items.size(); i++) {
|
||||
FeedItem item = items.get(i);
|
||||
if(item != null && item.getId() == id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static long[] getIds(FeedItem... items) {
|
||||
if(items == null || items.length == 0) {
|
||||
return new long[0];
|
||||
}
|
||||
long[] result = new long[items.length];
|
||||
for(int i=0; i < items.length; i++) {
|
||||
result[i] = items[i].getId();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static long[] getIds(List<FeedItem> items) {
|
||||
if(items == null || items.size() == 0) {
|
||||
return new long[0];
|
||||
}
|
||||
long[] result = new long[items.size()];
|
||||
for(int i=0; i < items.size(); i++) {
|
||||
result[i] = items.get(i).getId();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean containsAnyId(List<FeedItem> items, long[] ids) {
|
||||
if(items == null || items.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
for(FeedItem item : items) {
|
||||
for(long id : ids) {
|
||||
if(item.getId() == id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,127 +0,0 @@
|
|||
package de.danoeh.antennapod.core.util.gui;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.nineoldandroids.animation.Animator;
|
||||
import com.nineoldandroids.animation.AnimatorListenerAdapter;
|
||||
import com.nineoldandroids.view.ViewHelper;
|
||||
import com.nineoldandroids.view.ViewPropertyAnimator;
|
||||
|
||||
import de.danoeh.antennapod.core.R;
|
||||
|
||||
import static com.nineoldandroids.view.ViewPropertyAnimator.animate;
|
||||
|
||||
public class UndoBarController<T> {
|
||||
private View mBarView;
|
||||
private TextView mMessageView;
|
||||
private ViewPropertyAnimator mBarAnimator;
|
||||
private Handler mHideHandler = new Handler();
|
||||
|
||||
private UndoListener<T> mUndoListener;
|
||||
|
||||
// State objects
|
||||
private T mUndoToken;
|
||||
private CharSequence mUndoMessage;
|
||||
|
||||
public interface UndoListener<T> {
|
||||
/**
|
||||
* This callback function is called when the undo button is pressed
|
||||
*
|
||||
* @param token
|
||||
*/
|
||||
void onUndo(T token);
|
||||
|
||||
/**
|
||||
*
|
||||
* This callback function is called when the bar fades out without button press
|
||||
*
|
||||
* @param token
|
||||
*/
|
||||
void onHide(T token);
|
||||
}
|
||||
|
||||
public UndoBarController(View undoBarView, UndoListener<T> undoListener) {
|
||||
mBarView = undoBarView;
|
||||
mBarAnimator = animate(mBarView);
|
||||
mUndoListener = undoListener;
|
||||
|
||||
mMessageView = (TextView) mBarView.findViewById(R.id.undobar_message);
|
||||
mBarView.findViewById(R.id.undobar_button)
|
||||
.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
hideUndoBar(false);
|
||||
mUndoListener.onUndo(mUndoToken);
|
||||
}
|
||||
});
|
||||
|
||||
hideUndoBar(true);
|
||||
}
|
||||
|
||||
public void showUndoBar(boolean immediate, CharSequence message, T undoToken) {
|
||||
mUndoToken = undoToken;
|
||||
mUndoMessage = message;
|
||||
mMessageView.setText(mUndoMessage);
|
||||
|
||||
mHideHandler.removeCallbacks(mHideRunnable);
|
||||
mHideHandler.postDelayed(mHideRunnable,
|
||||
mBarView.getResources().getInteger(R.integer.undobar_hide_delay));
|
||||
|
||||
mBarView.setVisibility(View.VISIBLE);
|
||||
if (immediate) {
|
||||
ViewHelper.setAlpha(mBarView, 1);
|
||||
} else {
|
||||
mBarAnimator.cancel();
|
||||
mBarAnimator
|
||||
.alpha(1)
|
||||
.setDuration(
|
||||
mBarView.getResources()
|
||||
.getInteger(android.R.integer.config_shortAnimTime))
|
||||
.setListener(null);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isShowing() {
|
||||
return mBarView.getVisibility() == View.VISIBLE;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
hideUndoBar(true);
|
||||
if(mUndoListener != null) {
|
||||
mUndoListener.onHide(mUndoToken);
|
||||
}
|
||||
}
|
||||
|
||||
public void hideUndoBar(boolean immediate) {
|
||||
mHideHandler.removeCallbacks(mHideRunnable);
|
||||
if (immediate) {
|
||||
mBarView.setVisibility(View.GONE);
|
||||
ViewHelper.setAlpha(mBarView, 0);
|
||||
mUndoMessage = null;
|
||||
} else {
|
||||
mBarAnimator.cancel();
|
||||
mBarAnimator
|
||||
.alpha(0)
|
||||
.setDuration(mBarView.getResources()
|
||||
.getInteger(android.R.integer.config_shortAnimTime))
|
||||
.setListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
mBarView.setVisibility(View.GONE);
|
||||
mUndoMessage = null;
|
||||
mUndoToken = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private Runnable mHideRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
hideUndoBar(false);
|
||||
mUndoListener.onHide(mUndoToken);
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue