Skip to content

Commit

Permalink
Merge pull request #25 from vestrel00/feature/#8-create-example-3
Browse files Browse the repository at this point in the history
Created example3: an activity with 1 fragment that contains a child fragment that utilizes the scoped utils. Closes #8
  • Loading branch information
vestrel00 committed Jul 24, 2017
2 parents 3c0cd9b + bf1d911 commit 89ad5cd
Show file tree
Hide file tree
Showing 17 changed files with 688 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@
<activity
android:name=".ui.example_2.Example2Activity"
android:label="@string/example_2" />

<activity
android:name=".ui.example_3.Example3Activity"
android:label="@string/example_3" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,7 +41,8 @@
subcomponents = {
MainActivitySubcomponent.class,
Example1ActivitySubcomponent.class,
Example2ActivitySubcomponent.class
Example2ActivitySubcomponent.class,
Example3ActivitySubcomponent.class
})
abstract class AppModule {

Expand Down Expand Up @@ -75,4 +78,15 @@ abstract class AppModule {
@ActivityKey(Example2Activity.class)
abstract AndroidInjector.Factory<? extends Activity>
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<? extends Activity>
example3ActivityInjectorFactory(Example3ActivitySubcomponent.Builder builder);
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends Fragment>
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}."
* <p>
* 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);
}
Original file line number Diff line number Diff line change
@@ -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<Example3Activity> {

/**
* The builder for this subcomponent.
*/
@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<Example3Activity> {
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector
*/
@PerChildFragment
@Subcomponent(modules = Example3ChildFragmentModule.class)
public interface Example3ChildFragmentSubcomponent extends AndroidInjector<Example3ChildFragment> {

/**
* The builder for this subcomponent.
*/
@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<Example3ChildFragment> {
}
}
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit 89ad5cd

Please sign in to comment.