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

Commit b4a1be8

Browse files
author
Evgeniy Kanivets
committed
Added helper class to improve architecture of app
1 parent 398e11f commit b4a1be8

File tree

10 files changed

+219
-115
lines changed

10 files changed

+219
-115
lines changed

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

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.blogspot.e_kanivets.moneytracker.R;
1818
import com.blogspot.e_kanivets.moneytracker.adapter.RecordAdapter;
1919
import com.blogspot.e_kanivets.moneytracker.helper.DBHelper;
20+
import com.blogspot.e_kanivets.moneytracker.helper.MTHelper;
2021
import com.blogspot.e_kanivets.moneytracker.model.Record;
2122
import com.blogspot.e_kanivets.moneytracker.ui.AddExpenseDialog;
2223
import com.blogspot.e_kanivets.moneytracker.ui.AddIncomeDialog;
@@ -25,14 +26,14 @@
2526

2627
import java.util.ArrayList;
2728
import java.util.List;
29+
import java.util.Observable;
30+
import java.util.Observer;
2831

2932

30-
public class MainActivity extends ActionBarActivity {
33+
public class MainActivity extends ActionBarActivity implements Observer{
3134

3235
private Activity activity;
3336

34-
private DBHelper dbHelper;
35-
3637
private ListView listView;
3738

3839
private Button btnAddIncome;
@@ -44,7 +45,6 @@ protected void onCreate(Bundle savedInstanceState) {
4445
setContentView(R.layout.activity_main);
4546

4647
activity = this;
47-
dbHelper = MTApp.get().getDbHelper();
4848

4949
//Link views
5050
btnAddIncome = (Button) findViewById(R.id.b_add_income);
@@ -57,15 +57,6 @@ protected void onCreate(Bundle savedInstanceState) {
5757
@Override
5858
public void onClick(View v) {
5959
AddIncomeDialog dialog = new AddIncomeDialog(activity);
60-
61-
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
62-
@Override
63-
public void onDismiss(DialogInterface dialog) {
64-
Log.d(Constants.TAG, "onDismiss");
65-
retrieveDataFromDB();
66-
}
67-
});
68-
6960
dialog.show();
7061
}
7162
});
@@ -74,20 +65,15 @@ public void onDismiss(DialogInterface dialog) {
7465
@Override
7566
public void onClick(View v) {
7667
AddExpenseDialog dialog = new AddExpenseDialog(activity);
77-
78-
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
79-
@Override
80-
public void onDismiss(DialogInterface dialog) {
81-
Log.d(Constants.TAG, "onDismiss");
82-
retrieveDataFromDB();
83-
}
84-
});
85-
8668
dialog.show();
8769
}
8870
});
8971

90-
retrieveDataFromDB();
72+
listView.setAdapter(new RecordAdapter(activity, MTHelper.getInstance().getRecords()));
73+
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
74+
75+
//Subscribe to helper
76+
MTHelper.getInstance().addObserver(this);
9177
}
9278

9379

@@ -110,41 +96,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
11096
return super.onOptionsItemSelected(item);
11197
}
11298

113-
private void retrieveDataFromDB() {
114-
SQLiteDatabase db = dbHelper.getReadableDatabase();
115-
116-
Cursor cursor = db.query(Constants.TABLE_RECORDS, null, null, null, null, null, null);
117-
118-
final List<Record> records = new ArrayList<Record>();
119-
120-
if(cursor.moveToFirst()) {
121-
//Get indexes of columns
122-
int idColIndex = cursor.getColumnIndex("id");
123-
int titleColIndex = cursor.getColumnIndex("title");
124-
int categoryColIndex = cursor.getColumnIndex("category_id");
125-
int priceColIndex = cursor.getColumnIndex("price");
126-
127-
do {
128-
//Add record to list
129-
records.add(new Record(cursor.getInt(idColIndex),
130-
cursor.getString(titleColIndex),
131-
Integer.toString(cursor.getInt(categoryColIndex)),
132-
cursor.getString(priceColIndex)));
133-
} while (cursor.moveToNext());
134-
}
135-
136-
db.close();
137-
138-
listView.setAdapter(new RecordAdapter(activity, records, new View.OnClickListener() {
139-
@Override
140-
public void onClick(View v) {
141-
SQLiteDatabase db = dbHelper.getWritableDatabase();
142-
db.delete(Constants.TABLE_RECORDS, "id=?",
143-
new String[] {Integer.toString(records.get((Integer)v.getTag()).getId())});
144-
db.close();
145-
retrieveDataFromDB();
146-
}
147-
}));
99+
@Override
100+
public void update(Observable observable, Object data) {
101+
Log.d(Constants.TAG, "data has been changed");
148102
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
149103
}
150104
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/RecordAdapter.java

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

33
import android.content.Context;
44
import android.database.sqlite.SQLiteDatabase;
5+
import android.util.Log;
56
import android.view.LayoutInflater;
67
import android.view.View;
78
import android.view.ViewGroup;
@@ -11,6 +12,7 @@
1112

1213
import com.blogspot.e_kanivets.moneytracker.R;
1314
import com.blogspot.e_kanivets.moneytracker.helper.DBHelper;
15+
import com.blogspot.e_kanivets.moneytracker.helper.MTHelper;
1416
import com.blogspot.e_kanivets.moneytracker.model.Record;
1517
import com.blogspot.e_kanivets.moneytracker.util.Constants;
1618

@@ -24,12 +26,10 @@ public class RecordAdapter extends BaseAdapter{
2426
private Context context;
2527
private List<Record> records;
2628

27-
private View.OnClickListener onClickListener;
28-
29-
public RecordAdapter(Context context, List<Record> records, View.OnClickListener onClickListener) {
29+
public RecordAdapter(Context context, List<Record> records) {
3030
this.context = context;
3131
this.records = records;
32-
this.onClickListener = onClickListener;
32+
3333
}
3434

3535
@Override
@@ -58,12 +58,17 @@ public View getView(final int position, View convertView, ViewGroup parent) {
5858

5959
Button bRemove = (Button) convertView.findViewById(R.id.b_remove);
6060

61-
tvPrice.setText(records.get(position).getPrice());
61+
tvPrice.setText(Integer.toString(records.get(position).getPrice()));
6262
tvTitle.setText(records.get(position).getTitle());
6363
tvCategory.setText(records.get(position).getCategory());
6464

6565
bRemove.setTag(position);
66-
bRemove.setOnClickListener(onClickListener);
66+
bRemove.setOnClickListener(new View.OnClickListener() {
67+
@Override
68+
public void onClick(View v) {
69+
MTHelper.getInstance().deleteRecordById(Integer.parseInt(v.getTag().toString()));
70+
}
71+
});
6772

6873
return convertView;
6974
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/helper/DBHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public void onCreate(SQLiteDatabase db) {
2424
+ "title TEXT,"
2525
+ "category_id INTEGER,"
2626
+ "price INTEGER" + ");");
27+
28+
db.execSQL("CREATE TABLE " + Constants.TABLE_CATEGORIES + "("
29+
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
30+
+ "name TEXT" + ");");
2731
}
2832

2933
@Override
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.blogspot.e_kanivets.moneytracker.helper;
2+
3+
import android.content.ContentValues;
4+
import android.database.Cursor;
5+
import android.database.sqlite.SQLiteDatabase;
6+
import android.util.Log;
7+
8+
import com.blogspot.e_kanivets.moneytracker.model.Category;
9+
import com.blogspot.e_kanivets.moneytracker.model.Record;
10+
import com.blogspot.e_kanivets.moneytracker.util.Constants;
11+
import com.blogspot.e_kanivets.moneytracker.util.MTApp;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.Observable;
16+
17+
/**
18+
* Created by eugene on 01/09/14.
19+
*/
20+
public class MTHelper extends Observable {
21+
22+
private static MTHelper mtHelper;
23+
24+
private DBHelper dbHelper;
25+
26+
private List<Category> categories;
27+
private List<Record> records;
28+
29+
public static MTHelper getInstance() {
30+
if(mtHelper == null) {
31+
mtHelper = new MTHelper();
32+
}
33+
return mtHelper;
34+
}
35+
36+
private MTHelper() {
37+
dbHelper = new DBHelper(MTApp.get());
38+
}
39+
40+
public void initialize() {
41+
SQLiteDatabase db = dbHelper.getReadableDatabase();
42+
43+
//Read categories table from db
44+
Cursor cursor = db.query(Constants.TABLE_CATEGORIES, null, null, null, null, null, null);
45+
categories = new ArrayList<Category>();
46+
47+
if(cursor.moveToFirst()) {
48+
int idColIndex = cursor.getColumnIndex("id");
49+
int nameColIndex = cursor.getColumnIndex("name");
50+
51+
do {
52+
//Read a record from DB
53+
Category category = new Category(cursor.getInt(idColIndex),
54+
cursor.getString(nameColIndex));
55+
56+
//Add record to list
57+
categories.add(category);
58+
} while (cursor.moveToNext());
59+
}
60+
61+
cursor.close();
62+
63+
//Read records table from db
64+
cursor = db.query(Constants.TABLE_RECORDS, null, null, null, null, null, null);
65+
records = new ArrayList<Record>();
66+
67+
if(cursor.moveToFirst()) {
68+
//Get indexes of columns
69+
int idColIndex = cursor.getColumnIndex("id");
70+
int timeColIndex = cursor.getColumnIndex("time");
71+
int typeColIndex = cursor.getColumnIndex("type");
72+
int titleColIndex = cursor.getColumnIndex("title");
73+
int categoryColIndex = cursor.getColumnIndex("category_id");
74+
int priceColIndex = cursor.getColumnIndex("price");
75+
76+
do {
77+
//Read a record from DB
78+
Record record = new Record(cursor.getInt(idColIndex),
79+
cursor.getInt(timeColIndex),
80+
cursor.getInt(typeColIndex),
81+
cursor.getString(titleColIndex),
82+
cursor.getInt(categoryColIndex),
83+
cursor.getInt(priceColIndex));
84+
85+
//Add record to list
86+
records.add(record);
87+
} while (cursor.moveToNext());
88+
}
89+
90+
db.close();
91+
}
92+
93+
public List<Record> getRecords() {
94+
return records;
95+
}
96+
97+
public void addRecord(int time, int type, String title, int category_id, int price) {
98+
//Add record to DB
99+
ContentValues contentValues = new ContentValues();
100+
101+
SQLiteDatabase db = dbHelper.getWritableDatabase();
102+
103+
contentValues.put("type", "1");
104+
contentValues.put("title", title);
105+
contentValues.put("category_id", 1);
106+
contentValues.put("price", price);
107+
108+
int Id = (int)db.insert(Constants.TABLE_RECORDS, null, contentValues);
109+
Log.d(Constants.TAG, "Id = " + Id);
110+
111+
db.close();
112+
113+
//Add record to app list
114+
records.add(new Record(Id, time, type, title, category_id, price));
115+
116+
//notify observers
117+
setChanged();
118+
notifyObservers();
119+
}
120+
121+
public void deleteRecordById(int id) {
122+
for (Record record : records) {
123+
if(record.getId() == id) {
124+
SQLiteDatabase db = dbHelper.getWritableDatabase();
125+
db.delete(Constants.TABLE_RECORDS, "id=?",
126+
new String[] {Integer.toString(id)});
127+
db.close();
128+
129+
records.remove(record);
130+
131+
//notify observers
132+
setChanged();
133+
notifyObservers();
134+
}
135+
}
136+
}
137+
138+
public String getCategoryById(int id) {
139+
for (Category category : categories) {
140+
if(category.getId() == id) return category.getName();
141+
}
142+
143+
return null;
144+
}
145+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.blogspot.e_kanivets.moneytracker.model;
2+
3+
import java.util.Calendar;
4+
5+
/**
6+
* Created by eugene on 01/09/14.
7+
*/
8+
public class Category {
9+
10+
private int id;
11+
private String name;
12+
13+
public Category(int id, String name) {
14+
this.id = id;
15+
this.name = name;
16+
}
17+
18+
public int getId() {
19+
return id;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Record.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
package com.blogspot.e_kanivets.moneytracker.model;
22

3+
import com.blogspot.e_kanivets.moneytracker.helper.MTHelper;
4+
35
/**
46
* Created by eugene on 01/09/14.
57
*/
68
public class Record {
79

810
private int id;
9-
11+
private int time;
12+
private int type;
1013
private String title;
14+
private int categoryId;
1115
private String category;
12-
private String price;
16+
private int price;
1317

14-
public Record(int id, String title, String category, String price) {
18+
public Record(int id, int time, int type, String title, int categoryId, int price) {
1519
this.id = id;
20+
this.time = time;
21+
this.type = type;
1622
this.title = title;
17-
this.category = category;
23+
this.categoryId = categoryId;
1824
this.price = price;
25+
26+
category = MTHelper.getInstance().getCategoryById(categoryId);
1927
}
2028

2129
public int getId() {
@@ -30,7 +38,7 @@ public String getCategory() {
3038
return category;
3139
}
3240

33-
public String getPrice() {
41+
public int getPrice() {
3442
return price;
3543
}
3644
}

0 commit comments

Comments
 (0)