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

Commit 5ea309e

Browse files
author
Evgeniy Kanivets
committed
Added edit option
1 parent 9492acd commit 5ea309e

File tree

6 files changed

+108
-13
lines changed

6 files changed

+108
-13
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ protected void onCreate(Bundle savedInstanceState) {
8181
btnAddIncome.setOnClickListener(new View.OnClickListener() {
8282
@Override
8383
public void onClick(View v) {
84-
AddIncomeDialog dialog = new AddIncomeDialog(activity);
84+
AddIncomeDialog dialog = new AddIncomeDialog(activity, null, AddIncomeDialog.Mode.MODE_ADD);
8585
dialog.show();
8686
}
8787
});
8888

8989
btnAddExpense.setOnClickListener(new View.OnClickListener() {
9090
@Override
9191
public void onClick(View v) {
92-
AddExpenseDialog dialog = new AddExpenseDialog(activity);
92+
AddExpenseDialog dialog = new AddExpenseDialog(activity, null, AddExpenseDialog.Mode.MODE_ADD);
9393
dialog.show();
9494
}
9595
});
@@ -157,10 +157,18 @@ public boolean onContextItemSelected(MenuItem item) {
157157

158158
switch (item.getItemId()) {
159159
case R.id.edit:
160+
Record record = MTHelper.getInstance().getRecords().get(info.position);
161+
if(record.isIncome()) {
162+
AddIncomeDialog dialog = new AddIncomeDialog(activity, record, AddIncomeDialog.Mode.MODE_EDIT);
163+
dialog.show();
164+
} else {
165+
AddExpenseDialog dialog = new AddExpenseDialog(activity, record, AddExpenseDialog.Mode.MODE_EDIT);
166+
dialog.show();
167+
}
160168
return true;
161169
case R.id.delete:
162-
Log.d(Constants.TAG, "pos = " + info.position + " id = " + MTHelper.getInstance().getRecords().
163-
get(info.position).getId());
170+
/*Log.d(Constants.TAG, "pos = " + info.position + " id = " + MTHelper.getInstance().getRecords().
171+
get(info.position).getId());*/
164172
MTHelper.getInstance().deleteRecordById(MTHelper.getInstance().getRecords().
165173
get(info.position).getId());
166174
return true;

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void addRecord(long time, int type, String title, String category, int pr
130130
if(getCategoryIdByName(category) == -1) {
131131
addCategory(category);
132132
}
133-
int category_id = getCategoryIdByName(category);
133+
int categoryId = getCategoryIdByName(category);
134134

135135
//Add record to DB
136136
SQLiteDatabase db = dbHelper.getWritableDatabase();
@@ -139,15 +139,46 @@ public void addRecord(long time, int type, String title, String category, int pr
139139
contentValues.put("time", time);
140140
contentValues.put("type", type);
141141
contentValues.put("title", title);
142-
contentValues.put("category_id", MTHelper.getInstance().getCategoryIdByName(category));
142+
contentValues.put("category_id", categoryId);
143143
contentValues.put("price", price);
144144

145145
int id = (int) db.insert(Constants.TABLE_RECORDS, null, contentValues);
146146

147147
db.close();
148148

149149
//Add record to app list
150-
records.add(new Record(id, time, type, title, category_id, price));
150+
records.add(new Record(id, time, type, title, categoryId, price));
151+
152+
//notify observers
153+
setChanged();
154+
notifyObservers();
155+
}
156+
157+
public void updateRecordById(int id, String title, String category, int price) {
158+
//Add category if it does not exist yet
159+
if(getCategoryIdByName(category) == -1) {
160+
addCategory(category);
161+
}
162+
int categoryId = getCategoryIdByName(category);
163+
164+
SQLiteDatabase db = dbHelper.getWritableDatabase();
165+
166+
ContentValues contentValues = new ContentValues();
167+
contentValues.put("title", title);
168+
contentValues.put("category_id", categoryId);
169+
contentValues.put("price", price);
170+
171+
db.update(Constants.TABLE_RECORDS, contentValues, "id=?", new String[] {new Integer(id).toString()});
172+
173+
//Change particular record
174+
for(Record record : records) {
175+
if(record.getId() == id) {
176+
record.setTitle(title);
177+
record.setCategoryId(categoryId);
178+
record.setCategory(category);
179+
record.setPrice(price);
180+
}
181+
}
151182

152183
//notify observers
153184
setChanged();

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,20 @@ public long getTime() {
5454
public boolean isIncome() {
5555
return type == 0;
5656
}
57+
58+
public void setTitle(String title) {
59+
this.title = title;
60+
}
61+
62+
public void setCategoryId(int categoryId) {
63+
this.categoryId = categoryId;
64+
}
65+
66+
public void setCategory(String category) {
67+
this.category = category;
68+
}
69+
70+
public void setPrice(int price) {
71+
this.price = price;
72+
}
5773
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/AddExpenseDialog.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.blogspot.e_kanivets.moneytracker.R;
2424
import com.blogspot.e_kanivets.moneytracker.helper.DBHelper;
2525
import com.blogspot.e_kanivets.moneytracker.helper.MTHelper;
26+
import com.blogspot.e_kanivets.moneytracker.model.Record;
2627
import com.blogspot.e_kanivets.moneytracker.util.AppUtils;
2728
import com.blogspot.e_kanivets.moneytracker.util.Constants;
2829
import com.blogspot.e_kanivets.moneytracker.util.MTApp;
@@ -36,16 +37,20 @@ public class AddExpenseDialog extends AlertDialog {
3637

3738
private Context context;
3839

39-
public AddExpenseDialog(Context context) {
40+
private Record record;
41+
private Mode mode;
42+
43+
public AddExpenseDialog(Context context, Record record, Mode mode) {
4044
super(context);
4145
this.context = context;
46+
this.record = record;
47+
this.mode = mode;
4248
}
4349

4450
@Override
4551
protected void onCreate(Bundle savedInstanceState) {
4652
super.onCreate(savedInstanceState);
4753

48-
4954
View view = getLayoutInflater().inflate(R.layout.dialog_add_record, null);
5055
setContentView(view);
5156

@@ -72,7 +77,12 @@ public void onClick(View v) {
7277
try {
7378
price = Integer.parseInt(((EditText) findViewById(R.id.et_price)).getText().toString());
7479
if(price >= 0 && price <= 1000000000) {
75-
MTHelper.getInstance().addRecord(new Date().getTime(), 1, title, category, price);
80+
if(mode == Mode.MODE_ADD) {
81+
MTHelper.getInstance().addRecord(new Date().getTime(), 1, title, category, price);
82+
}
83+
if(mode == Mode.MODE_EDIT) {
84+
MTHelper.getInstance().updateRecordById(record.getId(), title, category, price);
85+
}
7686
dismiss();
7787
} else {
7888
throw new NumberFormatException();
@@ -94,6 +104,13 @@ public void onClick(View v) {
94104
}
95105
});
96106

107+
//Add texts to dialog if it's edit dialog
108+
if(mode == Mode.MODE_EDIT) {
109+
((EditText) findViewById(R.id.et_title)).setText(record.getTitle());
110+
((EditText) findViewById(R.id.et_category)).setText(record.getCategory());
111+
((EditText) findViewById(R.id.et_price)).setText(Integer.toString(record.getPrice()));
112+
}
113+
97114
//Horrible thing to show a software keyboard
98115
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
99116

@@ -110,4 +127,6 @@ public void onShow(DialogInterface dialog) {
110127
}
111128
});
112129
}
130+
131+
public enum Mode {MODE_ADD, MODE_EDIT}
113132
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/AddIncomeDialog.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.blogspot.e_kanivets.moneytracker.R;
2525
import com.blogspot.e_kanivets.moneytracker.helper.DBHelper;
2626
import com.blogspot.e_kanivets.moneytracker.helper.MTHelper;
27+
import com.blogspot.e_kanivets.moneytracker.model.Record;
2728
import com.blogspot.e_kanivets.moneytracker.util.AppUtils;
2829
import com.blogspot.e_kanivets.moneytracker.util.Constants;
2930
import com.blogspot.e_kanivets.moneytracker.util.MTApp;
@@ -38,9 +39,14 @@ public class AddIncomeDialog extends AlertDialog {
3839

3940
private Context context;
4041

41-
public AddIncomeDialog(Context context) {
42+
private Record record;
43+
private Mode mode;
44+
45+
public AddIncomeDialog(Context context, Record record, Mode mode) {
4246
super(context);
4347
this.context = context;
48+
this.record = record;
49+
this.mode = mode;
4450
}
4551

4652
@Override
@@ -73,7 +79,12 @@ public void onClick(View v) {
7379
try {
7480
price = Integer.parseInt(((EditText) findViewById(R.id.et_price)).getText().toString());
7581
if(price >= 0 && price <= 1000000000) {
76-
MTHelper.getInstance().addRecord(new Date().getTime(), 0, title, category, price);
82+
if(mode == Mode.MODE_ADD) {
83+
MTHelper.getInstance().addRecord(new Date().getTime(), 1, title, category, price);
84+
}
85+
if(mode == Mode.MODE_EDIT) {
86+
MTHelper.getInstance().updateRecordById(record.getId(), title, category, price);
87+
}
7788
dismiss();
7889
} else {
7990
throw new NumberFormatException();
@@ -93,6 +104,13 @@ public void onClick(View v) {
93104
}
94105
});
95106

107+
//Add texts to dialog if it's edit dialog
108+
if(mode == Mode.MODE_EDIT) {
109+
((EditText) findViewById(R.id.et_title)).setText(record.getTitle());
110+
((EditText) findViewById(R.id.et_category)).setText(record.getCategory());
111+
((EditText) findViewById(R.id.et_price)).setText(Integer.toString(record.getPrice()));
112+
}
113+
96114
//Horrible thing to show a software keyboard
97115
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
98116
setOnShowListener(new OnShowListener() {
@@ -108,4 +126,6 @@ public void onShow(DialogInterface dialog) {
108126
}
109127
});
110128
}
129+
130+
public enum Mode {MODE_ADD, MODE_EDIT}
111131
}

app/src/main/res/layout/view_report_item.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<LinearLayout
1616
android:orientation="horizontal"
1717
android:layout_width="match_parent"
18-
android:layout_height="wrap_content">
18+
android:layout_height="wrap_content"
19+
android:background="@color/trans">
1920

2021
<TextView
2122
android:layout_width="match_parent"

0 commit comments

Comments
 (0)