Skip to content

Commit e261dd3

Browse files
author
Evgenii Kanivets
committed
yev-kanivets#67[30m]. Autocomplete category by title. Fix bug with passing too large recordList to ReportActivity.
1 parent 8854d40 commit e261dd3

File tree

5 files changed

+104
-14
lines changed

5 files changed

+104
-14
lines changed

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/record/AddRecordActivity.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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;
@@ -76,9 +77,12 @@ public class AddRecordActivity extends BaseBackActivity {
7677
AccountController accountController;
7778
@Inject
7879
FormatController formatController;
80+
@Inject
81+
PreferenceController preferenceController;
7982

8083
private IValidator<Record> recordValidator;
8184
private AddRecordUiDecorator uiDecorator;
85+
private CategoryAutoCompleter autoCompleter;
8286

8387
@Bind(R.id.content)
8488
View contentView;
@@ -126,6 +130,7 @@ protected void initViews() {
126130
super.initViews();
127131

128132
recordValidator = new RecordValidator(AddRecordActivity.this, contentView);
133+
autoCompleter = new CategoryAutoCompleter(categoryController, preferenceController);
129134

130135
// Add texts to dialog if it's edit dialog
131136
if (mode == Mode.MODE_EDIT) {
@@ -137,8 +142,10 @@ protected void initViews() {
137142
uiDecorator.decorateActionBar(getSupportActionBar(), mode, type);
138143
presentSpinnerAccount();
139144

140-
etCategory.setAdapter(new CategoryAutoCompleteAdapter(AddRecordActivity.this,
141-
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);
142149
etCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
143150
@Override
144151
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@@ -153,6 +160,21 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
153160
return false;
154161
}
155162
});
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+
});
156178

157179
// Restrict ';' for input, because it's used as delimiter when exporting
158180
etTitle.setFilters(new InputFilter[]{new SemicolonInputFilter()});
@@ -321,14 +343,23 @@ private boolean addRecord() {
321343
title = category;
322344
}
323345

346+
Record newRecord = null;
347+
324348
if (mode == Mode.MODE_ADD) {
325-
return recordController.create(new Record(timestamp, type, title,
326-
new Category(category), price, account, account.getCurrency())) != null;
349+
newRecord = recordController.create(new Record(timestamp, type, title,
350+
new Category(category), price, account, account.getCurrency()));
327351
} else if (mode == Mode.MODE_EDIT) {
328352
long recordId = record == null ? -1 : record.getId();
329-
return recordController.update(new Record(recordId, timestamp, type, title,
330-
new Category(category), price, account, account.getCurrency())) != null;
331-
} else return false;
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+
}
332363
} else {
333364
return false;
334365
}

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);

app/src/main/java/com/blogspot/e_kanivets/moneytracker/util/CategoryAutoCompleter.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.blogspot.e_kanivets.moneytracker.util;
22

3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
6+
import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
37
import com.blogspot.e_kanivets.moneytracker.controller.data.CategoryController;
48
import com.blogspot.e_kanivets.moneytracker.entity.data.Category;
59

610
import java.util.ArrayList;
711
import java.util.List;
12+
import java.util.Map;
813

914
/**
1015
* Util class to encapsulate category autocomplete logic.
@@ -13,12 +18,22 @@
1318
* @author Evgenii Kanivets
1419
*/
1520
public class CategoryAutoCompleter {
21+
1622
private List<String> categoryList;
17-
private CategoryController categoryController;
23+
private Map<String, String> recordTitleCategoryMap;
24+
25+
@NonNull
26+
private final CategoryController categoryController;
27+
@NonNull
28+
private final PreferenceController preferenceController;
1829

19-
public CategoryAutoCompleter(CategoryController categoryController) {
30+
public CategoryAutoCompleter(@NonNull CategoryController categoryController,
31+
@NonNull PreferenceController preferenceController) {
2032
this.categoryController = categoryController;
33+
this.preferenceController = preferenceController;
34+
2135
categoryList = new ArrayList<>();
36+
recordTitleCategoryMap = preferenceController.readRecordTitleCategoryPairs();
2237

2338
for (Category category : categoryController.readFiltered()) {
2439
categoryList.add(category.getName());
@@ -39,4 +54,17 @@ public void removeFromAutoComplete(String category) {
3954
categoryList.remove(category);
4055
categoryController.disableCategory(category);
4156
}
57+
58+
@Nullable
59+
public String completeByRecordTitle(String title) {
60+
return recordTitleCategoryMap.get(title);
61+
}
62+
63+
public void addRecordTitleCategoryPair(String title, String category) {
64+
if (title.isEmpty() || category.isEmpty()) {
65+
return;
66+
}
67+
recordTitleCategoryMap.put(title, category);
68+
preferenceController.writeRecordTitleCategoryPairs(recordTitleCategoryMap);
69+
}
4270
}

0 commit comments

Comments
 (0)