* SubscriptionsAdapter now using its own ItemAccess instead of MainActivity

* SubscriptionFragment now properly reloading data when it should
This commit is contained in:
Tom Hennen 2016-03-31 15:14:06 -04:00
parent 10317913d2
commit d340fdd6e9
3 changed files with 73 additions and 37 deletions

View File

@ -302,7 +302,6 @@ public class MainActivity extends AppCompatActivity implements NavDrawerActivity
break;
case SubscriptionFragment.TAG:
SubscriptionFragment subscriptionFragment = new SubscriptionFragment();
subscriptionFragment.setItemAccess(itemAccess);
fragment = subscriptionFragment;
break;
default:

View File

@ -1,7 +1,9 @@
package de.danoeh.antennapod.adapter;
import android.content.Context;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -32,17 +34,13 @@ public class SubscriptionsAdapter extends BaseAdapter implements AdapterView.OnI
/** the position in the view that holds the add item */
private static final int ADD_POSITION = 0;
private NavListAdapter.ItemAccess itemAccess;
private static final String TAG = "SubscriptionsAdapter";
private final WeakReference<MainActivity> mainActivityRef;
private final ItemAccess itemAccess;
public SubscriptionsAdapter(MainActivity mainActivity, NavListAdapter.ItemAccess itemAccess) {
this.itemAccess = itemAccess;
public SubscriptionsAdapter(MainActivity mainActivity, ItemAccess itemAccess) {
this.mainActivityRef = new WeakReference<>(mainActivity);
}
public void setItemAccess(NavListAdapter.ItemAccess itemAccess) {
this.itemAccess = itemAccess;
}
@ -104,17 +102,22 @@ public class SubscriptionsAdapter extends BaseAdapter implements AdapterView.OnI
final Feed feed = (Feed) getItem(position);
if (feed == null) return null;
holder.feedTitle.setText(feed.getTitle());
String title = feed.getTitle();
long feedId = feed.getId();
int counter = itemAccess.getFeedCounter(feedId);
Uri imageUri = feed.getImageUri();
Log.i(TAG, String.format("Title: %s id: %d counter: %d uri: %s", title, feedId, counter, imageUri.toString()));
holder.feedTitle.setText(title);
holder.feedTitle.setVisibility(View.VISIBLE);
holder.count.setPrimaryText(String.valueOf(itemAccess.getFeedCounter(feed.getId())));
holder.count.setPrimaryText(String.valueOf(counter));
holder.count.setVisibility(View.VISIBLE);
Glide.with(mainActivityRef.get())
.load(feed.getImageUri())
.load(imageUri)
.error(R.color.light_gray)
.diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
.fitCenter()
.dontAnimate()
.into(new CoverTarget(feed.getImageUri(), holder.feedTitle, holder.imageView, mainActivityRef.get()));
.into(new CoverTarget(null, holder.feedTitle, holder.imageView, mainActivityRef.get()));
return convertView;
}
@ -134,4 +137,10 @@ public class SubscriptionsAdapter extends BaseAdapter implements AdapterView.OnI
public ImageView imageView;
public TriangleLabelView count;
}
public interface ItemAccess {
int getCount();
Feed getItem(int position);
int getFeedCounter(long feedId);
}
}

View File

@ -14,15 +14,12 @@ import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;
import java.util.List;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.adapter.NavListAdapter;
import de.danoeh.antennapod.adapter.SubscriptionsAdapter;
import de.danoeh.antennapod.core.asynctask.FeedRemover;
import de.danoeh.antennapod.core.dialog.ConfirmationDialog;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
@ -40,23 +37,19 @@ public class SubscriptionFragment extends Fragment {
public static final String TAG = "SubscriptionFragment";
private GridView mSubscriptionGridLayout;
private DBReader.NavDrawerData mDrawerData;
private SubscriptionsAdapter mSubscriptionAdapter;
private NavListAdapter.ItemAccess mItemAccess;
private static final int EVENTS = EventDistributor.FEED_LIST_UPDATE
| EventDistributor.UNREAD_ITEMS_UPDATE;
private GridView subscriptionGridLayout;
private DBReader.NavDrawerData navDrawerData;
private SubscriptionsAdapter subscriptionAdapter;
private List<Feed> mSubscriptionList = new ArrayList<>();
private int mPosition = -1;
public SubscriptionFragment() {
}
public void setItemAccess(NavListAdapter.ItemAccess itemAccess) {
mItemAccess = itemAccess;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -67,26 +60,27 @@ public class SubscriptionFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_subscriptions, container, false);
mSubscriptionGridLayout = (GridView) root.findViewById(R.id.subscriptions_grid);
registerForContextMenu(mSubscriptionGridLayout);
subscriptionGridLayout = (GridView) root.findViewById(R.id.subscriptions_grid);
registerForContextMenu(subscriptionGridLayout);
return root;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mSubscriptionAdapter = new SubscriptionsAdapter((MainActivity)getActivity(), mItemAccess);
subscriptionAdapter = new SubscriptionsAdapter((MainActivity)getActivity(), itemAccess);
mSubscriptionGridLayout.setAdapter(mSubscriptionAdapter);
subscriptionGridLayout.setAdapter(subscriptionAdapter);
loadSubscriptions();
mSubscriptionGridLayout.setOnItemClickListener(mSubscriptionAdapter);
subscriptionGridLayout.setOnItemClickListener(subscriptionAdapter);
if (getActivity() instanceof MainActivity) {
((MainActivity) getActivity()).getSupportActionBar().setTitle(R.string.subscriptions_label);
}
EventDistributor.getInstance().register(contentUpdate);
}
private void loadSubscriptions() {
@ -94,10 +88,8 @@ public class SubscriptionFragment extends Fragment {
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
mDrawerData = result;
mSubscriptionList = mDrawerData.feeds;
mSubscriptionAdapter.setItemAccess(mItemAccess);
mSubscriptionAdapter.notifyDataSetChanged();
navDrawerData = result;
subscriptionAdapter.notifyDataSetChanged();
}, error -> {
Log.e(TAG, Log.getStackTraceString(error));
});
@ -109,7 +101,7 @@ public class SubscriptionFragment extends Fragment {
AdapterView.AdapterContextMenuInfo adapterInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
int position = adapterInfo.position;
Object selectedObject = mSubscriptionAdapter.getItem(position);
Object selectedObject = subscriptionAdapter.getItem(position);
if (selectedObject.equals(SubscriptionsAdapter.ADD_ITEM_OBJ)) {
mPosition = position;
return;
@ -134,7 +126,7 @@ public class SubscriptionFragment extends Fragment {
return false;
}
Object selectedObject = mSubscriptionAdapter.getItem(position);
Object selectedObject = subscriptionAdapter.getItem(position);
if (selectedObject.equals(SubscriptionsAdapter.ADD_ITEM_OBJ)) {
// this is the add object, do nothing
return false;
@ -201,5 +193,41 @@ public class SubscriptionFragment extends Fragment {
@Override
public void onResume() {
super.onResume();
loadSubscriptions();
}
private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if ((EVENTS & arg) != 0) {
Log.d(TAG, "Received contentUpdate Intent.");
loadSubscriptions();
}
}
};
private SubscriptionsAdapter.ItemAccess itemAccess = new SubscriptionsAdapter.ItemAccess() {
@Override
public int getCount() {
if (navDrawerData != null) {
return navDrawerData.feeds.size();
} else {
return 0;
}
}
@Override
public Feed getItem(int position) {
if (navDrawerData != null && 0 <= position && position < navDrawerData.feeds.size()) {
return navDrawerData.feeds.get(position);
} else {
return null;
}
}
@Override
public int getFeedCounter(long feedId) {
return navDrawerData != null ? navDrawerData.feedCounters.get(feedId) : 0;
}
};
}