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

Java tutorial

Introduction

Here is the source code for de.aw.monma.reports.ReportGeldflussCats.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.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;

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

import de.aw.awlib.adapters.AWItemListAdapter;
import de.aw.awlib.recyclerview.AWItemListRecyclerViewFragment;
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.gv.ReportStammdaten;
import de.aw.monma.monmamain.MonMaInterface;

/**
 * Zeigt die Kategorien eines Reports. Angezeigt werden Kat-Name und Summe der Umsaetze ueber den in
 * ReportBasisdaten angegebenenZeitraum
 */
public class ReportGeldflussCats extends AWItemListRecyclerViewFragment<ReportGeldflussItem>
        implements MonMaInterface {
    protected static final DBDefinition tbd = DBDefinition.ReportGeldfluss;
    private static final int layout = R.layout.report_geldfluss;
    private static final int viewHolderLayout = R.layout.report_geldfluss_item;
    private static final String[] projection = new String[] { column_catname, column_catID, column_amount,
            column_incomecat, column_verglamount, _id };
    protected ReportStammdaten mReportStammdaten;
    private ReportSummenBindable mReportSummenBinder;

    public static ReportGeldflussCats newInstance(ReportStammdaten stammdaten) {
        Bundle args = new Bundle();
        args.putParcelable(REPORTSTAMMDATEN, stammdaten);
        ReportGeldflussCats fragment = new ReportGeldflussCats();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected AWItemListAdapter<ReportGeldflussItem> createListAdapter() {
        return new ReportAdapter(this);
    }

    /**
     * Baut die Selection auf. Wird regelmaessig verwendet, wenn sich der Cursor aendert. Kann
     * ueberschrieben werden.
     *
     * @return Selection
     */
    protected String getSelection() {
        return mReportStammdaten.getReportSelection();
    }

    @Override
    public void onBindViewHolder(AWLibViewHolder holder, ReportGeldflussItem item, int position) {
        holder.setVariable(BR.geldfluss, item);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReportStammdaten = args.getParcelable(REPORTSTAMMDATEN);
        mReportSummenBinder = new ReportSummenBindable(mReportStammdaten.getReportTyp());
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
        super.onLoadFinished(loader, c);
        List<ReportGeldflussItem> list = new ArrayList<>();
        long summeInAmount = 0;
        long summeInVerglAmount = 0;
        long summeOutAmount = 0;
        long summeOutVerglAmount = 0;
        if (c.moveToFirst()) {
            do {
                ReportGeldflussItem item = new ReportGeldflussItem(mReportStammdaten, tbd, c);
                list.add(item);
                if (item.isIncome()) {
                    summeInAmount += item.getAmount();
                    summeInVerglAmount += item.getVerglAmount();
                } else {
                    summeOutAmount += item.getAmount();
                    summeOutVerglAmount += item.getVerglAmount();
                }
            } while (c.moveToNext());
        }
        mReportSummenBinder.notifyPropertyChanged(summeInAmount, summeInVerglAmount, summeOutAmount,
                summeOutVerglAmount);
        getAdapter().swap(list);
    }

    @Override
    public void onRecyclerItemClick(View v, int position, ReportGeldflussItem item) {
        ReportGeldflussDetails f = ReportGeldflussDetails.newInstance(mReportStammdaten, item.getID());
        replaceFragment(f);
        super.onRecyclerItemClick(v, position, item);
    }

    @Override
    public boolean onRecyclerItemLongClick(View v, int position, ReportGeldflussItem item) {
        super.onRecyclerItemLongClick(v, position, item);
        ReportPayee f = ReportPayee.newInstance(mReportStammdaten, item.getID());
        replaceFragment(f);
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        setTitle(mReportStammdaten.getReportName());
        args.putString(SELECTION, mReportStammdaten.getReportSelection());
        startOrRestartLoader(layout, args);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewDataBinding binding = DataBindingUtil.bind(view);
        binding.setVariable(BR.reportsummen, mReportSummenBinder);
    }

    @Override
    public void setCursorLoaderArguments(int p1, Bundle args) {
        args.putString(SELECTION, getSelection());
    }

    @Override
    protected void setInternalArguments(Bundle args) {
        super.setInternalArguments(args);
        args.putStringArray(PROJECTION, projection);
        args.putParcelable(DBDEFINITION, tbd);
        args.putInt(LAYOUT, layout);
        args.putInt(VIEWHOLDERLAYOUT, viewHolderLayout);
        mReportStammdaten = args.getParcelable(REPORTSTAMMDATEN);
    }
}