From 8e22b32cb79edbe8d637855d9aa8117210e7ac89 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Sun, 27 Mar 2016 12:51:24 -0400 Subject: [PATCH] added 'add podcast' and back now works properly --- .../antennapod/activity/MainActivity.java | 4 -- .../adapter/SubscriptionsAdapter.java | 67 ++++++++++++++++--- .../fragment/SubscriptionFragment.java | 55 +++++++++------ 3 files changed, 91 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java index 1beef2071..5bb5e389c 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -679,10 +679,6 @@ public class MainActivity extends AppCompatActivity implements NavDrawerActivity loadData(); } - public void onEvent(SubscriptionFragment.SubscriptionEvent event) { - loadFeedFragmentById(event.feed.getId(), null); - } - public void onEventMainThread(ProgressEvent event) { Log.d(TAG, "onEvent(" + event + ")"); switch(event.action) { diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/SubscriptionsAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/SubscriptionsAdapter.java index 269d8410e..d10674819 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/SubscriptionsAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/SubscriptionsAdapter.java @@ -2,9 +2,11 @@ package de.danoeh.antennapod.adapter; import android.content.Context; import android.net.Uri; +import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; @@ -14,55 +16,74 @@ import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.target.Target; +import java.lang.ref.WeakReference; + import de.danoeh.antennapod.R; +import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.glide.ApGlideSettings; +import de.danoeh.antennapod.fragment.AddFeedFragment; +import de.danoeh.antennapod.fragment.ItemlistFragment; import jp.shts.android.library.TriangleLabelView; /** * Adapter for subscriptions */ -public class SubscriptionsAdapter extends BaseAdapter { +public class SubscriptionsAdapter extends BaseAdapter implements AdapterView.OnItemClickListener { + + /** placeholder object that indicates item should be added */ + public static final Object ADD_ITEM_OBJ = new Object(); + + /** the position in the view that holds the add item */ + private static final int ADD_POSITION = 0; private NavListAdapter.ItemAccess itemAccess; - private final Context context; + private final WeakReference mainActivityRef; - public SubscriptionsAdapter(Context context, NavListAdapter.ItemAccess itemAccess) { + public SubscriptionsAdapter(MainActivity mainActivity, NavListAdapter.ItemAccess itemAccess) { this.itemAccess = itemAccess; - this.context = context; + this.mainActivityRef = new WeakReference<>(mainActivity); } public void setItemAccess(NavListAdapter.ItemAccess itemAccess) { this.itemAccess = itemAccess; } + private int getAdjustedPosition(int origPosition) { + return origPosition - 1; + } + @Override public int getCount() { - return itemAccess.getCount(); + return 1 + itemAccess.getCount(); } @Override public Object getItem(int position) { - return itemAccess.getItem(position); + if (position == ADD_POSITION) { + return ADD_ITEM_OBJ; + } + return itemAccess.getItem(getAdjustedPosition(position)); } @Override public long getItemId(int position) { - return itemAccess.getItem(position).getId(); + if (position == ADD_POSITION) { + return 0; + } + return itemAccess.getItem(getAdjustedPosition(position)).getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder; - final Feed feed = (Feed) getItem(position); - if (feed == null) return null; if (convertView == null) { holder = new Holder(); LayoutInflater layoutInflater = - (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + (LayoutInflater) mainActivityRef.get().getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.subscription_item, parent, false); holder.feedTitle = (TextView) convertView.findViewById(R.id.txtvTitle); holder.imageView = (ImageView) convertView.findViewById(R.id.imgvCover); @@ -74,9 +95,23 @@ public class SubscriptionsAdapter extends BaseAdapter { holder = (Holder) convertView.getTag(); } + if (position == ADD_POSITION) { + holder.feedTitle.setText(R.string.add_feed_label); + holder.count.setVisibility(View.INVISIBLE); + Glide.with(mainActivityRef.get()) + .load(R.drawable.ic_add_grey_600_48dp) + .dontAnimate() + .into(holder.imageView); + return convertView; + } + + final Feed feed = (Feed) getItem(position); + if (feed == null) return null; + holder.feedTitle.setText(feed.getTitle()); + holder.count.setVisibility(View.VISIBLE); holder.count.setPrimaryText(String.valueOf(itemAccess.getFeedCounter(feed.getId()))); - Glide.with(context) + Glide.with(mainActivityRef.get()) .load(feed.getImageUri()) .placeholder(R.color.light_gray) .error(R.color.light_gray) @@ -100,6 +135,16 @@ public class SubscriptionsAdapter extends BaseAdapter { return convertView; } + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + if (position == ADD_POSITION) { + mainActivityRef.get().loadChildFragment(new AddFeedFragment()); + } else { + Fragment fragment = ItemlistFragment.newInstance(getItemId(position)); + mainActivityRef.get().loadChildFragment(fragment); + } + } + static class Holder { public TextView feedTitle; public ImageView imageView; diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java index d25d7f2c3..b3af107c7 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java @@ -29,7 +29,6 @@ import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.util.FeedItemUtil; -import de.greenrobot.event.EventBus; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; @@ -76,18 +75,13 @@ public class SubscriptionFragment extends Fragment { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - mSubscriptionAdapter = new SubscriptionsAdapter(getActivity(), mItemAccess); + mSubscriptionAdapter = new SubscriptionsAdapter((MainActivity)getActivity(), mItemAccess); mSubscriptionGridLayout.setAdapter(mSubscriptionAdapter); loadSubscriptions(); - mSubscriptionGridLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - EventBus.getDefault().post(new SubscriptionEvent(mSubscriptionList.get(position))); - } - }); + mSubscriptionGridLayout.setOnItemClickListener(mSubscriptionAdapter); if (getActivity() instanceof MainActivity) { ((MainActivity) getActivity()).getSupportActionBar().setTitle(R.string.subscriptions_label); @@ -115,9 +109,17 @@ public class SubscriptionFragment extends Fragment { AdapterView.AdapterContextMenuInfo adapterInfo = (AdapterView.AdapterContextMenuInfo) menuInfo; int position = adapterInfo.position; + Object selectedObject = mSubscriptionAdapter.getItem(position); + if (selectedObject.equals(SubscriptionsAdapter.ADD_ITEM_OBJ)) { + mPosition = position; + return; + } + + Feed feed = (Feed)selectedObject; + MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.nav_feed_context, menu); - Feed feed = (Feed)mSubscriptionAdapter.getItem(position); + menu.setHeaderTitle(feed.getTitle()); mPosition = position; @@ -131,13 +133,34 @@ public class SubscriptionFragment extends Fragment { if(position < 0) { return false; } - Feed feed = mDrawerData.feeds.get(position); + + Object selectedObject = mSubscriptionAdapter.getItem(position); + if (selectedObject.equals(SubscriptionsAdapter.ADD_ITEM_OBJ)) { + // this is the add object, do nothing + return false; + } + + Feed feed = (Feed)selectedObject; switch(item.getItemId()) { case R.id.mark_all_seen_item: - DBWriter.markFeedSeen(feed.getId()); + Observable.fromCallable(() -> DBWriter.markFeedSeen(feed.getId())) + .subscribeOn(Schedulers.newThread()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(result -> { + loadSubscriptions(); + }, error -> { + Log.e(TAG, Log.getStackTraceString(error)); + }); return true; case R.id.mark_all_read_item: - DBWriter.markFeedRead(feed.getId()); + Observable.fromCallable(() -> DBWriter.markFeedRead(feed.getId())) + .subscribeOn(Schedulers.newThread()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(result -> { + loadSubscriptions(); + }, error -> { + Log.e(TAG, Log.getStackTraceString(error)); + }); return true; case R.id.remove_item: final FeedRemover remover = new FeedRemover(getContext(), feed) { @@ -179,12 +202,4 @@ public class SubscriptionFragment extends Fragment { public void onResume() { super.onResume(); } - - public class SubscriptionEvent { - public final Feed feed; - - SubscriptionEvent(Feed f) { - feed = f; - } - } }