Search in sources :

Example 1 with Item

use of io.github.hidroh.materialistic.data.Item in project materialistic by hidroh.

the class ItemFragment method onCreate.

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    if (savedInstanceState != null) {
        mCacheMode = savedInstanceState.getInt(STATE_CACHE_MODE, ItemManager.MODE_DEFAULT);
        mItem = savedInstanceState.getParcelable(STATE_ITEM);
        mItemId = savedInstanceState.getString(STATE_ITEM_ID);
        mAdapterItems = savedInstanceState.getParcelable(STATE_ADAPTER_ITEMS);
    } else {
        mCacheMode = getArguments().getInt(EXTRA_CACHE_MODE, ItemManager.MODE_DEFAULT);
        WebItem item = getArguments().getParcelable(EXTRA_ITEM);
        if (item instanceof Item) {
            mItem = (Item) item;
        }
        mItemId = item != null ? item.getId() : null;
    }
}
Also used : MenuItem(android.view.MenuItem) WebItem(io.github.hidroh.materialistic.data.WebItem) Item(io.github.hidroh.materialistic.data.Item) WebItem(io.github.hidroh.materialistic.data.WebItem)

Example 2 with Item

use of io.github.hidroh.materialistic.data.Item in project materialistic by hidroh.

the class StoryRecyclerViewAdapter method onAttachedToRecyclerView.

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    ContentResolver cr = recyclerView.getContext().getContentResolver();
    cr.registerContentObserver(MaterialisticProvider.URI_VIEWED, true, mObserver);
    cr.registerContentObserver(MaterialisticProvider.URI_FAVORITE, true, mObserver);
    mCallback = new ItemTouchHelperCallback(recyclerView.getContext(), Preferences.getListSwipePreferences(recyclerView.getContext())) {

        @Override
        public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            Item item = getItem(viewHolder.getAdapterPosition());
            if (item == null) {
                return 0;
            }
            mSaved = item.isFavorite();
            return checkSwipeDir(0, ItemTouchHelper.LEFT, mCallback.getLeftSwipeAction(), item) | checkSwipeDir(0, ItemTouchHelper.RIGHT, mCallback.getRightSwipeAction(), item);
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            Preferences.SwipeAction action = direction == ItemTouchHelper.LEFT ? getLeftSwipeAction() : getRightSwipeAction();
            Item item = getItem(viewHolder.getAdapterPosition());
            if (item == null) {
                return;
            }
            switch(action) {
                case Save:
                    toggleSave(item);
                    break;
                case Refresh:
                    refresh(item, viewHolder);
                    break;
                case Vote:
                    notifyItemChanged(viewHolder.getAdapterPosition());
                    vote(item, viewHolder);
                    break;
            }
        }

        private int checkSwipeDir(int swipeDirs, int swipeDir, Preferences.SwipeAction action, Item item) {
            switch(action) {
                case None:
                    break;
                case Vote:
                    if (!item.isVoted() && !item.isPendingVoted()) {
                        swipeDirs |= swipeDir;
                    }
                    break;
                default:
                    swipeDirs |= swipeDir;
                    break;
            }
            return swipeDirs;
        }
    };
    mItemTouchHelper = new ItemTouchHelper(mCallback);
    mItemTouchHelper.attachToRecyclerView(recyclerView);
    toggleAutoMarkAsViewed(recyclerView.getContext());
    mPrefObservable.subscribe(recyclerView.getContext(), (key, contextChanged) -> {
        mCallback.setSwipePreferences(recyclerView.getContext(), Preferences.getListSwipePreferences(recyclerView.getContext()));
        notifyDataSetChanged();
    }, R.string.pref_list_swipe_left, R.string.pref_list_swipe_right);
}
Also used : ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) Item(io.github.hidroh.materialistic.data.Item) RecyclerView(android.support.v7.widget.RecyclerView) Preferences(io.github.hidroh.materialistic.Preferences) ContentResolver(android.content.ContentResolver)

Example 3 with Item

use of io.github.hidroh.materialistic.data.Item in project materialistic by hidroh.

the class StoryRecyclerViewAdapter method markAsViewed.

@Synthetic
void markAsViewed(int position) {
    if (position < 0) {
        return;
    }
    Item item = mItems != null && position < mItems.size() ? mItems.get(position) : null;
    if (item == null || !isItemAvailable(item) || item.isViewed()) {
        return;
    }
    mSessionManager.view(mContext, item.getId());
}
Also used : Item(io.github.hidroh.materialistic.data.Item) Synthetic(io.github.hidroh.materialistic.annotation.Synthetic)

Example 4 with Item

use of io.github.hidroh.materialistic.data.Item in project materialistic by hidroh.

the class StoryRecyclerViewAdapter method loadItem.

@Override
protected void loadItem(final int adapterPosition) {
    Item item = getItem(adapterPosition);
    if (item.getLocalRevision() == 0) {
        return;
    }
    item.setLocalRevision(0);
    mItemManager.getItem(item.getId(), getItemCacheMode(), new ItemResponseListener(this, item));
}
Also used : Item(io.github.hidroh.materialistic.data.Item)

Example 5 with Item

use of io.github.hidroh.materialistic.data.Item in project materialistic by hidroh.

the class StoryRecyclerViewAdapter method bindItem.

@Override
protected void bindItem(final ItemViewHolder holder) {
    final Item story = getItem(holder.getAdapterPosition());
    bindItemUpdated(holder, story);
    highlightUserPost(holder, story);
    holder.mStoryView.setViewed(story.isViewed());
    if (story.getLocalRevision() < mFavoriteRevision) {
        story.setFavorite(false);
    }
    holder.mStoryView.setFavorite(story.isFavorite());
    holder.itemView.setOnLongClickListener(v -> {
        showMoreOptions(holder.mStoryView.getMoreOptions(), story, holder);
        return true;
    });
    holder.mStoryView.getMoreOptions().setOnClickListener(v -> showMoreOptions(v, story, holder));
}
Also used : Item(io.github.hidroh.materialistic.data.Item)

Aggregations

Item (io.github.hidroh.materialistic.data.Item)16 WebItem (io.github.hidroh.materialistic.data.WebItem)6 Bundle (android.os.Bundle)4 Fragment (android.support.v4.app.Fragment)4 RecyclerView (android.support.v7.widget.RecyclerView)4 Intent (android.content.Intent)3 MenuItem (android.view.MenuItem)3 Context (android.content.Context)2 ItemTouchHelper (android.support.v7.widget.helper.ItemTouchHelper)2 Synthetic (io.github.hidroh.materialistic.annotation.Synthetic)2 TestHnItem (io.github.hidroh.materialistic.data.TestHnItem)2 TestItem (io.github.hidroh.materialistic.test.TestItem)2 SlowTest (io.github.hidroh.materialistic.test.suite.SlowTest)2 Test (org.junit.Test)2 RoboMenuItem (org.robolectric.fakes.RoboMenuItem)2 SuppressLint (android.annotation.SuppressLint)1 PendingIntent (android.app.PendingIntent)1 BroadcastReceiver (android.content.BroadcastReceiver)1 ContentResolver (android.content.ContentResolver)1 IntentFilter (android.content.IntentFilter)1