de.aw.monma.cash.FragmentBuchungenSaldoListe.java Source code

Java tutorial

Introduction

Here is the source code for de.aw.monma.cash.FragmentBuchungenSaldoListe.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.cash;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.util.SparseArrayCompat;

import de.aw.awlib.adapters.AWCursorAdapter;
import de.aw.awlib.application.AWApplication;
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.databinding.UmsatzListItem;
import de.aw.monma.gv.Account;

/**
 * Erstellt ein Fragment mit einer Liste von Buchungen zu einem Konto. Ausserdem wird bei jedem
 * Umsatz der aktuelle Saldo angezeigt. Die Liste wird immer Buchungstag-absteigend sortiert
 * angezeigt.
 */
public class FragmentBuchungenSaldoListe extends FragmentBuchungenListe {
    private static final DBDefinition tbd = DBDefinition.VFragmentBuchungenListeNew;
    private static final String selection = column_accountID + " = ? ";
    private static final String orderBy = column_btag + " DESC";
    private static int listLayout = R.layout.buchung_list_saldoitem;
    private SaldoAdapter mAdapter;

    /**
     * Erstellt ein neues FragmentBuchungenListe
     *
     * @param account
     *         Konto, dessen Buchungen angezeigt werden sollen.
     *
     * @return Neues Fragment
     */
    public static FragmentBuchungenSaldoListe newInstance(Account account) {
        Bundle args = new Bundle();
        args.putParcelable(ACCOUNT, account);
        args.putString(SELECTION, selection);
        String[] selectionArgs = new String[] { String.valueOf(account.getID()) };
        args.putStringArray(SELECTIONARGS, selectionArgs);
        args.putString(ORDERBY, orderBy);
        FragmentBuchungenSaldoListe f = new FragmentBuchungenSaldoListe();
        f.setArguments(args);
        return f;
    }

    /**
     * Fuer die Anzeige des Saldos wird ein separater Adapter verwendet.
     *
     * @return {@link SaldoAdapter}
     */
    @Override
    protected AWCursorAdapter createCursorAdapter() {
        if (mAdapter == null) {
            mAdapter = new SaldoAdapter(this);
        }
        return mAdapter;
    }

    /**
     * Setzt im Item den aktuellen Saldo, den der Adapter liefert.
     */
    @Override
    public void onBindViewHolder(AWLibViewHolder holder, Cursor cursor, int position) {
        UmsatzListItem item = new UmsatzListItem(cursor);
        item.setItemSaldo(mAdapter.getSaldo(position));
        item.zeigeDetails(zeigeDetails);
        holder.setVariable(BR.cashumsatz, item);
    }

    @Override
    protected void setInternalArguments(Bundle args) {
        super.setInternalArguments(args);
        args.putInt(VIEWHOLDERLAYOUT, listLayout);
    }

    /**
     * CursorAdapter, der den Saldo zum Zeitpunkt des Umsatzes berechnet.
     */
    private class SaldoAdapter extends AWCursorAdapter {
        private SparseArrayCompat<Long> saldoList = new SparseArrayCompat<>();
        private long mSaldo;
        private int columnAmount;
        private int mLastPosition;
        private int mSaldoColumn;

        SaldoAdapter(AWCursorAdapterBinder binder) {
            super(binder);
        }

        /**
         * Liefert den Saldo an der Position. Es werden immer die naechsten 20 Salden berechnet.
         *
         * @param position
         *         Position
         *
         * @return Saldo an der Position
         */
        public long getSaldo(int position) {
            Cursor c = getCursor();
            int pos = c.getPosition();
            while (mLastPosition < position) {
                c.moveToPosition(mLastPosition);
                mSaldo -= c.getLong(columnAmount);
                mLastPosition++;
                saldoList.put(mLastPosition, mSaldo);
            }
            c.moveToPosition(pos);
            Long s = saldoList.get(position);
            if (s == null) {
                AWApplication.Log("NULL");
            }
            return saldoList.get(position);
        }

        @Override
        public Cursor swapCursor(Cursor newCursor) {
            saldoList.clear();
            if (newCursor != null) {
                columnAmount = newCursor.getColumnIndex(column_amount);
                mSaldoColumn = newCursor.getColumnIndex(column_saldo);
                if (newCursor.moveToFirst()) {
                    mSaldo = newCursor.getLong(mSaldoColumn);
                }
                saldoList.put(0, mSaldo);
                mLastPosition = 0;
            }
            return super.swapCursor(newCursor);
        }
    }
}