├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── me │ │ └── martinez │ │ └── sergio │ │ └── daggertwobasearchitecture │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── me │ │ └── martinez │ │ └── sergio │ │ └── daggertwobasearchitecture │ │ ├── activities │ │ ├── MainActivity.java │ │ ├── MainActivityComponent.java │ │ └── MainActivityModule.java │ │ ├── di │ │ ├── anotations │ │ │ └── scopes │ │ │ │ ├── PerActivity.java │ │ │ │ ├── PerFragment.java │ │ │ │ └── PerSection.java │ │ ├── components │ │ │ ├── AppComponent.java │ │ │ └── SectionComponentMain.java │ │ ├── injectableelements │ │ │ └── base │ │ │ │ ├── App.java │ │ │ │ ├── BaseFragment.java │ │ │ │ └── BaseInjectionActivity.java │ │ └── modules │ │ │ ├── ActivityModule.java │ │ │ ├── AppModule.java │ │ │ ├── FragmentModule.java │ │ │ └── IncludedModule.java │ │ ├── fragments │ │ ├── MainFragment.java │ │ ├── MainFragmentComponent.java │ │ ├── MainFragmentModule.java │ │ └── sections │ │ │ └── testsection │ │ │ ├── TestSectionModule.java │ │ │ ├── firststep │ │ │ ├── FirstFragment.java │ │ │ ├── FirstFragmentComponent.java │ │ │ ├── FirstFragmentModule.java │ │ │ └── presentation │ │ │ │ ├── FirstFragmentPresenter.java │ │ │ │ └── FirstFragmentView.java │ │ │ └── secondstep │ │ │ ├── SecondFragment.java │ │ │ ├── SecondFragmentComponent.java │ │ │ ├── SecondFragmentModule.java │ │ │ └── presentation │ │ │ ├── SecondFragmentPresenter.java │ │ │ └── SecondFragmentView.java │ │ ├── test │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ ├── E.java │ │ ├── NotScopedProperty.java │ │ └── SectionProperty.java │ │ └── utils │ │ ├── Log4Me.java │ │ └── Log4MeImpl.java │ └── res │ ├── layout │ ├── activity_main.xml │ └── fragment_main.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io 2 | 3 | ### Android ### 4 | # Built application files 5 | *.apk 6 | *.ap_ 7 | 8 | # Files for the Dalvik VM 9 | *.dex 10 | 11 | # Java class files 12 | *.class 13 | 14 | # Generated files 15 | bin/ 16 | gen/ 17 | 18 | # Gradle files 19 | .gradle/ 20 | build/ 21 | /*/build/ 22 | 23 | # Local configuration file (sdk path, etc) 24 | local.properties 25 | 26 | # Proguard folder generated by Eclipse 27 | proguard/ 28 | 29 | # Log Files 30 | *.log 31 | 32 | 33 | ### Intellij ### 34 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm 35 | 36 | *.iml 37 | 38 | ## Directory-based project format: 39 | .idea/ 40 | # if you remove the above rule, at least ignore the following: 41 | 42 | # User-specific stuff: 43 | # .idea/workspace.xml 44 | # .idea/tasks.xml 45 | # .idea/dictionaries 46 | 47 | # Sensitive or high-churn files: 48 | # .idea/dataSources.ids 49 | # .idea/dataSources.xml 50 | # .idea/sqlDataSources.xml 51 | # .idea/dynamic.xml 52 | # .idea/uiDesigner.xml 53 | 54 | # Gradle: 55 | # .idea/gradle.xml 56 | # .idea/libraries 57 | 58 | # Mongo Explorer plugin: 59 | # .idea/mongoSettings.xml 60 | 61 | ## File-based project format: 62 | *.ipr 63 | *.iws 64 | 65 | ## Plugin-specific files: 66 | 67 | # IntelliJ 68 | out/ 69 | 70 | # mpeltonen/sbt-idea plugin 71 | .idea_modules/ 72 | 73 | # JIRA plugin 74 | atlassian-ide-plugin.xml 75 | 76 | # Crashlytics plugin (for Android Studio and IntelliJ) 77 | com_crashlytics_export_strings.xml 78 | crashlytics.properties 79 | crashlytics-build.properties 80 | 81 | 82 | ### Eclipse ### 83 | *.pydevproject 84 | .metadata 85 | .gradle 86 | bin/ 87 | tmp/ 88 | *.tmp 89 | *.bak 90 | *.swp 91 | *~.nib 92 | local.properties 93 | .settings/ 94 | .loadpath 95 | 96 | # Eclipse Core 97 | .project 98 | 99 | # External tool builders 100 | .externalToolBuilders/ 101 | 102 | # Locally stored "Eclipse launch configurations" 103 | *.launch 104 | 105 | # CDT-specific 106 | .cproject 107 | 108 | # JDT-specific (Eclipse Java Development Tools) 109 | .classpath 110 | 111 | # PDT-specific 112 | .buildpath 113 | 114 | # sbteclipse plugin 115 | .target 116 | 117 | # TeXlipse plugin 118 | .texlipse 119 | 120 | 121 | ### Gradle ### 122 | .gradle 123 | build/ 124 | 125 | # Ignore Gradle GUI config 126 | gradle-app.setting 127 | 128 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 129 | !gradle-wrapper.jar 130 | 131 | 132 | ### OSX ### 133 | .DS_Store 134 | .AppleDouble 135 | .LSOverride 136 | 137 | # Icon must end with two \r 138 | Icon 139 | 140 | 141 | # Thumbnails 142 | ._* 143 | 144 | # Files that might appear on external disk 145 | .Spotlight-V100 146 | .Trashes 147 | 148 | # Directories potentially created on remote AFP share 149 | .AppleDB 150 | .AppleDesktop 151 | Network Trash Folder 152 | Temporary Items 153 | .apdisk -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DaggerTwoBaseArchitecture 2 | 3 | This dagger 2 implementation is giving support to some blog post that I've posted this week talking about my experience with Dagger 2. This implementation and the blog post try to be an approach that keeps the best practices I've learnt during the days I've spent playing with Dagger 2. Of course is not the best and nor the cleaner, but at least I feel more confortable with this implementation than with any other I've found googling. Anyway I really encourage readers to improve this approach, c'mon fork it, try something interesting and don't fotget... SHARE IT!! 4 | 5 | Don't laught about UI ... "What is essential is invisible to the eye" ... XD 6 | 7 | PD: I would really appreciate any comment or feedback you could give me! 8 | 9 | References: 10 | 11 | http://fsergio101dev.blogspot.com.es/2015/08/dagger-2-implementation.html --> Blog post about this code, follow and you will get an explanation about how has been this implementated. 12 | 13 | http://fsergio101dev.blogspot.com.es/2015/08/dagger-2-why-not.html --> Blog post about motivation of writting this code. 14 | 15 | https://github.com/android10/Android-CleanArchitecture --> Clean architecture with dagger 2 implementation 16 | 17 | 18 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'com.android.application' 18 | apply plugin: 'com.neenbedankt.android-apt' 19 | 20 | buildscript { 21 | repositories { 22 | jcenter() 23 | } 24 | dependencies { 25 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' 26 | } 27 | } 28 | 29 | 30 | android { 31 | compileSdkVersion 21 32 | buildToolsVersion "21.1.2" 33 | 34 | defaultConfig { 35 | applicationId "me.martinez.sergio.daggertwobasearchitecture" 36 | minSdkVersion 15 37 | targetSdkVersion 21 38 | versionCode 1 39 | versionName "1.0" 40 | } 41 | buildTypes { 42 | release { 43 | minifyEnabled false 44 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 45 | } 46 | } 47 | } 48 | 49 | dependencies { 50 | compile fileTree(dir: 'libs', include: ['*.jar']) 51 | compile 'com.android.support:appcompat-v7:22.2.0' 52 | apt 'com.google.dagger:dagger-compiler:2.0' 53 | compile 'com.google.dagger:dagger:2.0' 54 | provided 'org.glassfish:javax.annotation:10.0-b28' 55 | } 56 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/sergio.martinez/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/me/martinez/sergio/daggertwobasearchitecture/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture; 18 | 19 | import android.app.Application; 20 | import android.test.ApplicationTestCase; 21 | 22 | /** 23 | * Testing Fundamentals 24 | */ 25 | public class ApplicationTest extends ApplicationTestCase { 26 | public ApplicationTest() { 27 | super(Application.class); 28 | } 29 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 20 | 21 | 27 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/activities/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.activities; 18 | 19 | import android.os.Bundle; 20 | import android.support.v4.app.FragmentManager; 21 | import android.support.v4.app.FragmentTransaction; 22 | import android.view.Menu; 23 | import android.view.MenuItem; 24 | import android.view.View; 25 | import android.widget.Button; 26 | import java.util.List; 27 | import javax.inject.Inject; 28 | import me.martinez.sergio.daggertwobasearchitecture.di.components.DaggerSectionComponentMain; 29 | import me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base.BaseFragment; 30 | import me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base.BaseInjectionActivity; 31 | import me.martinez.sergio.daggertwobasearchitecture.di.modules.ActivityModule; 32 | import me.martinez.sergio.daggertwobasearchitecture.fragments.MainFragment; 33 | import me.martinez.sergio.daggertwobasearchitecture.R; 34 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.FirstFragment; 35 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.SecondFragment; 36 | import me.martinez.sergio.daggertwobasearchitecture.test.A; 37 | import me.martinez.sergio.daggertwobasearchitecture.test.B; 38 | import me.martinez.sergio.daggertwobasearchitecture.utils.Log4Me; 39 | 40 | public class MainActivity extends BaseInjectionActivity implements 41 | View.OnClickListener { 42 | 43 | private Button buttonClearFragments; 44 | private Button buttonAddFirstFragment; 45 | private Button buttonAddSecondFragment; 46 | 47 | @Inject A a; 48 | @Inject B b; 49 | @Inject Log4Me logger; 50 | @Inject List diInjectionHistory; 51 | 52 | private Object sectionComponent; 53 | 54 | @Override protected void onCreate(Bundle savedInstanceState) { 55 | super.onCreate(savedInstanceState); 56 | setContentView(R.layout.activity_main); 57 | initButtons(); 58 | initFragment(); 59 | initOnClickListeners(); 60 | testDI(); 61 | } 62 | 63 | private void initButtons() { 64 | buttonAddSecondFragment = (Button) findViewById(R.id.buttonAddFirstFragment); 65 | buttonAddFirstFragment = (Button) findViewById(R.id.buttonAddSecondFragment); 66 | buttonClearFragments = (Button) findViewById(R.id.buttonClearFragments); 67 | } 68 | 69 | private void initOnClickListeners() { 70 | buttonAddSecondFragment.setOnClickListener(this); 71 | buttonAddFirstFragment.setOnClickListener(this); 72 | buttonClearFragments.setOnClickListener(this); 73 | } 74 | 75 | //region Init methods 76 | private void initFragment() { 77 | FragmentManager fragmentManager = getSupportFragmentManager(); 78 | MainFragment mainFragment = new MainFragment(); 79 | FragmentTransaction ft = fragmentManager.beginTransaction(); 80 | ft.replace(R.id.fragmentContainer, mainFragment); 81 | ft.commit(); 82 | } 83 | 84 | private void addFragment(BaseFragment baseFragment) { 85 | FragmentManager fragmentManager = getSupportFragmentManager(); 86 | FragmentTransaction ft = fragmentManager.beginTransaction(); 87 | ft.add(R.id.fragmentContainer, baseFragment). 88 | addToBackStack(baseFragment.getClass().getSimpleName()).commit(); 89 | } 90 | 91 | private void clearFragmentStack() { 92 | FragmentManager fragmentManager = getSupportFragmentManager(); 93 | fragmentManager.popBackStack(FirstFragment.class.getSimpleName(), FragmentManager.POP_BACK_STACK_INCLUSIVE); 94 | } 95 | 96 | //endregion 97 | 98 | //region Android AutoGenerated methods 99 | 100 | @Override public boolean onCreateOptionsMenu(Menu menu) { 101 | // Inflate the menu; this adds items to the action bar if it is present. 102 | getMenuInflater().inflate(R.menu.menu_main, menu); 103 | return true; 104 | } 105 | 106 | @Override public boolean onOptionsItemSelected(MenuItem item) { 107 | // Handle action bar item clicks here. The action bar will 108 | // automatically handle clicks on the Home/Up button, so long 109 | // as you specify a parent activity in AndroidManifest.xml. 110 | int id = item.getItemId(); 111 | 112 | //noinspection SimplifiableIfStatement 113 | if (id == R.id.action_settings) { 114 | return true; 115 | } 116 | 117 | return super.onOptionsItemSelected(item); 118 | } 119 | 120 | //endregion 121 | 122 | //region Test DI stuff 123 | private void testDI() { 124 | 125 | diInjectionHistory.add(MainActivity.class.getName()); 126 | diInjectionHistory.add(logger.toString()); 127 | diInjectionHistory.add(a.toString()); 128 | diInjectionHistory.add(b.toString()); 129 | 130 | } 131 | //endregion 132 | 133 | //region dependency injection Methods 134 | 135 | @Override protected void initDI() { 136 | activityComponent = DaggerMainActivityComponent.builder().appComponent(getAppComponent()) 137 | .activityModule(new ActivityModule(this)) 138 | .mainActivityModule(new MainActivityModule(this)).build(); 139 | activityComponent.injectActivity(this); 140 | } 141 | 142 | //region Section Stuff 143 | 144 | private void initSectionComponent() { 145 | sectionComponent = 146 | DaggerSectionComponentMain.builder().mainActivityComponent(activityComponent).build(); 147 | } 148 | 149 | private void clearSectionComponent(){ 150 | sectionComponent = null; 151 | } 152 | 153 | @Override 154 | public Object getActivityComponent() { 155 | if (sectionComponent!=null){ 156 | return sectionComponent; 157 | }else{ 158 | return activityComponent; 159 | } 160 | } 161 | 162 | //endregion 163 | 164 | //endregion 165 | 166 | @Override public void onClick(View v) { 167 | switch (v.getId()){ 168 | case R.id.buttonAddFirstFragment: 169 | initSectionComponent(); 170 | addFragment(new FirstFragment()); 171 | break; 172 | case R.id.buttonAddSecondFragment: 173 | addFragment(new SecondFragment()); 174 | break; 175 | case R.id.buttonClearFragments: 176 | clearSectionComponent(); 177 | clearFragmentStack(); 178 | break; 179 | default: 180 | break; 181 | } 182 | } 183 | 184 | 185 | 186 | } 187 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/activities/MainActivityComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.activities; 18 | 19 | import dagger.Component; 20 | import java.util.List; 21 | import me.martinez.sergio.daggertwobasearchitecture.di.components.AppComponent; 22 | import me.martinez.sergio.daggertwobasearchitecture.di.modules.ActivityModule; 23 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerActivity; 24 | import me.martinez.sergio.daggertwobasearchitecture.fragments.MainFragment; 25 | import me.martinez.sergio.daggertwobasearchitecture.test.A; 26 | import me.martinez.sergio.daggertwobasearchitecture.utils.Log4Me; 27 | 28 | /** 29 | * Created by Sergio Martinez Rodriguez 30 | * Date 15/7/15. 31 | */ 32 | @PerActivity 33 | @Component(dependencies = AppComponent.class, modules = {ActivityModule.class, MainActivityModule.class}) 34 | public interface MainActivityComponent extends MainFragment.Pluser{ 35 | void injectActivity(MainActivity mainActivity); 36 | A provideA(); 37 | List provideDiInjectionHistory(); 38 | Log4Me provideLog4Me(); 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/activities/MainActivityModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.activities; 18 | 19 | import dagger.Module; 20 | 21 | /** 22 | * Created by Sergio Martinez Rodriguez 23 | * Date 15/7/15. 24 | */ 25 | @Module 26 | public class MainActivityModule{ 27 | 28 | MainActivity mainActivity; 29 | 30 | public MainActivityModule(MainActivity mainActivity) { 31 | this.mainActivity = mainActivity; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/anotations/scopes/PerActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes; 18 | 19 | import java.lang.annotation.Retention; 20 | import javax.inject.Scope; 21 | 22 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 23 | 24 | /** 25 | * Created by Sergio Martinez Rodriguez 26 | * Date 15/7/15. 27 | */ 28 | @Scope 29 | @Retention(RUNTIME) 30 | public @interface PerActivity {} 31 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/anotations/scopes/PerFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes; 18 | 19 | import java.lang.annotation.Retention; 20 | import javax.inject.Scope; 21 | 22 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 23 | 24 | /** 25 | * Created by Sergio Martinez Rodriguez 26 | * Date 15/7/15. 27 | */ 28 | @Scope 29 | @Retention(RUNTIME) 30 | public @interface PerFragment {} 31 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/anotations/scopes/PerSection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes; 18 | 19 | import java.lang.annotation.Retention; 20 | import javax.inject.Scope; 21 | 22 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 23 | 24 | /** 25 | * Created by Sergio Martinez Rodriguez 26 | * Date 15/7/15. 27 | */ 28 | @Scope 29 | @Retention(RUNTIME) 30 | public @interface PerSection {} 31 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/components/AppComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.components; 18 | 19 | import dagger.Component; 20 | import java.util.List; 21 | import javax.inject.Singleton; 22 | import me.martinez.sergio.daggertwobasearchitecture.test.A; 23 | import me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base.App; 24 | import me.martinez.sergio.daggertwobasearchitecture.di.modules.AppModule; 25 | import me.martinez.sergio.daggertwobasearchitecture.utils.Log4Me; 26 | 27 | /** 28 | * Created by Sergio Martinez Rodriguez 29 | * Date 15/7/15. 30 | */ 31 | @Singleton @Component(modules = AppModule.class) 32 | public interface AppComponent { 33 | void inject(App app); 34 | A provideA(); 35 | List provideDiInjectionHistory(); 36 | Log4Me provideLog4Me(); 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/components/SectionComponentMain.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.components; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 15/7/15. 22 | */ 23 | 24 | import dagger.Component; 25 | import me.martinez.sergio.daggertwobasearchitecture.activities.MainActivityComponent; 26 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerSection; 27 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.TestSectionModule; 28 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.FirstFragment; 29 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.SecondFragment; 30 | import me.martinez.sergio.daggertwobasearchitecture.test.SectionProperty; 31 | 32 | @PerSection 33 | @Component( dependencies = MainActivityComponent.class, modules = TestSectionModule.class) 34 | public interface SectionComponentMain extends 35 | FirstFragment.Pluser, 36 | SecondFragment.Pluser{ 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/injectableelements/base/App.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base; 18 | 19 | import android.app.Application; 20 | import android.util.Log; 21 | import java.util.HashMap; 22 | import java.util.List; 23 | import javax.inject.Inject; 24 | import me.martinez.sergio.daggertwobasearchitecture.di.components.AppComponent; 25 | import me.martinez.sergio.daggertwobasearchitecture.di.components.DaggerAppComponent; 26 | import me.martinez.sergio.daggertwobasearchitecture.test.A; 27 | import me.martinez.sergio.daggertwobasearchitecture.utils.Log4Me; 28 | 29 | /** 30 | * Created by Sergio Martinez Rodriguez 31 | * Date 15/7/15. 32 | */ 33 | public class App extends Application { 34 | 35 | @Inject A a; 36 | @Inject Log4Me logger; 37 | @Inject List diContainer; 38 | 39 | private AppComponent appComponent; 40 | 41 | @Override public void onCreate() { 42 | super.onCreate(); 43 | 44 | initDI(); 45 | testDI(); 46 | 47 | } 48 | 49 | private void initDI() { 50 | appComponent = DaggerAppComponent.create(); 51 | 52 | /** DaggerAppComponent.create() is equivalent to : 53 | * DaggerAppComponent.builder().appModule(new AppModule()).build() 54 | * when module has not arguments 55 | **/ 56 | 57 | appComponent.inject(this); 58 | } 59 | 60 | private void testDI() { 61 | 62 | diContainer.add(App.class.getName()); 63 | 64 | if (logger != null ){ 65 | diContainer.add(logger.toString()); 66 | } 67 | 68 | if (a!=null){ 69 | logger.log("Dependency Injection is working"); 70 | diContainer.add(a.toString()); 71 | 72 | } 73 | } 74 | 75 | public AppComponent getAppComponent() { 76 | return appComponent; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/injectableelements/base/BaseFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base; 18 | 19 | import android.os.Bundle; 20 | import android.support.v4.app.Fragment; 21 | 22 | /** 23 | * Created by Sergio Martinez Rodriguez 24 | * Date 15/7/15. 25 | */ 26 | public abstract class BaseFragment extends Fragment { 27 | 28 | protected T fragmentComponent; 29 | 30 | @Override public void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | initDIComponent(); 33 | setRetainInstance(true); 34 | } 35 | 36 | public T getFragmentComponent() { 37 | return fragmentComponent; 38 | } 39 | 40 | protected abstract void initDIComponent(); 41 | 42 | /** 43 | * Gets a component for dependency injection by its type. 44 | */ 45 | protected C getParentComponent(Class componentType) { 46 | return componentType.cast(((BaseInjectionActivity) getActivity()).getActivityComponent()); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/injectableelements/base/BaseInjectionActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base; 18 | 19 | import android.os.Bundle; 20 | import android.support.v7.app.AppCompatActivity; 21 | import me.martinez.sergio.daggertwobasearchitecture.di.components.AppComponent; 22 | 23 | /** 24 | * Created by Sergio Martinez Rodriguez 25 | * Date 15/7/15. 26 | */ 27 | public abstract class BaseInjectionActivity extends AppCompatActivity{ 28 | 29 | protected T activityComponent; 30 | 31 | @Override protected void onCreate(Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | initDI(); 34 | } 35 | 36 | protected abstract void initDI(); 37 | 38 | public AppComponent getAppComponent() { 39 | AppComponent appComponent = ((App) getApplication()).getAppComponent(); 40 | return appComponent; 41 | } 42 | 43 | public Object getActivityComponent() { 44 | return activityComponent; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/modules/ActivityModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.modules; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerActivity; 22 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerSection; 23 | import me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base.BaseInjectionActivity; 24 | import me.martinez.sergio.daggertwobasearchitecture.test.B; 25 | import me.martinez.sergio.daggertwobasearchitecture.test.SectionProperty; 26 | 27 | /** 28 | * Created by Sergio Martinez Rodriguez 29 | * Date 15/7/15. 30 | */ 31 | @Module 32 | public class ActivityModule { 33 | 34 | BaseInjectionActivity baseInjectionActivity; 35 | 36 | public ActivityModule(BaseInjectionActivity baseInjectionActivity) { 37 | this.baseInjectionActivity = baseInjectionActivity; 38 | } 39 | 40 | @Provides @PerActivity B provideB(){ 41 | return new B(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/modules/AppModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.modules; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | import javax.inject.Singleton; 24 | 25 | /** 26 | * Created by Sergio Martinez Rodriguez 27 | * Date 15/7/15. 28 | */ 29 | @Module(includes = IncludedModule.class) 30 | public class AppModule { 31 | 32 | @Singleton @Provides List provideDiInjectionHistory(){ 33 | return new ArrayList<>(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/modules/FragmentModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.modules; 18 | 19 | import dagger.Module; 20 | import me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base.BaseFragment; 21 | 22 | /** 23 | * Created by Sergio Martinez Rodriguez 24 | * Date 16/7/15. 25 | */ 26 | @Module 27 | public class FragmentModule { 28 | BaseFragment baseFragment; 29 | 30 | public FragmentModule(BaseFragment baseFragment) { 31 | this.baseFragment = baseFragment; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/di/modules/IncludedModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.di.modules; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import javax.inject.Singleton; 22 | import me.martinez.sergio.daggertwobasearchitecture.test.A; 23 | import me.martinez.sergio.daggertwobasearchitecture.utils.Log4Me; 24 | import me.martinez.sergio.daggertwobasearchitecture.utils.Log4MeImpl; 25 | 26 | /** 27 | * Created by Sergio Martinez Rodriguez 28 | * Date 15/7/15. 29 | */ 30 | @Module 31 | public class IncludedModule { 32 | @Provides @Singleton A provideA(){ 33 | return new A(); 34 | } 35 | @Provides @Singleton Log4Me provideLog4Me(){ 36 | return new Log4MeImpl(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/MainFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments; 18 | 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import java.util.List; 25 | import javax.inject.Inject; 26 | import me.martinez.sergio.daggertwobasearchitecture.R; 27 | import me.martinez.sergio.daggertwobasearchitecture.activities.MainActivityComponent; 28 | import me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base.BaseFragment; 29 | import me.martinez.sergio.daggertwobasearchitecture.di.modules.FragmentModule; 30 | import me.martinez.sergio.daggertwobasearchitecture.test.A; 31 | import me.martinez.sergio.daggertwobasearchitecture.test.B; 32 | import me.martinez.sergio.daggertwobasearchitecture.test.C; 33 | import me.martinez.sergio.daggertwobasearchitecture.test.D; 34 | 35 | /** 36 | * Created by Sergio Martinez Rodriguez 37 | * Date 15/7/15. 38 | */ 39 | public class MainFragment extends BaseFragment { 40 | 41 | @Inject B b; 42 | @Inject A a; 43 | @Inject C c; 44 | @Inject List diInjectioHistoric; 45 | 46 | @Override 47 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 48 | return inflater.inflate(R.layout.fragment_main, container, false); 49 | } 50 | 51 | @Override public void onStart() { 52 | super.onStart(); 53 | testDI(); 54 | } 55 | 56 | private void testDI() { 57 | diInjectioHistoric.add(MainFragment.class.getName()); 58 | diInjectioHistoric.add(b.toString()); 59 | diInjectioHistoric.add(a.toString()); 60 | diInjectioHistoric.add(c.toString()); 61 | } 62 | 63 | //region dependency injection Methods 64 | @Override protected void initDIComponent() { 65 | fragmentComponent = (getParentComponent(MainActivityComponent.class)).plus( 66 | new FragmentModule(this), new MainFragmentModule(this)); 67 | fragmentComponent.injectFragment(this); 68 | } 69 | 70 | public interface Pluser{ 71 | MainFragmentComponent plus(FragmentModule secondFragmentModule, MainFragmentModule baseFragmentModule); 72 | } 73 | //endregion 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/MainFragmentComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 15/7/15. 22 | */ 23 | 24 | import dagger.Subcomponent; 25 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerFragment; 26 | import me.martinez.sergio.daggertwobasearchitecture.di.modules.FragmentModule; 27 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.FirstFragment; 28 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.FirstFragmentModule; 29 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.SecondFragment; 30 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.SecondFragmentModule; 31 | 32 | @PerFragment 33 | @Subcomponent( modules = {FragmentModule.class, MainFragmentModule.class}) 34 | public interface MainFragmentComponent{ 35 | void injectFragment(MainFragment mainFragment); 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/MainFragmentModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import me.martinez.sergio.daggertwobasearchitecture.test.C; 22 | 23 | /** 24 | * Created by Sergio Martinez Rodriguez 25 | * Date 15/7/15. 26 | */ 27 | @Module() 28 | public class MainFragmentModule{ 29 | MainFragment mainFragment; 30 | 31 | public MainFragmentModule(MainFragment mainFragment) { 32 | this.mainFragment = mainFragment; 33 | } 34 | 35 | @Provides C provideC(){ 36 | return new C(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/TestSectionModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import javax.inject.Singleton; 22 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerSection; 23 | import me.martinez.sergio.daggertwobasearchitecture.test.NotScopedProperty; 24 | import me.martinez.sergio.daggertwobasearchitecture.test.SectionProperty; 25 | 26 | /** 27 | * Created by Sergio Martinez Rodriguez 28 | * Date 16/7/15. 29 | */ 30 | @Module 31 | public class TestSectionModule{ 32 | 33 | @PerSection @Provides public SectionProperty provideSectionProperty(){ 34 | return new SectionProperty(); 35 | } 36 | 37 | @Provides public NotScopedProperty provideNotScopedProperty(){ 38 | return new NotScopedProperty(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/firststep/FirstFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep; 18 | 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import java.util.List; 25 | import javax.inject.Inject; 26 | import me.martinez.sergio.daggertwobasearchitecture.R; 27 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerSection; 28 | import me.martinez.sergio.daggertwobasearchitecture.di.components.SectionComponentMain; 29 | import me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base.BaseFragment; 30 | import me.martinez.sergio.daggertwobasearchitecture.di.modules.FragmentModule; 31 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.presentation.FirstFragmentPresenter; 32 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.presentation.FirstFragmentView; 33 | import me.martinez.sergio.daggertwobasearchitecture.test.A; 34 | import me.martinez.sergio.daggertwobasearchitecture.test.D; 35 | import me.martinez.sergio.daggertwobasearchitecture.test.E; 36 | import me.martinez.sergio.daggertwobasearchitecture.test.NotScopedProperty; 37 | import me.martinez.sergio.daggertwobasearchitecture.test.SectionProperty; 38 | 39 | /** 40 | * Created by Sergio Martinez Rodriguez 41 | * Date 15/7/15. 42 | */ 43 | public class FirstFragment extends BaseFragment implements 44 | FirstFragmentView { 45 | 46 | @Inject A a; 47 | @Inject E e; 48 | @Inject D d; 49 | @Inject List diInjectionHistory; 50 | @Inject FirstFragmentPresenter firstFragmentPresenter; 51 | @Inject SectionProperty sectionProperty; 52 | @Inject NotScopedProperty notScopedProperty; 53 | 54 | @Override 55 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 56 | return inflater.inflate(R.layout.fragment_main, container, false); 57 | } 58 | 59 | @Override public void onStart() { 60 | super.onStart(); 61 | testDI(); 62 | } 63 | 64 | //region Test DI stuff 65 | private void testDI() { 66 | 67 | diInjectionHistory.add(FirstFragment.class.getName()); 68 | diInjectionHistory.add(firstFragmentPresenter.toString()); 69 | diInjectionHistory.add(notScopedProperty.toString()); 70 | diInjectionHistory.add(sectionProperty.toString()); 71 | diInjectionHistory.add(a.toString()); 72 | diInjectionHistory.add(e.toString()); 73 | diInjectionHistory.add(d.toString()); 74 | 75 | } 76 | 77 | @Override protected void initDIComponent() { 78 | fragmentComponent = (getParentComponent(SectionComponentMain.class)).plus( 79 | new FragmentModule(this), new FirstFragmentModule(this)); 80 | fragmentComponent.injectFragment(this); 81 | } 82 | 83 | public interface Pluser{ 84 | FirstFragmentComponent plus(FragmentModule fragmentModule, FirstFragmentModule firstFragmentModule); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/firststep/FirstFragmentComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 15/7/15. 22 | */ 23 | 24 | import dagger.Subcomponent; 25 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerFragment; 26 | import me.martinez.sergio.daggertwobasearchitecture.di.modules.FragmentModule; 27 | 28 | @PerFragment 29 | @Subcomponent( modules = {FragmentModule.class, FirstFragmentModule.class}) 30 | public interface FirstFragmentComponent { 31 | void injectFragment(FirstFragment firstFragment); 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/firststep/FirstFragmentModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerFragment; 22 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.presentation.FirstFragmentPresenter; 23 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.presentation.FirstFragmentView; 24 | import me.martinez.sergio.daggertwobasearchitecture.test.D; 25 | import me.martinez.sergio.daggertwobasearchitecture.test.E; 26 | 27 | /** 28 | * Created by Sergio Martinez Rodriguez 29 | * Date 15/7/15. 30 | */ 31 | @Module() 32 | public class FirstFragmentModule{ 33 | 34 | private FirstFragmentView firstFragmentView; 35 | 36 | public FirstFragmentModule(FirstFragmentView firstFragmentView) { 37 | this.firstFragmentView = firstFragmentView; 38 | } 39 | 40 | @PerFragment @Provides FirstFragmentPresenter provideFirstFragmentPresenter(D d){ 41 | return new FirstFragmentPresenter(firstFragmentView, d); 42 | } 43 | 44 | @PerFragment @Provides E provideE(){ 45 | return new E(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/firststep/presentation/FirstFragmentPresenter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.presentation; 18 | 19 | import me.martinez.sergio.daggertwobasearchitecture.test.D; 20 | 21 | /** 22 | * Created by Sergio Martinez Rodriguez 23 | * Date 18/7/15. 24 | */ 25 | public class FirstFragmentPresenter { 26 | 27 | private final FirstFragmentView firstFragmentView; 28 | private final D d; 29 | 30 | public FirstFragmentPresenter(FirstFragmentView firstFragmentView, D d) { 31 | this.firstFragmentView = firstFragmentView; 32 | this.d = d; 33 | } 34 | 35 | public D getD(){ 36 | return d; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/firststep/presentation/FirstFragmentView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.presentation; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 15/7/15. 22 | */ 23 | public interface FirstFragmentView{ 24 | 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/secondstep/SecondFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep; 18 | 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import java.util.List; 25 | import javax.inject.Inject; 26 | import me.martinez.sergio.daggertwobasearchitecture.R; 27 | import me.martinez.sergio.daggertwobasearchitecture.di.components.SectionComponentMain; 28 | import me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base.BaseFragment; 29 | import me.martinez.sergio.daggertwobasearchitecture.di.modules.FragmentModule; 30 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.presentation.SecondFragmentPresenter; 31 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.presentation.SecondFragmentView; 32 | import me.martinez.sergio.daggertwobasearchitecture.test.D; 33 | import me.martinez.sergio.daggertwobasearchitecture.test.E; 34 | import me.martinez.sergio.daggertwobasearchitecture.test.NotScopedProperty; 35 | import me.martinez.sergio.daggertwobasearchitecture.test.SectionProperty; 36 | import me.martinez.sergio.daggertwobasearchitecture.utils.Log4Me; 37 | 38 | /** 39 | * Created by Sergio Martinez Rodriguez 40 | * Date 15/7/15. 41 | */ 42 | public class SecondFragment extends BaseFragment implements 43 | SecondFragmentView{ 44 | 45 | @Inject SecondFragmentPresenter secondFragmentPresenter; 46 | @Inject SectionProperty sectionProperty; 47 | @Inject NotScopedProperty notScopedProperty; 48 | @Inject List diInjectionHistory; 49 | @Inject Log4Me logger; 50 | @Inject E e; 51 | @Inject D d; 52 | 53 | @Override 54 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 55 | return inflater.inflate(R.layout.fragment_main, container, false); 56 | } 57 | 58 | @Override public void onStart() { 59 | super.onStart(); 60 | testDI(); 61 | } 62 | 63 | //region Test DI stuff 64 | private void testDI() { 65 | 66 | diInjectionHistory.add(SecondFragment.class.getName()); 67 | diInjectionHistory.add(secondFragmentPresenter.toString()); 68 | diInjectionHistory.add(notScopedProperty.toString()); 69 | diInjectionHistory.add(sectionProperty.toString()); 70 | diInjectionHistory.add(logger.toString()); 71 | diInjectionHistory.add(e.toString()); 72 | diInjectionHistory.add(d.toString()); 73 | 74 | logger.log(diInjectionHistory.toString()); 75 | 76 | 77 | } 78 | 79 | @Override protected void initDIComponent() { 80 | fragmentComponent = (getParentComponent(SectionComponentMain.class)).plus( 81 | new FragmentModule(this), new SecondFragmentModule(this)); 82 | fragmentComponent.injectFragment(this); 83 | } 84 | 85 | public interface Pluser{ 86 | SecondFragmentComponent plus(FragmentModule secondFragmentModule, SecondFragmentModule firstFragmentModule); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/secondstep/SecondFragmentComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 15/7/15. 22 | */ 23 | 24 | import dagger.Subcomponent; 25 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerFragment; 26 | import me.martinez.sergio.daggertwobasearchitecture.di.modules.FragmentModule; 27 | 28 | @PerFragment 29 | @Subcomponent( modules = {FragmentModule.class, SecondFragmentModule.class}) 30 | public interface SecondFragmentComponent { 31 | void injectFragment(SecondFragment secondFragment); 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/secondstep/SecondFragmentModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import me.martinez.sergio.daggertwobasearchitecture.di.anotations.scopes.PerFragment; 22 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.presentation.SecondFragmentPresenter; 23 | import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.presentation.SecondFragmentView; 24 | import me.martinez.sergio.daggertwobasearchitecture.test.E; 25 | 26 | /** 27 | * Created by Sergio Martinez Rodriguez 28 | * Date 15/7/15. 29 | */ 30 | @Module() 31 | public class SecondFragmentModule{ 32 | 33 | private SecondFragmentView secondFragmentView; 34 | 35 | public SecondFragmentModule(SecondFragmentView secondFragmentView) { 36 | this.secondFragmentView = secondFragmentView; 37 | } 38 | 39 | @PerFragment @Provides SecondFragmentPresenter provideSecondFragmentPresenter(){ 40 | return new SecondFragmentPresenter(secondFragmentView); 41 | } 42 | 43 | @PerFragment @Provides E provideE(){ 44 | return new E(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/secondstep/presentation/SecondFragmentPresenter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.presentation; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 18/7/15. 22 | */ 23 | public class SecondFragmentPresenter { 24 | 25 | private final SecondFragmentView secondFragmentView; 26 | 27 | public SecondFragmentPresenter(SecondFragmentView secondFragmentView) { 28 | this.secondFragmentView = secondFragmentView; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/fragments/sections/testsection/secondstep/presentation/SecondFragmentView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.presentation; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 15/7/15. 22 | */ 23 | public interface SecondFragmentView { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/test/A.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.test; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 15/7/15. 22 | */ 23 | public class A {} 24 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/test/B.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.test; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 15/7/15. 22 | */ 23 | public class B {} 24 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/test/C.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.test; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 15/7/15. 22 | */ 23 | public class C {} 24 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/test/D.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.test; 18 | 19 | import javax.inject.Inject; 20 | 21 | /** 22 | * Created by Sergio Martinez Rodriguez 23 | * Date 15/7/15. 24 | */ 25 | public class D { 26 | 27 | @Inject 28 | public D() { 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/test/E.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.test; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 15/7/15. 22 | */ 23 | public class E { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/test/NotScopedProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.test; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 11/8/15. 22 | */ 23 | public class NotScopedProperty { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/test/SectionProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.test; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 18/7/15. 22 | */ 23 | public class SectionProperty { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/utils/Log4Me.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.utils; 18 | 19 | /** 20 | * Created by Sergio Martinez Rodriguez 21 | * Date 13/8/15. 22 | */ 23 | public interface Log4Me { 24 | void log(String message, int logLevel, Object clazz); 25 | void log(String message, int logLevel); 26 | void log(String message); 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/me/martinez/sergio/daggertwobasearchitecture/utils/Log4MeImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Sergio Martinez Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package me.martinez.sergio.daggertwobasearchitecture.utils; 18 | 19 | import android.util.Log; 20 | import javax.inject.Inject; 21 | 22 | /** 23 | * Created by Sergio Martinez Rodriguez 24 | * Date 13/8/15. 25 | */ 26 | public class Log4MeImpl implements Log4Me { 27 | 28 | private int defaultLoglevel; 29 | private static final String LOGS_PREFIX = "DI TESTS :: "; 30 | 31 | public Log4MeImpl() { 32 | defaultLoglevel = Log.DEBUG; 33 | } 34 | 35 | @Override public void log(String message, int logLevel, Object clazz) { 36 | if (logLevel == Log.DEBUG){ 37 | Log.d(clazz.getClass().getName(), LOGS_PREFIX + message); 38 | } 39 | } 40 | 41 | @Override public void log(String message, int logLevel) { 42 | log(message, defaultLoglevel, Log4MeImpl.class.getName()) ; 43 | } 44 | 45 | @Override public void log(String message) { 46 | log(message, defaultLoglevel); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 26 | 27 | 34 |