├── .gitignore ├── LICENSE.txt ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── kotlinandroidviewbindings ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── marcinmoskala │ └── kotlinandroidviewbindings │ ├── BindToClick.kt │ ├── BindToEditText.kt │ ├── BindToEditorActions.kt │ ├── BindToErrorId.kt │ ├── BindToLoading.kt │ ├── BindToLongClick.kt │ ├── BindToRequestFocus.kt │ ├── BindToSwipeRefresh.kt │ ├── BindToText.kt │ ├── BindToTextId.kt │ └── BindToVisibility.kt ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── marcinmoskala │ │ └── kotlinandroidviewbindings │ │ ├── BindToActionTests.kt │ │ ├── BindToClickTests.kt │ │ ├── BindToTextIdTests.kt │ │ └── BindToTextTests.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── marcinmoskala │ │ │ └── kotlinandroidviewbindings │ │ │ ├── Utills.kt │ │ │ └── login │ │ │ ├── LoginActivity.kt │ │ │ ├── LoginPresenter.kt │ │ │ ├── LoginRepository.kt │ │ │ ├── LoginResponse.kt │ │ │ ├── LoginUseCase.kt │ │ │ ├── LoginView.kt │ │ │ └── ValidateLoginFieldsUseCase.kt │ └── res │ │ ├── layout │ │ └── activity_login.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── marcinmoskala │ └── kotlinandroidviewbindings │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/* 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KotlinAndroidViewBindings 2 | 3 | Bindings for properties with Kotlin types (Boolean, String) to layout traits (visibility, text). 4 | 5 | [![](https://jitpack.io/v/MarcinMoskala/KotlinAndroidViewBindings.svg)](https://jitpack.io/#MarcinMoskala/KotlinAndroidViewBindings) 6 | [![Analytics](https://ga-beacon.appspot.com/UA-92159206-7/main-page?pixel)](https://github.com/MarcinMoskala/MarvelGallery) 7 | 8 | Library allows to bind properties of basic types (String, Int, functional) to view element properties. Example: 9 | 10 | ```kotlin 11 | var email: String by bindToTextView(R.id.emailView) 12 | val emailRequestFocus: ()->Unit by bindToRequestFocus(R.id.emailView) 13 | var emailErrorId: Int? by bindToErrorId(R.id.emailView) 14 | ``` 15 | 16 | This allows to make clear and simple MVP. Example: 17 | 18 | LoginView.kt 19 | ```kotlin 20 | interface LoginView { 21 | var progressVisible: Boolean 22 | 23 | var email: String 24 | val emailRequestFocus: ()->Unit 25 | var emailErrorId: Int? 26 | 27 | var password: String 28 | val passwordRequestFocus: ()->Unit 29 | var passwordErrorId: Int? 30 | 31 | var loginButtonClickedCallback: ()->Unit 32 | } 33 | ``` 34 | 35 | LoginActivity.kt 36 | ```kotlin 37 | class LoginActivity : AppCompatActivity(), LoginView { 38 | 39 | override var progressVisible by bindToLoading(R.id.progressView, R.id.loginFormView) 40 | 41 | override var email by bindToTextView(R.id.emailView) 42 | override val emailRequestFocus by bindToRequestFocus(R.id.emailView) 43 | override var emailErrorId by bindToErrorId(R.id.emailView) 44 | 45 | override var password by bindToTextView(R.id.passwordView) 46 | override val passwordRequestFocus by bindToRequestFocus(R.id.passwordView) 47 | override var passwordErrorId by bindToErrorId(R.id.passwordView) 48 | 49 | override var loginButtonClickedCallback by bindToClick(R.id.loginButton) 50 | 51 | //... 52 | } 53 | ``` 54 | 55 | Presenter.kt 56 | ```kotlin 57 | class LoginPresenter(val view: LoginView) { 58 | 59 | fun onCreate() { 60 | view.loginButtonClickedCallback = { attemptLogin() } 61 | } 62 | 63 | //... 64 | } 65 | ``` 66 | 67 | This makes presenters fully unit-testable while keeping Activities short and simple. Full example [here](https://github.com/MarcinMoskala/KotlinAndroidViewBindings/tree/master/sample/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/login). Library usage in wider context [here](https://github.com/MarcinMoskala/SimpleKotlinMvpBoilerplate). 68 | 69 | ## Installation 70 | 71 | To add KotlinAndroidViewBindings to the project, add to `build.gradle` file: 72 | 73 | ```Groovy 74 | dependencies { 75 | implementation 'com.github.MarcinMoskala:KotlinAndroidViewBindings:0.12' 76 | } 77 | ``` 78 | 79 | While library is located on JitPack, remember to add to module `build.gradle` (unless you already have it): 80 | 81 | ```Groovy 82 | repositories { 83 | maven { url 'https://jitpack.io' } 84 | } 85 | ``` 86 | 87 | ## Media 88 | 89 | * [Still MVP or already MVVM?](https://medium.com/@marcinmoskala/still-mvp-or-already-mvvm-d3e2c7c44a03) 90 | 91 | ## Other libraries 92 | 93 | If you like it, remember to leave the star and check out my other libraries: 94 | * [ActivityStarter](https://github.com/MarcinMoskala/ActivityStarter/blob/master/README.md) - Simple Android Library, that provides easy way to start and save state of Activities, Fragments, Services and Receivers with arguments. 95 | * [PreferenceHolder](https://github.com/MarcinMoskala/PreferenceHolder) - Library for simple SharedPreference management in Kotlin 96 | * [ArcSeekBar](https://github.com/MarcinMoskala/ArcSeekBar) - Good looking curved Android SeekBar 97 | * [VideoPlayView](https://github.com/MarcinMoskala/VideoPlayView) - Custom Android view with video player, loader and placeholder image 98 | 99 | License 100 | ------- 101 | 102 | Copyright 2017 Marcin Moskała 103 | 104 | Licensed under the Apache License, Version 2.0 (the "License"); 105 | you may not use this file except in compliance with the License. 106 | You may obtain a copy of the License at 107 | 108 | http://www.apache.org/licenses/LICENSE-2.0 109 | 110 | Unless required by applicable law or agreed to in writing, software 111 | distributed under the License is distributed on an "AS IS" BASIS, 112 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 113 | See the License for the specific language governing permissions and 114 | limitations under the License. 115 | 116 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.1.0' 3 | repositories { 4 | jcenter() 5 | } 6 | dependencies { 7 | classpath 'com.android.tools.build:gradle:2.3.3' 8 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' 9 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 10 | } 11 | } 12 | 13 | allprojects { 14 | repositories { 15 | jcenter() 16 | maven { url 'https://jitpack.io' } 17 | } 18 | } 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | task clean(type: Delete) { 25 | delete rootProject.buildDir 26 | } 27 | -------------------------------------------------------------------------------- /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 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcinMoskala/KotlinAndroidViewBindings/16595985637c21c91e24ca49a511698717abcc7a/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Apr 20 13:12:23 CEST 2017 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-3.3-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 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /kotlinandroidviewbindings/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /kotlinandroidviewbindings/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | 4 | apply plugin: 'com.github.dcendents.android-maven' 5 | 6 | group='com.marcinmoskala.kotlinandroidviewbindings' 7 | 8 | android { 9 | compileSdkVersion 25 10 | buildToolsVersion "25.0.3" 11 | 12 | defaultConfig { 13 | minSdkVersion 9 14 | targetSdkVersion 25 15 | versionCode 1 16 | versionName "0.1" 17 | } 18 | } 19 | 20 | dependencies { 21 | compile 'org.jetbrains.kotlin:kotlin-stdlib:1.1.51' 22 | compile 'com.android.support:support-annotations:25.3.1' 23 | compile 'com.android.support:design:25.3.1' 24 | compile "com.android.support:support-v4:25.3.1" 25 | } 26 | -------------------------------------------------------------------------------- /kotlinandroidviewbindings/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 C:\Users\marci\AppData\Local\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 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToClick.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.view.View 9 | import android.widget.FrameLayout 10 | import kotlin.properties.ReadWriteProperty 11 | import kotlin.reflect.KProperty 12 | 13 | fun Activity.bindToClick(@IdRes viewId: Int): ReadWriteProperty Unit> 14 | = bindToClickP { findViewById(viewId) } 15 | 16 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 17 | fun Fragment.bindToClick(@IdRes viewId: Int): ReadWriteProperty Unit> 18 | = bindToClickP { view.findViewById(viewId) } 19 | 20 | fun android.support.v4.app.Fragment.bindToClick(@IdRes viewId: Int): ReadWriteProperty Unit> 21 | = bindToClickP { view!!.findViewById(viewId) } 22 | 23 | fun FrameLayout.bindToClick(@IdRes viewId: Int): ReadWriteProperty Unit> 24 | = bindToClickP { findViewById(viewId) } 25 | 26 | fun Activity.bindToClick(viewProvider: ()->View): ReadWriteProperty Unit> 27 | = bindToClickP { viewProvider() } 28 | 29 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 30 | fun Fragment.bindToClick(viewProvider: ()->View): ReadWriteProperty Unit> 31 | = bindToClickP { viewProvider() } 32 | 33 | fun android.support.v4.app.Fragment.bindToClick(viewProvider: ()->View): ReadWriteProperty Unit> 34 | = bindToClickP { viewProvider() } 35 | 36 | fun View.bindToClick(): ReadWriteProperty Unit> 37 | = bindToClickP { this } 38 | 39 | private fun bindToClickP(viewProvider: () -> View): ReadWriteProperty Unit> 40 | = OnClickBinding(lazy(viewProvider)) 41 | 42 | private class OnClickBinding(viewProvider: Lazy) : ReadWriteProperty Unit> { 43 | 44 | val view by viewProvider 45 | var function: (() -> Unit)? = null 46 | 47 | override fun getValue(thisRef: Any?, property: KProperty<*>): () -> Unit { 48 | return function ?: noop 49 | } 50 | 51 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: () -> Unit) { 52 | setUpListener() 53 | function = value 54 | } 55 | 56 | fun setUpListener() { 57 | if (function == null) view.setOnClickListener { function?.invoke() } 58 | } 59 | 60 | companion object { 61 | val noop: () -> Unit = {} 62 | } 63 | } -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToEditText.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.widget.EditText 9 | import android.widget.FrameLayout 10 | import kotlin.properties.ReadWriteProperty 11 | import kotlin.reflect.KProperty 12 | 13 | fun Activity.bindToEditText(@IdRes editTextId: Int): ReadWriteProperty 14 | = bindToEditTextP { findViewById(editTextId) as EditText } 15 | 16 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 17 | fun Fragment.bindToEditText(@IdRes viewId: Int): ReadWriteProperty 18 | = bindToEditTextP { view.findViewById(viewId) as EditText } 19 | 20 | fun android.support.v4.app.Fragment.bindToEditText(@IdRes viewId: Int): ReadWriteProperty 21 | = bindToEditTextP { view!!.findViewById(viewId) as EditText } 22 | 23 | fun FrameLayout.bindToEditText(@IdRes viewId: Int): ReadWriteProperty 24 | = bindToEditTextP { findViewById(viewId) as EditText } 25 | 26 | fun EditText.bindToEditText(): ReadWriteProperty 27 | = bindToEditTextP { this } 28 | 29 | fun Activity.bindToEditText(viewProvider: () -> EditText): ReadWriteProperty 30 | = bindToEditTextP { viewProvider() } 31 | 32 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 33 | fun Fragment.bindToEditText(viewProvider: () -> EditText): ReadWriteProperty 34 | = bindToEditTextP { viewProvider() } 35 | 36 | fun android.support.v4.app.Fragment.bindToEditText(viewProvider: () -> EditText): ReadWriteProperty 37 | = bindToEditTextP { viewProvider() } 38 | 39 | private fun bindToEditTextP(viewProvider: () -> EditText): ReadWriteProperty 40 | = EditTextViewTextBinding(lazy(viewProvider)) 41 | 42 | private class EditTextViewTextBinding(lazyViewProvider: Lazy) : ReadWriteProperty { 43 | 44 | val view by lazyViewProvider 45 | 46 | override fun getValue(thisRef: Any?, property: KProperty<*>): String { 47 | return view.text.toString() 48 | } 49 | 50 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: String) { 51 | view.setText(value) 52 | } 53 | } -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToEditorActions.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.view.inputmethod.EditorInfo 9 | import android.widget.EditText 10 | import android.widget.FrameLayout 11 | import kotlin.properties.ReadWriteProperty 12 | import kotlin.reflect.KProperty 13 | 14 | fun Activity.bindToEditorActions(@IdRes editTextId: Int, predicate: (Int?, Int?) -> Boolean): ReadWriteProperty Unit> 15 | = bindToEditorActionsP(predicate, { findViewById(editTextId) as EditText }) 16 | 17 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 18 | fun Fragment.bindToEditorActions(@IdRes viewId: Int, predicate: (Int?, Int?) -> Boolean): ReadWriteProperty Unit> 19 | = bindToEditorActionsP(predicate) { view.findViewById(viewId) as EditText } 20 | 21 | fun android.support.v4.app.Fragment.bindToEditorActions(@IdRes viewId: Int, predicate: (Int?, Int?) -> Boolean): ReadWriteProperty Unit> 22 | = bindToEditorActionsP(predicate) { view!!.findViewById(viewId) as EditText } 23 | 24 | fun FrameLayout.bindToEditorActions(@IdRes viewId: Int, predicate: (Int?, Int?) -> Boolean): ReadWriteProperty Unit> 25 | = bindToEditorActionsP(predicate) { findViewById(viewId) as EditText } 26 | 27 | fun Activity.bindToEditorActions(viewProvider: () -> EditText, predicate: (Int?, Int?) -> Boolean): ReadWriteProperty Unit> 28 | = bindToEditorActionsP(predicate, { viewProvider() }) 29 | 30 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 31 | fun Fragment.bindToEditorActions(viewProvider: () -> EditText, predicate: (Int?, Int?) -> Boolean): ReadWriteProperty Unit> 32 | = bindToEditorActionsP(predicate) { viewProvider() } 33 | 34 | fun android.support.v4.app.Fragment.bindToEditorActions(viewProvider: () -> EditText, predicate: (Int?, Int?) -> Boolean): ReadWriteProperty Unit> 35 | = bindToEditorActionsP(predicate) { viewProvider() } 36 | 37 | fun EditText.bindToEditorActions(predicate: (Int?, Int?) -> Boolean): ReadWriteProperty Unit> 38 | = bindToEditorActionsP(predicate) { this } 39 | 40 | private fun bindToEditorActionsP(predicate: (Int?, Int?) -> Boolean, viewProvider: () -> EditText): ReadWriteProperty Unit> 41 | = OnEditorActionBinding(predicate, lazy(viewProvider)) 42 | 43 | private class OnEditorActionBinding( 44 | val predicate: (Int?, Int?) -> Boolean, 45 | viewProvider: Lazy 46 | ) : ReadWriteProperty Unit> { 47 | 48 | val view by viewProvider 49 | var function: (() -> Unit)? = null 50 | 51 | override fun getValue(thisRef: Any?, property: KProperty<*>): () -> Unit { 52 | return function ?: noop 53 | } 54 | 55 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: () -> Unit) { 56 | setUpListener() 57 | function = value 58 | } 59 | 60 | fun setUpListener() { 61 | if (function == null) { 62 | view.setOnEditorActionListener { _, actionId: Int?, event -> 63 | val handleAction = predicate(actionId, event?.keyCode) 64 | if (handleAction) function?.invoke() 65 | return@setOnEditorActionListener handleAction 66 | } 67 | } 68 | } 69 | 70 | companion object { 71 | val noop: () -> Unit = {} 72 | } 73 | } -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToErrorId.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.widget.EditText 9 | import android.widget.FrameLayout 10 | import kotlin.properties.ReadWriteProperty 11 | import kotlin.reflect.KProperty 12 | 13 | fun Activity.bindToErrorId(@IdRes editTextId: Int): ReadWriteProperty 14 | = bindToErrorIdP { findViewById(editTextId) as EditText } 15 | 16 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 17 | fun Fragment.bindToErrorId(@IdRes viewId: Int): ReadWriteProperty 18 | = bindToErrorIdP { view.findViewById(viewId) as EditText } 19 | 20 | fun android.support.v4.app.Fragment.bindToErrorId(@IdRes viewId: Int): ReadWriteProperty 21 | = bindToErrorIdP { view!!.findViewById(viewId) as EditText } 22 | 23 | fun FrameLayout.bindToErrorId(@IdRes viewId: Int): ReadWriteProperty 24 | = bindToErrorIdP { findViewById(viewId) as EditText } 25 | 26 | fun Activity.bindToErrorId(viewProvider: () -> EditText): ReadWriteProperty 27 | = bindToErrorIdP { viewProvider() } 28 | 29 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 30 | fun Fragment.bindToErrorId(viewProvider: () -> EditText): ReadWriteProperty 31 | = bindToErrorIdP { viewProvider() } 32 | 33 | fun android.support.v4.app.Fragment.bindToErrorId(viewProvider: () -> EditText): ReadWriteProperty 34 | = bindToErrorIdP { viewProvider() } 35 | 36 | fun EditText.bindToErrorId(): ReadWriteProperty 37 | = bindToErrorIdP { this } 38 | 39 | private fun bindToErrorIdP(viewProvider: () -> EditText): ReadWriteProperty 40 | = EditTextViewErrorIdBinding(lazy(viewProvider)) 41 | 42 | private class EditTextViewErrorIdBinding(lazyViewProvider: Lazy) : ReadWriteProperty { 43 | 44 | val view by lazyViewProvider 45 | var currentError: Int? = null 46 | 47 | override fun getValue(thisRef: Any?, property: KProperty<*>): Int? { 48 | return currentError 49 | } 50 | 51 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: Int?) { 52 | currentError = value 53 | view.error = value?.let { view.context.getString(value) } 54 | } 55 | } -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToLoading.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.view.View 9 | import android.widget.EditText 10 | import android.widget.FrameLayout 11 | import kotlin.properties.ReadWriteProperty 12 | import kotlin.reflect.KProperty 13 | 14 | fun Activity.bindToLoading( 15 | @IdRes progressViewId: Int, 16 | @IdRes restViewHolderId: Int 17 | ): ReadWriteProperty = bindToLoadingP( 18 | progressViewProvider = { findViewById(progressViewId) }, 19 | restViewHolderProvider = { findViewById(restViewHolderId) } 20 | ) 21 | 22 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 23 | fun Fragment.bindToLoading( 24 | @IdRes progressViewId: Int, 25 | @IdRes restViewHolderId: Int 26 | ): ReadWriteProperty = bindToLoadingP( 27 | progressViewProvider = { view.findViewById(progressViewId) }, 28 | restViewHolderProvider = { view.findViewById(restViewHolderId) } 29 | ) 30 | 31 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 32 | fun android.support.v4.app.Fragment.bindToLoading( 33 | @IdRes progressViewId: Int, 34 | @IdRes restViewHolderId: Int 35 | ): ReadWriteProperty = bindToLoadingP( 36 | progressViewProvider = { view!!.findViewById(progressViewId) }, 37 | restViewHolderProvider = { view!!.findViewById(restViewHolderId) } 38 | ) 39 | 40 | fun FrameLayout.bindToLoading( 41 | @IdRes progressViewId: Int, 42 | @IdRes restViewHolderId: Int 43 | ): ReadWriteProperty = bindToLoadingP( 44 | progressViewProvider = { findViewById(progressViewId) }, 45 | restViewHolderProvider = { findViewById(restViewHolderId) } 46 | ) 47 | 48 | fun Activity.bindToLoading( 49 | progressViewProvider: () -> View, 50 | restViewProvider: () -> View 51 | ): ReadWriteProperty = bindToLoadingP( 52 | progressViewProvider = { progressViewProvider() }, 53 | restViewHolderProvider = { restViewProvider() } 54 | ) 55 | 56 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 57 | fun Fragment.bindToLoading( 58 | progressViewProvider: () -> View, 59 | restViewProvider: () -> View 60 | ): ReadWriteProperty = bindToLoadingP( 61 | progressViewProvider = { progressViewProvider() }, 62 | restViewHolderProvider = { restViewProvider() } 63 | ) 64 | 65 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 66 | fun android.support.v4.app.Fragment.bindToLoading( 67 | progressViewProvider: () -> View, 68 | restViewProvider: () -> View 69 | ): ReadWriteProperty = bindToLoadingP( 70 | progressViewProvider = { progressViewProvider() }, 71 | restViewHolderProvider = { restViewProvider() } 72 | ) 73 | 74 | fun Pair.bindToLoading(): ReadWriteProperty = bindToLoadingP( 75 | progressViewProvider = { first }, 76 | restViewHolderProvider = { second } 77 | ) 78 | 79 | private fun bindToLoadingP( 80 | progressViewProvider: () -> View, 81 | restViewHolderProvider: () -> View 82 | ): ReadWriteProperty = LoadingBinding( 83 | lazy(progressViewProvider), 84 | lazy(restViewHolderProvider) 85 | ) 86 | 87 | private class LoadingBinding( 88 | progressLazyViewProvider: Lazy, 89 | restViewHolderLazyProvider: Lazy 90 | ) : ReadWriteProperty { 91 | 92 | val progressView by progressLazyViewProvider 93 | val restOfView by restViewHolderLazyProvider 94 | 95 | override fun getValue(thisRef: Any?, property: KProperty<*>): Boolean { 96 | return progressView.visibility == View.VISIBLE 97 | } 98 | 99 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: Boolean) { 100 | progressView.visibility = if (value) View.VISIBLE else View.GONE 101 | restOfView?.visibility = if (value) View.GONE else View.VISIBLE 102 | } 103 | } -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToLongClick.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.view.View 9 | import android.widget.FrameLayout 10 | import kotlin.properties.ReadWriteProperty 11 | import kotlin.reflect.KProperty 12 | 13 | fun Activity.bindToLongClick(@IdRes viewId: Int): ReadWriteProperty Unit> 14 | = bindToLongClickP { findViewById(viewId) } 15 | 16 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 17 | fun Fragment.bindToLongClick(@IdRes viewId: Int): ReadWriteProperty Unit> 18 | = bindToLongClickP { view.findViewById(viewId) } 19 | 20 | fun android.support.v4.app.Fragment.bindToLongClick(@IdRes viewId: Int): ReadWriteProperty Unit> 21 | = bindToLongClickP { view!!.findViewById(viewId) } 22 | 23 | fun FrameLayout.bindToLongClick(@IdRes viewId: Int): ReadWriteProperty Unit> 24 | = bindToLongClickP { findViewById(viewId) } 25 | 26 | fun Activity.bindToLongClick(viewProvider: () -> View): ReadWriteProperty Unit> 27 | = bindToLongClickP { viewProvider() } 28 | 29 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 30 | fun Fragment.bindToLongClick(viewProvider: () -> View): ReadWriteProperty Unit> 31 | = bindToLongClickP { viewProvider() } 32 | 33 | fun android.support.v4.app.Fragment.bindToLongClick(viewProvider: () -> View): ReadWriteProperty Unit> 34 | = bindToLongClickP { viewProvider() } 35 | 36 | fun View.bindToLongClick(): ReadWriteProperty Unit> 37 | = bindToLongClickP { this } 38 | 39 | private fun bindToLongClickP(viewProvider: () -> View): ReadWriteProperty Unit> 40 | = OnLongClickBinding(lazy(viewProvider)) 41 | 42 | private class OnLongClickBinding(viewProvider: Lazy) : ReadWriteProperty Unit> { 43 | 44 | val view by viewProvider 45 | var function: (() -> Unit)? = null 46 | 47 | override fun getValue(thisRef: Any?, property: KProperty<*>): () -> Unit { 48 | return function ?: noop 49 | } 50 | 51 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: () -> Unit) { 52 | function = value 53 | view.setOnLongClickListener { function?.invoke(); true } 54 | } 55 | 56 | companion object { 57 | val noop: () -> Unit = {} 58 | } 59 | } -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToRequestFocus.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.view.View 9 | import android.widget.FrameLayout 10 | import kotlin.properties.ReadOnlyProperty 11 | import kotlin.reflect.KProperty 12 | 13 | fun Activity.bindToRequestFocus(@IdRes editViewId: Int): ReadOnlyProperty Unit> 14 | = bindToRequestFocusP { findViewById(editViewId) } 15 | 16 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 17 | fun Fragment.bindToRequestFocus(@IdRes editViewId: Int): ReadOnlyProperty Unit> 18 | = bindToRequestFocusP { view.findViewById(editViewId) } 19 | 20 | fun android.support.v4.app.Fragment.bindToRequestFocus(@IdRes editViewId: Int): ReadOnlyProperty Unit> 21 | = bindToRequestFocusP { view!!.findViewById(editViewId) } 22 | 23 | fun FrameLayout.bindToRequestFocus(@IdRes editViewId: Int): ReadOnlyProperty Unit> 24 | = bindToRequestFocusP { findViewById(editViewId) } 25 | 26 | fun Activity.bindToRequestFocus(viewProvider: () -> View): ReadOnlyProperty Unit> 27 | = bindToRequestFocusP { viewProvider() } 28 | 29 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 30 | fun Fragment.bindToRequestFocus(viewProvider: () -> View): ReadOnlyProperty Unit> 31 | = bindToRequestFocusP { viewProvider() } 32 | 33 | fun android.support.v4.app.Fragment.bindToRequestFocus(viewProvider: () -> View): ReadOnlyProperty Unit> 34 | = bindToRequestFocusP { viewProvider() } 35 | 36 | fun View.bindToRequestFocus(): ReadOnlyProperty Unit> 37 | = bindToRequestFocusP { this } 38 | 39 | private fun bindToRequestFocusP(viewProvider: () -> View): ReadOnlyProperty Unit> 40 | = RequestFocusBinding(lazy(viewProvider)) 41 | 42 | private class RequestFocusBinding(viewProvider: Lazy) : ReadOnlyProperty Unit> { 43 | 44 | val view by viewProvider 45 | val requestFocus: ()->Unit by lazy { fun() { view.requestFocus() } } 46 | 47 | override fun getValue(thisRef: Any?, property: KProperty<*>) = requestFocus 48 | } -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToSwipeRefresh.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.support.v4.widget.SwipeRefreshLayout 9 | import android.widget.FrameLayout 10 | import kotlin.properties.ReadWriteProperty 11 | import kotlin.reflect.KProperty 12 | 13 | fun Activity.bindToSwipeRefresh(@IdRes swipeRefreshLayoutId: Int): ReadWriteProperty 14 | = bindToSwipeRefreshP { findViewById(swipeRefreshLayoutId) as SwipeRefreshLayout } 15 | 16 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 17 | fun Fragment.bindToSwipeRefresh(@IdRes swipeRefreshLayoutId: Int): ReadWriteProperty 18 | = bindToSwipeRefreshP { view.findViewById(swipeRefreshLayoutId) as SwipeRefreshLayout } 19 | 20 | fun android.support.v4.app.Fragment.bindToSwipeRefresh(@IdRes swipeRefreshLayoutId: Int): ReadWriteProperty 21 | = bindToSwipeRefreshP { view!!.findViewById(swipeRefreshLayoutId) as SwipeRefreshLayout } 22 | 23 | fun FrameLayout.bindToSwipeRefresh(@IdRes swipeRefreshLayoutId: Int): ReadWriteProperty 24 | = bindToSwipeRefreshP { findViewById(swipeRefreshLayoutId) as SwipeRefreshLayout } 25 | 26 | fun Activity.bindToSwipeRefresh(swipeRefreshLayoutProvider: () -> SwipeRefreshLayout): ReadWriteProperty 27 | = bindToSwipeRefreshP { swipeRefreshLayoutProvider() } 28 | 29 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 30 | fun Fragment.bindToSwipeRefresh(viewProvider: () -> SwipeRefreshLayout): ReadWriteProperty 31 | = bindToSwipeRefreshP { viewProvider() } 32 | 33 | fun android.support.v4.app.Fragment.bindToSwipeRefresh(viewProvider: () -> SwipeRefreshLayout): ReadWriteProperty 34 | = bindToSwipeRefreshP { viewProvider() } 35 | 36 | fun SwipeRefreshLayout.bindToSwipeRefresh(): ReadWriteProperty 37 | = bindToSwipeRefreshP { this } 38 | 39 | private fun bindToSwipeRefreshP(viewProvider: () -> SwipeRefreshLayout): ReadWriteProperty 40 | = SwipeRefreshBinding(lazy(viewProvider)) 41 | 42 | private class SwipeRefreshBinding(viewProvider: Lazy) : ReadWriteProperty { 43 | 44 | val view by viewProvider 45 | 46 | override fun getValue(thisRef: Any?, property: KProperty<*>): Boolean { 47 | return view.isRefreshing 48 | } 49 | 50 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: Boolean) { 51 | view.isRefreshing = value 52 | } 53 | } -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToText.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.widget.FrameLayout 9 | import android.widget.TextView 10 | import kotlin.properties.ReadWriteProperty 11 | import kotlin.reflect.KProperty 12 | 13 | fun Activity.bindToText(@IdRes textViewId: Int): ReadWriteProperty 14 | = bindToTextP { findViewById(textViewId) as TextView } 15 | 16 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 17 | fun Fragment.bindToText(@IdRes textViewId: Int): ReadWriteProperty 18 | = bindToTextP { view.findViewById(textViewId) as TextView } 19 | 20 | fun android.support.v4.app.Fragment.bindToText(@IdRes textViewId: Int): ReadWriteProperty 21 | = bindToTextP { view!!.findViewById(textViewId) as TextView } 22 | 23 | fun FrameLayout.bindToText(@IdRes textViewId: Int): ReadWriteProperty 24 | = bindToTextP { findViewById(textViewId) as TextView } 25 | 26 | fun Activity.bindToText(viewProvider: () -> TextView): ReadWriteProperty 27 | = bindToTextP { viewProvider() } 28 | 29 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 30 | fun Fragment.bindToText(viewProvider: () -> TextView): ReadWriteProperty 31 | = bindToTextP { viewProvider() } 32 | 33 | fun android.support.v4.app.Fragment.bindToText(viewProvider: () -> TextView): ReadWriteProperty 34 | = bindToTextP { viewProvider() } 35 | 36 | fun TextView.bindToText(): ReadWriteProperty 37 | = bindToTextP { this } 38 | 39 | private fun bindToTextP(viewProvider: () -> TextView): ReadWriteProperty 40 | = TextBinding(lazy(viewProvider)) 41 | 42 | private class TextBinding(lazyViewProvider: Lazy) : ReadWriteProperty { 43 | 44 | val view by lazyViewProvider 45 | 46 | override fun getValue(thisRef: Any?, property: KProperty<*>): String { 47 | return view.text.toString() 48 | } 49 | 50 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: String) { 51 | view.text = value 52 | } 53 | } -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToTextId.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.widget.FrameLayout 9 | import android.widget.TextView 10 | import kotlin.properties.ReadWriteProperty 11 | import kotlin.reflect.KProperty 12 | 13 | fun Activity.bindToTextId(@IdRes textViewId: Int): ReadWriteProperty 14 | = bindToTextIdP { findViewById(textViewId) as TextView } 15 | 16 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 17 | fun Fragment.bindToTextId(@IdRes textViewId: Int): ReadWriteProperty 18 | = bindToTextIdP { view.findViewById(textViewId) as TextView } 19 | 20 | fun android.support.v4.app.Fragment.bindToTextId(@IdRes textViewId: Int): ReadWriteProperty 21 | = bindToTextIdP { view!!.findViewById(textViewId) as TextView } 22 | 23 | fun FrameLayout.bindToTextId(@IdRes textViewId: Int): ReadWriteProperty 24 | = bindToTextIdP { findViewById(textViewId) as TextView } 25 | 26 | fun Activity.bindToTextId(viewProvider: () -> TextView): ReadWriteProperty 27 | = bindToTextIdP { viewProvider() } 28 | 29 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 30 | fun Fragment.bindToTextId(viewProvider: () -> TextView): ReadWriteProperty 31 | = bindToTextIdP { viewProvider() } 32 | 33 | fun android.support.v4.app.Fragment.bindToTextId(viewProvider: () -> TextView): ReadWriteProperty 34 | = bindToTextIdP { viewProvider() } 35 | 36 | fun TextView.bindToTextId(): ReadWriteProperty 37 | = bindToTextIdP { this } 38 | 39 | private fun bindToTextIdP(viewProvider: () -> TextView): ReadWriteProperty 40 | = TextIdBinding(lazy(viewProvider)) 41 | 42 | private class TextIdBinding(lazyViewProvider: Lazy) : ReadWriteProperty { 43 | 44 | val view by lazyViewProvider 45 | var textId: Int? = null 46 | 47 | override fun getValue(thisRef: Any?, property: KProperty<*>): Int? { 48 | return textId 49 | } 50 | 51 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: Int?) { 52 | value ?: return 53 | textId = value 54 | view.setText(value) 55 | } 56 | } -------------------------------------------------------------------------------- /kotlinandroidviewbindings/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/BindToVisibility.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.app.Activity 4 | import android.app.Fragment 5 | import android.os.Build 6 | import android.support.annotation.IdRes 7 | import android.support.annotation.RequiresApi 8 | import android.view.View 9 | import android.widget.FrameLayout 10 | import kotlin.properties.ReadWriteProperty 11 | import kotlin.reflect.KProperty 12 | 13 | fun Activity.bindToVisibility(@IdRes editTextId: Int): ReadWriteProperty 14 | = bindToVisibilityP { findViewById(editTextId) } 15 | 16 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 17 | fun Fragment.bindToVisibility(@IdRes editTextId: Int): ReadWriteProperty 18 | = bindToVisibilityP { view.findViewById(editTextId) } 19 | 20 | fun android.support.v4.app.Fragment.bindToVisibility(@IdRes editTextId: Int): ReadWriteProperty 21 | = bindToVisibilityP { view!!.findViewById(editTextId) } 22 | 23 | fun FrameLayout.bindToVisibility(@IdRes editTextId: Int): ReadWriteProperty 24 | = bindToVisibilityP { findViewById(editTextId) } 25 | 26 | fun Activity.bindToVisibility(viewProvider: () -> View): ReadWriteProperty 27 | = bindToVisibilityP { viewProvider() } 28 | 29 | @RequiresApi(Build.VERSION_CODES.HONEYCOMB) 30 | fun Fragment.bindToVisibility(viewProvider: () -> View): ReadWriteProperty 31 | = bindToVisibilityP { viewProvider() } 32 | 33 | fun android.support.v4.app.Fragment.bindToVisibility(viewProvider: () -> View): ReadWriteProperty 34 | = bindToVisibilityP { viewProvider() } 35 | 36 | fun View.bindToVisibility(): ReadWriteProperty 37 | = bindToVisibilityP { this } 38 | 39 | private fun bindToVisibilityP(viewProvider: () -> View): ReadWriteProperty 40 | = ViewVisibilityBinding(lazy(viewProvider)) 41 | 42 | private class ViewVisibilityBinding(viewProvider: Lazy) : ReadWriteProperty { 43 | 44 | val view by viewProvider 45 | 46 | override fun getValue(thisRef: Any?, property: KProperty<*>): Boolean { 47 | return view.visibility == View.VISIBLE 48 | } 49 | 50 | override fun setValue(thisRef: Any?, property: KProperty<*>, value: Boolean) { 51 | view.visibility = if(value) View.VISIBLE else View.GONE 52 | } 53 | } -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | 4 | android { 5 | compileSdkVersion 25 6 | buildToolsVersion "25.0.2" 7 | defaultConfig { 8 | applicationId "com.marcinmoskala.kotlinandroidviewbindings" 9 | minSdkVersion 16 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | } 16 | 17 | dependencies { 18 | compile project(':kotlinandroidviewbindings') 19 | compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 20 | compile 'io.reactivex:rxjava:1.1.6' 21 | compile 'io.reactivex:rxandroid:1.2.1' 22 | compile 'com.android.support:design:25.3.1' 23 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 24 | exclude group: 'com.android.support', module: 'support-annotations' 25 | }) 26 | testCompile 'junit:junit:4.12' 27 | } 28 | repositories { 29 | mavenCentral() 30 | } 31 | -------------------------------------------------------------------------------- /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 C:\Users\marci\AppData\Local\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 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/marcinmoskala/kotlinandroidviewbindings/BindToActionTests.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.content.Context 4 | import android.support.test.InstrumentationRegistry 5 | import android.support.test.runner.AndroidJUnit4 6 | import android.view.KeyEvent 7 | import android.view.inputmethod.EditorInfo 8 | import android.widget.Button 9 | import android.widget.EditText 10 | import org.junit.Test 11 | import org.junit.runner.RunWith 12 | import android.view.inputmethod.BaseInputConnection 13 | 14 | 15 | 16 | @RunWith(AndroidJUnit4::class) 17 | class BindToActionTests { 18 | 19 | val context: Context 20 | get() = InstrumentationRegistry.getTargetContext() 21 | 22 | @Test 23 | fun bindToEditorActionsInvokedWhenActionCalled() { 24 | val editText = EditText(context) 25 | var buttonClicked by editText.bindToEditorActions { actionId, eventCode -> actionId == EditorInfo.IME_ACTION_DONE || eventCode == KeyEvent.KEYCODE_ENTER } 26 | var invoked = false 27 | buttonClicked = { invoked = true } 28 | editText.onEditorAction(EditorInfo.IME_ACTION_DONE) 29 | assert(invoked) 30 | } 31 | 32 | @Test 33 | fun bindToEditorActionsInvokedWhenKeyClickedCalled() { 34 | val editText = EditText(context) 35 | var buttonClicked by editText.bindToEditorActions { actionId, eventCode -> actionId == EditorInfo.IME_ACTION_DONE || eventCode == KeyEvent.KEYCODE_ENTER } 36 | var invoked = false 37 | buttonClicked = { invoked = true } 38 | BaseInputConnection(editText, true).sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)) 39 | assert(invoked) 40 | } 41 | } -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/marcinmoskala/kotlinandroidviewbindings/BindToClickTests.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.content.Context 4 | import android.support.test.InstrumentationRegistry 5 | import android.support.test.runner.AndroidJUnit4 6 | import android.widget.Button 7 | import org.junit.Test 8 | import org.junit.runner.RunWith 9 | 10 | @RunWith(AndroidJUnit4::class) 11 | class BindToClickTests { 12 | 13 | val context: Context 14 | get() = InstrumentationRegistry.getTargetContext() 15 | 16 | @Test 17 | fun buttonBindToClickClickCalled() { 18 | val button = Button(context) 19 | var buttonClicked by button.bindToClick() 20 | var invoked = false 21 | buttonClicked = { invoked = true } 22 | button.callOnClick() 23 | assert(invoked) 24 | } 25 | @Test 26 | fun buttonBindToClickClickPerformed() { 27 | val button = Button(context) 28 | var buttonClicked by button.bindToClick() 29 | var invoked = false 30 | buttonClicked = { invoked = true } 31 | button.performClick() 32 | assert(invoked) 33 | } 34 | } -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/marcinmoskala/kotlinandroidviewbindings/BindToTextIdTests.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.content.Context 4 | import android.support.test.InstrumentationRegistry 5 | import android.support.test.runner.AndroidJUnit4 6 | import android.widget.Button 7 | import android.widget.EditText 8 | import android.widget.TextView 9 | import org.junit.Assert.assertEquals 10 | import org.junit.Test 11 | import org.junit.runner.RunWith 12 | 13 | @RunWith(AndroidJUnit4::class) 14 | class BindToTextIdTests { 15 | 16 | val context: Context get() = InstrumentationRegistry.getTargetContext() 17 | 18 | val text1id get() = android.R.string.copy 19 | val text1 get() = context.getText(android.R.string.copy) 20 | val text2id get() = android.R.string.cancel 21 | val text2 get() = context.getText(android.R.string.cancel) 22 | 23 | @Test 24 | fun buttonBindIdToText() { 25 | val button = Button(context) 26 | var buttonTextId by button.bindToTextId() 27 | buttonTextId = text1id 28 | assertEquals(text1id, buttonTextId) 29 | assertEquals(text1, button.text.toString()) 30 | buttonTextId = text2id 31 | assertEquals(text2id, buttonTextId) 32 | assertEquals(text2, button.text.toString()) 33 | } 34 | 35 | @Test 36 | fun textViewBindIdToText() { 37 | val textView = TextView(context) 38 | var textViewTextId by textView.bindToTextId() 39 | textViewTextId = text1id 40 | assertEquals(text1id, textViewTextId) 41 | assertEquals(text1, textView.text.toString()) 42 | textViewTextId = text2id 43 | assertEquals(text2id, textViewTextId) 44 | assertEquals(text2, textView.text.toString()) 45 | } 46 | 47 | @Test 48 | fun editTextBindIdToText() { 49 | val editText = EditText(context) 50 | var editTextTextId by editText.bindToTextId() 51 | editTextTextId = text1id 52 | assertEquals(text1id, editTextTextId) 53 | assertEquals(text1, editText.text.toString()) 54 | editTextTextId = text2id 55 | assertEquals(text2id, editTextTextId) 56 | assertEquals(text2, editText.text.toString()) 57 | } 58 | } -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/marcinmoskala/kotlinandroidviewbindings/BindToTextTests.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.content.Context 4 | import android.support.test.InstrumentationRegistry 5 | import android.support.test.runner.AndroidJUnit4 6 | import android.widget.Button 7 | import org.junit.Assert.assertEquals 8 | import org.junit.Test 9 | import org.junit.runner.RunWith 10 | 11 | @RunWith(AndroidJUnit4::class) 12 | class BindToTextTests { 13 | 14 | val context: Context 15 | get() = InstrumentationRegistry.getTargetContext() 16 | 17 | val text1 get() = "A" 18 | val text2 get() = "B" 19 | 20 | @Test 21 | fun buttonBindToText() { 22 | val button = Button(context) 23 | var buttonText by button.bindToText() 24 | buttonText = text1 25 | assertEquals(text1, buttonText) 26 | assertEquals(text1, button.text.toString()) 27 | buttonText = text2 28 | assertEquals(text2, buttonText) 29 | assertEquals(text2, button.text.toString()) 30 | } 31 | } -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /sample/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/Utills.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings 2 | 3 | import android.content.Context 4 | import android.support.annotation.IdRes 5 | import android.support.v7.app.AppCompatActivity 6 | import android.widget.EditText 7 | import android.widget.Toast 8 | import rx.Observable 9 | import rx.Subscription 10 | import rx.android.schedulers.AndroidSchedulers 11 | import rx.schedulers.Schedulers 12 | 13 | inline fun AppCompatActivity.bindView(viewId: Int) = lazy { findViewById(viewId) as T } 14 | 15 | fun Observable.applySchedulers(): Observable = 16 | subscribeOn(Schedulers.io()) 17 | .unsubscribeOn(Schedulers.io()) 18 | .observeOn(AndroidSchedulers.mainThread()) 19 | 20 | fun Observable.smartSubscribe( 21 | onStart: (() -> Unit)? = null, 22 | onError: ((Throwable) -> Unit)? = null, 23 | onFinish: (() -> Unit)? = null, 24 | onSuccess: (T) -> Unit = {}): Subscription = 25 | addStartFinishActions(onStart, onFinish) 26 | .subscribe(onSuccess, { onError?.invoke(it) }) 27 | 28 | fun Observable.addStartFinishActions(onStart: (() -> Unit)? = null, onFinish: (() -> Unit)? = null): Observable { 29 | onStart?.invoke() 30 | return doOnTerminate({ onFinish?.invoke() }) 31 | } 32 | 33 | fun Context.toast(text: String, length: Int = Toast.LENGTH_LONG) { 34 | Toast.makeText(this, text, length).show() 35 | } 36 | 37 | fun EditText.setErrorId(@IdRes errorId: Int?) { 38 | this.error = if (errorId == null) null else context.getString(errorId) 39 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/login/LoginActivity.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings.login 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.view.KeyEvent 6 | import android.view.inputmethod.EditorInfo 7 | import com.marcinmoskala.kotlinandroidviewbindings.* 8 | 9 | class LoginActivity : AppCompatActivity(), LoginView { 10 | 11 | override var progressVisible by bindToLoading(R.id.progressView, R.id.loginFormView) 12 | 13 | override var email by bindToText(R.id.emailView) 14 | override val emailRequestFocus by bindToRequestFocus(R.id.emailView) 15 | override var emailErrorId by bindToErrorId(R.id.emailView) 16 | override var onEmaiEnterPressed by bindToEditorActions(R.id.emailView) { actionId, eventCode -> 17 | actionId == EditorInfo.IME_ACTION_DONE || eventCode == KeyEvent.KEYCODE_ENTER 18 | } 19 | 20 | override var password by bindToText(R.id.passwordView) 21 | override val passwordRequestFocus by bindToRequestFocus(R.id.passwordView) 22 | override var passwordErrorId by bindToErrorId(R.id.passwordView) 23 | override var onPasswordEnterPressed by bindToEditorActions(R.id.passwordView) { actionId, eventCode -> 24 | actionId == EditorInfo.IME_ACTION_DONE || eventCode == KeyEvent.KEYCODE_ENTER 25 | } 26 | 27 | override var loginButtonClickedCallback by bindToClick(R.id.loginButton) 28 | 29 | val presenter by lazy { LoginPresenter(this) } 30 | 31 | override fun onCreate(savedInstanceState: Bundle?) { 32 | super.onCreate(savedInstanceState) 33 | setContentView(R.layout.activity_login) 34 | presenter.onCreate() 35 | } 36 | 37 | override fun onDestroy() { 38 | super.onDestroy() 39 | presenter.onDestroy() 40 | } 41 | 42 | override fun informAboutLoginSuccess(token: String) { 43 | toast("Login succeed. Token: $token") 44 | } 45 | 46 | override fun informAboutError(error: Throwable) { 47 | toast("Error: " + error.message) 48 | } 49 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/login/LoginPresenter.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings.login 2 | 3 | import com.marcinmoskala.kotlinandroidviewbindings.applySchedulers 4 | import com.marcinmoskala.kotlinandroidviewbindings.smartSubscribe 5 | import rx.Subscription 6 | 7 | class LoginPresenter(val view: LoginView) { 8 | 9 | val loginUseCase by lazy { LoginUseCase() } 10 | val validateLoginFieldsUseCase by lazy { ValidateLoginFieldsUseCase() } 11 | var subscriptions: List = emptyList() 12 | 13 | fun onCreate() { 14 | view.loginButtonClickedCallback = { attemptLogin() } 15 | view.onEmaiEnterPressed = { view.passwordRequestFocus } 16 | view.onPasswordEnterPressed = { attemptLogin() } 17 | } 18 | 19 | fun onDestroy() { 20 | subscriptions.forEach { it.unsubscribe() } 21 | } 22 | 23 | fun attemptLogin() { 24 | val (email, password) = view.email to view.password 25 | subscriptions += validateLoginFieldsUseCase.validateLogin(email, password) 26 | .smartSubscribe( 27 | onSuccess = { (emailErrorId, passwordErrorId) -> 28 | view.passwordErrorId = passwordErrorId 29 | view.emailErrorId = emailErrorId 30 | when { 31 | emailErrorId != null -> view.emailRequestFocus() 32 | passwordErrorId != null -> view.passwordRequestFocus() 33 | else -> sendLoginRequest(email, password) 34 | } 35 | }, 36 | onError = view::informAboutError 37 | ) 38 | } 39 | 40 | private fun sendLoginRequest(email: String, password: String) { 41 | loginUseCase.sendLoginRequest(email, password) 42 | .applySchedulers() 43 | .smartSubscribe( 44 | onStart = { view.progressVisible = true }, 45 | onSuccess = { (token) -> view.informAboutLoginSuccess(token) }, 46 | onError = view::informAboutError, 47 | onFinish = { view.progressVisible = false } 48 | ) 49 | } 50 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/login/LoginRepository.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings.login 2 | 3 | import rx.Observable 4 | 5 | interface LoginRepository { 6 | 7 | fun attemptLogin(email: String, pass: String): Observable 8 | 9 | class MockLoginRepository : LoginRepository { 10 | override fun attemptLogin(email: String, pass: String): Observable = when { 11 | email.endsWith(".pl") -> throw Error("Invalid Email") 12 | else -> Observable.just(LoginResponse("TokenToken")) 13 | } 14 | } 15 | 16 | companion object { 17 | fun lazyGet(): Lazy = lazy { MockLoginRepository() } 18 | } 19 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/login/LoginResponse.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings.login 2 | 3 | data class LoginResponse(val token: String) -------------------------------------------------------------------------------- /sample/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/login/LoginUseCase.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings.login 2 | 3 | class LoginUseCase { 4 | 5 | val loginRepository by LoginRepository.lazyGet() 6 | 7 | fun sendLoginRequest(email: String, password: String) = loginRepository 8 | .attemptLogin(email, password) 9 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/login/LoginView.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings.login 2 | 3 | interface LoginView { 4 | var progressVisible: Boolean 5 | var email: String 6 | val emailRequestFocus: ()->Unit 7 | var emailErrorId: Int? 8 | var onEmaiEnterPressed: ()->Unit 9 | var password: String 10 | val passwordRequestFocus: ()->Unit 11 | var passwordErrorId: Int? 12 | var onPasswordEnterPressed: ()->Unit 13 | var loginButtonClickedCallback: ()->Unit 14 | fun informAboutLoginSuccess(token: String) 15 | fun informAboutError(error: Throwable) 16 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/marcinmoskala/kotlinandroidviewbindings/login/ValidateLoginFieldsUseCase.kt: -------------------------------------------------------------------------------- 1 | package com.marcinmoskala.kotlinandroidviewbindings.login 2 | 3 | import com.marcinmoskala.kotlinandroidviewbindings.R 4 | import rx.Observable 5 | 6 | class ValidateLoginFieldsUseCase { 7 | 8 | fun validateLogin(email: String, password: String) = Observable.just(getLoginErrors(email, password)) 9 | 10 | private fun getLoginErrors(email: String, password: String) = LoginErrors(getEmailErrorId(email), getPasswordErrorId(password)) 11 | 12 | private fun getEmailErrorId(email: String) = when { 13 | email.isEmpty() -> R.string.error_field_required 14 | emailInvalid(email) -> R.string.error_invalid_email 15 | else -> null 16 | } 17 | 18 | private fun getPasswordErrorId(password: String) = when { 19 | password.isEmpty() -> R.string.error_field_required 20 | passwordInvalid(password) -> R.string.error_invalid_password 21 | else -> null 22 | } 23 | 24 | private fun emailInvalid(email: String): Boolean = !email.contains("@") 25 | 26 | private fun passwordInvalid(password: String): Boolean = password.length <= 4 27 | 28 | data class LoginErrors(val emailErrorId: Int? = null, val passwordErrorId: Int? = null) { 29 | val correct get() = emailErrorId == null && passwordErrorId == null 30 | } 31 | } -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_login.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 13 | 14 | 19 | 20 | 23 | 24 | 31 | 32 | 33 | 34 | 37 | 38 | 48 | 49 | 50 | 51 |