org.rssin.android.FeedItemsListFragment.java Source code

Java tutorial

Introduction

Here is the source code for org.rssin.android.FeedItemsListFragment.java

Source

/**
 * RSSin - Clever RSS reader for Android
 * Copyright (C) 2015 Randy Wanga, Jos Craaijo, Joep Bernards, Camil Staps
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.rssin.android;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.rssin.listener.RealtimeListener;
import org.rssin.rssin.Feed;
import org.rssin.rssin.FeedLoaderAndSorter;
import org.rssin.rssin.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Common class for a Fragment with a list of feeditems that need to be loaded from Feeds
 * @author Camil Staps
 */
public class FeedItemsListFragment extends Fragment {

    private List<Feed> feeds;
    private View loadingView;
    private FeedItemAdapter feedItemAdapter;
    private Context context;

    /**
     * Create a new instance based on a list of feeds
     * @param feeds
     * @return
     */
    public static FeedItemsListFragment newInstance(List<Feed> feeds) {
        FeedItemsListFragment fragment = new FeedItemsListFragment();
        fragment.setFeedsList(feeds);
        return fragment;
    }

    /**
     * Create a new instance
     * @param feeds
     */
    public void setFeedsList(List<Feed> feeds) {
        this.feeds = feeds;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        container.removeAllViews();

        View rootView = inflater.inflate(R.layout.fragment_feeditems_list, container, false);

        loadingView = rootView.findViewById(R.id.loading);
        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.unified_inbox_feeditems);
        context = recyclerView.getContext();

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);

        feedItemAdapter = new FeedItemAdapter(new ArrayList<SortedFeedItemContainer>());
        recyclerView.setAdapter(feedItemAdapter);
        recyclerView.setHasFixedSize(false);

        refresh();

        return rootView;
    }

    /**
     * Refresh the RecyclerView
     */
    private void refresh() {
        loadingView.setVisibility(View.VISIBLE);

        FeedLoaderAndSorter loaderAndSorter = new FeedLoaderAndSorter(feeds);

        try {
            loaderAndSorter.getFilteredFeedItems(DefaultStorageProvider.getInstance(context),
                    VolleyFetcher.getInstance(context), getActivity(),
                    new RealtimeListener<List<SortedFeedItemContainer>, Object>() {
                        @Override
                        public void finish() {
                            loadingView.setVisibility(View.GONE);
                        }

                        @Override
                        public void onReceive(List<SortedFeedItemContainer> data) {
                            feedItemAdapter.feedItems = data;
                            feedItemAdapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onError(Object error) {
                            Frontend.error(context, R.string.error_net_load);
                            Log.w("FILF", error.toString());
                        }
                    });
        } catch (Exception e) {
            Frontend.error(context, R.string.error_unknown);
        }
    }

}