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

Java tutorial

Introduction

Here is the source code for de.aw.monma.reports.ActivityReports.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.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;

import java.io.File;

import de.aw.awlib.activities.AWWebViewActivity;
import de.aw.awlib.csvimportexport.AWCSVExporter;
import de.aw.awlib.fragments.AWFragment;
import de.aw.monma.R;
import de.aw.monma.charts.ChartReportPie;
import de.aw.monma.charts.ChartTemplate;
import de.aw.monma.charts.ChartTyp;
import de.aw.monma.csvexport.CSVExporter;
import de.aw.monma.enums.ReportTyp;
import de.aw.monma.gv.GeschaeftsObjekt;
import de.aw.monma.gv.ReportStammdaten;
import de.aw.monma.monmamain.MonMaMainActivity;

/**
 * Activity fuer Steuerung der Reports
 */
public class ActivityReports extends MonMaMainActivity
        implements ChartTemplate.OnChartValueSelectedListener, AWCSVExporter.ExportCursorListener {
    private static final int layout = R.layout.activity_reports;
    private static final int reportZeitraumHeader = R.id.reportDetailFragment;
    private static final int reportSummenFooter = R.id.reportFooter;
    private CSVExporter mCSVExporter;
    private ReportStammdaten mReportStammdaten;
    private ReportTyp mReportTyp;

    @Override
    public void onChartValueSelected(ChartTyp chartTyp, Entry e, int dataSetIndex, Highlight h) {
        switch (chartTyp) {
        case ReportMonatsBar:
            int stackIndex = h.getStackIndex();
            int data = (int) e.getData();
            int year = data / 100;
            int month = data - year * 100;
            ChartReportPie f = ChartReportPie.newInstance(mReportStammdaten, year, month, stackIndex == 0);
            replaceFragment(f);
            break;
        }
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, layout);
        if (savedInstanceState == null) {
            AWFragment f = null;
            try {
                mReportStammdaten = new ReportStammdaten(args.getLong(REPORTID));
                mReportTyp = mReportStammdaten.getReportTyp();
                args.putParcelable(REPORTSTAMMDATEN, mReportStammdaten);
                switch (mReportTyp) {
                case ReportWPBestand:
                    f = FragmentReportWPBestand.newInstance(mReportStammdaten);
                    break;
                case Geldfluss:
                case VerglReport:
                    f = ReportGeldflussCats.newInstance(mReportStammdaten);
                    break;
                case WertpapierReportCats:
                    f = ReportWPCats.newInstance(mReportStammdaten);
                    break;
                case WertpapierReportDetail:
                    f = ReportWPDetail.newInstance(mReportStammdaten);
                    break;
                case EmpfaengerBericht:
                    f = ReportPayee.newInstance(mReportStammdaten);
                    break;
                case KontoStandReport:
                    f = FragmentReportKontostaende.newInstance(mReportStammdaten);
                    break;
                }
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.add(container, f, null);
                switch (mReportTyp) {
                case ReportWPBestand:
                case KontoStandReport:
                    // Kein Header/Footer, wenn Kontostand oder WPBestandsReport
                    break;
                default:
                    Fragment header = FragmentSelectionZeitraum.newInstance(mReportStammdaten);
                    ft.add(reportZeitraumHeader, header, null);
                }
                ft.commit();
            } catch (GeschaeftsObjekt.LineNotFoundException e) {
                //TODO Execption bearbeiten
                e.printStackTrace();
            }
        } else {
            mReportStammdaten = args.getParcelable(REPORTSTAMMDATEN);
            mReportTyp = mReportStammdaten.getReportTyp();
        }
        setSubTitle(mReportTyp.mTextID);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.activity_report, menu);
        return true;
    }

    @Override
    public void onFinishExport(int result) {
        switch (result) {
        case RESULT_OK:
            Snackbar snackbar = Snackbar
                    .make(findViewById(container), de.aw.awlib.R.string.exportErfolgreich, Snackbar.LENGTH_LONG)
                    .setAction(de.aw.awlib.R.string.awlib_openFile, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            File file = new File(mCSVExporter.getFilename());
                            final Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(file), "text/csv");
                            startActivity(intent);
                        }
                    });
            snackbar.show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        AWFragment f;
        switch (item.getItemId()) {
        case R.id.menu_item_exportReport:
            mCSVExporter = new CSVExporter(this, this, mReportStammdaten);
            return true;
        case R.id.menu_item_reportChart:
            f = FragmentCharts.newInstance(mReportStammdaten);
            break;
        case R.id.awlib_menu_item_hilfe:
            Intent intent = new Intent(this, AWWebViewActivity.class);
            intent.putExtra(ID, "Reports.html");
            startActivity(intent);
            return true;
        case R.id.menu_item_reportEditCat:
            f = FragmentSelectionListCat.newInstance(mReportStammdaten);
            break;
        case R.id.menu_item_reportEditCatClass:
            f = FragmentSelectionListCatClass.newInstance(mReportStammdaten);
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        if (f != null) {
            replaceFragment(f);
            return true;
        }
        return false;
    }

    @Override
    public void onStartExport() {
    }

    /**
     * Erstezt ein Fragment im container und entfernt dabei den Header und Footer
     *
     * @param f
     *         MonMaFragment fuer Container
     */
    private void replaceFragment(AWFragment f) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(container, f, f.getTAG());
        Fragment footer = fm.findFragmentById(reportSummenFooter);
        if (footer != null) {
            ft.remove(footer);
        }
        Fragment header = fm.findFragmentById(reportZeitraumHeader);
        if (header != null) {
            ft.remove(header);
        }
        ft.addToBackStack(null).commit();
    }
}