de.aw.monma.mainscreen.FragmentFinanzen.java Source code

Java tutorial

Introduction

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

import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.Loader;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.android.databinding.library.baseAdapters.BR;

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

import de.aw.awlib.adapters.AWSortedItemListAdapter;
import de.aw.awlib.database.AbstractDBHelper;
import de.aw.awlib.recyclerview.AWItemListRecyclerViewFragment;
import de.aw.awlib.recyclerview.AWLibViewHolder;
import de.aw.awlib.views.AWLoaderTextViewCurrencyValue;
import de.aw.monma.R;
import de.aw.monma.database.DBDefinition;
import de.aw.monma.database.TableColums;
import de.aw.monma.databinding.AccountListItem;

/**
 * Fragment zur Anzeige der Konten mit entsprechendem Saldo.
 */
public class FragmentFinanzen extends AWItemListRecyclerViewFragment<AccountListItem>
        implements SharedPreferences.OnSharedPreferenceChangeListener, TableColums {
    private static final String orderBy = AbstractDBHelper.getCommaSeperatedList(column_accounttyp,
            column_accountname);
    private static DBDefinition tbd = DBDefinition.Account;
    private static int layout = R.layout.finanzuebersicht;
    private static int viewHolderLayout = R.layout.finanzuebersicht_item;
    private static String[] projection = new String[] { column_accountname, column_saldo, column_hbci_saldo,
            column_accounttyp, _id };
    private static int[] viewResIDs = new int[] { R.id.tvUebersichtName, R.id.tvUebersichtSaldo, R.id.tvHBCISaldo };
    private boolean zeigeAusgeblendeteKonten;
    private AWLoaderTextViewCurrencyValue mSaldoView;

    @Override
    protected AWSortedItemListAdapter<AccountListItem> createListAdapter() {
        AWSortedItemListAdapter<AccountListItem> adapter = new AWSortedItemListAdapter<AccountListItem>(
                AccountListItem.class, this) {
            @Override
            protected boolean areContentsTheSame(AccountListItem item, AccountListItem other) {
                return item.areContentsTheSame(other);
            }

            @Override
            protected boolean areItemsTheSame(AccountListItem item, AccountListItem other) {
                return item.areItemsTheSame(other);
            }

            @Override
            protected int compare(AccountListItem item, AccountListItem other) {
                return item.compare(other);
            }

            @Override
            protected long getID(@NonNull AccountListItem item) {
                return item.getID();
            }
        };
        adapter.setHasStableIds(true);
        return adapter;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        startOrRestartLoader(layout, args);
    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.fragment_finanzen, menu);
        inflater.inflate(R.menu.item_hide_accounts, menu);
        menu.findItem(R.id.menu_item_showHiddenAccounts).setChecked(zeigeAusgeblendeteKonten);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor newCursor) {
        super.onLoadFinished(loader, newCursor);
        List<AccountListItem> list = new ArrayList<>();
        if (newCursor != null && newCursor.moveToFirst()) {
            AccountListItem header = new AccountListItem(newCursor.getString(3));
            header.setID(-header.getAccountTyp().ordinal());
            long saldo = 0;
            do {
                AccountListItem item = new AccountListItem(newCursor);
                list.add(item);
                if (header.getAccountTyp() == item.getAccountTyp()) {
                    saldo += item.getSaldo();
                } else {
                    header.setSaldo(saldo);
                    list.add(header);
                    header = new AccountListItem(newCursor.getString(3));
                    header.setID(-header.getAccountTyp().ordinal());
                    saldo = item.getSaldo();
                }
            } while (newCursor.moveToNext());
            header.setSaldo(saldo);
            list.add(header);
        }
        getAdapter().swap(list);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_item_showHiddenAccounts:
            zeigeAusgeblendeteKonten = !zeigeAusgeblendeteKonten;
            item.setChecked(zeigeAusgeblendeteKonten);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean(getString(R.string.menu_item_hideAccounts), zeigeAusgeblendeteKonten).apply();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        String mSharedPreference = getString(R.string.menu_item_hideAccounts);
        if (mSharedPreference.equals(key)) {
            zeigeAusgeblendeteKonten = sharedPreferences.getBoolean(key, false);
            String selection = null;
            if (!zeigeAusgeblendeteKonten) {
                selection = " NOT " + column_ausgeblendet;
            }
            mSaldoView.restart(selection, null);
            args.putString(SELECTION, selection);
            startOrRestartLoader(layout, args);
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSaldoView = view.findViewById(R.id.tvGesamtMarktwert);
        String selection = args.getString(SELECTION);
        String projection = AbstractDBHelper.SQLSumItem(column_saldo);
        mSaldoView.initialize(getLoaderManager(), tbd, projection, selection, null);
    }

    @Override
    protected void setInternalArguments(Bundle args) {
        super.setInternalArguments(args);
        args.putParcelable(DBDEFINITION, tbd);
        args.putInt(LAYOUT, layout);
        args.putInt(VIEWHOLDERLAYOUT, viewHolderLayout);
        args.putStringArray(PROJECTION, projection);
        args.putString(ORDERBY, orderBy);
        args.putIntArray(VIEWRESIDS, viewResIDs);
        zeigeAusgeblendeteKonten = prefs.getBoolean(getString(R.string.menu_item_hideAccounts), false);
        if (!zeigeAusgeblendeteKonten) {
            String selection = " NOT " + column_ausgeblendet;
            args.putString(SELECTION, selection);
        }
    }
}