From 7a35963729fec2eb669419bb70d4d54d0037b6e8 Mon Sep 17 00:00:00 2001 From: Vandolf Estrellado Date: Mon, 24 Jul 2017 11:15:30 -0400 Subject: [PATCH] Created example2: an activity with 2 fragments that utilizes the scoped utils. Closes #7 --- app/src/main/AndroidManifest.xml | 4 + .../daggerbutterknifemvp/AppModule.java | 17 +++- .../example_1/fragment/Example1Fragment.java | 4 +- .../ui/example_2/Example2Activity.java | 42 +++++++++ .../ui/example_2/Example2ActivityModule.java | 81 +++++++++++++++++ .../Example2ActivitySubcomponent.java | 40 +++++++++ .../fragment_a/Example2AFragment.java | 86 +++++++++++++++++++ .../fragment_a/Example2AFragmentModule.java | 49 +++++++++++ .../Example2AFragmentSubcomponent.java | 39 +++++++++ .../fragment_b/Example2BFragment.java | 86 +++++++++++++++++++ .../fragment_b/Example2BFragmentModule.java | 49 +++++++++++ .../Example2BFragmentSubcomponent.java | 39 +++++++++ .../ui/example_2/package-info.java | 20 +++++ .../ui/main/MainActivity.java | 5 +- .../main/res/layout/example_2_activity.xml | 46 ++++++++++ .../main/res/layout/example_2_fragment_a.xml | 41 +++++++++ .../main/res/layout/example_2_fragment_b.xml | 41 +++++++++ app/src/main/res/values/strings.xml | 2 + 18 files changed, 685 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2Activity.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2ActivityModule.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2ActivitySubcomponent.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragment.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragmentModule.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragmentSubcomponent.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragment.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragmentModule.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragmentSubcomponent.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/package-info.java create mode 100644 app/src/main/res/layout/example_2_activity.xml create mode 100644 app/src/main/res/layout/example_2_fragment_a.xml create mode 100644 app/src/main/res/layout/example_2_fragment_b.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dade2cf..ec5993f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -34,5 +34,9 @@ + + diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java index aa51050..3897e77 100644 --- a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java @@ -20,6 +20,8 @@ import com.vestrel00.daggerbutterknifemvp.ui.example_1.Example1Activity; import com.vestrel00.daggerbutterknifemvp.ui.example_1.Example1ActivitySubcomponent; +import com.vestrel00.daggerbutterknifemvp.ui.example_2.Example2Activity; +import com.vestrel00.daggerbutterknifemvp.ui.example_2.Example2ActivitySubcomponent; import com.vestrel00.daggerbutterknifemvp.ui.main.MainActivity; import com.vestrel00.daggerbutterknifemvp.ui.main.MainActivitySubcomponent; @@ -36,7 +38,8 @@ @Module(includes = AndroidInjectionModule.class, subcomponents = { MainActivitySubcomponent.class, - Example1ActivitySubcomponent.class + Example1ActivitySubcomponent.class, + Example2ActivitySubcomponent.class }) abstract class AppModule { @@ -51,7 +54,6 @@ abstract class AppModule { abstract AndroidInjector.Factory mainActivityInjectorFactory(MainActivitySubcomponent.Builder builder); - /** * Provides the injector for the {@link Example1Activity}, which has access to the dependencies * provided by this application instance (singleton scoped objects). @@ -62,4 +64,15 @@ abstract class AppModule { @ActivityKey(Example1Activity.class) abstract AndroidInjector.Factory example1ActivityInjectorFactory(Example1ActivitySubcomponent.Builder builder); + + /** + * Provides the injector for the {@link Example2Activity}, which has access to the dependencies + * provided by this application instance (singleton scoped objects). + */ + // TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + @Binds + @IntoMap + @ActivityKey(Example2Activity.class) + abstract AndroidInjector.Factory + example2ActivityInjectorFactory(Example2ActivitySubcomponent.Builder builder); } \ No newline at end of file diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1Fragment.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1Fragment.java index 21f66b6..a4b7599 100644 --- a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1Fragment.java +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1Fragment.java @@ -36,8 +36,6 @@ */ public final class Example1Fragment extends BaseFragment implements View.OnClickListener { - private TextView someText; - @Inject SingletonUtil singletonUtil; @@ -47,6 +45,8 @@ public final class Example1Fragment extends BaseFragment implements View.OnClick @Inject PerFragmentUtil perFragmentUtil; + private TextView someText; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2Activity.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2Activity.java new file mode 100644 index 0000000..6e2d39e --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2Activity.java @@ -0,0 +1,42 @@ +/* + * Copyright 2017 Vandolf Estrellado + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.vestrel00.daggerbutterknifemvp.ui.example_2; + +import android.os.Bundle; +import android.support.annotation.Nullable; + +import com.vestrel00.daggerbutterknifemvp.R; +import com.vestrel00.daggerbutterknifemvp.ui.common.BaseActivity; +import com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_a.Example2AFragment; +import com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_b.Example2BFragment; + +/** + * Activity that contains 2 Fragments. + */ +public final class Example2Activity extends BaseActivity { + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.example_2_activity); + + if (savedInstanceState == null) { + addFragment(R.id.fragment_a_container, new Example2AFragment()); + addFragment(R.id.fragment_b_container, new Example2BFragment()); + } + } +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2ActivityModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2ActivityModule.java new file mode 100644 index 0000000..d04b868 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2ActivityModule.java @@ -0,0 +1,81 @@ +/* + * Copyright 2017 Vandolf Estrellado + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.vestrel00.daggerbutterknifemvp.ui.example_2; + +import android.app.Activity; +import android.app.Fragment; + +import com.vestrel00.daggerbutterknifemvp.inject.PerActivity; +import com.vestrel00.daggerbutterknifemvp.ui.common.BaseActivityModule; +import com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_a.Example2AFragment; +import com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_a.Example2AFragmentSubcomponent; +import com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_b.Example2BFragment; +import com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_b.Example2BFragmentSubcomponent; + +import dagger.Binds; +import dagger.Module; +import dagger.android.AndroidInjector; +import dagger.android.FragmentKey; +import dagger.multibindings.IntoMap; + +/** + * Provides example 2 activity dependencies. + */ +@Module(includes = BaseActivityModule.class, + subcomponents = { + Example2AFragmentSubcomponent.class, + Example2BFragmentSubcomponent.class + }) +abstract class Example2ActivityModule { + + /** + * Provides the injector for the {@link Example2AFragment}, which has access to the dependencies + * provided by this activity and application instance (singleton scoped objects). + */ + // TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + @Binds + @IntoMap + @FragmentKey(Example2AFragment.class) + abstract AndroidInjector.Factory + example2AFragmentInjectorFactory(Example2AFragmentSubcomponent.Builder builder); + + /** + * Provides the injector for the {@link Example2BFragment}, which has access to the dependencies + * provided by this activity and application instance (singleton scoped objects). + */ + // TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + @Binds + @IntoMap + @FragmentKey(Example2BFragment.class) + abstract AndroidInjector.Factory + example2BFragmentInjectorFactory(Example2BFragmentSubcomponent.Builder builder); + + /** + * As per the contract specified in {@link BaseActivityModule}; "This must be included in all + * activity modules, which must rovide a concrete implementation of {@link Activity}." + *

+ * This provides the activity required to inject the + * {@link BaseActivityModule#ACTIVITY_FRAGMENT_MANAGER} into the + * {@link com.vestrel00.daggerbutterknifemvp.ui.common.BaseActivity}. + * + * @param example2Activity the example 2 activity + * @return the activity + */ + @Binds + @PerActivity + abstract Activity activity(Example2Activity example2Activity); +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2ActivitySubcomponent.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2ActivitySubcomponent.java new file mode 100644 index 0000000..3a89727 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/Example2ActivitySubcomponent.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017 Vandolf Estrellado + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.vestrel00.daggerbutterknifemvp.ui.example_2; + + +import com.vestrel00.daggerbutterknifemvp.inject.PerActivity; + +import dagger.Subcomponent; +import dagger.android.AndroidInjector; + +/** + * Injects example 2 activity dependencies. + *

+ * TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + */ +@PerActivity +@Subcomponent(modules = Example2ActivityModule.class) +public interface Example2ActivitySubcomponent extends AndroidInjector { + + /** + * The builder for this subcomponent. + */ + @Subcomponent.Builder + abstract class Builder extends AndroidInjector.Builder { + } +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragment.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragment.java new file mode 100644 index 0000000..d924a18 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragment.java @@ -0,0 +1,86 @@ +/* + * Copyright 2017 Vandolf Estrellado + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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; +import android.widget.TextView; + +import com.vestrel00.daggerbutterknifemvp.R; +import com.vestrel00.daggerbutterknifemvp.ui.common.BaseFragment; +import com.vestrel00.daggerbutterknifemvp.util.PerActivityUtil; +import com.vestrel00.daggerbutterknifemvp.util.PerFragmentUtil; +import com.vestrel00.daggerbutterknifemvp.util.SingletonUtil; + +import javax.inject.Inject; + +/** + * A fragment that contains a button that does something. + */ +public final class Example2AFragment extends BaseFragment implements View.OnClickListener { + + @Inject + SingletonUtil singletonUtil; + + @Inject + PerActivityUtil perActivityUtil; + + @Inject + PerFragmentUtil perFragmentUtil; + + private 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() { + String something = singletonUtil.doSomething(); + something += "\n" + perActivityUtil.doSomething(); + something += "\n" + perFragmentUtil.doSomething(); + showSomething(something); + } + + private void showSomething(String something) { + someText.setText(something); + } +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragmentModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragmentModule.java new file mode 100644 index 0000000..253d654 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragmentModule.java @@ -0,0 +1,49 @@ +/* + * Copyright 2017 Vandolf Estrellado + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_a; + +import android.app.Fragment; + +import com.vestrel00.daggerbutterknifemvp.inject.PerFragment; +import com.vestrel00.daggerbutterknifemvp.ui.common.BaseFragmentModule; + +import javax.inject.Named; + +import dagger.Binds; +import dagger.Module; + +/** + * Provides example 2 A fragment dependencies. + */ +@Module(includes = { + BaseFragmentModule.class, +}) +abstract class Example2AFragmentModule { + + /** + * As per the contract specified in {@link BaseFragmentModule}; "This must be included in all + * fragment modules, which must provide a concrete implementation of {@link Fragment} + * and named {@link BaseFragmentModule#FRAGMENT}. + * + * @param example2AFragment the example 2 A fragment + * @return the fragment + */ + @Binds + @Named(BaseFragmentModule.FRAGMENT) + @PerFragment + abstract Fragment fragment(Example2AFragment example2AFragment); +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragmentSubcomponent.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragmentSubcomponent.java new file mode 100644 index 0000000..ac31cd2 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_a/Example2AFragmentSubcomponent.java @@ -0,0 +1,39 @@ +/* + * Copyright 2017 Vandolf Estrellado + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_a; + +import com.vestrel00.daggerbutterknifemvp.inject.PerFragment; + +import dagger.Subcomponent; +import dagger.android.AndroidInjector; + +/** + * Injects example 2 A fragment dependencies. + *

+ * TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + */ +@PerFragment +@Subcomponent(modules = Example2AFragmentModule.class) +public interface Example2AFragmentSubcomponent extends AndroidInjector { + + /** + * The builder for this subcomponent. + */ + @Subcomponent.Builder + abstract class Builder extends AndroidInjector.Builder { + } +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragment.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragment.java new file mode 100644 index 0000000..18f52c3 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragment.java @@ -0,0 +1,86 @@ +/* + * Copyright 2017 Vandolf Estrellado + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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; +import android.widget.TextView; + +import com.vestrel00.daggerbutterknifemvp.R; +import com.vestrel00.daggerbutterknifemvp.ui.common.BaseFragment; +import com.vestrel00.daggerbutterknifemvp.util.PerActivityUtil; +import com.vestrel00.daggerbutterknifemvp.util.PerFragmentUtil; +import com.vestrel00.daggerbutterknifemvp.util.SingletonUtil; + +import javax.inject.Inject; + +/** + * A fragment that contains a button that does something. + */ +public final class Example2BFragment extends BaseFragment implements View.OnClickListener { + + @Inject + SingletonUtil singletonUtil; + + @Inject + PerActivityUtil perActivityUtil; + + @Inject + PerFragmentUtil perFragmentUtil; + + private 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() { + String something = singletonUtil.doSomething(); + something += "\n" + perActivityUtil.doSomething(); + something += "\n" + perFragmentUtil.doSomething(); + showSomething(something); + } + + private void showSomething(String something) { + someText.setText(something); + } +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragmentModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragmentModule.java new file mode 100644 index 0000000..9473430 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragmentModule.java @@ -0,0 +1,49 @@ +/* + * Copyright 2017 Vandolf Estrellado + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_b; + +import android.app.Fragment; + +import com.vestrel00.daggerbutterknifemvp.inject.PerFragment; +import com.vestrel00.daggerbutterknifemvp.ui.common.BaseFragmentModule; + +import javax.inject.Named; + +import dagger.Binds; +import dagger.Module; + +/** + * Provides example 2 B fragment dependencies. + */ +@Module(includes = { + BaseFragmentModule.class, +}) +abstract class Example2BFragmentModule { + + /** + * As per the contract specified in {@link BaseFragmentModule}; "This must be included in all + * fragment modules, which must provide a concrete implementation of {@link Fragment} + * and named {@link BaseFragmentModule#FRAGMENT}. + * + * @param example2BFragment the example 2 B fragment + * @return the fragment + */ + @Binds + @Named(BaseFragmentModule.FRAGMENT) + @PerFragment + abstract Fragment fragment(Example2BFragment example2BFragment); +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragmentSubcomponent.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragmentSubcomponent.java new file mode 100644 index 0000000..72e0830 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/fragment_b/Example2BFragmentSubcomponent.java @@ -0,0 +1,39 @@ +/* + * Copyright 2017 Vandolf Estrellado + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_b; + +import com.vestrel00.daggerbutterknifemvp.inject.PerFragment; + +import dagger.Subcomponent; +import dagger.android.AndroidInjector; + +/** + * Injects example 2 B fragment dependencies. + *

+ * TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + */ +@PerFragment +@Subcomponent(modules = Example2BFragmentModule.class) +public interface Example2BFragmentSubcomponent extends AndroidInjector { + + /** + * The builder for this subcomponent. + */ + @Subcomponent.Builder + abstract class Builder extends AndroidInjector.Builder { + } +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/package-info.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/package-info.java new file mode 100644 index 0000000..608a177 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_2/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2017 Vandolf Estrellado + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Shows how to create an activity that contains 2 fragments. + */ +package com.vestrel00.daggerbutterknifemvp.ui.example_2; \ No newline at end of file diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/main/MainActivity.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/main/MainActivity.java index fa7612d..00328d7 100644 --- a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/main/MainActivity.java +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/main/MainActivity.java @@ -24,6 +24,7 @@ import com.vestrel00.daggerbutterknifemvp.R; import com.vestrel00.daggerbutterknifemvp.ui.common.BaseActivity; import com.vestrel00.daggerbutterknifemvp.ui.example_1.Example1Activity; +import com.vestrel00.daggerbutterknifemvp.ui.example_2.Example2Activity; /** * The main activity that provides a way to navigate to all other activities. @@ -48,8 +49,8 @@ public void onExample1Clicked() { @Override public void onExample2Clicked() { - // TODO start example 2 activity - Toast.makeText(this, "Launch example 2", Toast.LENGTH_SHORT).show(); + Intent intent = new Intent(this, Example2Activity.class); + startActivity(intent); } @Override diff --git a/app/src/main/res/layout/example_2_activity.xml b/app/src/main/res/layout/example_2_activity.xml new file mode 100644 index 0000000..7a36547 --- /dev/null +++ b/app/src/main/res/layout/example_2_activity.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/example_2_fragment_a.xml b/app/src/main/res/layout/example_2_fragment_a.xml new file mode 100644 index 0000000..a36541e --- /dev/null +++ b/app/src/main/res/layout/example_2_fragment_a.xml @@ -0,0 +1,41 @@ + + + + + + + +