Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 5691331

Browse files
author
Evgenii Kanivets
committed
#94[1h]. Update AddExchangeRate screen to input ExchangeRate pair.
1 parent fb68496 commit 5691331

File tree

7 files changed

+166
-60
lines changed

7 files changed

+166
-60
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/exchange_rate/AddExchangeRateActivity.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import com.blogspot.e_kanivets.moneytracker.R;
1111
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
1212
import com.blogspot.e_kanivets.moneytracker.controller.CurrencyController;
13+
import com.blogspot.e_kanivets.moneytracker.controller.FormatController;
1314
import com.blogspot.e_kanivets.moneytracker.controller.data.ExchangeRateController;
15+
import com.blogspot.e_kanivets.moneytracker.entity.ExchangeRatePair;
1416
import com.blogspot.e_kanivets.moneytracker.entity.data.ExchangeRate;
1517

1618
import java.util.ArrayList;
@@ -30,17 +32,21 @@ public class AddExchangeRateActivity extends BaseBackActivity {
3032
ExchangeRateController exchangeRateController;
3133
@Inject
3234
CurrencyController currencyController;
35+
@Inject
36+
FormatController formatController;
3337

3438
// This field passed from Intent and may be used for presetting from/to spinner values
3539
@Nullable
36-
private ExchangeRate exchangeRate;
40+
private ExchangeRatePair exchangeRatePair;
3741

3842
@Bind(R.id.spinner_from_currency)
3943
AppCompatSpinner spinnerFromCurrency;
4044
@Bind(R.id.spinner_to_currency)
4145
AppCompatSpinner spinnerToCurrency;
42-
@Bind(R.id.et_amount)
43-
EditText etAmount;
46+
@Bind(R.id.et_buy)
47+
EditText etBuy;
48+
@Bind(R.id.et_sell)
49+
EditText etSell;
4450

4551
@Override
4652
protected int getContentViewId() {
@@ -52,7 +58,7 @@ protected boolean initData() {
5258
boolean result = super.initData();
5359
getAppComponent().inject(AddExchangeRateActivity.this);
5460

55-
exchangeRate = getIntent().getParcelableExtra(KEY_EXCHANGE_RATE);
61+
exchangeRatePair = getIntent().getParcelableExtra(KEY_EXCHANGE_RATE);
5662

5763
return result;
5864
}
@@ -63,23 +69,26 @@ protected void initViews() {
6369
List<String> currencyList = currencyController.readAll();
6470

6571
spinnerFromCurrency.setAdapter(new ArrayAdapter<>(AddExchangeRateActivity.this,
66-
android.R.layout.simple_list_item_1,
72+
R.layout.view_spinner_item,
6773
new ArrayList<>(currencyList)));
6874

6975
spinnerToCurrency.setAdapter(new ArrayAdapter<>(AddExchangeRateActivity.this,
70-
android.R.layout.simple_list_item_1,
76+
R.layout.view_spinner_item,
7177
new ArrayList<>(currencyList)));
7278

7379
// Set selections from passed ExchangeRate
74-
if (exchangeRate != null) {
80+
if (exchangeRatePair != null) {
7581
for (int i = 0; i < currencyList.size(); i++) {
76-
if (currencyList.get(i).equals(exchangeRate.getFromCurrency())) {
82+
if (currencyList.get(i).equals(exchangeRatePair.getFromCurrency())) {
7783
spinnerFromCurrency.setSelection(i);
7884
}
79-
if (currencyList.get(i).equals(exchangeRate.getToCurrency())) {
85+
if (currencyList.get(i).equals(exchangeRatePair.getToCurrency())) {
8086
spinnerToCurrency.setSelection(i);
8187
}
8288
}
89+
90+
etBuy.setText(formatController.formatPrecisionNone(exchangeRatePair.getAmountBuy()));
91+
etSell.setText(formatController.formatPrecisionNone(exchangeRatePair.getAmountSell()));
8392
}
8493
}
8594

@@ -107,21 +116,30 @@ public boolean onOptionsItemSelected(MenuItem item) {
107116
private boolean addExchangeRate() {
108117
String fromCurrency = (String) spinnerFromCurrency.getSelectedItem();
109118
String toCurrency = (String) spinnerToCurrency.getSelectedItem();
110-
double amount = -1;
119+
double amountBuy = -1;
120+
double amountSell = -1;
111121

112122
try {
113-
amount = Double.parseDouble(etAmount.getText().toString().trim());
123+
amountBuy = Double.parseDouble(etBuy.getText().toString().trim());
124+
} catch (Exception e) {
125+
e.printStackTrace();
126+
}
127+
try {
128+
amountSell = Double.parseDouble(etSell.getText().toString().trim());
114129
} catch (Exception e) {
115130
e.printStackTrace();
116131
}
117132

118-
if (amount == -1) return false;
133+
if (amountBuy == -1 || amountSell == -1) return false;
119134

120135
ExchangeRate exchangeRate = new ExchangeRate(System.currentTimeMillis(),
121-
fromCurrency, toCurrency, amount);
136+
fromCurrency, toCurrency, amountBuy);
137+
ExchangeRate exchangeRateReverse = new ExchangeRate(System.currentTimeMillis(),
138+
toCurrency, fromCurrency, 1 / amountSell);
122139

123140
ExchangeRate createdRate = exchangeRateController.create(exchangeRate);
141+
ExchangeRate createdReverseRate = exchangeRateController.create(exchangeRateReverse);
124142

125-
return createdRate != null;
143+
return createdRate != null || createdReverseRate != null;
126144
}
127145
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/exchange_rate/ExchangeRatesActivity.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.blogspot.e_kanivets.moneytracker.adapter.ExchangeRateAdapter;
1515
import com.blogspot.e_kanivets.moneytracker.controller.data.ExchangeRateController;
1616
import com.blogspot.e_kanivets.moneytracker.entity.ExchangeRatePair;
17-
import com.blogspot.e_kanivets.moneytracker.entity.data.ExchangeRate;
1817
import com.blogspot.e_kanivets.moneytracker.util.ExchangeRatesSummarizer;
1918

2019
import java.util.Collections;
@@ -72,7 +71,7 @@ public boolean onContextItemSelected(MenuItem item) {
7271

7372
switch (item.getItemId()) {
7473
case R.id.delete:
75-
//rateController.delete(exchangeRateList.get(info.position));
74+
rateController.deleteExchangeRatePair(exchangeRateList.get(info.position));
7675
update();
7776
setResult(RESULT_OK);
7877
return true;
@@ -91,7 +90,7 @@ public void addExchangeRate() {
9190
public void addExchangeRateOnBaseOfExisted(int position) {
9291
if (position < 0 || position >= exchangeRateList.size()) return;
9392
Intent intent = new Intent(ExchangeRatesActivity.this, AddExchangeRateActivity.class);
94-
//intent.putExtra(AddExchangeRateActivity.KEY_EXCHANGE_RATE, exchangeRateList.get(position));
93+
intent.putExtra(AddExchangeRateActivity.KEY_EXCHANGE_RATE, exchangeRateList.get(position));
9594
startActivityForResult(intent, REQUEST_ADD_EXCHANGE_RATE);
9695
}
9796

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/base/BaseController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.support.annotation.NonNull;
44
import android.support.annotation.Nullable;
55

6+
import com.blogspot.e_kanivets.moneytracker.entity.ExchangeRatePair;
67
import com.blogspot.e_kanivets.moneytracker.repo.base.IRepo;
78

89
import java.util.List;

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/ExchangeRateController.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.blogspot.e_kanivets.moneytracker.controller.data;
22

3+
import android.support.annotation.Nullable;
4+
35
import com.blogspot.e_kanivets.moneytracker.controller.base.BaseController;
6+
import com.blogspot.e_kanivets.moneytracker.entity.ExchangeRatePair;
47
import com.blogspot.e_kanivets.moneytracker.entity.data.ExchangeRate;
58
import com.blogspot.e_kanivets.moneytracker.repo.base.IRepo;
69

10+
import java.util.ArrayList;
11+
import java.util.List;
12+
713
/**
814
* Controller class to encapsulate exchange rates handling logic.
915
* Created on 2/23/16.
@@ -14,4 +20,22 @@ public class ExchangeRateController extends BaseController<ExchangeRate> {
1420
public ExchangeRateController(IRepo<ExchangeRate> repo) {
1521
super(repo);
1622
}
17-
}
23+
24+
public void deleteExchangeRatePair(@Nullable ExchangeRatePair pair) {
25+
if (pair == null) return;
26+
27+
List<ExchangeRate> rateListToRemove = new ArrayList<>();
28+
for (ExchangeRate rate : readAll()) {
29+
if (rate.getFromCurrency().equals(pair.getFromCurrency())
30+
&& rate.getToCurrency().equals(pair.getToCurrency()))
31+
rateListToRemove.add(rate);
32+
if (rate.getFromCurrency().equals(pair.getToCurrency())
33+
&& rate.getToCurrency().equals(pair.getFromCurrency()))
34+
rateListToRemove.add(rate);
35+
}
36+
37+
for (ExchangeRate rate : rateListToRemove) {
38+
delete(rate);
39+
}
40+
}
41+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/ExchangeRatePair.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
package com.blogspot.e_kanivets.moneytracker.entity;
22

3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
36
/**
47
* Not data entity that's used for {@link com.blogspot.e_kanivets.moneytracker.adapter.ExchangeRateAdapter}.
58
* Created on 7/13/16.
69
*
710
* @author Evgenii Kanivets
811
*/
9-
public class ExchangeRatePair {
12+
public class ExchangeRatePair implements Parcelable {
1013
private String fromCurrency;
1114
private String toCurrency;
1215
private double amountBuy;
1316
private double amountSell;
1417

15-
public ExchangeRatePair(String fromCurrency, String toCurrency, double amountBuy, double amountSell) {
16-
this.fromCurrency = fromCurrency;
17-
this.toCurrency = toCurrency;
18-
this.amountBuy = amountBuy;
19-
this.amountSell = amountSell;
18+
public ExchangeRatePair() {
2019
}
2120

22-
public ExchangeRatePair() {
21+
protected ExchangeRatePair(Parcel in) {
22+
fromCurrency = in.readString();
23+
toCurrency = in.readString();
24+
amountBuy = in.readDouble();
25+
amountSell = in.readDouble();
2326
}
2427

28+
public static final Creator<ExchangeRatePair> CREATOR = new Creator<ExchangeRatePair>() {
29+
@Override
30+
public ExchangeRatePair createFromParcel(Parcel in) {
31+
return new ExchangeRatePair(in);
32+
}
33+
34+
@Override
35+
public ExchangeRatePair[] newArray(int size) {
36+
return new ExchangeRatePair[size];
37+
}
38+
};
39+
2540
public String getFromCurrency() {
2641
return fromCurrency;
2742
}
@@ -53,4 +68,17 @@ public void setAmountBuy(double amountBuy) {
5368
public void setAmountSell(double amountSell) {
5469
this.amountSell = amountSell;
5570
}
71+
72+
@Override
73+
public int describeContents() {
74+
return 0;
75+
}
76+
77+
@Override
78+
public void writeToParcel(Parcel dest, int flags) {
79+
dest.writeString(fromCurrency);
80+
dest.writeString(toCurrency);
81+
dest.writeDouble(amountBuy);
82+
dest.writeDouble(amountSell);
83+
}
5684
}

app/src/main/res/layout/activity_add_exchange_rate.xml

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,84 @@
2222

2323
<LinearLayout
2424
android:layout_width="match_parent"
25-
android:layout_height="wrap_content"
25+
android:layout_height="match_parent"
26+
android:baselineAligned="false"
2627
android:orientation="horizontal"
27-
android:padding="@dimen/activity_vertical_margin"
28+
android:padding="@dimen/activity_horizontal_margin"
2829
app:layout_behavior="@string/appbar_scrolling_view_behavior">
2930

30-
<TextView
31-
android:layout_width="wrap_content"
31+
<LinearLayout
32+
android:layout_width="0dp"
3233
android:layout_height="wrap_content"
33-
android:gravity="center"
34-
android:text="1" />
34+
android:layout_marginEnd="@dimen/half_margin"
35+
android:layout_weight="1"
36+
android:orientation="vertical">
3537

36-
<android.support.v7.widget.AppCompatSpinner
37-
android:id="@+id/spinner_from_currency"
38-
android:layout_width="wrap_content"
39-
android:layout_height="wrap_content"
40-
android:layout_gravity="center_vertical"
41-
android:gravity="center|end" />
38+
<TextView
39+
android:layout_width="wrap_content"
40+
android:layout_height="wrap_content"
41+
android:layout_gravity="start"
42+
android:text="@string/convert_from"
43+
android:theme="@style/Text_Hint" />
4244

43-
<TextView
44-
android:layout_width="match_parent"
45-
android:layout_height="wrap_content"
46-
android:layout_weight="1"
47-
android:gravity="center"
48-
android:text="=" />
45+
<android.support.v7.widget.AppCompatSpinner
46+
android:id="@+id/spinner_from_currency"
47+
android:layout_width="match_parent"
48+
android:layout_height="@dimen/spinner_height"
49+
tools:listitem="@layout/view_spinner_item" />
4950

50-
<EditText
51-
android:id="@+id/et_amount"
52-
android:layout_width="100dp"
53-
android:layout_height="wrap_content"
54-
android:gravity="center"
55-
android:inputType="numberDecimal"
56-
android:maxLines="1" />
51+
<android.support.design.widget.TextInputLayout
52+
android:layout_width="match_parent"
53+
android:layout_height="wrap_content">
5754

58-
<TextView
59-
android:layout_width="match_parent"
55+
<EditText
56+
android:id="@+id/et_buy"
57+
android:layout_width="match_parent"
58+
android:layout_height="wrap_content"
59+
android:ems="10"
60+
android:hint="@string/bank_buys"
61+
android:inputType="numberDecimal"
62+
android:maxLines="1"
63+
android:nextFocusDown="@+id/et_to_amount"
64+
android:singleLine="true" />
65+
</android.support.design.widget.TextInputLayout>
66+
</LinearLayout>
67+
68+
<LinearLayout
69+
android:layout_width="0dp"
6070
android:layout_height="wrap_content"
71+
android:layout_marginStart="@dimen/half_margin"
6172
android:layout_weight="1"
62-
android:gravity="center"
63-
android:text="*" />
73+
android:orientation="vertical">
6474

65-
<android.support.v7.widget.AppCompatSpinner
66-
android:id="@+id/spinner_to_currency"
67-
android:layout_width="wrap_content"
68-
android:layout_height="wrap_content"
69-
android:layout_gravity="center_vertical"
70-
android:gravity="center|start" />
75+
<TextView
76+
android:layout_width="wrap_content"
77+
android:layout_height="wrap_content"
78+
android:layout_gravity="start"
79+
android:text="@string/convert_to"
80+
android:theme="@style/Text_Hint" />
81+
82+
<android.support.v7.widget.AppCompatSpinner
83+
android:id="@+id/spinner_to_currency"
84+
android:layout_width="match_parent"
85+
android:layout_height="@dimen/spinner_height"
86+
tools:listitem="@layout/view_spinner_item" />
87+
88+
<android.support.design.widget.TextInputLayout
89+
android:layout_width="match_parent"
90+
android:layout_height="wrap_content">
91+
92+
<EditText
93+
android:id="@+id/et_sell"
94+
android:layout_width="match_parent"
95+
android:layout_height="wrap_content"
96+
android:ems="10"
97+
android:hint="@string/bank_sells"
98+
android:inputType="numberDecimal"
99+
android:maxLines="1"
100+
android:singleLine="true" />
101+
</android.support.design.widget.TextInputLayout>
102+
</LinearLayout>
71103
</LinearLayout>
72104

73105
</android.support.design.widget.CoordinatorLayout>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
<b>Example:</b> 1466948795712;metro;transport;-20.0;UAH</string>
8686

8787
<string name="slash" translatable="false">/</string>
88+
<string name="bank_sells">Bank sells</string>
89+
<string name="convert_to">Convert to</string>
90+
<string name="bank_buys">Bank buys</string>
91+
<string name="convert_from">Convert from</string>
8892

8993
<!-- Not translated strings -->
9094
</resources>

0 commit comments

Comments
 (0)