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

Commit e64c870

Browse files
author
Evgenii Kanivets
committed
#69[1h]. Add import feature.
1 parent c8ae2fc commit e64c870

File tree

15 files changed

+236
-19
lines changed

15 files changed

+236
-19
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
android:label="@string/title_charts"
7474
android:screenOrientation="portrait"
7575
android:theme="@style/Theme.Default" />
76+
<activity
77+
android:name=".activity.ImportActivity"
78+
android:label="@string/title_import"
79+
android:screenOrientation="portrait"
80+
android:theme="@style/Theme.Default" />
7681

7782
<provider
7883
android:name="android.support.v4.content.FileProvider"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import com.blogspot.e_kanivets.moneytracker.R;
99
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
10-
import com.blogspot.e_kanivets.moneytracker.controller.ExportController;
10+
import com.blogspot.e_kanivets.moneytracker.controller.external.ExportController;
1111

1212
import java.io.File;
1313
import java.io.FileNotFoundException;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.blogspot.e_kanivets.moneytracker.activity;
2+
3+
import android.widget.EditText;
4+
5+
import com.blogspot.e_kanivets.moneytracker.R;
6+
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
7+
import com.blogspot.e_kanivets.moneytracker.controller.external.ImportController;
8+
import com.blogspot.e_kanivets.moneytracker.entity.data.Record;
9+
10+
import java.util.List;
11+
12+
import javax.inject.Inject;
13+
14+
import butterknife.Bind;
15+
import butterknife.OnClick;
16+
17+
public class ImportActivity extends BaseBackActivity {
18+
@Inject
19+
ImportController importController;
20+
21+
@Bind(R.id.et_import_data)
22+
EditText etImportData;
23+
24+
@Override
25+
protected int getContentViewId() {
26+
return R.layout.activity_import;
27+
}
28+
29+
@Override
30+
protected boolean initData() {
31+
boolean result = super.initData();
32+
getAppComponent().inject(ImportActivity.this);
33+
return result;
34+
}
35+
36+
@OnClick(R.id.btn_import)
37+
public void importRecords() {
38+
String data = etImportData.getText().toString().trim();
39+
List<Record> recordList = importController.importRecordsFromCsv(data);
40+
showToast(getString(R.string.records_imported, recordList.size()));
41+
42+
setResult(RESULT_OK);
43+
finish();
44+
}
45+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/base/BaseDrawerActivity.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.blogspot.e_kanivets.moneytracker.R;
1212
import com.blogspot.e_kanivets.moneytracker.activity.ChartsActivity;
1313
import com.blogspot.e_kanivets.moneytracker.activity.ExportActivity;
14+
import com.blogspot.e_kanivets.moneytracker.activity.ImportActivity;
1415
import com.blogspot.e_kanivets.moneytracker.activity.SettingsActivity;
1516
import com.blogspot.e_kanivets.moneytracker.activity.account.AccountsActivity;
1617
import com.blogspot.e_kanivets.moneytracker.activity.exchange_rate.ExchangeRatesActivity;
@@ -29,6 +30,7 @@ public abstract class BaseDrawerActivity extends BaseActivity
2930
private static final int REQUEST_ACCOUNTS = 1;
3031
private static final int REQUEST_RATES = 2;
3132
private static final int REQUEST_SETTINGS = 3;
33+
private static final int REQUEST_IMPORT = 4;
3234

3335
@Bind(R.id.drawer_layout)
3436
DrawerLayout drawer;
@@ -82,6 +84,11 @@ public boolean onNavigationItemSelected(MenuItem item) {
8284
startActivity(new Intent(BaseDrawerActivity.this, ChartsActivity.class));
8385
break;
8486

87+
case R.id.nav_import:
88+
startActivityForResult(new Intent(BaseDrawerActivity.this, ImportActivity.class),
89+
REQUEST_IMPORT);
90+
break;
91+
8592
case R.id.nav_export:
8693
startActivity(new Intent(BaseDrawerActivity.this, ExportActivity.class));
8794
break;
@@ -117,6 +124,10 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
117124
update();
118125
break;
119126

127+
case REQUEST_IMPORT:
128+
update();
129+
break;
130+
120131
default:
121132
break;
122133
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/ExportController.java renamed to app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/external/ExportController.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blogspot.e_kanivets.moneytracker.controller;
1+
package com.blogspot.e_kanivets.moneytracker.controller.external;
22

33
import com.blogspot.e_kanivets.moneytracker.controller.data.CategoryController;
44
import com.blogspot.e_kanivets.moneytracker.controller.data.RecordController;
@@ -16,13 +16,6 @@
1616
* @author Evgenii Kanivets
1717
*/
1818
public class ExportController {
19-
private static final String HEAD_TIME = "time";
20-
private static final String HEAD_TITLE = "title";
21-
private static final String HEAD_CATEGORY = "category";
22-
private static final String HEAD_PRICE = "price";
23-
private static final String HEAD_CURRENCY = "currency";
24-
private static final String DELIMITER = ";";
25-
2619
private final RecordController recordController;
2720
private final CategoryController categoryController;
2821

@@ -37,11 +30,11 @@ public List<String> getRecordsForExport(long fromDate, long toDate) {
3730
/* First of all add a header */
3831
@SuppressWarnings("StringBufferReplaceableByString")
3932
StringBuilder sb = new StringBuilder();
40-
sb.append(HEAD_TIME).append(DELIMITER);
41-
sb.append(HEAD_TITLE).append(DELIMITER);
42-
sb.append(HEAD_CATEGORY).append(DELIMITER);
43-
sb.append(HEAD_PRICE).append(DELIMITER);
44-
sb.append(HEAD_CURRENCY);
33+
sb.append(Head.TIME).append(Head.DELIMITER);
34+
sb.append(Head.TITLE).append(Head.DELIMITER);
35+
sb.append(Head.CATEGORY).append(Head.DELIMITER);
36+
sb.append(Head.PRICE).append(Head.DELIMITER);
37+
sb.append(Head.CURRENCY);
4538

4639
result.add(sb.toString());
4740

@@ -52,16 +45,16 @@ public List<String> getRecordsForExport(long fromDate, long toDate) {
5245

5346
for (Record record : recordList) {
5447
sb = new StringBuilder();
55-
sb.append(record.getTime()).append(DELIMITER);
56-
sb.append(record.getTitle()).append(DELIMITER);
48+
sb.append(record.getTime()).append(Head.DELIMITER);
49+
sb.append(record.getTitle()).append(Head.DELIMITER);
5750

5851
Category category = null;
5952
if (record.getCategory() != null)
6053
category = categoryController.read(record.getCategory().getId());
6154

62-
sb.append(category == null ? "NONE" : category.getName()).append(DELIMITER);
55+
sb.append(category == null ? "NONE" : category.getName()).append(Head.DELIMITER);
6356
sb.append(record.getType() == 0 ? record.getFullPrice()
64-
: -record.getFullPrice()).append(DELIMITER);
57+
: -record.getFullPrice()).append(Head.DELIMITER);
6558
sb.append(record.getCurrency() == null ? DbHelper.DEFAULT_ACCOUNT_CURRENCY
6659
: record.getCurrency());
6760

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.blogspot.e_kanivets.moneytracker.controller.external;
2+
3+
/**
4+
* Interface for head titles in import/export features.
5+
* Created on 6/28/16.
6+
*
7+
* @author Evgenii Kanivets
8+
*/
9+
public interface Head {
10+
String TIME = "time";
11+
String TITLE = "title";
12+
String CATEGORY = "category";
13+
String PRICE = "price";
14+
String CURRENCY = "currency";
15+
String DELIMITER = ";";
16+
int COLUMN_COUNT = 5;
17+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.blogspot.e_kanivets.moneytracker.controller.external;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
6+
import com.blogspot.e_kanivets.moneytracker.controller.data.RecordController;
7+
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
8+
import com.blogspot.e_kanivets.moneytracker.entity.data.Category;
9+
import com.blogspot.e_kanivets.moneytracker.entity.data.Record;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
/**
15+
* Controller class to encapsulate import logic.
16+
* Created on 6/28/16.
17+
*
18+
* @author Evgenii Kanivets
19+
*/
20+
public class ImportController {
21+
private final RecordController recordController;
22+
23+
public ImportController(RecordController recordController) {
24+
this.recordController = recordController;
25+
}
26+
27+
@NonNull
28+
public List<Record> importRecordsFromCsv(@Nullable String csv) {
29+
List<Record> recordList = new ArrayList<>();
30+
if (csv == null) return recordList;
31+
32+
String lines[] = csv.split("\\r?\\n");
33+
for (String line : lines) {
34+
String[] words = line.split(Head.DELIMITER);
35+
if (words.length != Head.COLUMN_COUNT) continue;
36+
37+
String timeCol = words[0];
38+
String titleCol = words[1];
39+
String categoryCol = words[2];
40+
String priceCol = words[3];
41+
String currencyCol = words[4];
42+
43+
try {
44+
long time = Long.parseLong(timeCol);
45+
String title = titleCol.trim();
46+
String categoryName = categoryCol.trim();
47+
double price = Double.parseDouble(priceCol);
48+
String currency = currencyCol.trim();
49+
50+
if (currency.length() != 3) continue;
51+
if (categoryName.isEmpty()) continue;
52+
53+
int type;
54+
if (price < 0.0) type = Record.TYPE_EXPENSE;
55+
else type = Record.TYPE_INCOME;
56+
57+
Category category = new Category(categoryName);
58+
Account account = new Account(-1, "MOCK", -1, currency, 0);
59+
60+
Record record = new Record(time, type, title, category, Math.abs(price), account, currency);
61+
Record createdRecord = recordController.create(record);
62+
if (createdRecord != null) recordList.add(createdRecord);
63+
} catch (Exception e) {
64+
e.printStackTrace();
65+
}
66+
}
67+
68+
return recordList;
69+
}
70+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/AppComponent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.blogspot.e_kanivets.moneytracker.activity.ChartsActivity;
44
import com.blogspot.e_kanivets.moneytracker.activity.ExportActivity;
5+
import com.blogspot.e_kanivets.moneytracker.activity.ImportActivity;
56
import com.blogspot.e_kanivets.moneytracker.activity.ReportActivity;
67
import com.blogspot.e_kanivets.moneytracker.activity.SettingsActivity;
78
import com.blogspot.e_kanivets.moneytracker.activity.account.AccountsActivity;
@@ -48,6 +49,8 @@ public interface AppComponent {
4849

4950
void inject(TransferActivity transferActivity);
5051

52+
void inject(ImportActivity importActivity);
53+
5154
void inject(ExportActivity exportActivity);
5255

5356
void inject(ReportActivity reportActivity);

app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/module/ControllerModule.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.content.Context;
44
import android.support.annotation.NonNull;
55

6-
import com.blogspot.e_kanivets.moneytracker.controller.ExportController;
6+
import com.blogspot.e_kanivets.moneytracker.controller.external.ExportController;
77
import com.blogspot.e_kanivets.moneytracker.controller.FormatController;
88
import com.blogspot.e_kanivets.moneytracker.controller.PeriodController;
99
import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController;
@@ -13,6 +13,7 @@
1313
import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
1414
import com.blogspot.e_kanivets.moneytracker.controller.data.RecordController;
1515
import com.blogspot.e_kanivets.moneytracker.controller.data.TransferController;
16+
import com.blogspot.e_kanivets.moneytracker.controller.external.ImportController;
1617
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
1718
import com.blogspot.e_kanivets.moneytracker.entity.data.Category;
1819
import com.blogspot.e_kanivets.moneytracker.entity.data.ExchangeRate;
@@ -114,4 +115,11 @@ public ExportController providesExportController(RecordController recordControll
114115
CategoryController categoryController) {
115116
return new ExportController(recordController, categoryController);
116117
}
118+
119+
@Provides
120+
@NonNull
121+
@Singleton
122+
public ImportController providesImportController(RecordController recordController) {
123+
return new ImportController(recordController);
124+
}
117125
}
802 Bytes
Loading

0 commit comments

Comments
 (0)