Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A: 6 - Created example1: an activity with a fragment that utilizes the scoped utils. Closes #6 #23

Merged
merged 1 commit into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

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

Expand All @@ -32,7 +34,10 @@
* Provides application-wide dependencies.
*/
@Module(includes = AndroidInjectionModule.class,
subcomponents = MainActivitySubcomponent.class)
subcomponents = {
MainActivitySubcomponent.class,
Example1ActivitySubcomponent.class
})
abstract class AppModule {

/**
Expand All @@ -45,4 +50,16 @@ abstract class AppModule {
@ActivityKey(MainActivity.class)
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).
*/
// TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector
@Binds
@IntoMap
@ActivityKey(Example1Activity.class)
abstract AndroidInjector.Factory<? extends Activity>
example1ActivityInjectorFactory(Example1ActivitySubcomponent.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_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());
}
}
}
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_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<? extends Fragment>
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}."
* <p>
* 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);
}
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_1;


import com.vestrel00.daggerbutterknifemvp.inject.PerActivity;

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

/**
* Injects example 1 activity dependencies.
* <p>
* TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector
*/
@PerActivity
@Subcomponent(modules = Example1ActivityModule.class)
public interface Example1ActivitySubcomponent extends AndroidInjector<Example1Activity> {

/**
* The builder for this subcomponent.
*/
@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<Example1Activity> {
}
}
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_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;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private should be below package protected to follow the public, protected, package, private access modifier order.

Fixed in the next PR.


@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);
}
}
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_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);
}
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_1.fragment;

import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;

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

/**
* Injects example 1 fragment dependencies.
* <p>
* TODO (ContributesAndroidInjector) remove this in favor of @ContributesAndroidInjector
*/
@PerFragment
@Subcomponent(modules = Example1FragmentModule.class)
public interface Example1FragmentSubcomponent extends AndroidInjector<Example1Fragment> {

/**
* The builder for this subcomponent.
*/
@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<Example1Fragment> {
}
}
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.
*/
package com.vestrel00.daggerbutterknifemvp.ui.example_1;
Loading