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

Commit d385db2

Browse files
author
Evgenii
authored
Merge pull request #139 from evgenii-kanivets/dev
Version 1.8.3[1h 30m]. Autocomplete category by title. Fixes.
2 parents f39c043 + e261dd3 commit d385db2

File tree

18 files changed

+195
-132
lines changed

18 files changed

+195
-132
lines changed

app/build.gradle

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,19 @@ apply plugin: 'com.getkeepsafe.dexcount'
6363
dependencies {
6464
compile fileTree(dir: 'libs', include: ['*.jar'])
6565

66-
// View annotation bindings
67-
// Dependency injection tool
68-
// Charts
69-
// Advanced logging tool
70-
71-
7266
compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
7367
transitive = true;
7468
}
69+
7570
compile 'com.android.support:support-v4:25.1.0'
7671
compile 'com.android.support:appcompat-v7:25.1.0'
7772
compile 'com.android.support:design:25.1.0'
78-
compile 'com.jakewharton:butterknife:7.0.1'
79-
compile 'com.google.dagger:dagger:2.0.1'
80-
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
81-
compile 'com.jakewharton.timber:timber:4.1.2'
73+
74+
compile 'com.jakewharton:butterknife:7.0.1' // View annotation bindings
75+
compile 'com.google.dagger:dagger:2.0.1' // Dependency injection tool
76+
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4' // Charts
77+
compile 'com.jakewharton.timber:timber:4.1.2' // Advanced logging tool
78+
8279
testCompile 'junit:junit:4.12'
8380
testCompile 'org.mockito:mockito-core:2.0.43-beta'
8481
androidTestCompile 'com.crittercism.dexmaker:dexmaker:1.4'

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.blogspot.e_kanivets.moneytracker.adapter.ExpandableListReportAdapter;
1212
import com.blogspot.e_kanivets.moneytracker.controller.CurrencyController;
1313
import com.blogspot.e_kanivets.moneytracker.controller.data.ExchangeRateController;
14+
import com.blogspot.e_kanivets.moneytracker.controller.data.RecordController;
1415
import com.blogspot.e_kanivets.moneytracker.entity.Period;
1516
import com.blogspot.e_kanivets.moneytracker.entity.data.Record;
1617
import com.blogspot.e_kanivets.moneytracker.report.record.RecordReportConverter;
@@ -29,8 +30,9 @@ public class ReportActivity extends BaseBackActivity {
2930
private static final String TAG = "ReportActivity";
3031

3132
public static final String KEY_PERIOD = "key_period";
32-
public static final String KEY_RECORD_LIST = "key_record_list";
3333

34+
@Inject
35+
RecordController recordController;
3436
@Inject
3537
ExchangeRateController rateController;
3638
@Inject
@@ -57,12 +59,11 @@ protected boolean initData() {
5759
super.initData();
5860
getAppComponent().inject(ReportActivity.this);
5961

60-
recordList = getIntent().getParcelableArrayListExtra(KEY_RECORD_LIST);
61-
if (recordList == null) return false;
62-
6362
period = getIntent().getParcelableExtra(KEY_PERIOD);
6463
if (period == null) return false;
6564

65+
recordList = recordController.getRecordsForPeriod(period);
66+
6667
return true;
6768
}
6869

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/AddAccountActivity.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.view.MenuItem;
66
import android.view.View;
77
import android.widget.ArrayAdapter;
8+
import android.widget.EditText;
89

910
import com.blogspot.e_kanivets.moneytracker.R;
1011
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
@@ -34,6 +35,10 @@ public class AddAccountActivity extends BaseBackActivity {
3435

3536
@Bind(R.id.content)
3637
View contentView;
38+
@Bind(R.id.et_title)
39+
EditText etTitle;
40+
@Bind(R.id.et_init_sum)
41+
EditText etInitSum;
3742
@Bind(R.id.spinner)
3843
AppCompatSpinner spinner;
3944

@@ -88,8 +93,13 @@ private void tryAddAccount() {
8893

8994
@SuppressWarnings("SimplifiableIfStatement")
9095
private boolean addAccount() {
91-
Account account = accountValidator.validate();
92-
if (account == null) return false;
93-
else return accountController.create(account) != null;
96+
if (accountValidator.validate()) {
97+
String title = etTitle.getText().toString().trim();
98+
double initSum = Double.parseDouble(etInitSum.getText().toString().trim());
99+
String currency = (String) spinner.getSelectedItem();
100+
return accountController.create(new Account(title, initSum, currency)) != null;
101+
} else {
102+
return false;
103+
}
94104
}
95105
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/TransferActivity.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.view.MenuItem;
66
import android.view.View;
77
import android.widget.ArrayAdapter;
8+
import android.widget.EditText;
89

910
import com.blogspot.e_kanivets.moneytracker.R;
1011
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
@@ -42,6 +43,10 @@ public class TransferActivity extends BaseBackActivity {
4243
AppCompatSpinner spinnerFrom;
4344
@Bind(R.id.spinner_to)
4445
AppCompatSpinner spinnerTo;
46+
@Bind(R.id.et_from_amount)
47+
EditText etFromAmount;
48+
@Bind(R.id.et_to_amount)
49+
EditText etToAmount;
4550

4651
@Override
4752
protected int getContentViewId() {
@@ -65,7 +70,7 @@ protected void initViews() {
6570
accounts.add(account.getTitle());
6671
}
6772

68-
transferValidator = new TransferValidator(TransferActivity.this, contentView, accountList);
73+
transferValidator = new TransferValidator(TransferActivity.this, contentView);
6974

7075
if (accounts.size() == 0) {
7176
accounts.add(getString(R.string.none));
@@ -109,8 +114,16 @@ private void tryTransfer() {
109114

110115
@SuppressWarnings("SimplifiableIfStatement")
111116
private boolean doTransfer() {
112-
Transfer transfer = transferValidator.validate();
113-
if (transfer == null) return false;
114-
else return transferController.create(transfer) != null;
117+
if (transferValidator.validate()) {
118+
Account fromAccount = accountList.get(spinnerFrom.getSelectedItemPosition());
119+
Account toAccount = accountList.get(spinnerTo.getSelectedItemPosition());
120+
double fromAmount = Double.parseDouble(etFromAmount.getText().toString());
121+
double toAmount = Double.parseDouble(etToAmount.getText().toString());
122+
123+
return transferController.create(new Transfer(System.currentTimeMillis(),
124+
fromAccount.getId(), toAccount.getId(), fromAmount, toAmount)) != null;
125+
} else {
126+
return false;
127+
}
115128
}
116129
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,16 @@ private void tryAddExchangeRate() {
136136

137137
@SuppressWarnings("SimplifiableIfStatement")
138138
private boolean addExchangeRate() {
139-
ExchangeRatePair pair = exchangeRatePairValidator.validate();
140-
if (pair == null) return false;
141-
else return exchangeRateController.createExchangeRatePair(pair) != null;
139+
if (exchangeRatePairValidator.validate()) {
140+
String fromCurrency = (String) spinnerFromCurrency.getSelectedItem();
141+
String toCurrency = (String) spinnerToCurrency.getSelectedItem();
142+
double amountBuy = Double.parseDouble(etBuy.getText().toString().trim());
143+
double amountSell = Double.parseDouble(etSell.getText().toString().trim());
144+
145+
return exchangeRateController.createExchangeRatePair(
146+
new ExchangeRatePair(fromCurrency, toCurrency, amountBuy, amountSell)) != null;
147+
} else {
148+
return false;
149+
}
142150
}
143151
}

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

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
2626
import com.blogspot.e_kanivets.moneytracker.adapter.CategoryAutoCompleteAdapter;
2727
import com.blogspot.e_kanivets.moneytracker.controller.FormatController;
28+
import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
2829
import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController;
2930
import com.blogspot.e_kanivets.moneytracker.controller.data.CategoryController;
3031
import com.blogspot.e_kanivets.moneytracker.controller.data.RecordController;
3132
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
33+
import com.blogspot.e_kanivets.moneytracker.entity.data.Category;
3234
import com.blogspot.e_kanivets.moneytracker.entity.data.Record;
3335
import com.blogspot.e_kanivets.moneytracker.ui.AddRecordUiDecorator;
3436
import com.blogspot.e_kanivets.moneytracker.util.AnswersProxy;
@@ -75,9 +77,12 @@ public class AddRecordActivity extends BaseBackActivity {
7577
AccountController accountController;
7678
@Inject
7779
FormatController formatController;
80+
@Inject
81+
PreferenceController preferenceController;
7882

7983
private IValidator<Record> recordValidator;
8084
private AddRecordUiDecorator uiDecorator;
85+
private CategoryAutoCompleter autoCompleter;
8186

8287
@Bind(R.id.content)
8388
View contentView;
@@ -124,9 +129,8 @@ record = getIntent().getParcelableExtra(KEY_RECORD);
124129
protected void initViews() {
125130
super.initViews();
126131

127-
long recordId = record == null ? -1 : record.getId();
128-
recordValidator = new RecordValidator(AddRecordActivity.this, contentView, mode,
129-
accountList, timestamp, type, recordId);
132+
recordValidator = new RecordValidator(AddRecordActivity.this, contentView);
133+
autoCompleter = new CategoryAutoCompleter(categoryController, preferenceController);
130134

131135
// Add texts to dialog if it's edit dialog
132136
if (mode == Mode.MODE_EDIT) {
@@ -138,8 +142,10 @@ protected void initViews() {
138142
uiDecorator.decorateActionBar(getSupportActionBar(), mode, type);
139143
presentSpinnerAccount();
140144

141-
etCategory.setAdapter(new CategoryAutoCompleteAdapter(AddRecordActivity.this,
142-
R.layout.view_category_item, new CategoryAutoCompleter(categoryController)));
145+
146+
final CategoryAutoCompleteAdapter categoryAutoCompleteAdapter = new CategoryAutoCompleteAdapter(
147+
AddRecordActivity.this, R.layout.view_category_item, autoCompleter);
148+
etCategory.setAdapter(categoryAutoCompleteAdapter);
143149
etCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
144150
@Override
145151
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@@ -154,6 +160,21 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
154160
return false;
155161
}
156162
});
163+
etCategory.setOnFocusChangeListener(new View.OnFocusChangeListener() {
164+
@Override
165+
public void onFocusChange(View view, boolean hasFocus) {
166+
if (hasFocus && etCategory.getText().toString().trim().isEmpty()) {
167+
String title = etTitle.getText().toString().trim();
168+
String prediction = autoCompleter.completeByRecordTitle(title);
169+
if (prediction != null) {
170+
etCategory.setAdapter(null);
171+
etCategory.setText(prediction);
172+
etCategory.selectAll();
173+
etCategory.setAdapter(categoryAutoCompleteAdapter);
174+
}
175+
}
176+
}
177+
});
157178

158179
// Restrict ';' for input, because it's used as delimiter when exporting
159180
etTitle.setFilters(new InputFilter[]{new SemicolonInputFilter()});
@@ -306,14 +327,41 @@ private void tryRecord() {
306327

307328
@SuppressWarnings("SimplifiableIfStatement")
308329
private boolean addRecord() {
309-
Record newRecord = recordValidator.validate();
310-
if (newRecord == null) return false;
311-
else {
330+
if (recordValidator.validate()) {
331+
long now = new Date().getTime();
332+
if (timestamp > now) {
333+
showToast(R.string.record_in_future);
334+
return false;
335+
}
336+
337+
String title = etTitle.getText().toString().trim();
338+
String category = etCategory.getText().toString().trim();
339+
double price = Double.parseDouble(etPrice.getText().toString());
340+
Account account = accountList.get(spinnerAccount.getSelectedItemPosition());
341+
342+
if (title.isEmpty()) {
343+
title = category;
344+
}
345+
346+
Record newRecord = null;
347+
312348
if (mode == Mode.MODE_ADD) {
313-
return recordController.create(newRecord) != null;
349+
newRecord = recordController.create(new Record(timestamp, type, title,
350+
new Category(category), price, account, account.getCurrency()));
314351
} else if (mode == Mode.MODE_EDIT) {
315-
return recordController.update(newRecord) != null;
316-
} else return false;
352+
long recordId = record == null ? -1 : record.getId();
353+
newRecord = recordController.update(new Record(recordId, timestamp, type, title,
354+
new Category(category), price, account, account.getCurrency()));
355+
}
356+
357+
if (newRecord == null) {
358+
return false;
359+
} else {
360+
autoCompleter.addRecordTitleCategoryPair(title, category);
361+
return true;
362+
}
363+
} else {
364+
return false;
317365
}
318366
}
319367

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/MainActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ public void showReport() {
147147
AnswersProxy.get().logButton("Show Report");
148148
Intent intent = new Intent(MainActivity.this, ReportActivity.class);
149149
intent.putExtra(ReportActivity.KEY_PERIOD, period);
150-
intent.putExtra(ReportActivity.KEY_RECORD_LIST, (ArrayList<Record>) recordList);
151150
startActivity(intent);
152151
}
153152

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/PreferenceController.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import com.blogspot.e_kanivets.moneytracker.R;
1010

1111
import java.util.HashSet;
12+
import java.util.Map;
1213
import java.util.Set;
14+
import java.util.TreeMap;
15+
import java.util.TreeSet;
1316

1417
/**
1518
* Controller class to encapsulate Shared Preferences handling logic.
@@ -26,6 +29,7 @@ public class PreferenceController {
2629
private static final String KEY_PERIOD_TYPE = "key_period_type";
2730
private static final String KEY_DROPBOX_ACCESS_TOKEN = "key_dropbox_access_token";
2831
private static final String KEY_FILTERED_CATEGORIES = "key_filtered_categories";
32+
private static final String KEY_RECORD_TITLE_CATEGORY_PAIRS = "key_record_title_category_pairs";
2933

3034
private static final int RATE_PERIOD = 5;
3135

@@ -91,6 +95,17 @@ public void writeFilteredCategories(@Nullable Set<String> categorySet) {
9195
editor.apply();
9296
}
9397

98+
public void writeRecordTitleCategoryPairs(@NonNull Map<String, String> map) {
99+
Set<String> set = new TreeSet<>();
100+
for (String key : map.keySet()) {
101+
set.add(key + ";" + map.get(key));
102+
}
103+
104+
SharedPreferences.Editor editor = getEditor();
105+
editor.putStringSet(KEY_RECORD_TITLE_CATEGORY_PAIRS, set);
106+
editor.apply();
107+
}
108+
94109
public long readDefaultAccountId() {
95110
String defaultAccountPref = context.getString(R.string.pref_default_account);
96111
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
@@ -138,6 +153,22 @@ public Set<String> readFilteredCategories() {
138153
return new HashSet<>(getDefaultPrefs().getStringSet(KEY_FILTERED_CATEGORIES, new HashSet<String>()));
139154
}
140155

156+
@NonNull
157+
public Map<String, String> readRecordTitleCategoryPairs() {
158+
Map<String, String> map = new TreeMap<>();
159+
160+
Set<String> set = getDefaultPrefs().getStringSet(KEY_RECORD_TITLE_CATEGORY_PAIRS,
161+
new HashSet<String>());
162+
for (String entry : set) {
163+
String[] words = entry.split(";");
164+
if (words.length == 2) {
165+
map.put(words[0], words[1]);
166+
}
167+
}
168+
169+
return map;
170+
}
171+
141172
@NonNull
142173
private SharedPreferences getDefaultPrefs() {
143174
return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);

0 commit comments

Comments
 (0)