diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 225084f..dade2cf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -30,5 +30,9 @@ + + diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java index a901f81..aa51050 100644 --- a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/AppModule.java @@ -18,6 +18,8 @@ import android.app.Activity; +import com.vestrel00.daggerbutterknifemvp.ui.example_1.Example1Activity; +import com.vestrel00.daggerbutterknifemvp.ui.example_1.Example1ActivitySubcomponent; import com.vestrel00.daggerbutterknifemvp.ui.main.MainActivity; import com.vestrel00.daggerbutterknifemvp.ui.main.MainActivitySubcomponent; @@ -32,7 +34,10 @@ * Provides application-wide dependencies. */ @Module(includes = AndroidInjectionModule.class, - subcomponents = MainActivitySubcomponent.class) + subcomponents = { + MainActivitySubcomponent.class, + Example1ActivitySubcomponent.class + }) abstract class AppModule { /** @@ -45,4 +50,16 @@ abstract class AppModule { @ActivityKey(MainActivity.class) 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). + */ + // TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + @Binds + @IntoMap + @ActivityKey(Example1Activity.class) + abstract AndroidInjector.Factory + example1ActivityInjectorFactory(Example1ActivitySubcomponent.Builder builder); } \ No newline at end of file diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/Example1Activity.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/Example1Activity.java new file mode 100644 index 0000000..aaf66db --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/Example1Activity.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_1; + +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_1.fragment.Example1Fragment; + +/** + * Activity that contains a single Fragment. + */ +public final class Example1Activity extends BaseActivity { + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.example_1_activity); + + if (savedInstanceState == null) { + addFragment(R.id.fragment_container, new Example1Fragment()); + } + } +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/Example1ActivityModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/Example1ActivityModule.java new file mode 100644 index 0000000..a4bb19c --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/Example1ActivityModule.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_1; + +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_1.fragment.Example1Fragment; +import com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.Example1FragmentSubcomponent; + +import dagger.Binds; +import dagger.Module; +import dagger.android.AndroidInjector; +import dagger.android.FragmentKey; +import dagger.multibindings.IntoMap; + +/** + * Provides example 1 activity dependencies. + */ +@Module(includes = BaseActivityModule.class, + subcomponents = Example1FragmentSubcomponent.class) +abstract class Example1ActivityModule { + + /** + * Provides the injector for the {@link Example1Fragment}, 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(Example1Fragment.class) + abstract AndroidInjector.Factory + example1FragmentInjectorFactory(Example1FragmentSubcomponent.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 example1Activity the example 1 activity + * @return the activity + */ + @Binds + @PerActivity + abstract Activity activity(Example1Activity example1Activity); +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/Example1ActivitySubcomponent.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/Example1ActivitySubcomponent.java new file mode 100644 index 0000000..dff6e71 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/Example1ActivitySubcomponent.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_1; + + +import com.vestrel00.daggerbutterknifemvp.inject.PerActivity; + +import dagger.Subcomponent; +import dagger.android.AndroidInjector; + +/** + * Injects example 1 activity dependencies. + *

+ * TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + */ +@PerActivity +@Subcomponent(modules = Example1ActivityModule.class) +public interface Example1ActivitySubcomponent 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_1/fragment/Example1Fragment.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1Fragment.java new file mode 100644 index 0000000..21f66b6 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1Fragment.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_1.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.PerFragmentUtil; +import com.vestrel00.daggerbutterknifemvp.util.SingletonUtil; + +import javax.inject.Inject; + +/** + * A fragment that contains a button that does something. + */ +public final class Example1Fragment extends BaseFragment implements View.OnClickListener { + + private TextView someText; + + @Inject + SingletonUtil singletonUtil; + + @Inject + PerActivityUtil perActivityUtil; + + @Inject + PerFragmentUtil perFragmentUtil; + + @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() { + 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_1/fragment/Example1FragmentModule.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1FragmentModule.java new file mode 100644 index 0000000..e9c0008 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1FragmentModule.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_1.fragment; + +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 1 fragment dependencies. + */ +@Module(includes = { + BaseFragmentModule.class, +}) +abstract class Example1FragmentModule { + + /** + * 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 example1Fragment the example 1 fragment + * @return the fragment + */ + @Binds + @Named(BaseFragmentModule.FRAGMENT) + @PerFragment + abstract Fragment fragment(Example1Fragment example1Fragment); +} diff --git a/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1FragmentSubcomponent.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1FragmentSubcomponent.java new file mode 100644 index 0000000..2d16bfc --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/fragment/Example1FragmentSubcomponent.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_1.fragment; + +import com.vestrel00.daggerbutterknifemvp.inject.PerFragment; + +import dagger.Subcomponent; +import dagger.android.AndroidInjector; + +/** + * Injects example 1 fragment dependencies. + *

+ * TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector + */ +@PerFragment +@Subcomponent(modules = Example1FragmentModule.class) +public interface Example1FragmentSubcomponent 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_1/package-info.java b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/package-info.java new file mode 100644 index 0000000..9dc20d7 --- /dev/null +++ b/app/src/main/java/com/vestrel00/daggerbutterknifemvp/ui/example_1/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. + */ +package com.vestrel00.daggerbutterknifemvp.ui.example_1; \ 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 172e378..fa7612d 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 @@ -16,12 +16,14 @@ package com.vestrel00.daggerbutterknifemvp.ui.main; +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; /** * The main activity that provides a way to navigate to all other activities. @@ -40,8 +42,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { @Override public void onExample1Clicked() { - // TODO start example 1 activity - Toast.makeText(this, "Launch example 1", Toast.LENGTH_SHORT).show(); + Intent intent = new Intent(this, Example1Activity.class); + startActivity(intent); } @Override diff --git a/app/src/main/res/drawable/fragment_background.xml b/app/src/main/res/drawable/fragment_background.xml new file mode 100644 index 0000000..4c4a7d4 --- /dev/null +++ b/app/src/main/res/drawable/fragment_background.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/app/src/main/res/layout/example_1_activity.xml b/app/src/main/res/layout/example_1_activity.xml new file mode 100644 index 0000000..89ce10f --- /dev/null +++ b/app/src/main/res/layout/example_1_activity.xml @@ -0,0 +1,32 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/example_1_fragment.xml b/app/src/main/res/layout/example_1_fragment.xml new file mode 100644 index 0000000..6184dad --- /dev/null +++ b/app/src/main/res/layout/example_1_fragment.xml @@ -0,0 +1,41 @@ + + + + + + + +