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
9
package ru.orangesoftware.financisto.activity;
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;
25
import java.text.DecimalFormat;
26
import java.util.ArrayList;
27
import java.util.List;
29
import static ru.orangesoftware.financisto.activity.ExchangeRateActivity.formatRateDate;
32
* Created by IntelliJ IDEA.
33
* User: denis.solonenko
34
* Date: 3/15/12 16:40 PM
36
public abstract class AbstractTotalsDetailsActivity extends AbstractActivity {
38
private LinearLayout layout;
39
private View calculatingNode;
41
protected boolean shouldShowHomeCurrencyTotal = true;
43
private final int titleNodeResId;
45
protected AbstractTotalsDetailsActivity(int titleNodeResId) {
46
this.titleNodeResId = titleNodeResId;
50
protected void onCreate(Bundle savedInstanceState) {
51
super.onCreate(savedInstanceState);
53
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
54
setContentView(R.layout.totals_details);
57
layout = (LinearLayout)findViewById(R.id.list);
58
calculatingNode = x.addTitleNodeNoDivider(layout, R.string.calculating);
60
Button bOk = (Button)findViewById(R.id.bOK);
61
bOk.setOnClickListener(new View.OnClickListener() {
63
public void onClick(View view) {
72
protected void internalOnCreate() {}
74
private void calculateTotals() {
75
CalculateAccountsTotalsTask task = new CalculateAccountsTotalsTask();
80
protected void onClick(View v, int id) {
83
private class CalculateAccountsTotalsTask extends AsyncTask<Void, Void, TotalsInfo> {
85
private final DecimalFormat nf = new DecimalFormat("0.00000");
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);
99
return new TotalsInfo(result, totalInHomeCurrency);
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);
109
if (shouldShowHomeCurrencyTotal) {
110
addHomeCurrencyAmountNode(totals.totalInHomeCurrency);
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);
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);
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);
139
protected abstract Total getTotalInHomeCurrency();
141
protected abstract Total[] getTotals();
143
private static class TotalInfo {
145
public final Total total;
146
public final ExchangeRate rate;
148
public TotalInfo(Total total, ExchangeRate rate) {
154
private static class TotalsInfo {
156
public final List<TotalInfo> totals;
157
public final Total totalInHomeCurrency;
159
public TotalsInfo(List<TotalInfo> totals, Total totalInHomeCurrency) {
160
this.totals = totals;
161
this.totalInHomeCurrency = totalInHomeCurrency;