org.rssin.android.NavigationDrawerAdapter.java Source code

Java tutorial

Introduction

Here is the source code for org.rssin.android.NavigationDrawerAdapter.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.content.Intent;
import android.database.DataSetObserver;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;

import org.rssin.rssin.Feed;
import org.rssin.rssin.Filter;
import org.rssin.rssin.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Jos.
 */
class NavigationDrawerAdapter implements ListAdapter {
    private static final int ITEM_VIEW_TYPE_NORMAL = 0;
    private static final int ITEM_VIEW_TYPE_SEPARATOR = 1;
    private static final int ITEM_VIEW_TYPE_COUNT = 2;
    private final List<MenuItem> menuItems = new ArrayList<>();

    public NavigationDrawerAdapter(Context context) throws IOException {
        updateMenu(context);
    }

    public void updateMenu(Context context) throws IOException {
        menuItems.clear();
        menuItems.add(new MenuSection(context.getString(R.string.navigation_drawer_start), ""));
        menuItems.add(new MenuUnifiedInbox(context));
        menuItems.add(new ManageFiltersSection(context.getString(R.string.navigation_drawer_edit_filters),
                context.getString(R.string.navigation_drawer_edit_text)));

        for (Filter f : FiltersList.getInstance(context).getFilters()) {
            menuItems.add(new MenuFilterItem(f));
        }

        menuItems.add(new ManageFeedsSection(context.getString(R.string.navigation_drawer_edit_feeds),
                context.getString(R.string.navigation_drawer_edit_text)));

        for (Feed f : FeedsList.getInstance(context).getFeeds()) {
            menuItems.add(new MenuFeedItem(f));
        }

        for (DataSetObserver observer : observers) {
            observer.onChanged();
        }
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        // Position 0 is always "Start"
        return position != 0;
    }

    private List<DataSetObserver> observers = new ArrayList<>();

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        observers.add(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        observers.remove(observer);
    }

    @Override
    public int getCount() {
        return menuItems.size();
    }

    @Override
    public Object getItem(int position) {
        return menuItems.get(position);
    }

    public String getText(int position) {
        return menuItems.get(position).getText();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int type = getItemViewType(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(type == ITEM_VIEW_TYPE_SEPARATOR ? R.layout.separator_navigation_drawer
                            : R.layout.item_navigation_drawer, parent, false);
        }

        if (type == ITEM_VIEW_TYPE_SEPARATOR) {
            final MenuSection section = (MenuSection) getItem(position);
            ((TextView) convertView.findViewById(R.id.separator_navigation_drawer_text)).setText(section.getText());
            ((TextView) convertView.findViewById(R.id.separator_navigation_drawer_edit))
                    .setText(section.getRightText());
        } else {
            final MenuItem video = (MenuItem) getItem(position);
            ((TextView) convertView.findViewById(R.id.item_navigation_drawer_text)).setText(video.getText());
        }

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return menuItems.get(position).getItemViewType();
    }

    @Override
    public int getViewTypeCount() {
        return ITEM_VIEW_TYPE_COUNT;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    public Fragment getFragment(int position) {
        return menuItems.get(position).getFragment();
    }

    public MenuItem getTypedItem(int position) {
        return menuItems.get(position);
    }

    class MenuItem {
        protected String text;

        public MenuItem(String text) {
            this.text = text;
        }

        public void invokeActionSettings(Context context) {

        }

        public int getItemViewType() {
            return ITEM_VIEW_TYPE_NORMAL;
        }

        public Fragment getFragment() {
            return null;
        }

        public String getText() {
            return text;
        }

        public int getPreferredMenuId() {
            return R.menu.global;
        }
    }

    class MenuUnifiedInbox extends MenuItem {
        public MenuUnifiedInbox(Context context) {
            super(context.getString(R.string.navigation_drawer_all_feeds));
        }

        @Override
        public int getItemViewType() {
            return ITEM_VIEW_TYPE_NORMAL;
        }

        @Override
        public Fragment getFragment() {
            return NavigationDrawerAllFeedsFragment.newInstance();
        }

        @Override
        public int getPreferredMenuId() {
            return R.menu.home_screen;
        }
    }

    class MenuFeedItem extends MenuItem {
        private Feed feed;

        public MenuFeedItem(Feed feed) {
            super(feed.getTitle());
            this.feed = feed;
        }

        @Override
        public void invokeActionSettings(Context context) {
            Intent intent = new Intent(context, FeedsActivity.class);
            context.startActivity(intent);
        }

        @Override
        public int getItemViewType() {
            return ITEM_VIEW_TYPE_NORMAL;
        }

        @Override
        public Fragment getFragment() {
            return NavigationDrawerFeedFragment.newInstance(feed);
        }
    }

    class MenuFilterItem extends MenuItem {
        private Filter filter;

        public MenuFilterItem(Filter filter) {
            super(filter.getTitle());
            this.filter = filter;
        }

        @Override
        public void invokeActionSettings(Context context) {
            filter.ensureFeeds(DefaultStorageProvider.getInstance(context));
            Intent intent = new Intent(context.getApplicationContext(), FilterSettingsActivity.class);
            intent.putExtra("filter", filter.hashCode());
            context.startActivity(intent);
        }

        @Override
        public int getItemViewType() {
            return ITEM_VIEW_TYPE_NORMAL;
        }

        @Override
        public Fragment getFragment() {
            return NavigationDrawerFilterFragment.newInstance(filter);
        }
    }

    class MenuSection extends MenuItem {
        private String rightText;

        public MenuSection(String text, String rightText) {
            super(text);
            this.rightText = rightText;
        }

        @Override
        public int getItemViewType() {
            return ITEM_VIEW_TYPE_SEPARATOR;
        }

        public String getRightText() {
            return rightText;
        }
    }

    class ManageFiltersSection extends MenuSection {
        public ManageFiltersSection(String text, String rightText) {
            super(text, rightText);
        }

        @Override
        public Fragment getFragment() {
            return NavigationDrawerManageFiltersFragment.newInstance();
        }

        @Override
        public int getPreferredMenuId() {
            return R.menu.filters;
        }
    }

    class ManageFeedsSection extends MenuSection {
        public ManageFeedsSection(String text, String rightText) {
            super(text, rightText);
        }

        @Override
        public void invokeActionSettings(Context context) {

        }

        @Override
        public Fragment getFragment() {
            return NavigationDrawerManageFeedsFragment.newInstance();
        }

        @Override
        public int getPreferredMenuId() {
            return R.menu.feeds;
        }
    }
}