de.aw.monma.reports.FragmentSelectionListCatClass.java Source code

Java tutorial

Introduction

Here is the source code for de.aw.monma.reports.FragmentSelectionListCatClass.java

Source

/*
 * MonMa: Eine freie Android-Application fuer die Verwaltung privater Finanzen
 *
 * Copyright [2015] [Alexander Winkler, 2373 Dahme/Germany]
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
package de.aw.monma.reports;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckedTextView;

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

import de.aw.awlib.recyclerview.AWCursorRecyclerViewFragment;
import de.aw.awlib.recyclerview.AWLibViewHolder;
import de.aw.monma.BR;
import de.aw.monma.R;
import de.aw.monma.database.DBDefinition;
import de.aw.monma.database.DBHelper;
import de.aw.monma.gv.ReportStammdaten;
import de.aw.monma.gv.Type_Class;
import de.aw.monma.monmamain.MonMaInterface;

/**
 * Liste der Kategorien fuer einen Report
 */
public class FragmentSelectionListCatClass extends AWCursorRecyclerViewFragment implements MonMaInterface {
    private static final DBDefinition tbd = DBDefinition.Type_Class;
    private static final String orderBy = column_classname;
    protected final List<Long> excludedItems = new ArrayList<>();
    private final int itemView = R.layout.report_selection_list_catclass_item;
    protected int indexselection;
    protected ReportStammdaten mReportStammdaten;
    private int[] viewResIDs = new int[] { R.id.tvSelection_item };

    /**
     * Erstellt eine neue Categorie-Selectionsliste.
     *
     * @param stammdaten
     *         Reportstammdaten
     *
     * @return Fragment
     */
    public static FragmentSelectionListCatClass newInstance(ReportStammdaten stammdaten) {
        Bundle args = new Bundle();
        FragmentSelectionListCatClass f = new FragmentSelectionListCatClass();
        args.putParcelable(REPORTSTAMMDATEN, stammdaten);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onBindViewHolder(AWLibViewHolder holder, Cursor cursor, int position) {
        Type_Class item = new Type_Class(cursor);
        holder.setVariable(BR.catclass, item);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReportStammdaten = args.getParcelable(REPORTSTAMMDATEN);
        assert mReportStammdaten != null;
        String exc = mReportStammdaten.getAsString(column_excl_catclass);
        if (exc != null) {
            String[] escItems = exc.split(",");
            for (String escItem : escItems) {
                excludedItems.add(Long.parseLong(escItem.trim()));
            }
        }
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.actionmode_selectionlist, menu);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        super.onLoadFinished(loader, cursor);
        indexselection = cursor.getColumnIndexOrThrow(_id);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_item_selectAll:
            excludedItems.clear();
            mReportStammdaten.setExcludedCatClasses(excludedItems);
            startOrRestartLoader();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mReportStammdaten.setExcludedCatClasses(excludedItems);
        mReportStammdaten.update(DBHelper.getInstance());
    }

    @Override
    public void onRecyclerItemClick(View view, int position, long id) {
        super.onRecyclerItemClick(view, position, id);
        CheckedTextView cb = view.findViewById(R.id.tvSelection_item);
        cb.toggle();
        if (cb.isChecked()) {
            excludedItems.remove(id);
        } else {
            excludedItems.add(id);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        setTitle(R.string.menu_item_reportEditCat);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        args.putParcelable(REPORTSTAMMDATEN, mReportStammdaten);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void setInternalArguments(Bundle args) {
        super.setInternalArguments(args);
        args.putParcelable(DBDEFINITION, tbd);
        args.putInt(VIEWHOLDERLAYOUT, itemView);
        args.putString(ORDERBY, orderBy);
    }
}