diff options
author | Michael Kaiser <patheticpat@googlemail.com> | 2013-04-15 17:42:38 +0200 |
---|---|---|
committer | Michael Kaiser <patheticpat@googlemail.com> | 2013-04-15 18:53:52 +0200 |
commit | a66d225b9feff54bdd5bb597102839fe2cdbc75f (patch) | |
tree | d07a9a8fb00ad69bd1e61b46b13fc6a8cfa6fce1 /src/de/danoeh/antennapod | |
parent | 4707139def5500f1039de669e53822d47855008c (diff) | |
download | AntennaPod-a66d225b9feff54bdd5bb597102839fe2cdbc75f.zip |
Make removal of queued items undoable
Diffstat (limited to 'src/de/danoeh/antennapod')
-rw-r--r-- | src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java | 74 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/util/UndoBarController.java | 135 |
2 files changed, 206 insertions, 3 deletions
diff --git a/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java b/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java index 89001f7f5..dfb569a22 100644 --- a/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java +++ b/src/de/danoeh/antennapod/activity/OrganizeQueueActivity.java @@ -3,6 +3,8 @@ package de.danoeh.antennapod.activity; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; +import android.os.Parcel; +import android.os.Parcelable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -21,13 +23,15 @@ import de.danoeh.antennapod.feed.EventDistributor; import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.preferences.UserPreferences; +import de.danoeh.antennapod.util.UndoBarController; -public class OrganizeQueueActivity extends SherlockListActivity { +public class OrganizeQueueActivity extends SherlockListActivity implements UndoBarController.UndoListener { private static final String TAG = "OrganizeQueueActivity"; private static final int MENU_ID_ACCEPT = 2; private OrganizeAdapter adapter; + private UndoBarController undoBarController; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,6 +45,8 @@ public class OrganizeQueueActivity extends SherlockListActivity { adapter = new OrganizeAdapter(this); setListAdapter(adapter); + + undoBarController = new UndoBarController(findViewById(R.id.undobar), this); } @Override @@ -82,8 +88,10 @@ public class OrganizeQueueActivity extends SherlockListActivity { @Override public void remove(int which) { FeedManager manager = FeedManager.getInstance(); - manager.removeQueueItem(OrganizeQueueActivity.this, - (FeedItem) getListAdapter().getItem(which)); + FeedItem item = (FeedItem) getListAdapter().getItem(which); + manager.removeQueueItem(OrganizeQueueActivity.this, item); + undoBarController.showUndoBar(false, getString(R.string.removed_from_queue), + new UndoItem(item, which)); } }; @@ -110,6 +118,17 @@ public class OrganizeQueueActivity extends SherlockListActivity { } } + @Override + public void onUndo(Parcelable token) { + // Perform the undo + UndoItem undoItem = (UndoItem) token; + FeedItem feedItem = undoItem.getFeedItem(); + int position = undoItem.getPosition(); + + FeedManager manager = FeedManager.getInstance(); + manager.addQueueItemAt(OrganizeQueueActivity.this, feedItem, position, false); + } + /** * WARNING: If the PlaybackService is playing an episode from the queue, * this list adapter will ignore the first item in the list to make sure @@ -185,4 +204,53 @@ public class OrganizeQueueActivity extends SherlockListActivity { } + private static class UndoItem implements Parcelable { + private long itemId; + private long feedId; + private int position; + + public UndoItem(FeedItem item, int position) { + FeedManager manager = FeedManager.getInstance(); + this.itemId = item.getId(); + this.feedId = item.getFeed().getId(); + this.position = position; + } + + private UndoItem(Parcel in) { + itemId = in.readLong(); + feedId = in.readLong(); + position = in.readInt(); + } + + public static final Parcelable.Creator<UndoItem> CREATOR + = new Parcelable.Creator<UndoItem>() { + public UndoItem createFromParcel(Parcel in) { + return new UndoItem(in); + } + + public UndoItem[] newArray(int size) { + return new UndoItem[size]; + } + }; + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel out, int flags) { + out.writeLong(itemId); + out.writeLong(feedId); + out.writeInt(position); + } + + public FeedItem getFeedItem() { + FeedManager manager = FeedManager.getInstance(); + return manager.getFeedItem(itemId, feedId); + } + + public int getPosition() { + return position; + } + } + } diff --git a/src/de/danoeh/antennapod/util/UndoBarController.java b/src/de/danoeh/antennapod/util/UndoBarController.java new file mode 100644 index 000000000..e726717a1 --- /dev/null +++ b/src/de/danoeh/antennapod/util/UndoBarController.java @@ -0,0 +1,135 @@ +/* + * Copyright 2012 Roman Nurik + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.danoeh.antennapod.util; + +import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; +import android.os.Bundle; +import android.os.Handler; +import android.os.Parcelable; +import android.text.TextUtils; +import android.view.View; +import android.view.ViewPropertyAnimator; +import android.widget.TextView; + +import de.danoeh.antennapod.R; + +public class UndoBarController { + private View mBarView; + private TextView mMessageView; + private ViewPropertyAnimator mBarAnimator; + private Handler mHideHandler = new Handler(); + + private UndoListener mUndoListener; + + // State objects + private Parcelable mUndoToken; + private CharSequence mUndoMessage; + + public interface UndoListener { + void onUndo(Parcelable token); + } + + public UndoBarController(View undoBarView, UndoListener undoListener) { + mBarView = undoBarView; + mBarAnimator = mBarView.animate(); + 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, Parcelable 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) { + mBarView.setAlpha(1); + } else { + mBarAnimator.cancel(); + mBarAnimator + .alpha(1) + .setDuration( + mBarView.getResources() + .getInteger(android.R.integer.config_shortAnimTime)) + .setListener(null); + } + } + + public void hideUndoBar(boolean immediate) { + mHideHandler.removeCallbacks(mHideRunnable); + if (immediate) { + mBarView.setVisibility(View.GONE); + mBarView.setAlpha(0); + mUndoMessage = null; + mUndoToken = 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; + } + }); + } + } + + public void onSaveInstanceState(Bundle outState) { + outState.putCharSequence("undo_message", mUndoMessage); + outState.putParcelable("undo_token", mUndoToken); + } + + public void onRestoreInstanceState(Bundle savedInstanceState) { + if (savedInstanceState != null) { + mUndoMessage = savedInstanceState.getCharSequence("undo_message"); + mUndoToken = savedInstanceState.getParcelable("undo_token"); + + if (mUndoToken != null || !TextUtils.isEmpty(mUndoMessage)) { + showUndoBar(true, mUndoMessage, mUndoToken); + } + } + } + + private Runnable mHideRunnable = new Runnable() { + @Override + public void run() { + hideUndoBar(false); + } + }; +} |