~schunka/financisto/cz-locales

« back to all changes in this revision

Viewing changes to src/ru/orangesoftware/financisto/activity/AbstractTotalsDetailsActivity.java

  • Committer: Denis Solonenko
  • Date: 2012-04-15 14:38:26 UTC
  • Revision ID: denis.solonenko@gmail.com-20120415143826-ws7txou5y764cedk
Add extended total to blotter

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2012 Denis Solonenko.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the GNU Public License v2.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 
7
 */
 
8
 
 
9
package ru.orangesoftware.financisto.activity;
 
10
 
 
11
import android.os.AsyncTask;
 
12
import android.os.Bundle;
 
13
import android.view.View;
 
14
import android.view.Window;
 
15
import android.widget.Button;
 
16
import android.widget.LinearLayout;
 
17
import android.widget.TextView;
 
18
import ru.orangesoftware.financisto.R;
 
19
import ru.orangesoftware.financisto.model.Currency;
 
20
import ru.orangesoftware.financisto.model.Total;
 
21
import ru.orangesoftware.financisto.model.rates.ExchangeRate;
 
22
import ru.orangesoftware.financisto.model.rates.ExchangeRateProvider;
 
23
import ru.orangesoftware.financisto.utils.Utils;
 
24
 
 
25
import java.text.DecimalFormat;
 
26
import java.util.ArrayList;
 
27
import java.util.List;
 
28
 
 
29
import static ru.orangesoftware.financisto.activity.ExchangeRateActivity.formatRateDate;
 
30
 
 
31
/**
 
32
 * Created by IntelliJ IDEA.
 
33
 * User: denis.solonenko
 
34
 * Date: 3/15/12 16:40 PM
 
35
 */
 
36
public abstract class AbstractTotalsDetailsActivity extends AbstractActivity {
 
37
 
 
38
    private LinearLayout layout;
 
39
    private View calculatingNode;
 
40
    private Utils u;
 
41
    protected boolean shouldShowHomeCurrencyTotal = true;
 
42
 
 
43
    private final int titleNodeResId;
 
44
 
 
45
    protected AbstractTotalsDetailsActivity(int titleNodeResId) {
 
46
        this.titleNodeResId = titleNodeResId;
 
47
    }
 
48
 
 
49
    @Override
 
50
    protected void onCreate(Bundle savedInstanceState) {
 
51
        super.onCreate(savedInstanceState);
 
52
 
 
53
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
 
54
        setContentView(R.layout.totals_details);
 
55
 
 
56
        u = new Utils(this);
 
57
        layout = (LinearLayout)findViewById(R.id.list);
 
58
        calculatingNode = x.addTitleNodeNoDivider(layout, R.string.calculating);
 
59
 
 
60
        Button bOk = (Button)findViewById(R.id.bOK);
 
61
        bOk.setOnClickListener(new View.OnClickListener() {
 
62
            @Override
 
63
            public void onClick(View view) {
 
64
                finish();
 
65
            }
 
66
        });
 
67
 
 
68
        internalOnCreate();
 
69
        calculateTotals();
 
70
    }
 
71
 
 
72
    protected void internalOnCreate() {}
 
73
 
 
74
    private void calculateTotals() {
 
75
        CalculateAccountsTotalsTask task = new CalculateAccountsTotalsTask();
 
76
        task.execute();
 
77
    }
 
78
 
 
79
    @Override
 
80
    protected void onClick(View v, int id) {
 
81
    }
 
82
    
 
83
    private class CalculateAccountsTotalsTask extends AsyncTask<Void, Void, TotalsInfo> {
 
84
 
 
85
        private final DecimalFormat nf = new DecimalFormat("0.00000");
 
86
 
 
87
        @Override
 
88
        protected TotalsInfo doInBackground(Void... voids) {
 
89
            Total[] totals = getTotals();
 
90
            Total totalInHomeCurrency = getTotalInHomeCurrency();
 
91
            Currency homeCurrency = totalInHomeCurrency.currency;
 
92
            ExchangeRateProvider rates = db.getLatestRates();
 
93
            List<TotalInfo> result = new ArrayList<TotalInfo>();
 
94
            for (Total total : totals) {
 
95
                ExchangeRate rate = rates.getRate(total.currency, homeCurrency);
 
96
                TotalInfo info = new TotalInfo(total, rate);
 
97
                result.add(info);
 
98
            }
 
99
            return new TotalsInfo(result, totalInHomeCurrency);
 
100
        }
 
101
 
 
102
        @Override
 
103
        protected void onPostExecute(TotalsInfo totals) {
 
104
            calculatingNode.setVisibility(View.GONE);
 
105
            Currency homeCurrency = totals.totalInHomeCurrency.currency;
 
106
            for (TotalInfo total : totals.totals) {
 
107
                addAmountNode(total, homeCurrency);
 
108
            }
 
109
            if (shouldShowHomeCurrencyTotal) {
 
110
                addHomeCurrencyAmountNode(totals.totalInHomeCurrency);
 
111
            }
 
112
        }
 
113
 
 
114
        private void addAmountNode(TotalInfo total, Currency homeCurrency) {
 
115
            String title = getString(titleNodeResId, total.total.currency.name);
 
116
            x.addTitleNodeNoDivider(layout, title);
 
117
            TextView data = addAmountNode(total.total);
 
118
            String rateInfo = new StringBuilder().append("1").append(total.total.currency).append("=")
 
119
                    .append(nf.format(total.rate.rate)).append(homeCurrency)
 
120
                    .append(" (").append(formatRateDate(AbstractTotalsDetailsActivity.this, total.rate.date)).append(")").toString();
 
121
            data.setText(rateInfo);
 
122
        }
 
123
 
 
124
        private void addHomeCurrencyAmountNode(Total total) {
 
125
            x.addTitleNodeNoDivider(layout, getString(R.string.home_currency_total, total.currency.name));
 
126
            TextView data = addAmountNode(total);
 
127
            data.setText(R.string.home_currency);
 
128
        }
 
129
 
 
130
        private TextView addAmountNode(Total total) {
 
131
            TextView data = x.addInfoNode(layout, -1, "", "");
 
132
            View v = (View) data.getTag();
 
133
            TextView label = (TextView)v.findViewById(R.id.label);
 
134
            u.setAmountText(label, total.currency, total.balance, false);
 
135
            return data;
 
136
        }
 
137
    }
 
138
 
 
139
    protected abstract Total getTotalInHomeCurrency();
 
140
 
 
141
    protected abstract Total[] getTotals();
 
142
 
 
143
    private static class TotalInfo {
 
144
 
 
145
        public final Total total;
 
146
        public final ExchangeRate rate;
 
147
 
 
148
        public TotalInfo(Total total, ExchangeRate rate) {
 
149
            this.total = total;
 
150
            this.rate = rate;
 
151
        }
 
152
    }
 
153
    
 
154
    private static class TotalsInfo {
 
155
        
 
156
        public final List<TotalInfo> totals;
 
157
        public final Total totalInHomeCurrency;
 
158
 
 
159
        public TotalsInfo(List<TotalInfo> totals, Total totalInHomeCurrency) {
 
160
            this.totals = totals;
 
161
            this.totalInHomeCurrency = totalInHomeCurrency;
 
162
        }
 
163
 
 
164
    }
 
165
    
 
166
 
 
167
}