├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── mvvmapp ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── hibrianlee │ └── mvvmapp │ ├── MvvmApplication.java │ ├── activity │ └── ViewModelActivity.java │ ├── adapter │ └── RecyclerViewAdapter.java │ ├── fragment │ └── ViewModelFragment.java │ ├── inject │ ├── ActivityComponent.java │ ├── ActivityModule.java │ ├── AppComponent.java │ ├── AppContext.java │ ├── AppContextModule.java │ ├── AttachedActivity.java │ ├── AttachedViewModelActivity.java │ └── PerActivity.java │ └── viewmodel │ ├── ItemViewModel.java │ ├── RecyclerViewViewModel.java │ ├── ViewModel.java │ └── ViewModelBindings.java ├── sample ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── hibrianlee │ │ └── sample │ │ └── mvvm │ │ ├── BaseTest.java │ │ ├── MockModule.java │ │ ├── TestComponent.java │ │ └── viewmodel │ │ ├── AndroidVersionsActivityTest.java │ │ ├── ClickCountActivityTest.java │ │ ├── MainActivityTest.java │ │ └── MockViewModelFactory.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── hibrianlee │ │ │ └── sample │ │ │ └── mvvm │ │ │ ├── MvvmSampleApplication.java │ │ │ ├── SampleAppComponent.java │ │ │ ├── SampleAppModule.java │ │ │ ├── activity │ │ │ ├── AndroidVersionsActivity.java │ │ │ ├── BaseActivity.java │ │ │ ├── ClickCountActivity.java │ │ │ └── MainActivity.java │ │ │ ├── adapter │ │ │ └── AndroidVersionsAdapter.java │ │ │ ├── fragment │ │ │ ├── AndroidVersionsFragment.java │ │ │ └── BaseFragment.java │ │ │ ├── model │ │ │ └── AndroidVersion.java │ │ │ └── viewmodel │ │ │ ├── AndroidVersionItemViewModel.java │ │ │ ├── AndroidVersionsViewModel.java │ │ │ ├── AppViewModelFactory.java │ │ │ ├── ClickCountViewModel.java │ │ │ ├── MainViewModel.java │ │ │ └── ViewModelFactory.java │ └── res │ │ ├── drawable │ │ └── item_background.xml │ │ ├── layout │ │ ├── activity_android_versions.xml │ │ ├── activity_click_count.xml │ │ ├── activity_main.xml │ │ ├── fragment_android_versions.xml │ │ └── item_android_version.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── hibrianlee │ └── sample │ └── mvvm │ ├── BaseTest.java │ ├── TestComponent.java │ ├── TestModule.java │ └── viewmodel │ ├── AndroidVersionItemViewModelTest.java │ ├── AndroidVersionsViewModelTest.java │ ├── ClickCountViewModelTest.java │ ├── MainViewModelTest.java │ └── ViewModelTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | */build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | #misc 36 | .DS_STORE 37 | *.swp 38 | 39 | # Intellij project files 40 | *.iml 41 | *.ipr 42 | *.iws 43 | .idea/ 44 | -------------------------------------------------------------------------------- /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 | AndroidMvvm 2 | =============== 3 | Explore an Android app that: 4 | 5 | * Has (almost) no business logic in the Activity / Fragment. 6 | * Has fully unit testable ViewModels, including rotation (sorry Espresso). 7 | * Uses Android Data Binding. 8 | * Has minimum Espresso testing, solely to verify the bindings. 9 | 10 | Original blog post: https://medium.com/@hiBrianLee/writing-testable-android-mvvm-app-prelude-e49f7e6223 11 | 12 | Sample App 13 | ============= 14 | ### Main Screen 15 | 16 | ![Main Screen](../screenshots/screenshots/main_screen.png?raw=true) 17 | 18 | **Button Clicks** 19 | Goes to the Click Count screen. 20 | 21 | **RecyclerView** 22 | Goes to the Android Versions screen. 23 | 24 | **@hiBrianLee** 25 | Goes to https://twitter.com/hiBrianLee 26 | 27 | 28 | ### Click Count 29 | 30 | ![Click Count](../screenshots/screenshots/click_count.png?raw=true) 31 | 32 | **Click count: 0** 33 | Shows the total number of clicks 34 | 35 | **Click** 36 | Increments the number of clicks. 37 | 38 | **@hiBrianLee** 39 | Goes to https://twitter.com/hiBrianLee 40 | 41 | 42 | ### Android Versions 43 | 44 | ![Android Versions](../screenshots/screenshots/android_versions.png?raw=true) 45 | 46 | Lists the different Android versions in a RecyclerView. Clicking on an item will highlight it. 47 | 48 | **@hiBrianLee** 49 | Goes to https://twitter.com/hiBrianLee 50 | 51 | 52 | License 53 | ======= 54 | Copyright (c) 2015 Brian Lee (@hiBrianLee) 55 | 56 | Licensed under the Apache License, Version 2.0 (the "License"); 57 | you may not use this file except in compliance with the License. 58 | You may obtain a copy of the License at 59 | 60 | http://www.apache.org/licenses/LICENSE-2.0 61 | 62 | Unless required by applicable law or agreed to in writing, software 63 | distributed under the License is distributed on an "AS IS" BASIS, 64 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 65 | See the License for the specific language governing permissions and 66 | limitations under the License. 67 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.3.1' 9 | classpath 'com.android.databinding:dataBinder:1.0-rc4' 10 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | ext { 18 | minSdkVersion = 16 19 | compileSdkVersion = 23 20 | targetSdkVersion = 23 21 | buildToolsVersion = "23.0.2" 22 | javaVersion = JavaVersion.VERSION_1_7 23 | 24 | supportLibraryVersion = '23.1.0' 25 | dataBindingVersion = '1.0-rc4' 26 | daggerVersion = '2.0.1' 27 | dexMakerVersion = '1.4' 28 | } 29 | 30 | allprojects { 31 | repositories { 32 | jcenter() 33 | } 34 | } 35 | 36 | task clean(type: Delete) { 37 | delete rootProject.buildDir 38 | } 39 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hiBrianLee/AndroidMvvm/b507046cf7954339e03f80cd49968ccb358e5b44/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Oct 18 14:25:15 CDT 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /mvvmapp/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.android.databinding' 3 | apply plugin: 'com.neenbedankt.android-apt' 4 | 5 | android { 6 | compileSdkVersion rootProject.ext.compileSdkVersion 7 | buildToolsVersion rootProject.ext.buildToolsVersion 8 | 9 | defaultConfig { 10 | minSdkVersion rootProject.ext.minSdkVersion 11 | targetSdkVersion rootProject.ext.targetSdkVersion 12 | versionCode 1 13 | versionName "1.0" 14 | } 15 | 16 | compileOptions { 17 | sourceCompatibility rootProject.ext.javaVersion 18 | targetCompatibility rootProject.ext.javaVersion 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | } 28 | 29 | dependencies { 30 | compile fileTree(dir: 'libs', include: ['*.jar']) 31 | 32 | apt "com.android.databinding:compiler:${rootProject.ext.dataBindingVersion}" 33 | compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibraryVersion}" 34 | compile "com.android.support:recyclerview-v7:${rootProject.ext.supportLibraryVersion}" 35 | 36 | // dagger 37 | compile "com.google.dagger:dagger:${rootProject.ext.daggerVersion}" 38 | apt "com.google.dagger:dagger-compiler:${rootProject.ext.daggerVersion}" 39 | provided 'javax.annotation:javax.annotation-api:1.2' 40 | 41 | testCompile 'junit:junit:4.12' 42 | } 43 | -------------------------------------------------------------------------------- /mvvmapp/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/brianlee/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 | -------------------------------------------------------------------------------- /mvvmapp/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/MvvmApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp; 18 | 19 | import android.app.Application; 20 | 21 | import com.hibrianlee.mvvmapp.inject.AppComponent; 22 | 23 | public abstract class MvvmApplication extends Application { 24 | 25 | private AppComponent appComponent; 26 | 27 | protected abstract AppComponent createAppComponent(); 28 | 29 | @Override 30 | public void onCreate() { 31 | super.onCreate(); 32 | 33 | if (appComponent == null) { 34 | appComponent = createAppComponent(); 35 | } 36 | } 37 | 38 | public AppComponent getAppComponent() { 39 | return appComponent; 40 | } 41 | 42 | public void setAppComponent(AppComponent appComponent) { 43 | this.appComponent = appComponent; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/activity/ViewModelActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.activity; 18 | 19 | import android.os.Bundle; 20 | import android.support.annotation.Nullable; 21 | import android.support.v7.app.AppCompatActivity; 22 | 23 | import com.hibrianlee.mvvmapp.MvvmApplication; 24 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 25 | import com.hibrianlee.mvvmapp.inject.ActivityModule; 26 | import com.hibrianlee.mvvmapp.inject.AppComponent; 27 | import com.hibrianlee.mvvmapp.inject.DaggerActivityComponent; 28 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 29 | 30 | public abstract class ViewModelActivity extends AppCompatActivity { 31 | 32 | private static final String EXTRA_VIEW_MODEL_STATE = "viewModelState"; 33 | 34 | private ActivityComponent activityComponent; 35 | private ViewModel viewModel; 36 | 37 | protected void inject(AppComponent appComponent) { 38 | appComponent.inject(this); 39 | } 40 | 41 | @Override 42 | protected void onCreate(Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | 45 | AppComponent appComponent = ((MvvmApplication) getApplication()).getAppComponent(); 46 | inject(appComponent); 47 | 48 | activityComponent = DaggerActivityComponent.builder() 49 | .appComponent(appComponent) 50 | .activityModule(new ActivityModule(this)) 51 | .build(); 52 | 53 | ViewModel.State savedViewModelState = null; 54 | if (savedInstanceState != null) { 55 | savedViewModelState = savedInstanceState.getParcelable(EXTRA_VIEW_MODEL_STATE); 56 | } 57 | viewModel = createViewModel(savedViewModelState); 58 | } 59 | 60 | @Nullable 61 | protected ViewModel createViewModel(@Nullable ViewModel.State savedViewModelState) { 62 | return null; 63 | } 64 | 65 | @Override 66 | protected void onStart() { 67 | super.onStart(); 68 | if (viewModel != null) { 69 | viewModel.onStart(); 70 | } 71 | } 72 | 73 | @Override 74 | protected void onSaveInstanceState(Bundle outState) { 75 | super.onSaveInstanceState(outState); 76 | if (viewModel != null) { 77 | outState.putParcelable(EXTRA_VIEW_MODEL_STATE, viewModel.getInstanceState()); 78 | } 79 | } 80 | 81 | @Override 82 | protected void onStop() { 83 | super.onStop(); 84 | if (viewModel != null) { 85 | viewModel.onStop(); 86 | } 87 | } 88 | 89 | public final ActivityComponent getActivityComponent() { 90 | return activityComponent; 91 | } 92 | } 93 | 94 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/adapter/RecyclerViewAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.adapter; 18 | 19 | import android.databinding.ViewDataBinding; 20 | import android.support.annotation.NonNull; 21 | import android.support.v7.widget.RecyclerView; 22 | import android.view.View; 23 | 24 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 25 | import com.hibrianlee.mvvmapp.viewmodel.ItemViewModel; 26 | 27 | import java.util.ArrayList; 28 | 29 | public abstract class RecyclerViewAdapter> 30 | extends RecyclerView.Adapter> { 31 | 32 | protected final ArrayList items; 33 | 34 | private final ActivityComponent activityComponent; 35 | 36 | public RecyclerViewAdapter(@NonNull ActivityComponent activityComponent) { 37 | this.activityComponent = activityComponent; 38 | items = new ArrayList<>(); 39 | } 40 | 41 | @Override 42 | public final void onBindViewHolder(ItemViewHolder holder, int position) { 43 | holder.setItem(items.get(position)); 44 | } 45 | 46 | @Override 47 | public int getItemCount() { 48 | return items.size(); 49 | } 50 | 51 | protected final ActivityComponent getActivityComponent() { 52 | return activityComponent; 53 | } 54 | 55 | public static class ItemViewHolder> 56 | extends RecyclerView.ViewHolder { 57 | 58 | protected final VT viewModel; 59 | private final ViewDataBinding binding; 60 | 61 | public ItemViewHolder(View itemView, ViewDataBinding binding, VT viewModel) { 62 | super(itemView); 63 | this.binding = binding; 64 | this.viewModel = viewModel; 65 | } 66 | 67 | void setItem(T item) { 68 | viewModel.setItem(item); 69 | binding.executePendingBindings(); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/fragment/ViewModelFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.fragment; 18 | 19 | import android.os.Bundle; 20 | import android.support.annotation.NonNull; 21 | import android.support.annotation.Nullable; 22 | import android.support.v4.app.Fragment; 23 | import android.view.View; 24 | 25 | import com.hibrianlee.mvvmapp.MvvmApplication; 26 | import com.hibrianlee.mvvmapp.activity.ViewModelActivity; 27 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 28 | import com.hibrianlee.mvvmapp.inject.AppComponent; 29 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 30 | 31 | public abstract class ViewModelFragment extends Fragment { 32 | 33 | private static final String EXTRA_VIEW_MODEL_STATE = "viewModelState"; 34 | 35 | private ViewModel viewModel; 36 | 37 | protected void inject(AppComponent appComponent) { 38 | appComponent.inject(this); 39 | } 40 | 41 | @Nullable 42 | protected abstract ViewModel createAndBindViewModel(View root, 43 | @NonNull ActivityComponent activityComponent, 44 | @Nullable ViewModel.State savedViewModelState); 45 | 46 | @Override 47 | public void onCreate(@Nullable Bundle savedInstanceState) { 48 | super.onCreate(savedInstanceState); 49 | AppComponent appComponent = 50 | ((MvvmApplication) getActivity().getApplication()).getAppComponent(); 51 | inject(appComponent); 52 | } 53 | 54 | @Override 55 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 56 | super.onActivityCreated(savedInstanceState); 57 | ViewModel.State savedViewModelState = null; 58 | if (savedInstanceState != null) { 59 | savedViewModelState = savedInstanceState.getParcelable(EXTRA_VIEW_MODEL_STATE); 60 | } 61 | ViewModelActivity activity = (ViewModelActivity) getActivity(); 62 | viewModel = createAndBindViewModel(getView(), activity.getActivityComponent(), 63 | savedViewModelState); 64 | } 65 | 66 | @Override 67 | public void onStart() { 68 | super.onStart(); 69 | if (viewModel != null) { 70 | viewModel.onStart(); 71 | } 72 | } 73 | 74 | @Override 75 | public void onSaveInstanceState(Bundle outState) { 76 | super.onSaveInstanceState(outState); 77 | if (viewModel != null) { 78 | outState.putParcelable(EXTRA_VIEW_MODEL_STATE, viewModel.getInstanceState()); 79 | } 80 | } 81 | 82 | @Override 83 | public void onStop() { 84 | super.onStop(); 85 | if (viewModel != null) { 86 | viewModel.onStop(); 87 | } 88 | } 89 | } 90 | 91 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/inject/ActivityComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.inject; 18 | 19 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 20 | 21 | import dagger.Component; 22 | 23 | @PerActivity 24 | @Component( 25 | dependencies = AppComponent.class, 26 | modules = {ActivityModule.class}) 27 | public interface ActivityComponent { 28 | 29 | void inject(ViewModel viewModel); 30 | } 31 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/inject/ActivityModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.inject; 18 | 19 | import com.hibrianlee.mvvmapp.activity.ViewModelActivity; 20 | 21 | import dagger.Module; 22 | import dagger.Provides; 23 | 24 | @Module 25 | public final class ActivityModule { 26 | 27 | private final ViewModelActivity activity; 28 | 29 | public ActivityModule(ViewModelActivity activity) { 30 | this.activity = activity; 31 | } 32 | 33 | @Provides 34 | @PerActivity 35 | AttachedActivity provideAttachedActivity() { 36 | return new AttachedViewModelActivity(activity); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/inject/AppComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.inject; 18 | 19 | import android.content.Context; 20 | 21 | import com.hibrianlee.mvvmapp.activity.ViewModelActivity; 22 | import com.hibrianlee.mvvmapp.fragment.ViewModelFragment; 23 | 24 | import javax.inject.Singleton; 25 | 26 | import dagger.Component; 27 | 28 | public interface AppComponent { 29 | 30 | @AppContext 31 | Context appContext(); 32 | 33 | void inject(ViewModelActivity viewModelActivity); 34 | 35 | void inject(ViewModelFragment viewModelFragment); 36 | } 37 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/inject/AppContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.inject; 18 | 19 | import java.lang.annotation.Retention; 20 | 21 | import javax.inject.Qualifier; 22 | 23 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 24 | 25 | @Qualifier 26 | @Retention(RUNTIME) 27 | public @interface AppContext { 28 | } 29 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/inject/AppContextModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.inject; 18 | 19 | import android.app.Application; 20 | import android.content.Context; 21 | 22 | import javax.inject.Singleton; 23 | 24 | import dagger.Module; 25 | import dagger.Provides; 26 | 27 | @Module 28 | public class AppContextModule { 29 | 30 | private final Context appContext; 31 | 32 | public AppContextModule(Application application) { 33 | appContext = application; 34 | } 35 | 36 | @Provides 37 | @Singleton 38 | @AppContext 39 | Context provideAppContext() { 40 | return appContext; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/inject/AttachedActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.inject; 18 | 19 | import android.app.Activity; 20 | 21 | import java.net.URISyntaxException; 22 | 23 | public interface AttachedActivity { 24 | 25 | void startActivity(Class activityClass); 26 | 27 | void openUrl(String url) throws URISyntaxException; 28 | } 29 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/inject/AttachedViewModelActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.inject; 18 | 19 | import android.app.Activity; 20 | import android.content.Intent; 21 | 22 | import com.hibrianlee.mvvmapp.activity.ViewModelActivity; 23 | 24 | import java.lang.ref.WeakReference; 25 | import java.net.URISyntaxException; 26 | 27 | public final class AttachedViewModelActivity implements AttachedActivity { 28 | 29 | private final WeakReference weakActivity; 30 | 31 | AttachedViewModelActivity(ViewModelActivity activity) { 32 | weakActivity = new WeakReference<>(activity); 33 | } 34 | 35 | @Override 36 | public void startActivity(Class activityClass) { 37 | ViewModelActivity activity = weakActivity.get(); 38 | if (activity != null && !activity.isFinishing()) { 39 | activity.startActivity(new Intent(activity, activityClass)); 40 | } 41 | } 42 | 43 | @Override 44 | public void openUrl(String url) throws URISyntaxException { 45 | ViewModelActivity activity = weakActivity.get(); 46 | if (activity != null && !activity.isFinishing()) { 47 | activity.startActivity(Intent.parseUri(url, 0)); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/inject/PerActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.inject; 18 | 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | 22 | import javax.inject.Scope; 23 | 24 | @Scope 25 | @Retention(RetentionPolicy.RUNTIME) 26 | public @interface PerActivity { 27 | } 28 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/viewmodel/ItemViewModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.viewmodel; 18 | 19 | import android.support.annotation.NonNull; 20 | 21 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 22 | 23 | public abstract class ItemViewModel extends ViewModel { 24 | 25 | public ItemViewModel(@NonNull ActivityComponent activityComponent) { 26 | super(activityComponent, null); 27 | } 28 | 29 | public abstract void setItem(ITEM_T item); 30 | } 31 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/viewmodel/RecyclerViewViewModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.viewmodel; 18 | 19 | import android.os.Parcel; 20 | import android.os.Parcelable; 21 | import android.support.annotation.NonNull; 22 | import android.support.annotation.Nullable; 23 | import android.support.v7.widget.RecyclerView; 24 | 25 | import com.hibrianlee.mvvmapp.adapter.RecyclerViewAdapter; 26 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 27 | 28 | public abstract class RecyclerViewViewModel extends ViewModel { 29 | 30 | RecyclerView.LayoutManager layoutManager; 31 | private Parcelable savedLayoutManagerState; 32 | 33 | protected abstract RecyclerViewAdapter getAdapter(); 34 | protected abstract RecyclerView.LayoutManager createLayoutManager(); 35 | 36 | public RecyclerViewViewModel(@NonNull ActivityComponent activityComponent, 37 | @Nullable State savedInstanceState) { 38 | super(activityComponent, savedInstanceState); 39 | if (savedInstanceState instanceof RecyclerViewViewModelState) { 40 | savedLayoutManagerState = 41 | ((RecyclerViewViewModelState) savedInstanceState).layoutManagerState; 42 | } 43 | } 44 | 45 | @Override 46 | public RecyclerViewViewModelState getInstanceState() { 47 | return new RecyclerViewViewModelState(this); 48 | } 49 | 50 | public final void setupRecyclerView(RecyclerView recyclerView) { 51 | layoutManager = createLayoutManager(); 52 | if (savedLayoutManagerState != null) { 53 | layoutManager.onRestoreInstanceState(savedLayoutManagerState); 54 | savedLayoutManagerState = null; 55 | } 56 | recyclerView.setAdapter(getAdapter()); 57 | recyclerView.setLayoutManager(layoutManager); 58 | } 59 | 60 | protected static class RecyclerViewViewModelState extends State { 61 | 62 | private final Parcelable layoutManagerState; 63 | 64 | public RecyclerViewViewModelState(RecyclerViewViewModel viewModel) { 65 | super(viewModel); 66 | layoutManagerState = viewModel.layoutManager.onSaveInstanceState(); 67 | } 68 | 69 | public RecyclerViewViewModelState(Parcel in) { 70 | super(in); 71 | layoutManagerState = in.readParcelable( 72 | RecyclerView.LayoutManager.class.getClassLoader()); 73 | } 74 | 75 | @Override 76 | public void writeToParcel(Parcel dest, int flags) { 77 | super.writeToParcel(dest, flags); 78 | dest.writeParcelable(layoutManagerState, flags); 79 | } 80 | 81 | public static Creator CREATOR = 82 | new Creator() { 83 | @Override 84 | public RecyclerViewViewModelState createFromParcel(Parcel source) { 85 | return new RecyclerViewViewModelState(source); 86 | } 87 | 88 | @Override 89 | public RecyclerViewViewModelState[] newArray(int size) { 90 | return new RecyclerViewViewModelState[size]; 91 | } 92 | }; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/viewmodel/ViewModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.viewmodel; 18 | 19 | import android.content.Context; 20 | import android.databinding.BaseObservable; 21 | import android.os.Parcel; 22 | import android.os.Parcelable; 23 | import android.support.annotation.CallSuper; 24 | import android.support.annotation.NonNull; 25 | import android.support.annotation.Nullable; 26 | 27 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 28 | import com.hibrianlee.mvvmapp.inject.AppContext; 29 | import com.hibrianlee.mvvmapp.inject.AttachedActivity; 30 | 31 | import javax.inject.Inject; 32 | 33 | public abstract class ViewModel extends BaseObservable { 34 | 35 | @Inject 36 | @AppContext 37 | protected Context appContext; 38 | 39 | @Inject 40 | protected AttachedActivity attachedActivity; 41 | 42 | protected ViewModel(@NonNull ActivityComponent activityComponent, 43 | @Nullable State savedInstanceState) { 44 | activityComponent.inject(this); 45 | } 46 | 47 | @CallSuper 48 | public void onStart() { 49 | 50 | } 51 | 52 | public State getInstanceState() { 53 | return new State(this); 54 | } 55 | 56 | @CallSuper 57 | public void onStop() { 58 | 59 | } 60 | 61 | public static class State implements Parcelable { 62 | 63 | protected State(ViewModel viewModel) { 64 | 65 | } 66 | 67 | public State(Parcel in) { 68 | 69 | } 70 | 71 | @Override 72 | public int describeContents() { 73 | return 0; 74 | } 75 | 76 | @CallSuper 77 | @Override 78 | public void writeToParcel(Parcel dest, int flags) { 79 | 80 | } 81 | 82 | public static Creator CREATOR = new Creator() { 83 | @Override 84 | public State createFromParcel(Parcel source) { 85 | return new State(source); 86 | } 87 | 88 | @Override 89 | public State[] newArray(int size) { 90 | return new State[size]; 91 | } 92 | }; 93 | } 94 | } 95 | 96 | -------------------------------------------------------------------------------- /mvvmapp/src/main/java/com/hibrianlee/mvvmapp/viewmodel/ViewModelBindings.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.mvvmapp.viewmodel; 18 | 19 | import android.databinding.BindingAdapter; 20 | import android.support.v7.widget.RecyclerView; 21 | 22 | public class ViewModelBindings { 23 | 24 | @BindingAdapter("recyclerViewViewModel") 25 | public static void setRecyclerViewViewModel(RecyclerView recyclerView, 26 | RecyclerViewViewModel viewModel) { 27 | viewModel.setupRecyclerView(recyclerView); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.android.databinding' 3 | apply plugin: 'com.neenbedankt.android-apt' 4 | 5 | def versionMajor = 1 6 | def versionMinor = 0 7 | def versionPatch = 0 8 | def versionBuild = 0 // bump for dogfood builds, public betas, etc. 9 | 10 | android { 11 | compileSdkVersion rootProject.ext.compileSdkVersion 12 | buildToolsVersion rootProject.ext.buildToolsVersion 13 | 14 | defaultConfig { 15 | applicationId "com.hibrianlee.sample.mvvm" 16 | minSdkVersion rootProject.ext.minSdkVersion 17 | targetSdkVersion rootProject.ext.targetSdkVersion 18 | versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild; 19 | versionName "${versionMajor}.${versionMinor}.${versionPatch}" 20 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 21 | } 22 | 23 | compileOptions { 24 | sourceCompatibility rootProject.ext.javaVersion 25 | targetCompatibility rootProject.ext.javaVersion 26 | } 27 | 28 | buildTypes { 29 | release { 30 | minifyEnabled false 31 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 32 | } 33 | } 34 | } 35 | 36 | configurations { 37 | androidTestCompile.exclude module: 'support-annotations' 38 | } 39 | 40 | dependencies { 41 | compile project(':mvvmapp') 42 | 43 | compile fileTree(dir: 'libs', include: ['*.jar']) 44 | 45 | apt "com.android.databinding:compiler:${rootProject.ext.dataBindingVersion}" 46 | apt "com.google.dagger:dagger-compiler:${rootProject.ext.daggerVersion}" 47 | provided 'javax.annotation:javax.annotation-api:1.2' 48 | 49 | compile 'com.jakewharton:butterknife:7.0.1' 50 | 51 | testApt "com.google.dagger:dagger-compiler:${rootProject.ext.daggerVersion}" 52 | testCompile 'junit:junit:4.12' 53 | testCompile 'org.mockito:mockito-core:1.10.19' 54 | 55 | androidTestApt "com.google.dagger:dagger-compiler:${rootProject.ext.daggerVersion}" 56 | androidTestCompile 'org.mockito:mockito-core:1.10.19' 57 | androidTestCompile 'com.android.support.test:runner:0.4.1' 58 | androidTestCompile 'com.android.support.test:rules:0.4.1' 59 | androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' 60 | androidTestCompile "com.crittercism.dexmaker:dexmaker:${rootProject.ext.dexMakerVersion}" 61 | androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:${rootProject.ext.dexMakerVersion}" 62 | androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:${rootProject.ext.dexMakerVersion}" 63 | } 64 | -------------------------------------------------------------------------------- /sample/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/brianlee/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 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/hibrianlee/sample/mvvm/BaseTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm; 18 | 19 | import android.app.Instrumentation; 20 | import android.support.annotation.CallSuper; 21 | import android.support.test.InstrumentationRegistry; 22 | import android.support.v7.widget.RecyclerView; 23 | import android.view.View; 24 | 25 | import com.hibrianlee.mvvmapp.adapter.RecyclerViewAdapter; 26 | import com.hibrianlee.mvvmapp.inject.AppContextModule; 27 | import com.hibrianlee.sample.mvvm.viewmodel.MockViewModelFactory; 28 | 29 | import org.hamcrest.Description; 30 | import org.hamcrest.Matcher; 31 | import org.hamcrest.TypeSafeMatcher; 32 | import org.junit.Before; 33 | 34 | import javax.inject.Inject; 35 | 36 | public class BaseTest { 37 | 38 | @Inject 39 | protected MockViewModelFactory viewModelFactory; 40 | 41 | @CallSuper 42 | @Before 43 | public void setUp() throws Exception { 44 | Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 45 | MvvmSampleApplication application = (MvvmSampleApplication) instrumentation 46 | .getTargetContext().getApplicationContext(); 47 | TestComponent testComponent = DaggerTestComponent.builder() 48 | .appContextModule(new AppContextModule(application)) 49 | .build(); 50 | application.setAppComponent(testComponent); 51 | testComponent.inject(this); 52 | viewModelFactory.clear(); 53 | } 54 | 55 | public static Matcher withRecyclerViewPosition(final int position) { 56 | return new TypeSafeMatcher() { 57 | 58 | @Override 59 | public void describeTo(Description description) { 60 | description.appendText("with RecyclerView position: " + position); 61 | } 62 | 63 | @Override 64 | protected boolean matchesSafely(View view) { 65 | if (!(view.getParent() instanceof RecyclerView)) { 66 | return false; 67 | } 68 | 69 | RecyclerView recyclerView = (RecyclerView) view.getParent(); 70 | RecyclerView.Adapter adapter = recyclerView.getAdapter(); 71 | if (!(adapter instanceof RecyclerViewAdapter)) { 72 | return false; 73 | } 74 | return position == recyclerView.getChildAdapterPosition(view); 75 | } 76 | }; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/hibrianlee/sample/mvvm/MockModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm; 18 | 19 | import com.hibrianlee.sample.mvvm.viewmodel.MockViewModelFactory; 20 | import com.hibrianlee.sample.mvvm.viewmodel.ViewModelFactory; 21 | 22 | import javax.inject.Singleton; 23 | 24 | import dagger.Module; 25 | import dagger.Provides; 26 | 27 | @Module 28 | public class MockModule { 29 | 30 | @Provides 31 | @Singleton 32 | ViewModelFactory provideViewModelFactory(MockViewModelFactory mockViewModelFactory) { 33 | return mockViewModelFactory; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/hibrianlee/sample/mvvm/TestComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm; 18 | 19 | import com.hibrianlee.mvvmapp.inject.AppContextModule; 20 | 21 | import javax.inject.Singleton; 22 | 23 | import dagger.Component; 24 | 25 | @Singleton 26 | @Component(modules = { 27 | AppContextModule.class, 28 | MockModule.class 29 | }) 30 | public interface TestComponent extends SampleAppComponent { 31 | 32 | void inject(BaseTest baseTest); 33 | } 34 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/hibrianlee/sample/mvvm/viewmodel/AndroidVersionsActivityTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.viewmodel; 18 | 19 | import android.support.test.rule.ActivityTestRule; 20 | 21 | import com.hibrianlee.sample.mvvm.BaseTest; 22 | import com.hibrianlee.sample.mvvm.R; 23 | import com.hibrianlee.sample.mvvm.activity.AndroidVersionsActivity; 24 | import com.hibrianlee.sample.mvvm.model.AndroidVersion; 25 | 26 | import org.junit.Rule; 27 | import org.junit.Test; 28 | 29 | import static android.support.test.espresso.Espresso.onView; 30 | import static android.support.test.espresso.action.ViewActions.click; 31 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 32 | import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; 33 | import static android.support.test.espresso.matcher.ViewMatchers.isSelected; 34 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 35 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 36 | import static org.hamcrest.Matchers.allOf; 37 | import static org.mockito.Matchers.any; 38 | import static org.mockito.Mockito.atLeastOnce; 39 | import static org.mockito.Mockito.doNothing; 40 | import static org.mockito.Mockito.doReturn; 41 | import static org.mockito.Mockito.verify; 42 | 43 | public class AndroidVersionsActivityTest extends BaseTest { 44 | 45 | @Rule 46 | public ActivityTestRule activityTestRule = new ActivityTestRule<>( 47 | AndroidVersionsActivity.class, true, false); 48 | 49 | @Override 50 | public void setUp() throws Exception { 51 | super.setUp(); 52 | AndroidVersionItemInitializer initializer = new AndroidVersionItemInitializer(); 53 | initializer.version = "version"; 54 | initializer.codeName = "codeName"; 55 | initializer.selected = true; 56 | viewModelFactory.registerSpyInitializer(AndroidVersionItemViewModel.class, initializer); 57 | viewModelFactory.registerSpyInitializer(AndroidVersionsViewModel.class, 58 | new AndroidVersionsInitializer()); 59 | } 60 | 61 | @Test 62 | public void testInitialScreen() { 63 | activityTestRule.launchActivity(null); 64 | 65 | AndroidVersionItemViewModel viewModel = viewModelFactory.getAndroidVersionItemViewModel(); 66 | verify(viewModel, atLeastOnce()).setItem(any(AndroidVersion.class)); 67 | onView(withRecyclerViewPosition(0)) 68 | .check(matches(allOf( 69 | hasDescendant(withText("version")), 70 | hasDescendant(withText("codeName")), 71 | isSelected() 72 | ))); 73 | } 74 | 75 | @Test 76 | public void testItemClick() { 77 | activityTestRule.launchActivity(null); 78 | 79 | AndroidVersionItemViewModel viewModel = viewModelFactory.getAndroidVersionItemViewModel(); 80 | onView(withRecyclerViewPosition(0)).perform(click()); 81 | verify(viewModel).onClick(); 82 | } 83 | 84 | @Test 85 | public void testClickHiBrianLee() { 86 | activityTestRule.launchActivity(null); 87 | AndroidVersionsViewModel viewModel = viewModelFactory.getAndroidVersionsViewModel(); 88 | onView(withId(R.id.hiBrianLee)).perform(click()); 89 | 90 | verify(viewModel).onClickHiBrianLee(); 91 | } 92 | 93 | private static class AndroidVersionItemInitializer 94 | implements MockViewModelFactory.SpyInitializer { 95 | 96 | private String version; 97 | private String codeName; 98 | private boolean selected; 99 | 100 | @Override 101 | public void setupSpy(AndroidVersionItemViewModel viewModel) { 102 | doReturn(version).when(viewModel).getVersion(); 103 | doReturn(codeName).when(viewModel).getCodeName(); 104 | doReturn(selected).when(viewModel).getSelected(); 105 | doNothing().when(viewModel).onClick(); 106 | doNothing().when(viewModel).setItem(any(AndroidVersion.class)); 107 | } 108 | } 109 | 110 | private static class AndroidVersionsInitializer 111 | implements MockViewModelFactory.SpyInitializer { 112 | 113 | @Override 114 | public void setupSpy(AndroidVersionsViewModel viewModel) { 115 | doNothing().when(viewModel).onClickHiBrianLee(); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/hibrianlee/sample/mvvm/viewmodel/ClickCountActivityTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.viewmodel; 18 | 19 | import android.support.test.rule.ActivityTestRule; 20 | 21 | import com.hibrianlee.sample.mvvm.BaseTest; 22 | import com.hibrianlee.sample.mvvm.R; 23 | import com.hibrianlee.sample.mvvm.activity.ClickCountActivity; 24 | 25 | import org.junit.Rule; 26 | import org.junit.Test; 27 | 28 | import static android.support.test.espresso.Espresso.onView; 29 | import static android.support.test.espresso.action.ViewActions.click; 30 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 31 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 32 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 33 | import static org.mockito.Mockito.doNothing; 34 | import static org.mockito.Mockito.doReturn; 35 | import static org.mockito.Mockito.verify; 36 | 37 | public class ClickCountActivityTest extends BaseTest { 38 | 39 | @Rule 40 | public ActivityTestRule activityTestRule = new ActivityTestRule<>( 41 | ClickCountActivity.class, true, false); 42 | 43 | @Override 44 | public void setUp() throws Exception { 45 | super.setUp(); 46 | ClickCountSpyInitializer initializer = new ClickCountSpyInitializer(); 47 | initializer.clickCountText = "5 clicks"; 48 | viewModelFactory.registerSpyInitializer(ClickCountViewModel.class, initializer); 49 | } 50 | 51 | @Test 52 | public void testInitialScreen() { 53 | activityTestRule.launchActivity(null); 54 | onView(withId(R.id.clickCountText)).check(matches(withText("5 clicks"))); 55 | } 56 | 57 | @Test 58 | public void testClickButton() { 59 | activityTestRule.launchActivity(null); 60 | ClickCountViewModel viewModel = viewModelFactory.getClickCountViewModel(); 61 | onView(withId(R.id.clickButton)).perform(click()); 62 | 63 | verify(viewModel).onClickButton(); 64 | } 65 | 66 | @Test 67 | public void testClickHiBrianLee() { 68 | activityTestRule.launchActivity(null); 69 | ClickCountViewModel viewModel = viewModelFactory.getClickCountViewModel(); 70 | onView(withId(R.id.hiBrianLee)).perform(click()); 71 | 72 | verify(viewModel).onClickHiBrianLee(); 73 | } 74 | 75 | private static class ClickCountSpyInitializer 76 | implements MockViewModelFactory.SpyInitializer { 77 | 78 | private String clickCountText; 79 | 80 | @Override 81 | public void setupSpy(ClickCountViewModel viewModel) { 82 | doReturn(clickCountText).when(viewModel).getClickCountText(); 83 | doNothing().when(viewModel).onClickButton(); 84 | doNothing().when(viewModel).onClickHiBrianLee(); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/hibrianlee/sample/mvvm/viewmodel/MainActivityTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.viewmodel; 18 | 19 | import android.support.test.rule.ActivityTestRule; 20 | 21 | import com.hibrianlee.sample.mvvm.BaseTest; 22 | import com.hibrianlee.sample.mvvm.R; 23 | import com.hibrianlee.sample.mvvm.activity.MainActivity; 24 | 25 | import org.junit.Rule; 26 | import org.junit.Test; 27 | 28 | import static android.support.test.espresso.Espresso.onView; 29 | import static android.support.test.espresso.action.ViewActions.click; 30 | import static android.support.test.espresso.matcher.ViewMatchers.withId; 31 | import static org.mockito.Mockito.doNothing; 32 | import static org.mockito.Mockito.verify; 33 | 34 | public class MainActivityTest extends BaseTest { 35 | 36 | @Rule 37 | public ActivityTestRule activityTestRule = new ActivityTestRule<>( 38 | MainActivity.class, true, false); 39 | 40 | @Override 41 | public void setUp() throws Exception { 42 | super.setUp(); 43 | viewModelFactory.registerSpyInitializer(MainViewModel.class, new MainSpyInitializer()); 44 | } 45 | 46 | @Test 47 | public void testClickButtonClicks() { 48 | activityTestRule.launchActivity(null); 49 | MainViewModel mainViewModel = viewModelFactory.getMainViewModel(); 50 | onView(withId(R.id.buttonClicks)).perform(click()); 51 | 52 | verify(mainViewModel).onClickButtonClicks(); 53 | } 54 | 55 | @Test 56 | public void testClickRecyclerView() { 57 | activityTestRule.launchActivity(null); 58 | MainViewModel mainViewModel = viewModelFactory.getMainViewModel(); 59 | onView(withId(R.id.buttonRecyclerView)).perform(click()); 60 | 61 | verify(mainViewModel).onClickButtonRecyclerView(); 62 | } 63 | 64 | @Test 65 | public void testClickHiBrianLee() { 66 | activityTestRule.launchActivity(null); 67 | MainViewModel mainViewModel = viewModelFactory.getMainViewModel(); 68 | onView(withId(R.id.hiBrianLee)).perform(click()); 69 | 70 | verify(mainViewModel).onClickHiBrianLee(); 71 | } 72 | 73 | private static class MainSpyInitializer 74 | implements MockViewModelFactory.SpyInitializer { 75 | 76 | @Override 77 | public void setupSpy(MainViewModel viewModel) { 78 | doNothing().when(viewModel).onClickButtonClicks(); 79 | doNothing().when(viewModel).onClickButtonRecyclerView(); 80 | doNothing().when(viewModel).onClickHiBrianLee(); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/hibrianlee/sample/mvvm/viewmodel/MockViewModelFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.viewmodel; 18 | 19 | import android.support.annotation.NonNull; 20 | import android.support.annotation.Nullable; 21 | 22 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 23 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 24 | import com.hibrianlee.sample.mvvm.adapter.AndroidVersionsAdapter; 25 | 26 | import java.util.HashMap; 27 | import java.util.Map; 28 | 29 | import javax.inject.Inject; 30 | import javax.inject.Singleton; 31 | 32 | import static org.mockito.Mockito.doNothing; 33 | import static org.mockito.Mockito.spy; 34 | 35 | @Singleton 36 | public class MockViewModelFactory implements ViewModelFactory { 37 | 38 | private final Map, SpyInitializer> 39 | spyInitializerMap; 40 | 41 | private MainViewModel mainViewModel; 42 | private ClickCountViewModel clickCountViewModel; 43 | private AndroidVersionsViewModel androidVersionsViewModel; 44 | private AndroidVersionItemViewModel androidVersionItemViewModel; 45 | 46 | public interface SpyInitializer { 47 | void setupSpy(ViewModelT viewModel); 48 | } 49 | 50 | @Inject 51 | MockViewModelFactory() { 52 | spyInitializerMap = new HashMap<>(); 53 | } 54 | 55 | public void clear() { 56 | spyInitializerMap.clear(); 57 | mainViewModel = null; 58 | clickCountViewModel = null; 59 | androidVersionsViewModel = null; 60 | androidVersionItemViewModel = null; 61 | } 62 | 63 | public void registerSpyInitializer( 64 | Class viewModelClass, SpyInitializer spyInitializer) { 65 | spyInitializerMap.put(viewModelClass, spyInitializer); 66 | } 67 | 68 | @SuppressWarnings("unchecked") 69 | private void setupSpy(Class viewModelClass, 70 | ViewModelT viewModel) { 71 | SpyInitializer spyInitializer = spyInitializerMap.get(viewModelClass); 72 | if (spyInitializer != null) { 73 | spyInitializer.setupSpy(viewModel); 74 | } 75 | } 76 | 77 | public MainViewModel getMainViewModel() { 78 | return mainViewModel; 79 | } 80 | 81 | public ClickCountViewModel getClickCountViewModel() { 82 | return clickCountViewModel; 83 | } 84 | 85 | public AndroidVersionsViewModel getAndroidVersionsViewModel() { 86 | return androidVersionsViewModel; 87 | } 88 | 89 | public AndroidVersionItemViewModel getAndroidVersionItemViewModel() { 90 | return androidVersionItemViewModel; 91 | } 92 | 93 | @NonNull 94 | @Override 95 | public MainViewModel createMainViewModel(@NonNull ActivityComponent activityComponent, 96 | @Nullable ViewModel.State savedViewModelState) { 97 | mainViewModel = spy(new MainViewModel(activityComponent, savedViewModelState)); 98 | doNothingOnLifeCycle(mainViewModel); 99 | setupSpy(MainViewModel.class, mainViewModel); 100 | return mainViewModel; 101 | } 102 | 103 | @NonNull 104 | @Override 105 | public ClickCountViewModel createClickCountViewModel( 106 | @NonNull ActivityComponent activityComponent, 107 | @Nullable ViewModel.State savedViewModelState) { 108 | clickCountViewModel = spy(new ClickCountViewModel(activityComponent, savedViewModelState)); 109 | doNothingOnLifeCycle(clickCountViewModel); 110 | setupSpy(ClickCountViewModel.class, clickCountViewModel); 111 | return clickCountViewModel; 112 | } 113 | 114 | @NonNull 115 | @Override 116 | public AndroidVersionsViewModel createAndroidVersionsViewModel( 117 | @NonNull AndroidVersionsAdapter adapter, 118 | @NonNull ActivityComponent activityComponent, 119 | @Nullable ViewModel.State savedViewModelState) { 120 | androidVersionsViewModel = spy(new AndroidVersionsViewModel(adapter, activityComponent, 121 | savedViewModelState)); 122 | doNothingOnLifeCycle(androidVersionsViewModel); 123 | setupSpy(AndroidVersionsViewModel.class, androidVersionsViewModel); 124 | return androidVersionsViewModel; 125 | } 126 | 127 | @NonNull 128 | @Override 129 | public AndroidVersionItemViewModel createAndroidVersionItemViewModel( 130 | @NonNull ActivityComponent activityComponent) { 131 | if (androidVersionItemViewModel == null) { 132 | androidVersionItemViewModel = spy(new AndroidVersionItemViewModel(activityComponent)); 133 | doNothingOnLifeCycle(androidVersionItemViewModel); 134 | setupSpy(AndroidVersionItemViewModel.class, androidVersionItemViewModel); 135 | } 136 | return androidVersionItemViewModel; 137 | } 138 | 139 | private void doNothingOnLifeCycle(ViewModel viewModel) { 140 | doNothing().when(viewModel).onStart(); 141 | doNothing().when(viewModel).onStop(); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/MvvmSampleApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm; 18 | 19 | import com.hibrianlee.mvvmapp.MvvmApplication; 20 | import com.hibrianlee.mvvmapp.inject.AppComponent; 21 | import com.hibrianlee.mvvmapp.inject.AppContextModule; 22 | 23 | public class MvvmSampleApplication extends MvvmApplication { 24 | 25 | @Override 26 | protected AppComponent createAppComponent() { 27 | return DaggerSampleAppComponent.builder() 28 | .appContextModule(new AppContextModule(this)) 29 | .build(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/SampleAppComponent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm; 18 | 19 | import com.hibrianlee.mvvmapp.inject.AppComponent; 20 | import com.hibrianlee.mvvmapp.inject.AppContextModule; 21 | import com.hibrianlee.sample.mvvm.activity.BaseActivity; 22 | import com.hibrianlee.sample.mvvm.fragment.BaseFragment; 23 | 24 | import javax.inject.Singleton; 25 | 26 | import dagger.Component; 27 | 28 | @Singleton 29 | @Component(modules = { 30 | AppContextModule.class, 31 | SampleAppModule.class 32 | }) 33 | public interface SampleAppComponent extends AppComponent { 34 | 35 | void inject(BaseActivity baseActivity); 36 | 37 | void inject(BaseFragment baseFragment); 38 | } 39 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/SampleAppModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm; 18 | 19 | import com.hibrianlee.sample.mvvm.viewmodel.AppViewModelFactory; 20 | import com.hibrianlee.sample.mvvm.viewmodel.ViewModelFactory; 21 | 22 | import javax.inject.Singleton; 23 | 24 | import dagger.Module; 25 | import dagger.Provides; 26 | 27 | @Module 28 | public class SampleAppModule { 29 | 30 | @Provides 31 | @Singleton 32 | ViewModelFactory provideViewModelFactory(AppViewModelFactory appViewModelFactory) { 33 | return appViewModelFactory; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/activity/AndroidVersionsActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.activity; 18 | 19 | import android.os.Bundle; 20 | import android.support.v4.app.Fragment; 21 | import android.support.v4.app.FragmentManager; 22 | 23 | import com.hibrianlee.sample.mvvm.R; 24 | import com.hibrianlee.sample.mvvm.fragment.AndroidVersionsFragment; 25 | 26 | public class AndroidVersionsActivity extends BaseActivity { 27 | 28 | private static final String TAG_VERSIONS_FRAGMENT = "versionsFragment"; 29 | 30 | @Override 31 | protected void onCreate(Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | setContentView(R.layout.activity_android_versions); 34 | 35 | FragmentManager fm = getSupportFragmentManager(); 36 | Fragment fragment = fm.findFragmentByTag(TAG_VERSIONS_FRAGMENT); 37 | if (fragment == null) { 38 | fragment = new AndroidVersionsFragment(); 39 | fm.beginTransaction() 40 | .add(R.id.fragmentContainer, fragment, TAG_VERSIONS_FRAGMENT) 41 | .commit(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/activity/BaseActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.activity; 18 | 19 | import com.hibrianlee.mvvmapp.activity.ViewModelActivity; 20 | import com.hibrianlee.mvvmapp.inject.AppComponent; 21 | import com.hibrianlee.sample.mvvm.SampleAppComponent; 22 | import com.hibrianlee.sample.mvvm.viewmodel.ViewModelFactory; 23 | 24 | import javax.inject.Inject; 25 | 26 | public class BaseActivity extends ViewModelActivity { 27 | 28 | @Inject 29 | protected ViewModelFactory viewModelFactory; 30 | 31 | @Override 32 | protected void inject(AppComponent appComponent) { 33 | ((SampleAppComponent) appComponent).inject(this); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/activity/ClickCountActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.activity; 18 | 19 | import android.databinding.DataBindingUtil; 20 | import android.os.Bundle; 21 | import android.support.annotation.Nullable; 22 | 23 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 24 | import com.hibrianlee.sample.mvvm.R; 25 | import com.hibrianlee.sample.mvvm.databinding.ActivityClickCountBinding; 26 | import com.hibrianlee.sample.mvvm.viewmodel.ClickCountViewModel; 27 | 28 | import butterknife.ButterKnife; 29 | import butterknife.OnClick; 30 | 31 | public class ClickCountActivity extends BaseActivity { 32 | 33 | private ClickCountViewModel clickCountViewModel; 34 | 35 | @Override 36 | protected void onCreate(Bundle savedInstanceState) { 37 | super.onCreate(savedInstanceState); 38 | ActivityClickCountBinding binding = 39 | DataBindingUtil.setContentView(this, R.layout.activity_click_count); 40 | binding.setViewModel(clickCountViewModel); 41 | ButterKnife.bind(this); 42 | } 43 | 44 | @Nullable 45 | @Override 46 | protected ViewModel createViewModel(@Nullable ViewModel.State savedViewModelState) { 47 | clickCountViewModel = viewModelFactory.createClickCountViewModel(getActivityComponent(), 48 | savedViewModelState); 49 | return clickCountViewModel; 50 | } 51 | 52 | @OnClick(R.id.clickButton) 53 | void onClickButton() { 54 | clickCountViewModel.onClickButton(); 55 | } 56 | 57 | @OnClick(R.id.hiBrianLee) 58 | void onClickHiBrianLee() { 59 | clickCountViewModel.onClickHiBrianLee(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.activity; 18 | 19 | import android.os.Bundle; 20 | import android.support.annotation.Nullable; 21 | 22 | import com.hibrianlee.mvvmapp.activity.ViewModelActivity; 23 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 24 | import com.hibrianlee.sample.mvvm.R; 25 | import com.hibrianlee.sample.mvvm.viewmodel.MainViewModel; 26 | 27 | import butterknife.ButterKnife; 28 | import butterknife.OnClick; 29 | 30 | public class MainActivity extends BaseActivity { 31 | 32 | private MainViewModel mainViewModel; 33 | 34 | @Override 35 | protected void onCreate(Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_main); 38 | ButterKnife.bind(this); 39 | } 40 | 41 | @Nullable 42 | @Override 43 | protected ViewModel createViewModel(@Nullable ViewModel.State savedViewModelState) { 44 | mainViewModel = viewModelFactory.createMainViewModel(getActivityComponent(), 45 | savedViewModelState); 46 | return mainViewModel; 47 | } 48 | 49 | @OnClick(R.id.buttonClicks) 50 | void onClickButtonClicks() { 51 | mainViewModel.onClickButtonClicks(); 52 | } 53 | 54 | @OnClick(R.id.buttonRecyclerView) 55 | void onClickButtonRecyclerView() { 56 | mainViewModel.onClickButtonRecyclerView(); 57 | } 58 | 59 | @OnClick(R.id.hiBrianLee) 60 | void onClickHiBrianLee() { 61 | mainViewModel.onClickHiBrianLee(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/adapter/AndroidVersionsAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.adapter; 18 | 19 | import android.databinding.ViewDataBinding; 20 | import android.support.annotation.NonNull; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | 25 | import com.hibrianlee.mvvmapp.adapter.RecyclerViewAdapter; 26 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 27 | import com.hibrianlee.sample.mvvm.R; 28 | import com.hibrianlee.sample.mvvm.databinding.ItemAndroidVersionBinding; 29 | import com.hibrianlee.sample.mvvm.model.AndroidVersion; 30 | import com.hibrianlee.sample.mvvm.viewmodel.AndroidVersionItemViewModel; 31 | import com.hibrianlee.sample.mvvm.viewmodel.ViewModelFactory; 32 | 33 | import java.util.ArrayList; 34 | 35 | import butterknife.ButterKnife; 36 | import butterknife.OnClick; 37 | 38 | public class AndroidVersionsAdapter 39 | extends RecyclerViewAdapter { 40 | 41 | private final ViewModelFactory viewModelFactory; 42 | 43 | public AndroidVersionsAdapter(ViewModelFactory viewModelFactory, 44 | @NonNull ActivityComponent activityComponent) { 45 | super(activityComponent); 46 | this.viewModelFactory = viewModelFactory; 47 | } 48 | 49 | @Override 50 | public AndroidVersionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 51 | View itemView = LayoutInflater.from(parent.getContext()) 52 | .inflate(R.layout.item_android_version, parent, false); 53 | 54 | AndroidVersionItemViewModel viewModel = 55 | viewModelFactory.createAndroidVersionItemViewModel(getActivityComponent()); 56 | 57 | ItemAndroidVersionBinding binding = ItemAndroidVersionBinding.bind(itemView); 58 | binding.setViewModel(viewModel); 59 | 60 | return new AndroidVersionViewHolder(itemView, binding, viewModel); 61 | } 62 | 63 | public void setItems(ArrayList newItems) { 64 | items.clear(); 65 | items.addAll(newItems); 66 | notifyDataSetChanged(); 67 | } 68 | 69 | public ArrayList getItems() { 70 | return items; 71 | } 72 | 73 | static class AndroidVersionViewHolder 74 | extends ItemViewHolder { 75 | 76 | public AndroidVersionViewHolder(View itemView, ViewDataBinding binding, 77 | AndroidVersionItemViewModel viewModel) { 78 | super(itemView, binding, viewModel); 79 | ButterKnife.bind(this, itemView); 80 | } 81 | 82 | @OnClick(R.id.versionItem) 83 | void onClickVersionItem() { 84 | viewModel.onClick(); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/fragment/AndroidVersionsFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.fragment; 18 | 19 | import android.os.Bundle; 20 | import android.support.annotation.NonNull; 21 | import android.support.annotation.Nullable; 22 | import android.view.LayoutInflater; 23 | import android.view.View; 24 | import android.view.ViewGroup; 25 | 26 | import com.hibrianlee.mvvmapp.fragment.ViewModelFragment; 27 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 28 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 29 | import com.hibrianlee.sample.mvvm.R; 30 | import com.hibrianlee.sample.mvvm.adapter.AndroidVersionsAdapter; 31 | import com.hibrianlee.sample.mvvm.databinding.FragmentAndroidVersionsBinding; 32 | import com.hibrianlee.sample.mvvm.viewmodel.AndroidVersionsViewModel; 33 | 34 | import butterknife.ButterKnife; 35 | import butterknife.OnClick; 36 | 37 | public class AndroidVersionsFragment extends BaseFragment { 38 | 39 | private AndroidVersionsViewModel androidVersionsViewModel; 40 | 41 | @Override 42 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 43 | Bundle savedInstanceState) { 44 | View root = inflater.inflate(R.layout.fragment_android_versions, container, false); 45 | ButterKnife.bind(this, root); 46 | return root; 47 | } 48 | 49 | @Nullable 50 | @Override 51 | protected ViewModel createAndBindViewModel(View root, 52 | @NonNull ActivityComponent activityComponent, 53 | @Nullable ViewModel.State savedViewModelState) { 54 | androidVersionsViewModel = viewModelFactory.createAndroidVersionsViewModel( 55 | new AndroidVersionsAdapter(viewModelFactory, activityComponent), activityComponent, 56 | savedViewModelState); 57 | FragmentAndroidVersionsBinding binding = FragmentAndroidVersionsBinding.bind(root); 58 | binding.setViewModel(androidVersionsViewModel); 59 | return androidVersionsViewModel; 60 | } 61 | 62 | @OnClick(R.id.hiBrianLee) 63 | void onClickHiBrianLee() { 64 | androidVersionsViewModel.onClickHiBrianLee(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/fragment/BaseFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.fragment; 18 | 19 | import com.hibrianlee.mvvmapp.fragment.ViewModelFragment; 20 | import com.hibrianlee.mvvmapp.inject.AppComponent; 21 | import com.hibrianlee.sample.mvvm.SampleAppComponent; 22 | import com.hibrianlee.sample.mvvm.viewmodel.ViewModelFactory; 23 | 24 | import javax.inject.Inject; 25 | 26 | public abstract class BaseFragment extends ViewModelFragment { 27 | 28 | @Inject 29 | protected ViewModelFactory viewModelFactory; 30 | 31 | @Override 32 | protected void inject(AppComponent appComponent) { 33 | ((SampleAppComponent) appComponent).inject(this); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/model/AndroidVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.model; 18 | 19 | import android.os.Parcel; 20 | import android.os.Parcelable; 21 | 22 | public class AndroidVersion implements Parcelable { 23 | 24 | private final String codeName; 25 | private final String version; 26 | private boolean selected; 27 | 28 | public AndroidVersion(String codeName, String version) { 29 | this.codeName = codeName; 30 | this.version = version; 31 | } 32 | 33 | public AndroidVersion(Parcel in) { 34 | codeName = in.readString(); 35 | version = in.readString(); 36 | selected = in.readInt() == 1; 37 | } 38 | 39 | @Override 40 | public int describeContents() { 41 | return 0; 42 | } 43 | 44 | @Override 45 | public void writeToParcel(Parcel dest, int flags) { 46 | dest.writeString(codeName); 47 | dest.writeString(version); 48 | dest.writeInt(selected ? 1 : 0); 49 | } 50 | 51 | public String getCodeName() { 52 | return codeName; 53 | } 54 | 55 | public String getVersion() { 56 | return version; 57 | } 58 | 59 | public boolean isSelected() { 60 | return selected; 61 | } 62 | 63 | public void toggleSelected() { 64 | selected = !selected; 65 | } 66 | 67 | @Override 68 | public boolean equals(Object o) { 69 | if (this == o) return true; 70 | if (o == null || getClass() != o.getClass()) return false; 71 | 72 | AndroidVersion that = (AndroidVersion) o; 73 | 74 | if (selected != that.selected) return false; 75 | if (codeName != null ? !codeName.equals(that.codeName) : that.codeName != null) 76 | return false; 77 | return !(version != null ? !version.equals(that.version) : that.version != null); 78 | 79 | } 80 | 81 | @Override 82 | public int hashCode() { 83 | int result = codeName != null ? codeName.hashCode() : 0; 84 | result = 31 * result + (version != null ? version.hashCode() : 0); 85 | result = 31 * result + (selected ? 1 : 0); 86 | return result; 87 | } 88 | 89 | public static Creator CREATOR = new Creator() { 90 | @Override 91 | public AndroidVersion createFromParcel(Parcel source) { 92 | return new AndroidVersion(source); 93 | } 94 | 95 | @Override 96 | public AndroidVersion[] newArray(int size) { 97 | return new AndroidVersion[size]; 98 | } 99 | }; 100 | } 101 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/viewmodel/AndroidVersionItemViewModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.viewmodel; 18 | 19 | import android.databinding.Bindable; 20 | import android.support.annotation.NonNull; 21 | 22 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 23 | import com.hibrianlee.mvvmapp.viewmodel.ItemViewModel; 24 | import com.hibrianlee.sample.mvvm.BR; 25 | import com.hibrianlee.sample.mvvm.model.AndroidVersion; 26 | 27 | public class AndroidVersionItemViewModel extends ItemViewModel { 28 | 29 | private AndroidVersion androidVersion; 30 | 31 | AndroidVersionItemViewModel(@NonNull ActivityComponent activityComponent) { 32 | super(activityComponent); 33 | } 34 | 35 | @Override 36 | public void setItem(AndroidVersion item) { 37 | androidVersion = item; 38 | notifyChange(); 39 | } 40 | 41 | public void onClick() { 42 | androidVersion.toggleSelected(); 43 | notifyPropertyChanged(BR.selected); 44 | } 45 | 46 | @Bindable 47 | public String getVersion() { 48 | return androidVersion.getVersion(); 49 | } 50 | 51 | @Bindable 52 | public String getCodeName() { 53 | return androidVersion.getCodeName(); 54 | } 55 | 56 | @Bindable 57 | public boolean getSelected() { 58 | return androidVersion.isSelected(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/viewmodel/AndroidVersionsViewModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.viewmodel; 18 | 19 | import android.os.Parcel; 20 | import android.support.annotation.NonNull; 21 | import android.support.annotation.Nullable; 22 | import android.support.v7.widget.LinearLayoutManager; 23 | import android.support.v7.widget.RecyclerView; 24 | 25 | import com.hibrianlee.mvvmapp.adapter.RecyclerViewAdapter; 26 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 27 | import com.hibrianlee.mvvmapp.viewmodel.RecyclerViewViewModel; 28 | import com.hibrianlee.sample.mvvm.R; 29 | import com.hibrianlee.sample.mvvm.adapter.AndroidVersionsAdapter; 30 | import com.hibrianlee.sample.mvvm.model.AndroidVersion; 31 | 32 | import java.util.ArrayList; 33 | 34 | public class AndroidVersionsViewModel extends RecyclerViewViewModel { 35 | 36 | AndroidVersionsAdapter adapter; 37 | 38 | AndroidVersionsViewModel(@NonNull AndroidVersionsAdapter adapter, 39 | @NonNull ActivityComponent activityComponent, 40 | @Nullable State savedInstanceState) { 41 | super(activityComponent, savedInstanceState); 42 | 43 | ArrayList versions; 44 | if (savedInstanceState instanceof AndroidVersionsState) { 45 | versions = ((AndroidVersionsState) savedInstanceState).versions; 46 | } else { 47 | versions = getVersions(); 48 | } 49 | this.adapter = adapter; 50 | adapter.setItems(versions); 51 | } 52 | 53 | @Override 54 | protected RecyclerViewAdapter getAdapter() { 55 | return adapter; 56 | } 57 | 58 | @Override 59 | protected RecyclerView.LayoutManager createLayoutManager() { 60 | return new LinearLayoutManager(appContext); 61 | } 62 | 63 | @Override 64 | public RecyclerViewViewModelState getInstanceState() { 65 | return new AndroidVersionsState(this); 66 | } 67 | 68 | public void onClickHiBrianLee() { 69 | try { 70 | attachedActivity.openUrl(appContext.getString(R.string.twitter_url)); 71 | } catch (Exception e) { 72 | e.printStackTrace(); 73 | } 74 | } 75 | 76 | private ArrayList getVersions() { 77 | ArrayList versions = new ArrayList<>(); 78 | versions.add(new AndroidVersion("Cupcake", "Android 1.5")); 79 | versions.add(new AndroidVersion("Donut", "Android 1.6")); 80 | versions.add(new AndroidVersion("Eclair", "Android 2.0-2.1")); 81 | versions.add(new AndroidVersion("Froyo", "Android 2.2")); 82 | versions.add(new AndroidVersion("Gingerbread", "Android 2.3")); 83 | versions.add(new AndroidVersion("Honeycomb", "Android 3.0-3.2")); 84 | versions.add(new AndroidVersion("Ice Cream Sandwich", "Android 4.0")); 85 | versions.add(new AndroidVersion("Jelly Bean", "Android 4.1-4.3")); 86 | versions.add(new AndroidVersion("KitKat", "Android 4.4")); 87 | versions.add(new AndroidVersion("Lollipop", "Android 5.0-5.1")); 88 | versions.add(new AndroidVersion("Marshmallow", "Android 6.0")); 89 | return versions; 90 | } 91 | 92 | private static class AndroidVersionsState extends RecyclerViewViewModelState { 93 | 94 | private final ArrayList versions; 95 | 96 | public AndroidVersionsState(AndroidVersionsViewModel viewModel) { 97 | super(viewModel); 98 | versions = viewModel.adapter.getItems(); 99 | } 100 | 101 | public AndroidVersionsState(Parcel in) { 102 | super(in); 103 | versions = in.createTypedArrayList(AndroidVersion.CREATOR); 104 | } 105 | 106 | @Override 107 | public void writeToParcel(Parcel dest, int flags) { 108 | super.writeToParcel(dest, flags); 109 | dest.writeTypedList(versions); 110 | } 111 | 112 | public static Creator CREATOR = new Creator() { 113 | @Override 114 | public AndroidVersionsState createFromParcel(Parcel source) { 115 | return new AndroidVersionsState(source); 116 | } 117 | 118 | @Override 119 | public AndroidVersionsState[] newArray(int size) { 120 | return new AndroidVersionsState[size]; 121 | } 122 | }; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/viewmodel/AppViewModelFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.viewmodel; 18 | 19 | import android.support.annotation.NonNull; 20 | import android.support.annotation.Nullable; 21 | 22 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 23 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 24 | import com.hibrianlee.sample.mvvm.adapter.AndroidVersionsAdapter; 25 | 26 | import javax.inject.Inject; 27 | import javax.inject.Singleton; 28 | 29 | @Singleton 30 | public class AppViewModelFactory implements ViewModelFactory { 31 | 32 | @Inject 33 | AppViewModelFactory() { 34 | 35 | } 36 | 37 | @NonNull 38 | @Override 39 | public MainViewModel createMainViewModel(@NonNull ActivityComponent activityComponent, 40 | @Nullable ViewModel.State savedViewModelState) { 41 | return new MainViewModel(activityComponent, savedViewModelState); 42 | } 43 | 44 | @NonNull 45 | @Override 46 | public ClickCountViewModel createClickCountViewModel( 47 | @NonNull ActivityComponent activityComponent, 48 | @Nullable ViewModel.State savedViewModelState) { 49 | return new ClickCountViewModel(activityComponent, savedViewModelState); 50 | } 51 | 52 | @NonNull 53 | @Override 54 | public AndroidVersionsViewModel createAndroidVersionsViewModel( 55 | @NonNull AndroidVersionsAdapter adapter, 56 | @NonNull ActivityComponent activityComponent, 57 | @Nullable ViewModel.State savedViewModelState) { 58 | return new AndroidVersionsViewModel(adapter, activityComponent, savedViewModelState); 59 | } 60 | 61 | @NonNull 62 | @Override 63 | public AndroidVersionItemViewModel createAndroidVersionItemViewModel( 64 | @NonNull ActivityComponent activityComponent) { 65 | return new AndroidVersionItemViewModel(activityComponent); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/viewmodel/ClickCountViewModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.viewmodel; 18 | 19 | import android.databinding.Bindable; 20 | import android.os.Parcel; 21 | import android.support.annotation.NonNull; 22 | import android.support.annotation.Nullable; 23 | 24 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 25 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 26 | import com.hibrianlee.sample.mvvm.BR; 27 | import com.hibrianlee.sample.mvvm.R; 28 | 29 | public class ClickCountViewModel extends ViewModel { 30 | 31 | int clicks; 32 | 33 | ClickCountViewModel(@NonNull ActivityComponent activityComponent, 34 | @Nullable State savedInstanceState) { 35 | super(activityComponent, savedInstanceState); 36 | if (savedInstanceState instanceof ClickCountState) { 37 | clicks = ((ClickCountState) savedInstanceState).clicks; 38 | } 39 | } 40 | 41 | @Override 42 | public State getInstanceState() { 43 | return new ClickCountState(this); 44 | } 45 | 46 | @Bindable 47 | public String getClickCountText() { 48 | return String.format(appContext.getString(R.string.click_count), clicks); 49 | } 50 | 51 | public void onClickButton() { 52 | clicks++; 53 | notifyPropertyChanged(BR.clickCountText); 54 | } 55 | 56 | public void onClickHiBrianLee() { 57 | try { 58 | attachedActivity.openUrl(appContext.getString(R.string.twitter_url)); 59 | } catch (Exception e) { 60 | e.printStackTrace(); 61 | } 62 | } 63 | 64 | private static class ClickCountState extends State { 65 | 66 | private final int clicks; 67 | 68 | protected ClickCountState(ClickCountViewModel viewModel) { 69 | super(viewModel); 70 | clicks = viewModel.clicks; 71 | } 72 | 73 | public ClickCountState(Parcel in) { 74 | super(in); 75 | clicks = in.readInt(); 76 | } 77 | 78 | @Override 79 | public void writeToParcel(Parcel dest, int flags) { 80 | super.writeToParcel(dest, flags); 81 | dest.writeInt(clicks); 82 | } 83 | 84 | public static Creator CREATOR = new Creator() { 85 | @Override 86 | public ClickCountState createFromParcel(Parcel source) { 87 | return new ClickCountState(source); 88 | } 89 | 90 | @Override 91 | public ClickCountState[] newArray(int size) { 92 | return new ClickCountState[size]; 93 | } 94 | }; 95 | } 96 | } 97 | 98 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/viewmodel/MainViewModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.viewmodel; 18 | 19 | import android.support.annotation.NonNull; 20 | import android.support.annotation.Nullable; 21 | 22 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 23 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 24 | import com.hibrianlee.sample.mvvm.R; 25 | import com.hibrianlee.sample.mvvm.activity.AndroidVersionsActivity; 26 | import com.hibrianlee.sample.mvvm.activity.ClickCountActivity; 27 | 28 | import java.net.URISyntaxException; 29 | 30 | public class MainViewModel extends ViewModel { 31 | 32 | MainViewModel(@NonNull ActivityComponent activityComponent, 33 | @Nullable State savedInstanceState) { 34 | super(activityComponent, savedInstanceState); 35 | } 36 | 37 | public void onClickButtonClicks() { 38 | attachedActivity.startActivity(ClickCountActivity.class); 39 | } 40 | 41 | public void onClickButtonRecyclerView() { 42 | attachedActivity.startActivity(AndroidVersionsActivity.class); 43 | } 44 | 45 | public void onClickHiBrianLee() { 46 | try { 47 | attachedActivity.openUrl(appContext.getString(R.string.twitter_url)); 48 | } catch (URISyntaxException e) { 49 | e.printStackTrace(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /sample/src/main/java/com/hibrianlee/sample/mvvm/viewmodel/ViewModelFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Brian Lee (@hiBrianLee) 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 com.hibrianlee.sample.mvvm.viewmodel; 18 | 19 | import android.support.annotation.NonNull; 20 | import android.support.annotation.Nullable; 21 | 22 | import com.hibrianlee.mvvmapp.inject.ActivityComponent; 23 | import com.hibrianlee.mvvmapp.viewmodel.ViewModel; 24 | import com.hibrianlee.sample.mvvm.adapter.AndroidVersionsAdapter; 25 | 26 | public interface ViewModelFactory { 27 | 28 | @NonNull 29 | MainViewModel createMainViewModel(@NonNull ActivityComponent activityComponent, 30 | @Nullable ViewModel.State savedViewModelState); 31 | @NonNull 32 | ClickCountViewModel createClickCountViewModel(@NonNull ActivityComponent activityComponent, 33 | @Nullable ViewModel.State savedViewModelState); 34 | 35 | @NonNull 36 | AndroidVersionsViewModel createAndroidVersionsViewModel( 37 | @NonNull AndroidVersionsAdapter adapter, 38 | @NonNull ActivityComponent activityComponent, 39 | @Nullable ViewModel.State savedViewModelState); 40 | 41 | @NonNull 42 | AndroidVersionItemViewModel createAndroidVersionItemViewModel( 43 | @NonNull ActivityComponent activityComponent); 44 | } 45 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/item_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_android_versions.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_click_count.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 10 | 11 | 12 | 17 | 18 | 23 | 24 |