From bf1d91136d2f6b790b7150b69084da85d05fdb39 Mon Sep 17 00:00:00 2001 From: Vandolf Estrellado Date: Mon, 24 Jul 2017 11:31:11 -0400 Subject: [PATCH] Created example3: an activity with 1 fragment that contains a child fragment that utilizes the scoped utils. Closes #8 --- app/src/main/AndroidManifest.xml | 4 + .../daggerbutterknifemvp/AppModule.java | 16 +++- .../ui/example_3/Example3Activity.java | 40 ++++++++ .../ui/example_3/Example3ActivityModule.java | 65 +++++++++++++ .../Example3ActivitySubcomponent.java | 38 ++++++++ .../child_fragment/Example3ChildFragment.java | 91 +++++++++++++++++++ .../Example3ChildFragmentModule.java | 49 ++++++++++ .../Example3ChildFragmentSubcomponent.java | 39 ++++++++ .../ui/example_3/package-info.java | 20 ++++ .../Example3ParentFragment.java | 91 +++++++++++++++++++ .../Example3ParentFragmentModule.java | 67 ++++++++++++++ .../Example3ParentFragmentSubcomponent.java | 40 ++++++++ .../ui/main/MainActivity.java | 6 +- .../main/res/layout/example_3_activity.xml | 36 ++++++++ .../res/layout/example_3_child_fragment.xml | 41 +++++++++ .../res/layout/example_3_parent_fragment.xml | 47 ++++++++++ app/src/main/res/values/strings.xml | 2 + 17 files changed, 688 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3Activity.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3ActivityModule.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3ActivitySubcomponent.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragment.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragmentModule.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragmentSubcomponent.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/package-info.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragment.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragmentModule.java create mode 100644 app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragmentSubcomponent.java create mode 100644 app/src/main/res/layout/example_3_activity.xml create mode 100644 app/src/main/res/layout/example_3_child_fragment.xml create mode 100644 app/src/main/res/layout/example_3_parent_fragment.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ec5993f..49135aa 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -38,5 +38,9 @@ + + diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java index 3897e77..58aa4af 100644 --- a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java @@ -22,6 +22,8 @@ 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.example_3.Example3Activity; +import com.vestrel00.daggerbutterknifemvp.ui.example_3.Example3ActivitySubcomponent; import com.vestrel00.daggerbutterknifemvp.ui.main.MainActivity; import com.vestrel00.daggerbutterknifemvp.ui.main.MainActivitySubcomponent; @@ -39,7 +41,8 @@ subcomponents = { MainActivitySubcomponent.class, Example1ActivitySubcomponent.class, - Example2ActivitySubcomponent.class + Example2ActivitySubcomponent.class, + Example3ActivitySubcomponent.class }) abstract class AppModule { @@ -75,4 +78,15 @@ abstract class AppModule { @ActivityKey(Example2Activity.class) abstract AndroidInjector.Factory example2ActivityInjectorFactory(Example2ActivitySubcomponent.Builder builder); + + /** + * Provides the injector for the {@link Example3Activity}, 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(Example3Activity.class) + abstract AndroidInjector.Factory + example3ActivityInjectorFactory(Example3ActivitySubcomponent.Builder builder); } \ No newline at end of file diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3Activity.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3Activity.java new file mode 100644 index 0000000..c56ee68 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3Activity.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_3; + +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_3.parent_fragment.Example3ParentFragment; + +/** + * Activity that contains a single fragment that contains a child fragment. + */ +public final class Example3Activity extends BaseActivity { + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.example_3_activity); + + if (savedInstanceState == null) { + addFragment(R.id.parent_fragment_container, new Example3ParentFragment()); + } + } +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3ActivityModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3ActivityModule.java new file mode 100644 index 0000000..a52cfa7 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3ActivityModule.java @@ -0,0 +1,65 @@ +/* + * 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_3; + +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_3.parent_fragment.Example3ParentFragment; +import com.vestrel00.daggerbutterknifemvp.ui.example_3.parent_fragment.Example3ParentFragmentSubcomponent; + +import dagger.Binds; +import dagger.Module; +import dagger.android.AndroidInjector; +import dagger.android.FragmentKey; +import dagger.multibindings.IntoMap; + +/** + * Provides example 3 activity dependencies. + */ +@Module(includes = BaseActivityModule.class, + subcomponents = Example3ParentFragmentSubcomponent.class) +abstract class Example3ActivityModule { + + /** + * Provides the injector for the {@link Example3ParentFragment}, 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(Example3ParentFragment.class) + abstract AndroidInjector.Factory + example3ParentFragmentInjectorFactory(Example3ParentFragmentSubcomponent.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 example3Activity the example 3 activity + * @return the activity + */ + @Binds + @PerActivity + abstract Activity activity(Example3Activity example3Activity); +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3ActivitySubcomponent.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3ActivitySubcomponent.java new file mode 100644 index 0000000..f19e402 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/Example3ActivitySubcomponent.java @@ -0,0 +1,38 @@ +/* + * 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_3; + + +import com.vestrel00.daggerbutterknifemvp.inject.PerActivity; + +import dagger.Subcomponent; +import dagger.android.AndroidInjector; + +/** + * Injects example 3 activity dependencies. + */ +@PerActivity +@Subcomponent(modules = Example3ActivityModule.class) +public interface Example3ActivitySubcomponent 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_3/child_fragment/Example3ChildFragment.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragment.java new file mode 100644 index 0000000..e9f2400 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragment.java @@ -0,0 +1,91 @@ +/* + * 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_3.child_fragment; + +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.PerChildFragmentUtil; +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 Example3ChildFragment extends BaseFragment implements View.OnClickListener { + + @Inject + SingletonUtil singletonUtil; + + @Inject + PerActivityUtil perActivityUtil; + + @Inject + PerFragmentUtil perFragmentUtil; + + @Inject + PerChildFragmentUtil perChildFragmentUtil; + + private 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() { + String something = singletonUtil.doSomething(); + something += "\n" + perActivityUtil.doSomething(); + something += "\n" + perFragmentUtil.doSomething(); + something += "\n" + perChildFragmentUtil.doSomething(); + showSomething(something); + } + + private void showSomething(String something) { + someText.setText(something); + } +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragmentModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragmentModule.java new file mode 100644 index 0000000..b528ea6 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragmentModule.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_3.child_fragment; + +import android.app.Fragment; + +import com.vestrel00.daggerbutterknifemvp.inject.PerChildFragment; +import com.vestrel00.daggerbutterknifemvp.ui.common.BaseChildFragmentModule; + +import javax.inject.Named; + +import dagger.Binds; +import dagger.Module; + +/** + * Provides example 3 child fragment dependencies. + */ +@Module(includes = { + BaseChildFragmentModule.class, +}) +abstract class Example3ChildFragmentModule { + + /** + * As per the contract specified in {@link BaseChildFragmentModule}; "This must be included in + * all child fragment modules, which must provide a concrete implementation of the child + * {@link Fragment} and named {@link BaseChildFragmentModule#CHILD_FRAGMENT}.. + * + * @param example3ChildFragment the example 3 child fragment + * @return the fragment + */ + @Binds + @Named(BaseChildFragmentModule.CHILD_FRAGMENT) + @PerChildFragment + abstract Fragment fragment(Example3ChildFragment example3ChildFragment); +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragmentSubcomponent.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragmentSubcomponent.java new file mode 100644 index 0000000..69d9380 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/child_fragment/Example3ChildFragmentSubcomponent.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_3.child_fragment; + +import com.vestrel00.daggerbutterknifemvp.inject.PerChildFragment; + +import dagger.Subcomponent; +import dagger.android.AndroidInjector; + +/** + * Injects example 3 child fragment dependencies. + *

+ * TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + */ +@PerChildFragment +@Subcomponent(modules = Example3ChildFragmentModule.class) +public interface Example3ChildFragmentSubcomponent 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_3/package-info.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/package-info.java new file mode 100644 index 0000000..601417f --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/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 a single fragment that contains a child fragment. + */ +package com.vestrel00.daggerbutterknifemvp.ui.example_3; \ No newline at end of file diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragment.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragment.java new file mode 100644 index 0000000..5fd2789 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragment.java @@ -0,0 +1,91 @@ +/* + * 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_3.parent_fragment; + +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.ui.example_3.child_fragment.Example3ChildFragment; +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 Example3ParentFragment 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_3_parent_fragment, container, false); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, 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() { + 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_3/parent_fragment/Example3ParentFragmentModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragmentModule.java new file mode 100644 index 0000000..2b5264d --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragmentModule.java @@ -0,0 +1,67 @@ +/* + * 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_3.parent_fragment; + +import android.app.Fragment; + +import com.vestrel00.daggerbutterknifemvp.inject.PerFragment; +import com.vestrel00.daggerbutterknifemvp.ui.common.BaseFragmentModule; +import com.vestrel00.daggerbutterknifemvp.ui.example_3.child_fragment.Example3ChildFragment; +import com.vestrel00.daggerbutterknifemvp.ui.example_3.child_fragment.Example3ChildFragmentSubcomponent; + +import javax.inject.Named; + +import dagger.Binds; +import dagger.Module; +import dagger.android.AndroidInjector; +import dagger.android.FragmentKey; +import dagger.multibindings.IntoMap; + +/** + * Provides example 3 parent fragment dependencies. + */ +@Module(includes = { + BaseFragmentModule.class +}, + subcomponents = Example3ChildFragmentSubcomponent.class) +abstract class Example3ParentFragmentModule { + + /** + * Provides the injector for the {@link Example3ChildFragment}, which has access to the + * dependencies provided by this fragment and activity and application instance + * (singleton scoped objects). + */ + // TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + @Binds + @IntoMap + @FragmentKey(Example3ChildFragment.class) + abstract AndroidInjector.Factory + example3ChildFragmentInjectorFactory(Example3ChildFragmentSubcomponent.Builder builder); + + /** + * 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 example3ParentFragment the example 3 parent fragment + * @return the fragment + */ + @Binds + @Named(BaseFragmentModule.FRAGMENT) + @PerFragment + abstract Fragment fragment(Example3ParentFragment example3ParentFragment); +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragmentSubcomponent.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragmentSubcomponent.java new file mode 100644 index 0000000..d903df0 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_3/parent_fragment/Example3ParentFragmentSubcomponent.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_3.parent_fragment; + +import com.vestrel00.daggerbutterknifemvp.inject.PerFragment; + +import dagger.Subcomponent; +import dagger.android.AndroidInjector; + +/** + * Injects example 3 parent fragment dependencies. + *

+ * TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + */ +@PerFragment +@Subcomponent(modules = Example3ParentFragmentModule.class) +public interface Example3ParentFragmentSubcomponent + 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/main/MainActivity.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/main/MainActivity.java index 00328d7..da3a8e4 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 @@ -19,12 +19,12 @@ import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; -import android.widget.Toast; 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; +import com.vestrel00.daggerbutterknifemvp.ui.example_3.Example3Activity; /** * The main activity that provides a way to navigate to all other activities. @@ -55,7 +55,7 @@ public void onExample2Clicked() { @Override public void onExample3Clicked() { - // TODO start example 3 activity - Toast.makeText(this, "Launch example 3", Toast.LENGTH_SHORT).show(); + Intent intent = new Intent(this, Example3Activity.class); + startActivity(intent); } } diff --git a/app/src/main/res/layout/example_3_activity.xml b/app/src/main/res/layout/example_3_activity.xml new file mode 100644 index 0000000..43399a3 --- /dev/null +++ b/app/src/main/res/layout/example_3_activity.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/example_3_child_fragment.xml b/app/src/main/res/layout/example_3_child_fragment.xml new file mode 100644 index 0000000..d1a71ce --- /dev/null +++ b/app/src/main/res/layout/example_3_child_fragment.xml @@ -0,0 +1,41 @@ + + + + + + + +