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

Commit ce26f1e

Browse files
author
Evgenii Kanivets
committed
#4[1h]. Refactor all fragments.
1 parent 3cc2561 commit ce26f1e

File tree

6 files changed

+110
-175
lines changed

6 files changed

+110
-175
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@
1414
import com.blogspot.e_kanivets.moneytracker.fragment.RecordsFragment;
1515
import com.blogspot.e_kanivets.moneytracker.util.PrefUtils;
1616

17+
import butterknife.Bind;
18+
1719
public class NavDrawerActivity extends BaseActivity
1820
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
1921
@SuppressWarnings("unused")
2022
private static final String TAG = "NavDrawerActivity";
23+
24+
@Bind(R.id.drawer_layout)
25+
DrawerLayout drawerLayout;
26+
2127
/**
2228
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
2329
*/
@@ -48,8 +54,7 @@ protected void initViews() {
4854
mTitle = getTitle();
4955

5056
// Set up the drawer.
51-
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
52-
(DrawerLayout) findViewById(R.id.drawer_layout));
57+
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, drawerLayout);
5358
}
5459

5560
@Override

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ protected void initViews() {
7878
accounts.add(account.getTitle());
7979
}
8080

81-
spinnerAccount = (Spinner) findViewById(R.id.spinner_account);
8281
spinnerAccount.setAdapter(new ArrayAdapter<>(AddRecordBaseActivity.this,
8382
android.R.layout.simple_list_item_1, accounts));
8483

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

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,20 @@
2323
import com.blogspot.e_kanivets.moneytracker.controller.AccountController;
2424
import com.blogspot.e_kanivets.moneytracker.helper.DbHelper;
2525

26-
/**
27-
* A simple {@link Fragment} subclass.
28-
* Use the {@link AccountsFragment#newInstance} factory method to
29-
* create an instance of this fragment.
30-
*/
31-
public class AccountsFragment extends Fragment implements View.OnClickListener {
26+
import butterknife.Bind;
27+
import butterknife.ButterKnife;
28+
import butterknife.OnClick;
29+
30+
public class AccountsFragment extends Fragment {
3231
public static final String TAG = "AccountsFragment";
3332

3433
private static final int REQUEST_ADD_ACCOUNT = 1;
3534

36-
private ListView listView;
35+
@Bind(R.id.list_view)
36+
ListView listView;
3737

3838
private AccountController accountController;
3939

40-
/**
41-
* Use this factory method to create a new instance of
42-
* this fragment using the provided parameters.
43-
*
44-
* @return A new instance of fragment AccountsFragment.
45-
*/
4640
public static AccountsFragment newInstance() {
4741
AccountsFragment fragment = new AccountsFragment();
4842
Bundle args = new Bundle();
@@ -79,23 +73,15 @@ public void onAttach(Activity activity) {
7973
((NavDrawerActivity) activity).onSectionAttached(TAG);
8074
}
8175

82-
@Override
83-
public void onClick(View v) {
84-
switch (v.getId()) {
85-
case R.id.btn_add_account:
86-
Intent intent = new Intent(getActivity(), AddAccountActivity.class);
87-
startActivityForResult(intent, REQUEST_ADD_ACCOUNT);
88-
break;
89-
90-
default:
91-
break;
92-
}
76+
@OnClick(R.id.btn_add_account)
77+
public void addAccount() {
78+
Intent intent = new Intent(getActivity(), AddAccountActivity.class);
79+
startActivityForResult(intent, REQUEST_ADD_ACCOUNT);
9380
}
9481

9582
@Override
9683
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
9784
super.onCreateContextMenu(menu, v, menuInfo);
98-
9985
getActivity().getMenuInflater().inflate(R.menu.menu_account, menu);
10086
}
10187

@@ -136,9 +122,7 @@ private void update() {
136122

137123
private void initViews(View rootView) {
138124
if (rootView != null) {
139-
listView = (ListView) rootView.findViewById(R.id.list_view);
140-
141-
rootView.findViewById(R.id.btn_add_account).setOnClickListener(this);
125+
ButterKnife.bind(this, rootView);
142126

143127
listView.setAdapter(new AccountAdapter(getActivity(), accountController.getAccounts()));
144128
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
@@ -150,8 +134,6 @@ private void initViews(View rootView) {
150134

151135
private void initActionBar() {
152136
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
153-
if (actionBar != null) {
154-
actionBar.setCustomView(null);
155-
}
137+
if (actionBar != null) actionBar.setCustomView(null);
156138
}
157139
}

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

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,12 @@
2121
import java.io.PrintWriter;
2222
import java.util.List;
2323

24-
/**
25-
* A simple {@link Fragment} subclass.
26-
* Use the {@link ExportFragment#newInstance} factory method to
27-
* create an instance of this fragment.
28-
*/
29-
public class ExportFragment extends Fragment implements View.OnClickListener {
24+
import butterknife.ButterKnife;
25+
import butterknife.OnClick;
26+
27+
public class ExportFragment extends Fragment {
3028
public static final String TAG = "ExportFragment";
3129

32-
/**
33-
* Use this factory method to create a new instance of
34-
* this fragment using the provided parameters.
35-
*
36-
* @return A new instance of fragment ExportFragment.
37-
*/
3830
public static ExportFragment newInstance() {
3931
ExportFragment fragment = new ExportFragment();
4032
Bundle args = new Bundle();
@@ -46,18 +38,9 @@ public ExportFragment() {
4638
// Required empty public constructor
4739
}
4840

49-
@Override
50-
public void onCreate(Bundle savedInstanceState) {
51-
super.onCreate(savedInstanceState);
52-
53-
if (getArguments() != null) {
54-
}
55-
}
56-
5741
@Override
5842
public View onCreateView(LayoutInflater inflater, ViewGroup container,
5943
Bundle savedInstanceState) {
60-
// Inflate the layout for this fragment
6144
View rootView = inflater.inflate(R.layout.fragment_export, container, false);
6245
initViews(rootView);
6346
initActionBar();
@@ -71,22 +54,8 @@ public void onAttach(Activity activity) {
7154
((NavDrawerActivity) activity).onSectionAttached(TAG);
7255
}
7356

74-
@Override
75-
public void onClick(View v) {
76-
switch (v.getId()) {
77-
case R.id.btn_export:
78-
exportRecords();
79-
break;
80-
81-
default:
82-
break;
83-
}
84-
}
85-
8657
private void initViews(View rootView) {
87-
if (rootView != null) {
88-
rootView.findViewById(R.id.btn_export).setOnClickListener(this);
89-
}
58+
if (rootView != null) ButterKnife.bind(this, rootView);
9059
}
9160

9261
private void initActionBar() {
@@ -96,7 +65,8 @@ private void initActionBar() {
9665
}
9766
}
9867

99-
private void exportRecords() {
68+
@OnClick(R.id.btn_export)
69+
public void exportRecords() {
10070
RecordController recordController = new RecordController(new DbHelper(getActivity()));
10171
List<String> records = recordController.getRecordsForExport(0, Long.MAX_VALUE);
10272

0 commit comments

Comments
 (0)