Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B: 2 - Migrated sample project view bindings to butterknife. Closes #11 #28

Merged
merged 1 commit into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import dagger.android.AndroidInjection;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
Expand Down Expand Up @@ -53,12 +58,51 @@ public abstract class BaseFragment extends Fragment implements HasFragmentInject
@Inject
DispatchingAndroidInjector<Fragment> fragmentInjector;

@Nullable
private Unbinder viewUnbinder;

@Override
public void onAttach(Context context) {
AndroidInjection.inject(this);
super.onAttach(context);
}

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
View view = getView();
if (view != null) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null check not needed since onViewStateRestored only gets called when the View returned in onCreateView is not null. See 7370d99

/*
* Bind the views here instead of in onViewCreated so that view state changed listeners
* are not invoked automatically without user interaction.
*
* If we bind before this method (e.g. onViewCreated), then any checked changed
* listeners bound by ButterKnife will be invoked during fragment recreation (since
* Android itself saves and restores the views' states.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing closing parenthesis.

*
* The lifecycle order is as follows (same if added via xml or java or if retain
* instance is true):
*
* onAttach
* onCreateView
* onViewCreated
* onActivityCreated
* onViewStateRestored
* onResume
*/
viewUnbinder = ButterKnife.bind(this, getView());
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the view instead of calling getView() again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be onStart between onViewStateRestoredand onResume?

Copy link
Owner Author

@vestrel00 vestrel00 Aug 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I didn't include onStart because it wasn't relevant here. I also omitted onCreate on purpose.

}
}

@Override
public void onDestroyView() {
if (viewUnbinder != null) {
viewUnbinder.unbind();
}
super.onDestroyView();
}


@Override
public final AndroidInjector<Fragment> fragmentInjector() {
return fragmentInjector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -31,10 +30,13 @@

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
* A fragment that contains a button that does something.
*/
public final class Example1Fragment extends BaseFragment implements View.OnClickListener {
public final class Example1Fragment extends BaseFragment {

@Inject
SingletonUtil singletonUtil;
Expand All @@ -45,35 +47,17 @@ public final class Example1Fragment extends BaseFragment implements View.OnClick
@Inject
PerFragmentUtil perFragmentUtil;

private TextView someText;
@BindView(R.id.some_text)
TextView someText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.example_1_fragment, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

// TODO (Butterknife) replace with butterknife view binding
someText = (TextView) view.findViewById(R.id.some_text);
view.findViewById(R.id.do_something).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.do_something:
onDoSomethingClicked();
break;
default:
throw new IllegalArgumentException("Unhandled view " + v.getId());
}
}

private void onDoSomethingClicked() {
@OnClick(R.id.do_something)
void onDoSomethingClicked() {
String something = singletonUtil.doSomething();
something += "\n" + perActivityUtil.doSomething();
something += "\n" + perFragmentUtil.doSomething();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_a;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -31,10 +30,13 @@

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
* A fragment that contains a button that does something.
*/
public final class Example2AFragment extends BaseFragment implements View.OnClickListener {
public final class Example2AFragment extends BaseFragment {

@Inject
SingletonUtil singletonUtil;
Expand All @@ -45,35 +47,17 @@ public final class Example2AFragment extends BaseFragment implements View.OnClic
@Inject
PerFragmentUtil perFragmentUtil;

private TextView someText;
@BindView(R.id.some_text)
TextView someText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.example_2_fragment_a, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

// TODO (Butterknife) replace with butterknife view binding
someText = (TextView) view.findViewById(R.id.some_text);
view.findViewById(R.id.do_something).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.do_something:
onDoSomethingClicked();
break;
default:
throw new IllegalArgumentException("Unhandled view " + v.getId());
}
}

private void onDoSomethingClicked() {
@OnClick(R.id.do_something)
void onDoSomethingClicked() {
String something = singletonUtil.doSomething();
something += "\n" + perActivityUtil.doSomething();
something += "\n" + perFragmentUtil.doSomething();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_b;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -31,10 +30,13 @@

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
* A fragment that contains a button that does something.
*/
public final class Example2BFragment extends BaseFragment implements View.OnClickListener {
public final class Example2BFragment extends BaseFragment {

@Inject
SingletonUtil singletonUtil;
Expand All @@ -45,35 +47,17 @@ public final class Example2BFragment extends BaseFragment implements View.OnClic
@Inject
PerFragmentUtil perFragmentUtil;

private TextView someText;
@BindView(R.id.some_text)
TextView someText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.example_2_fragment_b, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

// TODO (Butterknife) replace with butterknife view binding
someText = (TextView) view.findViewById(R.id.some_text);
view.findViewById(R.id.do_something).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.do_something:
onDoSomethingClicked();
break;
default:
throw new IllegalArgumentException("Unhandled view " + v.getId());
}
}

private void onDoSomethingClicked() {
@OnClick(R.id.do_something)
void onDoSomethingClicked() {
String something = singletonUtil.doSomething();
something += "\n" + perActivityUtil.doSomething();
something += "\n" + perFragmentUtil.doSomething();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.vestrel00.daggerbutterknifemvp.ui.example_3.child_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -32,10 +31,13 @@

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
* A fragment that contains a button that does something.
*/
public final class Example3ChildFragment extends BaseFragment implements View.OnClickListener {
public final class Example3ChildFragment extends BaseFragment {

@Inject
SingletonUtil singletonUtil;
Expand All @@ -49,35 +51,17 @@ public final class Example3ChildFragment extends BaseFragment implements View.On
@Inject
PerChildFragmentUtil perChildFragmentUtil;

private TextView someText;
@BindView(R.id.some_text)
TextView someText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.example_3_child_fragment, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

// TODO (Butterknife) replace with butterknife view binding
someText = (TextView) view.findViewById(R.id.some_text);
view.findViewById(R.id.do_something).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.do_something:
onDoSomethingClicked();
break;
default:
throw new IllegalArgumentException("Unhandled view " + v.getId());
}
}

private void onDoSomethingClicked() {
@OnClick(R.id.do_something)
void onDoSomethingClicked() {
String something = singletonUtil.doSomething();
something += "\n" + perActivityUtil.doSomething();
something += "\n" + perFragmentUtil.doSomething();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
* A fragment that contains a button that does something.
*/
public final class Example3ParentFragment extends BaseFragment implements View.OnClickListener {
public final class Example3ParentFragment extends BaseFragment {

@Inject
SingletonUtil singletonUtil;
Expand All @@ -46,7 +49,8 @@ public final class Example3ParentFragment extends BaseFragment implements View.O
@Inject
PerFragmentUtil perFragmentUtil;

private TextView someText;
@BindView(R.id.some_text)
TextView someText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Expand All @@ -61,24 +65,10 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
if (savedInstanceState == null) {
addChildFragment(R.id.child_fragment_container, new Example3ChildFragment());
}

// TODO (Butterknife) replace with butterknife view binding
someText = (TextView) view.findViewById(R.id.some_text);
view.findViewById(R.id.do_something).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.do_something:
onDoSomethingClicked();
break;
default:
throw new IllegalArgumentException("Unhandled view " + v.getId());
}
}

private void onDoSomethingClicked() {
@OnClick(R.id.do_something)
void onDoSomethingClicked() {
String something = singletonUtil.doSomething();
something += "\n" + perActivityUtil.doSomething();
something += "\n" + perFragmentUtil.doSomething();
Expand Down
Loading