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

Commit 4e2ce06

Browse files
author
Evgenii Kanivets
committed
#46[1h]. Add goal, archived, color columns to DB and account entity.
1 parent b3dc49d commit 4e2ce06

File tree

6 files changed

+111
-11
lines changed

6 files changed

+111
-11
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ private boolean addAccount() {
9797
String title = etTitle.getText().toString().trim();
9898
double initSum = Double.parseDouble(etInitSum.getText().toString().trim());
9999
String currency = (String) spinner.getSelectedItem();
100-
return accountController.create(new Account(title, initSum, currency)) != null;
100+
double goal = 0;
101+
int color = 0;
102+
103+
Account account = new Account(title, initSum, currency, goal, false, color);
104+
return accountController.create(account) != null;
101105
} else {
102106
return false;
103107
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/external/ImportController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public List<Record> importRecordsFromCsv(@Nullable String csv) {
5555
else type = Record.TYPE_INCOME;
5656

5757
Category category = new Category(categoryName);
58-
Account account = new Account(-1, "MOCK", -1, currency, 0);
58+
Account account = new Account(-1, "MOCK", -1, currency, 0, -1, false, 0);
5959

6060
Record record = new Record(time, type, title, category, Math.abs(price), account, currency);
6161
Record createdRecord = recordController.create(record);

app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Account.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,32 @@ public class Account extends BaseEntity implements Parcelable {
1616
private long curSum;
1717
private final String currency;
1818
private long decimals;
19+
private double goal;
20+
private boolean archived;
21+
private int color;
1922

20-
public Account(long id, String title, long curSum, String currency, long decimals) {
23+
public Account(long id, String title, long curSum, String currency, long decimals, double goal,
24+
boolean archived, int color) {
2125
this.id = id;
2226
this.title = title;
2327
this.curSum = curSum;
2428
this.currency = currency;
2529
this.decimals = decimals;
30+
this.goal = goal;
31+
this.archived = archived;
32+
this.color = color;
2633
}
2734

28-
public Account(String title, double curSum, String currency) {
35+
public Account(String title, double curSum, String currency, double goal,
36+
boolean archived, int color) {
2937
this.id = -1;
3038
this.title = title;
3139
this.currency = currency;
3240
this.curSum = getLong(curSum);
3341
this.decimals = getDecimal(curSum);
42+
this.goal = goal;
43+
this.archived = archived;
44+
this.color = color;
3445
}
3546

3647
protected Account(Parcel in) {
@@ -39,6 +50,9 @@ protected Account(Parcel in) {
3950
curSum = in.readLong();
4051
currency = in.readString();
4152
decimals = in.readLong();
53+
goal = in.readDouble();
54+
archived = in.readByte() != 0;
55+
color = in.readInt();
4256
}
4357

4458
public static final Creator<Account> CREATOR = new Creator<Account>() {
@@ -96,7 +110,10 @@ public boolean equals(Object o) {
96110
&& equals(this.title, account.getTitle())
97111
&& this.curSum == account.getCurSum()
98112
&& equals(this.currency, account.getCurrency())
99-
&& this.decimals == account.decimals;
113+
&& this.decimals == account.decimals
114+
&& this.goal == account.goal
115+
&& this.archived == account.archived
116+
&& this.color == account.color;
100117
} else return false;
101118
}
102119

@@ -109,7 +126,10 @@ public String toString() {
109126
sb.append("title = ").append(title).append(", ");
110127
sb.append("curSum = ").append(curSum).append(", ");
111128
sb.append("currency = ").append(currency).append(", ");
112-
sb.append("decimals = ").append(decimals);
129+
sb.append("decimals = ").append(decimals).append(", ");
130+
sb.append("goal = ").append(goal).append(", ");
131+
sb.append("archived = ").append(archived).append(", ");
132+
sb.append("color = ").append(color);
113133
sb.append("}");
114134

115135
return sb.toString();
@@ -127,5 +147,8 @@ public void writeToParcel(Parcel dest, int flags) {
127147
dest.writeLong(curSum);
128148
dest.writeString(currency);
129149
dest.writeLong(decimals);
150+
dest.writeDouble(goal);
151+
dest.writeByte((byte) (archived ? 1 : 0));
152+
dest.writeInt(color);
130153
}
131154
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Record.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Record(long id, long time, int type, String title, long categoryId, long
3232
this.title = title;
3333
this.category = new Category(categoryId, null);
3434
this.price = price;
35-
this.account = new Account(accountId, null, -1, null, 0);
35+
this.account = new Account(accountId, null, -1, null, 0, -1, false, -1);
3636
this.currency = currency;
3737
this.decimals = decimals;
3838
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/DbHelper.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class DbHelper extends SQLiteOpenHelper {
1515

1616
/* DB_VERSION = 1 */
1717
public static final String DB_NAME = "database";
18-
public static final int DB_VERSION = 4;
18+
public static final int DB_VERSION = 5;
1919
public static final String TABLE_RECORDS = "records";
2020
public static final String TABLE_CATEGORIES = "categories";
2121

@@ -54,6 +54,11 @@ public class DbHelper extends SQLiteOpenHelper {
5454
public static final String DECIMALS_FROM_COLUMN = "decimals_from";
5555
public static final String DECIMALS_TO_COLUMN = "decimals_to";
5656

57+
/* DB_VERSION = 5 */
58+
public static final String GOAL_COLUMN = "goal";
59+
public static final String ARCHIVED_COLUMN = "archived";
60+
public static final String COLOR_COLUMN = "color";
61+
5762
public DbHelper(Context context) {
5863
super(context, DB_NAME, null, DB_VERSION);
5964
}
@@ -63,7 +68,8 @@ public void onCreate(SQLiteDatabase db) {
6368
//createDbVersion1(db);
6469
//createDbVersion2(db);
6570
//createDbVersion3(db);
66-
createDbVersion4(db);
71+
//createDbVersion4(db);
72+
createDbVersion5(db);
6773
}
6874

6975
@Override
@@ -133,6 +139,25 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
133139
db.setTransactionSuccessful();
134140
db.endTransaction();
135141
}
142+
143+
if (oldVersion < 5) {
144+
db.beginTransaction();
145+
146+
/* Add goal column to the accounts table */
147+
db.execSQL("ALTER TABLE " + TABLE_ACCOUNTS + " ADD COLUMN "
148+
+ GOAL_COLUMN + " REAL;");
149+
150+
/* Add archived flag column to the accounts table */
151+
db.execSQL("ALTER TABLE " + TABLE_ACCOUNTS + " ADD COLUMN "
152+
+ ARCHIVED_COLUMN + " INTEGER;");
153+
154+
/* Add color column to the accounts table */
155+
db.execSQL("ALTER TABLE " + TABLE_ACCOUNTS + " ADD COLUMN "
156+
+ COLOR_COLUMN + " INTEGER;");
157+
158+
db.setTransactionSuccessful();
159+
db.endTransaction();
160+
}
136161
}
137162

138163
@SuppressWarnings("unused")
@@ -258,6 +283,48 @@ private void createDbVersion4(SQLiteDatabase db) {
258283
insertDefaultAccount(db);
259284
}
260285

286+
private void createDbVersion5(SQLiteDatabase db) {
287+
db.execSQL("CREATE TABLE " + TABLE_RECORDS + "("
288+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
289+
+ TIME_COLUMN + " INTEGER,"
290+
+ TYPE_COLUMN + " INTEGER,"
291+
+ TITLE_COLUMN + " TEXT,"
292+
+ CATEGORY_ID_COLUMN + " INTEGER,"
293+
+ PRICE_COLUMN + " INTEGER,"
294+
+ ACCOUNT_ID_COLUMN + " INTEGER,"
295+
+ CURRENCY_COLUMN + " TEXT,"
296+
+ DECIMALS_COLUMN + " INTEGER);");
297+
298+
db.execSQL("CREATE TABLE " + TABLE_CATEGORIES + "("
299+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
300+
+ NAME_COLUMN + " TEXT" + ");");
301+
302+
db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "("
303+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
304+
+ CREATED_AT_COLUMN + " INTEGER,"
305+
+ TITLE_COLUMN + " TEXT,"
306+
+ CUR_SUM_COLUMN + " INTEGER,"
307+
+ CURRENCY_COLUMN + " TEXT,"
308+
+ DECIMALS_COLUMN + " INTEGER,"
309+
+ GOAL_COLUMN + " REAL,"
310+
+ ARCHIVED_COLUMN + " INTEGER,"
311+
+ COLOR_COLUMN + " INTEGER);");
312+
313+
db.execSQL("CREATE TABLE " + TABLE_TRANSFERS + "("
314+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
315+
+ TIME_COLUMN + " INTEGER,"
316+
+ FROM_ACCOUNT_ID_COLUMN + " INTEGER,"
317+
+ TO_ACCOUNT_ID_COLUMN + " INTEGER,"
318+
+ FROM_AMOUNT_COLUMN + " INTEGER,"
319+
+ TO_AMOUNT_COLUMN + " INTEGER,"
320+
+ DECIMALS_FROM_COLUMN + " INTEGER,"
321+
+ DECIMALS_TO_COLUMN + " INTEGER);");
322+
323+
createRatesTable(db);
324+
325+
insertDefaultAccount(db);
326+
}
327+
261328
private void createRatesTable(SQLiteDatabase db) {
262329
db.execSQL("CREATE TABLE " + TABLE_RATES + "("
263330
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"

app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/AccountRepo.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,25 @@ protected List<Account> getListFromCursor(@Nullable Cursor cursor) {
6060
int curSumColIndex = cursor.getColumnIndex(DbHelper.CUR_SUM_COLUMN);
6161
int currencyColIndex = cursor.getColumnIndex(DbHelper.CURRENCY_COLUMN);
6262
int decimalsColIndex = cursor.getColumnIndex(DbHelper.DECIMALS_COLUMN);
63+
int goalColIndex = cursor.getColumnIndex(DbHelper.GOAL_COLUMN);
64+
int archivedIndex = cursor.getColumnIndex(DbHelper.ARCHIVED_COLUMN);
65+
int colorIndex = cursor.getColumnIndex(DbHelper.COLOR_COLUMN);
6366

6467
do {
6568
// Read a account from DB
6669
Account account = new Account(cursor.getLong(idColIndex),
6770
cursor.getString(titleColIndex),
6871
cursor.getLong(curSumColIndex),
6972
cursor.getString(currencyColIndex),
70-
cursor.getLong(decimalsColIndex));
73+
cursor.getLong(decimalsColIndex),
74+
cursor.getDouble(goalColIndex),
75+
cursor.getInt(archivedIndex) != 0,
76+
cursor.getInt(colorIndex));
7177

7278
accountList.add(account);
7379
} while (cursor.moveToNext());
7480
}
7581

7682
return accountList;
7783
}
78-
}
84+
}

0 commit comments

Comments
 (0)