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

Commit 4be2023

Browse files
author
evgenii
committed
Added an accounts table and the fragment for one.
1 parent 0bb846d commit 4be2023

File tree

10 files changed

+204
-37
lines changed

10 files changed

+204
-37
lines changed

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ android {
4444

4545
dependencies {
4646
compile fileTree(dir: 'libs', include: ['*.jar'])
47-
compile 'com.google.android.gms:play-services:7.0.0'
48-
compile 'com.android.support:appcompat-v7:22.0.0'
49-
compile 'com.android.support:support-v4:22.0.0'
47+
compile 'com.google.android.gms:play-services:7.3.0'
48+
compile 'com.android.support:appcompat-v7:22.1.1'
49+
compile 'com.android.support:support-v4:22.1.1'
5050
}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
package com.blogspot.e_kanivets.moneytracker.activity;
22

3-
import android.app.Activity;
43
import android.support.v7.app.ActionBarActivity;
54
import android.support.v7.app.ActionBar;
65
import android.support.v4.app.Fragment;
76
import android.support.v4.app.FragmentManager;
87
import android.os.Bundle;
9-
import android.view.Gravity;
10-
import android.view.LayoutInflater;
118
import android.view.Menu;
129
import android.view.MenuItem;
13-
import android.view.View;
14-
import android.view.ViewGroup;
1510
import android.support.v4.widget.DrawerLayout;
16-
import android.widget.ArrayAdapter;
17-
import android.widget.LinearLayout;
18-
import android.widget.Spinner;
1911

2012
import com.blogspot.e_kanivets.moneytracker.R;
13+
import com.blogspot.e_kanivets.moneytracker.fragment.AccountsFragment;
2114
import com.blogspot.e_kanivets.moneytracker.fragment.ExportFragment;
2215
import com.blogspot.e_kanivets.moneytracker.fragment.NavigationDrawerFragment;
2316
import com.blogspot.e_kanivets.moneytracker.fragment.RecordsFragment;
@@ -65,6 +58,10 @@ public void onNavigationDrawerItemSelected(int position) {
6558
break;
6659

6760
case 2:
61+
fragment = AccountsFragment.newInstance(position + 1);
62+
break;
63+
64+
case 3:
6865
fragment = ExportFragment.newInstance(position + 1);
6966
break;
7067

@@ -83,9 +80,15 @@ public void onSectionAttached(int number) {
8380
case 1:
8481
mTitle = getString(R.string.title_records);
8582
break;
83+
8684
case 2:
85+
mTitle = getString(R.string.title_accounts);
86+
break;
87+
88+
case 3:
8789
mTitle = getString(R.string.title_export);
8890
break;
91+
8992
default:
9093
break;
9194
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.blogspot.e_kanivets.moneytracker.fragment;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.support.v4.app.Fragment;
6+
import android.support.v7.app.ActionBar;
7+
import android.support.v7.app.ActionBarActivity;
8+
import android.view.LayoutInflater;
9+
import android.view.View;
10+
import android.view.ViewGroup;
11+
12+
import com.blogspot.e_kanivets.moneytracker.R;
13+
import com.blogspot.e_kanivets.moneytracker.activity.NavDrawerActivity;
14+
15+
/**
16+
* A simple {@link Fragment} subclass.
17+
* Use the {@link AccountsFragment#newInstance} factory method to
18+
* create an instance of this fragment.
19+
*/
20+
public class AccountsFragment extends Fragment {
21+
private static final String KEY_POSITION = "key_position";
22+
23+
private int position;
24+
25+
/**
26+
* Use this factory method to create a new instance of
27+
* this fragment using the provided parameters.
28+
*
29+
* @return A new instance of fragment AccountsFragment.
30+
*/
31+
public static AccountsFragment newInstance(int position) {
32+
AccountsFragment fragment = new AccountsFragment();
33+
Bundle args = new Bundle();
34+
args.putInt(KEY_POSITION, position);
35+
fragment.setArguments(args);
36+
fragment.position = position;
37+
return fragment;
38+
}
39+
40+
public AccountsFragment() {
41+
// Required empty public constructor
42+
}
43+
44+
@Override
45+
public void onCreate(Bundle savedInstanceState) {
46+
super.onCreate(savedInstanceState);
47+
if (getArguments() != null) {
48+
position = getArguments().getInt(KEY_POSITION);
49+
}
50+
}
51+
52+
@Override
53+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
54+
Bundle savedInstanceState) {
55+
// Inflate the layout for this fragment
56+
View rootView = inflater.inflate(R.layout.fragment_accounts, container, false);
57+
initViews(rootView);
58+
initActionBar();
59+
return rootView;
60+
}
61+
62+
@Override
63+
public void onAttach(Activity activity) {
64+
super.onAttach(activity);
65+
66+
((NavDrawerActivity) activity).onSectionAttached(position);
67+
}
68+
69+
private void initViews(View rootView) {
70+
if (rootView != null) {
71+
}
72+
}
73+
74+
private void initActionBar() {
75+
ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
76+
if (actionBar != null) {
77+
actionBar.setCustomView(null);
78+
}
79+
}
80+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/fragment/ExportFragment.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ private void initViews(View rootView) {
9898

9999
private void initActionBar() {
100100
ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
101-
actionBar.setCustomView(null);
101+
if (actionBar != null) {
102+
actionBar.setCustomView(null);
103+
}
102104
}
103105

104106
private void exportRecords() {

app/src/main/java/com/blogspot/e_kanivets/moneytracker/fragment/NavigationDrawerFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
107107
android.R.id.text1,
108108
new String[]{
109109
getString(R.string.title_records),
110+
getString(R.string.title_accounts),
110111
getString(R.string.title_export)
111112
}));
112113
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.blogspot.e_kanivets.moneytracker.helper;
22

3+
import android.content.ContentValues;
34
import android.content.Context;
45
import android.database.sqlite.SQLiteDatabase;
56
import android.database.sqlite.SQLiteOpenHelper;
@@ -12,6 +13,12 @@
1213
*/
1314
public class DBHelper extends SQLiteOpenHelper {
1415

16+
/* DB_VERSION = 1 */
17+
public static final String DB_NAME = "database";
18+
public static final int DB_VERSION = 2;
19+
public static final String TABLE_RECORDS = "records";
20+
public static final String TABLE_CATEGORIES = "categories";
21+
1522
public static final String ID_COLUMN = "id";
1623
public static final String TIME_COLUMN = "time";
1724
public static final String TYPE_COLUMN = "type";
@@ -20,27 +27,92 @@ public class DBHelper extends SQLiteOpenHelper {
2027
public static final String PRICE_COLUMN = "price";
2128
public static final String NAME_COLUMN = "name";
2229

30+
/* DB_VERSION = 2 */
31+
public static final String TABLE_ACCOUNTS = "accounts";
32+
33+
public static final String ACCOUNT_ID_COLUMN = "account_id";
34+
public static final String CUR_SUM_COLUMN = "cur_sum";
35+
public static final String DEFAULT_ACCOUNT = "default_account";
36+
2337
public DBHelper(Context context) {
24-
super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
38+
super(context, DB_NAME, null, DB_VERSION);
2539
}
2640

2741
@Override
2842
public void onCreate(SQLiteDatabase db) {
29-
db.execSQL("CREATE TABLE " + Constants.TABLE_RECORDS + "("
30-
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
31-
+ TIME_COLUMN + " INTEGER,"
32-
+ TYPE_COLUMN + " INTEGER,"
33-
+ TITLE_COLUMN + " TEXT,"
34-
+ CATEGORY_ID_COLUMN + " INTEGER,"
35-
+ PRICE_COLUMN + " INTEGER" + ");");
36-
37-
db.execSQL("CREATE TABLE " + Constants.TABLE_CATEGORIES + "("
38-
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
39-
+ NAME_COLUMN + " TEXT" + ");");
43+
//createDbVersion1(db);
44+
createDbVersion2(db);
4045
}
4146

4247
@Override
4348
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
49+
if (oldVersion == 1 && newVersion == 2) {
50+
db.beginTransaction();
51+
52+
/* Create accounts table */
53+
db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "("
54+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
55+
+ TITLE_COLUMN + " TEXT,"
56+
+ CUR_SUM_COLUMN + " INTEGER);");
57+
58+
/* Add account_id column into the records table */
59+
db.execSQL("ALTER TABLE " + TABLE_RECORDS + " ADD COLUMN " + ACCOUNT_ID_COLUMN + " INTEGER;");
60+
61+
/* Insert default account for all records from DB_VERSION = 1 */
62+
ContentValues contentValues = new ContentValues();
63+
contentValues.put(TITLE_COLUMN, DEFAULT_ACCOUNT);
64+
contentValues.put(CUR_SUM_COLUMN, 0);
65+
int id = (int) db.insert(TABLE_ACCOUNTS, null, contentValues);
66+
67+
/* Set the default account for all records from DB_VERSION = 1 */
68+
contentValues = new ContentValues();
69+
contentValues.put(ACCOUNT_ID_COLUMN, id);
70+
db.update(DBHelper.TABLE_RECORDS, contentValues, null, null);
71+
72+
db.setTransactionSuccessful();
73+
74+
db.endTransaction();
75+
}
76+
}
77+
78+
private void createDbVersion1(SQLiteDatabase db) {
79+
db.execSQL("CREATE TABLE " + TABLE_RECORDS + "("
80+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
81+
+ TIME_COLUMN + " INTEGER,"
82+
+ TYPE_COLUMN + " INTEGER,"
83+
+ TITLE_COLUMN + " TEXT,"
84+
+ CATEGORY_ID_COLUMN + " INTEGER,"
85+
+ PRICE_COLUMN + " INTEGER" + ");");
86+
87+
db.execSQL("CREATE TABLE " + TABLE_CATEGORIES + "("
88+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
89+
+ NAME_COLUMN + " TEXT" + ");");
90+
}
91+
92+
private void createDbVersion2(SQLiteDatabase db) {
93+
db.execSQL("CREATE TABLE " + TABLE_RECORDS + "("
94+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
95+
+ TIME_COLUMN + " INTEGER,"
96+
+ TYPE_COLUMN + " INTEGER,"
97+
+ TITLE_COLUMN + " TEXT,"
98+
+ CATEGORY_ID_COLUMN + " INTEGER,"
99+
+ PRICE_COLUMN + " INTEGER"
100+
+ ACCOUNT_ID_COLUMN + " INTEGER" + ");");
101+
102+
db.execSQL("CREATE TABLE " + TABLE_CATEGORIES + "("
103+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
104+
+ NAME_COLUMN + " TEXT" + ");");
105+
106+
db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "("
107+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
108+
+ TITLE_COLUMN + " TEXT,"
109+
+ CUR_SUM_COLUMN + " INTEGER);");
110+
111+
/* Insert default account for all records from DB_VERSION = 1 */
112+
ContentValues contentValues = new ContentValues();
113+
contentValues.put(TITLE_COLUMN, DEFAULT_ACCOUNT);
114+
contentValues.put(CUR_SUM_COLUMN, 0);
44115

116+
db.insert(TABLE_ACCOUNTS, null, contentValues);
45117
}
46118
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void initialize() {
5151
SQLiteDatabase db = dbHelper.getReadableDatabase();
5252

5353
//Read categories table from db
54-
Cursor cursor = db.query(Constants.TABLE_CATEGORIES, null, null, null, null, null, null);
54+
Cursor cursor = db.query(DBHelper.TABLE_CATEGORIES, null, null, null, null, null, null);
5555
categories.clear();
5656

5757
if (cursor.moveToFirst()) {
@@ -77,7 +77,7 @@ public void initialize() {
7777
Long.toString(period.getLast().getTime())};
7878

7979
//Read records table from db
80-
cursor = db.query(Constants.TABLE_RECORDS, null, "time BETWEEN ? AND ?", args, null, null, null);
80+
cursor = db.query(DBHelper.TABLE_RECORDS, null, "time BETWEEN ? AND ?", args, null, null, null);
8181
records.clear();
8282

8383
if (cursor.moveToFirst()) {
@@ -134,7 +134,7 @@ public List<String> getRecordsForExport(long fromDate, long toDate) {
134134
Long.toString(toDate)};
135135

136136
//Read records table from db
137-
Cursor cursor = db.query(Constants.TABLE_RECORDS, null, "time BETWEEN ? AND ?", args, null, null, null);
137+
Cursor cursor = db.query(DBHelper.TABLE_RECORDS, null, "time BETWEEN ? AND ?", args, null, null, null);
138138

139139
if (cursor.moveToFirst()) {
140140
//Get indexes of columns
@@ -200,7 +200,7 @@ public void addRecord(long time, int type, String title, String category, int pr
200200
contentValues.put("category_id", categoryId);
201201
contentValues.put("price", price);
202202

203-
int id = (int) db.insert(Constants.TABLE_RECORDS, null, contentValues);
203+
int id = (int) db.insert(DBHelper.TABLE_RECORDS, null, contentValues);
204204

205205
db.close();
206206

@@ -226,7 +226,7 @@ public void updateRecordById(int id, String title, String category, int price) {
226226
contentValues.put("category_id", categoryId);
227227
contentValues.put("price", price);
228228

229-
db.update(Constants.TABLE_RECORDS, contentValues, "id=?", new String[]{Integer.valueOf(id).toString()});
229+
db.update(DBHelper.TABLE_RECORDS, contentValues, "id=?", new String[]{Integer.valueOf(id).toString()});
230230

231231
//Change particular record
232232
for (Record record : records) {
@@ -247,7 +247,7 @@ public void deleteRecordById(int id) {
247247
for (Record record : records) {
248248
if (record.getId() == id) {
249249
SQLiteDatabase db = dbHelper.getWritableDatabase();
250-
db.delete(Constants.TABLE_RECORDS, "id=?",
250+
db.delete(DBHelper.TABLE_RECORDS, "id=?",
251251
new String[]{Integer.toString(id)});
252252
db.close();
253253

@@ -269,7 +269,7 @@ public int addCategory(String name) {
269269
ContentValues contentValues = new ContentValues();
270270
contentValues.put("name", name);
271271

272-
int id = (int) db.insert(Constants.TABLE_CATEGORIES, null, contentValues);
272+
int id = (int) db.insert(DBHelper.TABLE_CATEGORIES, null, contentValues);
273273

274274
db.close();
275275

@@ -283,7 +283,7 @@ public void deleteCategoryById(int id) {
283283
for (Category category : categories) {
284284
if (category.getId() == id) {
285285
SQLiteDatabase db = dbHelper.getWritableDatabase();
286-
db.delete(Constants.TABLE_CATEGORIES, "id=?",
286+
db.delete(DBHelper.TABLE_CATEGORIES, "id=?",
287287
new String[]{Integer.toString(id)});
288288

289289
categories.remove(category);

app/src/main/java/com/blogspot/e_kanivets/moneytracker/util/Constants.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
* Created by eugene on 29/08/14.
66
*/
77
public class Constants {
8-
public static final String DB_NAME = "database";
9-
public static final int DB_VERSION = 1;
10-
public static final String TABLE_RECORDS = "records";
11-
public static final String TABLE_CATEGORIES = "categories";
12-
138
public static final String APP_NAME = "com.blogspot.e_kanivets.moneytracker";
149
public static final String GP_MARKET = "market://details?id=";
1510
public static final String APP_RATED = "app_rated";
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
3+
android:layout_height="match_parent"
4+
tools:context="com.blogspot.e_kanivets.moneytracker.fragment.AccountsFragment">
5+
6+
<!-- TODO: Update blank fragment layout -->
7+
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
8+
android:text="@string/hello_blank_fragment" />
9+
10+
</FrameLayout>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@
4545

4646
<!-- Not translated strings -->
4747
<string name="title_records">Records</string>
48+
<string name="title_accounts">Accounts</string>
4849
<string name="title_export">Export</string>
4950

5051
<string name="navigation_drawer_open">Open navigation drawer</string>
5152
<string name="navigation_drawer_close">Close navigation drawer</string>
5253

5354
<string name="export">Export</string>
5455

56+
<!-- TODO: Remove or change this placeholder text -->
57+
<string name="hello_blank_fragment">Hello blank fragment</string>
58+
5559
</resources>

0 commit comments

Comments
 (0)