com.none.tom.simplerssreader.fragment.CurrentFeedFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.none.tom.simplerssreader.fragment.CurrentFeedFragment.java

Source

// Copyright (c) 2017-2018, Tom Geiselmann
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY,WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.none.tom.simplerssreader.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.none.tom.simplerssreader.R;
import com.none.tom.simplerssreader.activity.MainActivity;
import com.none.tom.simplerssreader.adapter.CurrentFeedAdapter;
import com.none.tom.simplerssreader.feed.CurrentFeed;
import com.none.tom.simplerssreader.fragment.base.BaseRecyclerViewFragment;
import com.none.tom.simplerssreader.loader.IsFeedUpdateAvailableLoader;
import com.none.tom.simplerssreader.utils.CenterLayoutManager;
import com.none.tom.simplerssreader.utils.SharedPrefUtils;
import com.none.tom.simplerssreader.utils.VerticalSpaceItemDecoration;

public class CurrentFeedFragment extends BaseRecyclerViewFragment {
    public static final String TAG = CurrentFeedFragment.class.getSimpleName();

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;

    @SuppressWarnings("ConstantConditions")
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
            final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_current_feed_recyclerview, container, false);

        final Activity activity = getActivity();

        mSwipeRefreshLayout = view.findViewById(R.id.CurrentFeedSwipeToRefreshLayout);

        mSwipeRefreshLayout.setColorSchemeColors(activity.getResources().getIntArray(R.array.color_palette));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isFeedUpdateAvailable();
            }
        });

        final VerticalSpaceItemDecoration decor = new VerticalSpaceItemDecoration(activity);
        decor.setDrawable(activity.getDrawable(R.drawable.divider));

        mRecyclerView = view.findViewById(R.id.CurrentFeedRecyclerView);

        mRecyclerView.setLayoutManager(new CenterLayoutManager(activity));
        mRecyclerView.setAdapter(new CurrentFeedAdapter(activity));
        mRecyclerView.addItemDecoration(decor);

        super.onCreateView((CoordinatorLayout) view, mRecyclerView);

        return view;
    }

    @SuppressWarnings("ConstantConditions")
    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final Activity activity = getActivity();

        ((MainActivity) activity).getSupportActionBar().setTitle(SharedPrefUtils.getCurrentFeedTitle(activity));
    }

    @Override
    public void onViewStateRestored(final Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);

        if (savedInstanceState != null) {
            mRecyclerView.getLayoutManager().onRestoreInstanceState(savedInstanceState.getParcelable(KEY_STATE));
        } else {
            mRecyclerView.scrollToPosition(0);
        }
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_filter_by_categories:
            showFilterByCategoriesFragment();
            return true;
        case R.id.action_update:
            checkForCurrentFeedUpdate();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void scrollToTop() {
        mRecyclerView.getAdapter().notifyDataSetChanged();

        super.scrollToTop();
    }

    private void checkForCurrentFeedUpdate() {
        mSwipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(true);

                isFeedUpdateAvailable();
            }
        });
    }

    public void currentFeedUpdateCheckFinished() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    public void finishLayoutRefreshing() {
        if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }

        if (getActivity() != null) {
            final IsFeedUpdateAvailableLoader loader = (IsFeedUpdateAvailableLoader) getActivity()
                    .getLoaderManager().<String>getLoader(IsFeedUpdateAvailableLoader.ID);

            if (loader != null && loader.isStarted()) {
                loader.cancelLoadInBackground();
            }
        }
    }

    private void isFeedUpdateAvailable() {
        final MainActivity activity = (MainActivity) getActivity();
        activity.getLoaderManager().restartLoader(IsFeedUpdateAvailableLoader.ID, null, activity);
    }

    private void showFilterByCategoriesFragment() {
        FilterByCategoriesFragment.getInstance(CurrentFeed.getCategories()).show(getFragmentManager(),
                FilterByCategoriesFragment.TAG);
    }
}