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

Commit bea856c

Browse files
author
Evgenii Kanivets
committed
#7[30m]. Add created_at column to Accounts table. Add time column to Transfers table.
1 parent 112fe87 commit bea856c

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private boolean prepareRecord() {
136136
e.printStackTrace();
137137
}
138138

139-
if (price >= 0 && price <= 1000000000) {
139+
if (price >= 0 && price <= 1000000000 && spinnerAccount.getSelectedItemPosition() >= 0) {
140140
Account account = accountController.getAccounts().get(spinnerAccount.getSelectedItemPosition());
141141
return doRecord(title, category, price, account);
142142
} else return false;

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/AccountController.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public List<Account> getAccounts() {
5050
cursor.getInt(curSumColIndex),
5151
cursor.getString(currencyColIndex));
5252

53-
if (account.getTitle().equals(DbHelper.DEFAULT_ACCOUNT)) continue;
54-
5553
//Add account to list
5654
accountList.add(account);
5755
} while (cursor.moveToNext());

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ public class DbHelper extends SQLiteOpenHelper {
3333

3434
public static final String ACCOUNT_ID_COLUMN = "account_id";
3535
public static final String CUR_SUM_COLUMN = "cur_sum";
36-
public static final String DEFAULT_ACCOUNT = "default_account";
36+
public static final String DEFAULT_ACCOUNT = "Default";
37+
public static final String DEFAULT_ACCOUNT_CURRENCY = "NON";
38+
39+
public static final String TABLE_TRANSFERS = "transfers";
40+
public static final String FROM_ACCOUNT_ID_COLUMN = "from_account_id";
41+
public static final String TO_ACCOUND_ID_COLUMN = "to_account_id";
42+
public static final String FROM_AMOUNT_COLUMN = "from_amount";
43+
public static final String TO_AMOUNT_COLUMN = "to_amount";
44+
public static final String CREATED_AT_COLUMN = "created_at";
3745

3846
public DbHelper(Context context) {
3947
super(context, DB_NAME, null, DB_VERSION);
@@ -50,23 +58,18 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
5058
if (oldVersion == 1 && newVersion == 2) {
5159
db.beginTransaction();
5260

53-
/* Create accounts table */
54-
db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "("
55-
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
56-
+ TITLE_COLUMN + " TEXT,"
57-
+ CUR_SUM_COLUMN + " INTEGER);");
61+
createAccountsTable(db);
5862

5963
/* Add account_id column into the records table */
6064
db.execSQL("ALTER TABLE " + TABLE_RECORDS + " ADD COLUMN " + ACCOUNT_ID_COLUMN + " INTEGER;");
6165

66+
createTransfersTable(db);
67+
6268
/* Insert default account for all records from DB_VERSION = 1 */
63-
ContentValues contentValues = new ContentValues();
64-
contentValues.put(TITLE_COLUMN, DEFAULT_ACCOUNT);
65-
contentValues.put(CUR_SUM_COLUMN, 0);
66-
int id = (int) db.insert(TABLE_ACCOUNTS, null, contentValues);
69+
long id = insertDefaultAccount(db);
6770

6871
/* Set the default account for all records from DB_VERSION = 1 */
69-
contentValues = new ContentValues();
72+
ContentValues contentValues = new ContentValues();
7073
contentValues.put(ACCOUNT_ID_COLUMN, id);
7174
db.update(DbHelper.TABLE_RECORDS, contentValues, null, null);
7275

@@ -105,17 +108,40 @@ private void createDbVersion2(SQLiteDatabase db) {
105108
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
106109
+ NAME_COLUMN + " TEXT" + ");");
107110

111+
createAccountsTable(db);
112+
113+
createTransfersTable(db);
114+
115+
insertDefaultAccount(db);
116+
}
117+
118+
private void createAccountsTable(SQLiteDatabase db) {
108119
db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "("
109120
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
121+
+ CREATED_AT_COLUMN + " INTEGER,"
110122
+ TITLE_COLUMN + " TEXT,"
111123
+ CUR_SUM_COLUMN + " INTEGER,"
112124
+ CURRENCY_COLUMN + " TEXT );");
125+
}
126+
127+
private void createTransfersTable(SQLiteDatabase db) {
128+
db.execSQL("CREATE TABLE " + TABLE_TRANSFERS + "("
129+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
130+
+ TIME_COLUMN + " INTEGER,"
131+
+ FROM_ACCOUNT_ID_COLUMN + " INTEGER,"
132+
+ TO_ACCOUND_ID_COLUMN + " INTEGER,"
133+
+ FROM_AMOUNT_COLUMN + " INTEGER,"
134+
+ TO_AMOUNT_COLUMN + " INTEGER);");
135+
}
113136

137+
private long insertDefaultAccount(SQLiteDatabase db) {
114138
/* Insert default account for all records from DB_VERSION = 1 */
115139
ContentValues contentValues = new ContentValues();
116140
contentValues.put(TITLE_COLUMN, DEFAULT_ACCOUNT);
117141
contentValues.put(CUR_SUM_COLUMN, 0);
142+
contentValues.put(CURRENCY_COLUMN, DEFAULT_ACCOUNT_CURRENCY);
143+
contentValues.put(CREATED_AT_COLUMN, System.currentTimeMillis());
118144

119-
db.insert(TABLE_ACCOUNTS, null, contentValues);
145+
return db.insert(TABLE_ACCOUNTS, null, contentValues);
120146
}
121-
}
147+
}

0 commit comments

Comments
 (0)