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

Commit cbc7038

Browse files
author
evgenii
committed
Added an AddAccountFragment.
1 parent 93c8ae5 commit cbc7038

File tree

6 files changed

+220
-4
lines changed

6 files changed

+220
-4
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.blogspot.e_kanivets.moneytracker.R;
1313
import com.blogspot.e_kanivets.moneytracker.fragment.AccountsFragment;
14+
import com.blogspot.e_kanivets.moneytracker.fragment.AddAccountFragment;
1415
import com.blogspot.e_kanivets.moneytracker.fragment.AddExpenseFragment;
1516
import com.blogspot.e_kanivets.moneytracker.fragment.AddIncomeFragment;
1617
import com.blogspot.e_kanivets.moneytracker.fragment.ExportFragment;
@@ -85,6 +86,10 @@ public void onSectionAttached(String tag) {
8586
mTitle = getString(R.string.title_accounts);
8687
break;
8788

89+
case AddAccountFragment.TAG:
90+
mTitle = getString(R.string.title_add_account);
91+
break;
92+
8893
case AddExpenseFragment.TAG:
8994
mTitle = getString(R.string.title_add_expense);
9095
break;
@@ -175,4 +180,12 @@ private void showAddExpenseFragment(Record record, AddExpenseFragment.Mode mode)
175180
.addToBackStack("")
176181
.commit();
177182
}
183+
184+
public void showAddAccountFragment() {
185+
FragmentManager fragmentManager = getSupportFragmentManager();
186+
fragmentManager.beginTransaction()
187+
.replace(R.id.container, AddAccountFragment.newInstance())
188+
.addToBackStack(AddAccountFragment.TAG)
189+
.commit();
190+
}
178191
}

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
import com.blogspot.e_kanivets.moneytracker.adapter.RecordAdapter;
1818
import com.blogspot.e_kanivets.moneytracker.helper.MTHelper;
1919

20+
import java.util.Observable;
21+
import java.util.Observer;
22+
2023
/**
2124
* A simple {@link Fragment} subclass.
2225
* Use the {@link AccountsFragment#newInstance} factory method to
2326
* create an instance of this fragment.
2427
*/
25-
public class AccountsFragment extends Fragment implements View.OnClickListener {
28+
public class AccountsFragment extends Fragment implements View.OnClickListener, Observer {
2629
public static final String TAG = "AccountsFragment";
2730

31+
private ListView listView;
32+
2833
/**
2934
* Use this factory method to create a new instance of
3035
* this fragment using the provided parameters.
@@ -55,37 +60,48 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
5560
// Inflate the layout for this fragment
5661
View rootView = inflater.inflate(R.layout.fragment_accounts, container, false);
5762
initViews(rootView);
58-
initActionBar();
5963
return rootView;
6064
}
6165

6266
@Override
6367
public void onAttach(Activity activity) {
6468
super.onAttach(activity);
6569

70+
initActionBar();
71+
6672
((NavDrawerActivity) activity).onSectionAttached(TAG);
6773
}
6874

6975
@Override
7076
public void onClick(View v) {
7177
switch (v.getId()) {
7278
case R.id.btn_add_account:
73-
MTHelper.getInstance().addAccount("Cash", 2000);
79+
((NavDrawerActivity) getActivity()).showAddAccountFragment();
7480
break;
7581

7682
default:
7783
break;
7884
}
7985
}
8086

87+
@Override
88+
public void update(Observable observable, Object data) {
89+
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
90+
}
91+
8192
private void initViews(View rootView) {
8293
if (rootView != null) {
83-
ListView listView = (ListView) rootView.findViewById(R.id.list_view);
94+
listView = (ListView) rootView.findViewById(R.id.list_view);
8495

8596
rootView.findViewById(R.id.btn_add_account).setOnClickListener(this);
8697

8798
listView.setAdapter(new AccountAdapter(getActivity(), MTHelper.getInstance().getAccounts()));
8899
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
100+
101+
//Subscribe to helper
102+
MTHelper.getInstance().addObserver(this);
103+
104+
((NavDrawerActivity) getActivity()).onSectionAttached(TAG);
89105
}
90106
}
91107

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.blogspot.e_kanivets.moneytracker.fragment;
2+
3+
import android.app.Activity;
4+
import android.net.Uri;
5+
import android.os.Bundle;
6+
import android.support.v4.app.Fragment;
7+
import android.support.v7.app.ActionBar;
8+
import android.support.v7.app.ActionBarActivity;
9+
import android.view.LayoutInflater;
10+
import android.view.Menu;
11+
import android.view.MenuInflater;
12+
import android.view.MenuItem;
13+
import android.view.View;
14+
import android.view.ViewGroup;
15+
import android.widget.EditText;
16+
import android.widget.Toast;
17+
18+
import com.blogspot.e_kanivets.moneytracker.R;
19+
import com.blogspot.e_kanivets.moneytracker.activity.NavDrawerActivity;
20+
import com.blogspot.e_kanivets.moneytracker.helper.MTHelper;
21+
22+
import java.util.Date;
23+
24+
/**
25+
* A simple {@link Fragment} subclass.
26+
* Use the {@link AddAccountFragment#newInstance} factory method to
27+
* create an instance of this fragment.
28+
*/
29+
public class AddAccountFragment extends Fragment {
30+
public static final String TAG = "AddAccountFragment";
31+
32+
private EditText etTitle;
33+
private EditText etInitSum;
34+
35+
/**
36+
* Use this factory method to create a new instance of
37+
* this fragment using the provided parameters.
38+
*
39+
* @return A new instance of fragment AddAccountFragment.
40+
*/
41+
public static AddAccountFragment newInstance() {
42+
AddAccountFragment fragment = new AddAccountFragment();
43+
Bundle args = new Bundle();
44+
fragment.setArguments(args);
45+
return fragment;
46+
}
47+
48+
public AddAccountFragment() {
49+
// Required empty public constructor
50+
}
51+
52+
@Override
53+
public void onCreate(Bundle savedInstanceState) {
54+
super.onCreate(savedInstanceState);
55+
if (getArguments() != null) {
56+
}
57+
}
58+
59+
@Override
60+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
61+
Bundle savedInstanceState) {
62+
// Inflate the layout for this fragment
63+
View rootView = inflater.inflate(R.layout.fragment_add_account, container, false);
64+
initViews(rootView);
65+
initActionBar();
66+
setHasOptionsMenu(true);
67+
return rootView;
68+
}
69+
70+
@Override
71+
public void onAttach(Activity activity) {
72+
super.onAttach(activity);
73+
74+
((NavDrawerActivity) activity).onSectionAttached(TAG);
75+
}
76+
77+
@Override
78+
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
79+
inflater.inflate(R.menu.menu_add_record, menu);
80+
super.onCreateOptionsMenu(menu, inflater);
81+
}
82+
83+
@Override
84+
public boolean onOptionsItemSelected(MenuItem item) {
85+
switch (item.getItemId()) {
86+
case R.id.action_done:
87+
String title = etTitle.getText().toString();
88+
int initSum = Integer.parseInt(etInitSum.getText().toString());
89+
90+
MTHelper.getInstance().addAccount(title, initSum);
91+
92+
finish();
93+
return true;
94+
95+
case R.id.action_close:
96+
finish();
97+
return true;
98+
99+
default:
100+
return super.onOptionsItemSelected(item);
101+
}
102+
}
103+
104+
private void initViews(View rootView) {
105+
if (rootView != null) {
106+
etTitle = (EditText) rootView.findViewById(R.id.et_title);
107+
etInitSum = (EditText) rootView.findViewById(R.id.et_init_sum);
108+
}
109+
}
110+
111+
private void initActionBar() {
112+
ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
113+
if (actionBar != null) {
114+
actionBar.setCustomView(null);
115+
}
116+
}
117+
118+
private void finish() {
119+
getActivity().getSupportFragmentManager().popBackStack();
120+
}
121+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ public void addAccount(String title, int curSum) {
399399

400400
db.close();
401401

402+
//Add account to app list
403+
accounts.add(new Account(id, title, curSum));
404+
402405
//notify observers
403406
setChanged();
404407
notifyObservers();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical"
6+
tools:context="com.blogspot.e_kanivets.moneytracker.fragment.AddAccountFragment">
7+
8+
<LinearLayout
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content"
11+
android:orientation="vertical">
12+
13+
<LinearLayout
14+
android:layout_width="fill_parent"
15+
android:layout_height="wrap_content"
16+
android:layout_margin="2dp"
17+
android:orientation="horizontal">
18+
19+
<TextView
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent"
22+
android:layout_weight="3"
23+
android:gravity="center_vertical|left"
24+
android:paddingLeft="10dp"
25+
android:text="@string/title" />
26+
27+
<EditText
28+
android:id="@+id/et_title"
29+
android:layout_width="match_parent"
30+
android:layout_height="match_parent"
31+
android:layout_marginRight="10dp"
32+
android:layout_weight="1"
33+
android:inputType="text" />
34+
</LinearLayout>
35+
36+
<LinearLayout
37+
android:layout_width="match_parent"
38+
android:layout_height="wrap_content"
39+
android:layout_margin="2dp"
40+
android:layout_weight="1"
41+
android:orientation="horizontal">
42+
43+
<TextView
44+
android:layout_width="match_parent"
45+
android:layout_height="match_parent"
46+
android:layout_weight="3"
47+
android:gravity="center_vertical|left"
48+
android:paddingLeft="10dp"
49+
android:text="@string/initial_sum" />
50+
51+
<EditText
52+
android:id="@+id/et_init_sum"
53+
android:layout_width="match_parent"
54+
android:layout_height="match_parent"
55+
android:layout_marginRight="10dp"
56+
android:layout_weight="1"
57+
android:inputType="number" />
58+
59+
</LinearLayout>
60+
</LinearLayout>
61+
</LinearLayout>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@
4949
<string name="title_export">Export</string>
5050
<string name="title_add_income">Add income</string>
5151
<string name="title_add_expense">Add expense</string>
52+
<string name="title_add_account">Add account</string>
5253

5354
<string name="navigation_drawer_open">Open navigation drawer</string>
5455
<string name="navigation_drawer_close">Close navigation drawer</string>
5556

5657
<string name="export">Export</string>
5758
<string name="add_account">Add account</string>
59+
<string name="initial_sum">Initial sum</string>
5860

5961
</resources>

0 commit comments

Comments
 (0)