Skip to content

Commit

Permalink
Merge pull request #24 from vestrel00/feature/#7-create-example-2
Browse files Browse the repository at this point in the history
Created example2: an activity with 2 fragments that utilizes the scoped utils. Closes #7
  • Loading branch information
vestrel00 committed Jul 24, 2017
2 parents 33c9c68 + 7a35963 commit 3c0cd9b
Show file tree
Hide file tree
Showing 18 changed files with 685 additions and 6 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@
<activity
android:name=".ui.example_1.Example1Activity"
android:label="@string/example_1" />

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

Expand All @@ -36,7 +38,8 @@
@Module(includes = AndroidInjectionModule.class,
subcomponents = {
MainActivitySubcomponent.class,
Example1ActivitySubcomponent.class
Example1ActivitySubcomponent.class,
Example2ActivitySubcomponent.class
})
abstract class AppModule {

Expand All @@ -51,7 +54,6 @@ abstract class AppModule {
abstract AndroidInjector.Factory<? extends Activity>
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).
Expand All @@ -62,4 +64,15 @@ abstract class AppModule {
@ActivityKey(Example1Activity.class)
abstract AndroidInjector.Factory<? extends Activity>
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<? extends Activity>
example2ActivityInjectorFactory(Example2ActivitySubcomponent.Builder builder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
*/
public final class Example1Fragment extends BaseFragment implements View.OnClickListener {

private TextView someText;

@Inject
SingletonUtil singletonUtil;

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends Fragment>
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<? extends Fragment>
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}."
* <p>
* 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);
}
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_2;


import com.vestrel00.daggerbutterknifemvp.inject.PerActivity;

import dagger.Subcomponent;
import dagger.android.AndroidInjector;

/**
* Injects example 2 activity dependencies.
* <p>
* TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector
*/
@PerActivity
@Subcomponent(modules = Example2ActivityModule.class)
public interface Example2ActivitySubcomponent extends AndroidInjector<Example2Activity> {

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

0 comments on commit 3c0cd9b

Please sign in to comment.