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

Commit c4d89ff

Browse files
author
Evgenii Kanivets
committed
#33[30m]. Resolved issue with removed account on 'Edit record' screen.
1 parent d2affad commit c4d89ff

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/AddRecordActivity.java

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.annotation.SuppressLint;
44
import android.graphics.drawable.ColorDrawable;
55
import android.os.Build;
6+
import android.support.annotation.Nullable;
67
import android.view.Menu;
78
import android.view.MenuItem;
89
import android.view.Window;
@@ -91,30 +92,15 @@ record = getIntent().getParcelableExtra(KEY_RECORD);
9192
protected void initViews() {
9293
super.initViews();
9394

94-
List<String> accounts = new ArrayList<>();
95-
for (Account account : accountList) {
96-
accounts.add(account.getTitle());
97-
}
98-
99-
spinnerAccount.setAdapter(new ArrayAdapter<>(AddRecordActivity.this,
100-
android.R.layout.simple_list_item_1, accounts));
101-
10295
//Add texts to dialog if it's edit dialog
10396
if (mode == Mode.MODE_EDIT) {
10497
etTitle.setText(record.getTitle());
10598
if (record.getCategory() != null) etCategory.setText(record.getCategory().getName());
10699
etPrice.setText(Integer.toString(record.getPrice()));
107-
108-
if (record.getAccount() != null) {
109-
for (int i = 0; i < accountList.size(); i++) {
110-
Account account = accountList.get(i);
111-
if (account.getId() == record.getAccount().getId()) {
112-
spinnerAccount.setSelection(i);
113-
}
114-
}
115-
}
116100
}
117101

102+
presentSpinnerAccount();
103+
118104
if (getSupportActionBar() != null) {
119105
switch (type) {
120106
case Record.TYPE_EXPENSE:
@@ -170,6 +156,38 @@ public boolean onOptionsItemSelected(MenuItem item) {
170156
}
171157
}
172158

159+
private void presentSpinnerAccount() {
160+
List<String> accounts = new ArrayList<>();
161+
for (Account account : accountList) {
162+
accounts.add(account.getTitle());
163+
}
164+
165+
int selectedAccountIndex = 0;
166+
167+
if (mode == Mode.MODE_EDIT) {
168+
selectedAccountIndex = -1;
169+
170+
if (record.getAccount() != null) {
171+
for (int i = 0; i < accountList.size(); i++) {
172+
Account account = accountList.get(i);
173+
if (account.getId() == record.getAccount().getId()) selectedAccountIndex = i;
174+
}
175+
}
176+
177+
if (selectedAccountIndex == -1) {
178+
selectedAccountIndex = 0;
179+
spinnerAccount.setEnabled(false);
180+
181+
accounts = new ArrayList<>();
182+
accounts.add(getString(R.string.account_was_removed));
183+
}
184+
}
185+
186+
spinnerAccount.setAdapter(new ArrayAdapter<>(AddRecordActivity.this,
187+
android.R.layout.simple_list_item_1, accounts));
188+
spinnerAccount.setSelection(selectedAccountIndex);
189+
}
190+
173191
private boolean prepareRecord() {
174192
String title = etTitle.getText().toString().trim();
175193
String category = etCategory.getText().toString().trim();
@@ -184,12 +202,17 @@ private boolean prepareRecord() {
184202
}
185203

186204
if (price >= 0 && price <= 1000000000 && spinnerAccount.getSelectedItemPosition() >= 0) {
187-
Account account = accountList.get(spinnerAccount.getSelectedItemPosition());
205+
Account account = null;
206+
if (spinnerAccount.isEnabled())
207+
account = accountList.get(spinnerAccount.getSelectedItemPosition());
208+
188209
return doRecord(title, category, price, account);
189210
} else return false;
190211
}
191212

192-
protected boolean doRecord(String title, String category, int price, Account account) {
213+
private boolean doRecord(String title, String category, int price, @Nullable Account account) {
214+
if (account == null) return false;
215+
193216
if (mode == Mode.MODE_ADD) {
194217
switch (type) {
195218
case Record.TYPE_EXPENSE:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@
5151
<string name="cant_make_report">Can\'t make a report</string>
5252
<string name="rates_needed">Next exchange rates are needed:</string>
5353
<string name="arrow"><![CDATA[" -> "]]></string>
54+
<string name="account_was_removed">Account was removed</string>
5455

5556
</resources>

0 commit comments

Comments
 (0)