From b3c41ce9c7b265b3d8b20a24b1a14fa12dc3d2cf Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Sun, 27 Mar 2016 18:26:47 +0200 Subject: [PATCH] Episode view: Go to previous/next list item when swiping right/left --- .../adapter/AllEpisodesRecycleAdapter.java | 5 +- .../adapter/QueueRecyclerAdapter.java | 6 +- .../fragment/AllEpisodesFragment.java | 12 +++ .../fragment/CompletedDownloadsFragment.java | 8 +- .../antennapod/fragment/ItemFragment.java | 85 ++++++++++++++++--- .../antennapod/fragment/ItemlistFragment.java | 11 ++- .../fragment/PlaybackHistoryFragment.java | 8 +- .../antennapod/view/OnSwipeGesture.java | 9 ++ .../antennapod/view/SwipeGestureDetector.java | 44 ++++++++++ 9 files changed, 159 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/view/OnSwipeGesture.java create mode 100644 app/src/main/java/de/danoeh/antennapod/view/SwipeGestureDetector.java diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/AllEpisodesRecycleAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/AllEpisodesRecycleAdapter.java index d141e0239..c471699a6 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/AllEpisodesRecycleAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/AllEpisodesRecycleAdapter.java @@ -295,7 +295,8 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter 0) { + header.setOnTouchListener((v, event) -> headerGestureDetector.onTouchEvent(event)); + } + txtvPodcast = (TextView) layout.findViewById(R.id.txtvPodcast); txtvTitle = (TextView) layout.findViewById(R.id.txtvTitle); txtvDuration = (TextView) layout.findViewById(R.id.txtvDuration); @@ -155,7 +191,10 @@ public class ItemFragment extends Fragment { webvDescription.getSettings().setLayoutAlgorithm( WebSettings.LayoutAlgorithm.NARROW_COLUMNS); webvDescription.getSettings().setLoadWithOverviewMode(true); - webvDescription.setOnLongClickListener(webViewLongClickListener); + if(feedItems.length > 0) { + webvDescription.setOnLongClickListener(webViewLongClickListener); + } + webvDescription.setOnTouchListener((v, event) -> webviewGestureDetector.onTouchEvent(event)); webvDescription.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { @@ -245,6 +284,25 @@ public class ItemFragment extends Fragment { } } + @Override + public boolean onSwipeLeftToRight() { + Log.d(TAG, "onSwipeLeftToRight()"); + feedItemPos = feedItemPos - 1; + if(feedItemPos < 0) { + feedItemPos = feedItems.length - 1; + } + load(); + return true; + } + + @Override + public boolean onSwipeRightToLeft() { + Log.d(TAG, "onSwipeRightToLeft()"); + feedItemPos = (feedItemPos + 1) % feedItems.length; + load(); + return true; + } + @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { if(!isAdded() || item == null) { @@ -284,7 +342,6 @@ public class ItemFragment extends Fragment { private void onFragmentLoaded() { - progbarLoading.setVisibility(View.INVISIBLE); if (webviewData != null) { webvDescription.loadDataWithBaseURL(null, webviewData, "text/html", "utf-8", "about:blank"); @@ -457,13 +514,13 @@ public class ItemFragment extends Fragment { } public void onEventMainThread(QueueEvent event) { - if(event.contains(itemID)) { + if(event.contains(feedItems[feedItemPos])) { load(); } } public void onEventMainThread(FavoritesEvent event) { - if(event.item.getId() == itemID) { + if(event.item.getId() == feedItems[feedItemPos]) { load(); } } @@ -471,7 +528,7 @@ public class ItemFragment extends Fragment { public void onEventMainThread(FeedItemEvent event) { Log.d(TAG, "onEventMainThread() called with: " + "event = [" + event + "]"); for(FeedItem item : event.items) { - if(itemID == item.getId()) { + if(feedItems[feedItemPos] == item.getId()) { load(); return; } @@ -507,6 +564,7 @@ public class ItemFragment extends Fragment { if(subscription != null) { subscription.unsubscribe(); } + progbarLoading.setVisibility(View.VISIBLE); subscription = Observable.fromCallable(this::loadInBackground) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) @@ -514,6 +572,7 @@ public class ItemFragment extends Fragment { item = (FeedItem) result[0]; queue = (LongList) result[1]; favorites = (LongList) result[2]; + progbarLoading.setVisibility(View.GONE); if (!itemsLoaded) { itemsLoaded = true; onFragmentLoaded(); @@ -526,7 +585,7 @@ public class ItemFragment extends Fragment { } private Object[] loadInBackground() { - FeedItem feedItem = DBReader.getFeedItem(itemID); + FeedItem feedItem = DBReader.getFeedItem(feedItems[feedItemPos]); if (feedItem != null) { Timeline t = new Timeline(getActivity(), feedItem); webviewData = t.processShownotes(false); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/ItemlistFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/ItemlistFragment.java index 4faa7be2d..d304f3872 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/ItemlistFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/ItemlistFragment.java @@ -374,12 +374,11 @@ public class ItemlistFragment extends ListFragment { if(adapter == null) { return; } - FeedItem selection = adapter.getItem(position - l.getHeaderViewsCount()); - if (selection != null) { - MainActivity activity = (MainActivity) getActivity(); - activity.loadChildFragment(ItemFragment.newInstance(selection.getId())); - activity.getSupportActionBar().setTitle(feed.getTitle()); - } + position -= l.getHeaderViewsCount(); + MainActivity activity = (MainActivity) getActivity(); + long[] ids = FeedItemUtil.getIds(feed.getItems()); + activity.loadChildFragment(ItemFragment.newInstance(ids, position)); + activity.getSupportActionBar().setTitle(feed.getTitle()); } public void onEvent(QueueEvent event) { diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java index f6cb71af5..1d882aee1 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/PlaybackHistoryFragment.java @@ -28,6 +28,7 @@ import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.service.download.Downloader; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; +import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.LongList; import de.greenrobot.event.EventBus; import rx.Observable; @@ -140,10 +141,9 @@ public class PlaybackHistoryFragment extends ListFragment { @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); - FeedItem item = adapter.getItem(position - l.getHeaderViewsCount()); - if (item != null) { - ((MainActivity) getActivity()).loadChildFragment(ItemFragment.newInstance(item.getId())); - } + position -= l.getHeaderViewsCount(); + long[] ids = FeedItemUtil.getIds(playbackHistory); + ((MainActivity) getActivity()).loadChildFragment(ItemFragment.newInstance(ids, position)); } @Override diff --git a/app/src/main/java/de/danoeh/antennapod/view/OnSwipeGesture.java b/app/src/main/java/de/danoeh/antennapod/view/OnSwipeGesture.java new file mode 100644 index 000000000..eb72a7135 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/view/OnSwipeGesture.java @@ -0,0 +1,9 @@ +package de.danoeh.antennapod.view; + +public interface OnSwipeGesture { + + boolean onSwipeLeftToRight(); + + boolean onSwipeRightToLeft(); + +} diff --git a/app/src/main/java/de/danoeh/antennapod/view/SwipeGestureDetector.java b/app/src/main/java/de/danoeh/antennapod/view/SwipeGestureDetector.java new file mode 100644 index 000000000..f4ee092df --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/view/SwipeGestureDetector.java @@ -0,0 +1,44 @@ +package de.danoeh.antennapod.view; + +import android.util.Log; +import android.view.GestureDetector; +import android.view.MotionEvent; + +public class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener { + + private static final String TAG = "SwipeGestureDetector"; + + private static final int SWIPE_MIN_DISTANCE = 120; + private static final int SWIPE_MAX_OFF_PATH = 250; + private static final int SWIPE_THRESHOLD_VELOCITY = 200; + + private final OnSwipeGesture callback; + + public SwipeGestureDetector(OnSwipeGesture callback) { + this.callback = callback; + } + + @Override + public boolean onDown(MotionEvent e) { + return true; + } + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + try { + if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) + return false; + if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE + && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { + return callback.onSwipeRightToLeft(); + } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE + && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { + return callback.onSwipeLeftToRight(); + } + } catch (Exception e) { + Log.d(TAG, Log.getStackTraceString(e)); + } + return false; + } + +}