├── .circleci └── config.yml ├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── LICENSE ├── README.md ├── art ├── ascii_validator.gif ├── custom_validator.gif ├── email_validator.gif └── required_validator.gif ├── build.gradle ├── buildSrc ├── .gitignore ├── build.gradle.kts └── src │ └── main │ └── java │ └── dependencies │ ├── Depends.kt │ └── Versions.kt ├── example ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── co │ │ └── kyash │ │ └── vtl │ │ └── MainActivityTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── co │ │ │ └── kyash │ │ │ └── vtl │ │ │ └── example │ │ │ ├── App.kt │ │ │ ├── MainActivity.kt │ │ │ ├── api │ │ │ └── MaterialDesignColorsApi.kt │ │ │ └── validators │ │ │ └── MaterialDesignColorsValidator.kt │ └── res │ │ ├── drawable │ │ ├── btn_accent.xml │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.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 │ │ └── themes.xml │ └── test │ └── java │ └── co │ └── kyash │ └── vtl │ └── example │ ├── testing │ └── RxImmediateSchedulerRule.kt │ └── validators │ └── MaterialDesignColorsValidatorTest.kt ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── jitpack.yml ├── json └── colors.json ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── co │ │ │ └── kyash │ │ │ └── vtl │ │ │ ├── ValidatableTextInputLayout.kt │ │ │ ├── ValidatableView.kt │ │ │ ├── VtlValidationFailureException.kt │ │ │ └── validators │ │ │ ├── AlphabetOnlyValidator.kt │ │ │ ├── AsciiOnlyValidator.kt │ │ │ ├── EmailValidator.kt │ │ │ ├── HiraganaOnlyValidator.kt │ │ │ ├── KatakanaOnlyValidator.kt │ │ │ ├── MinLengthValidator.kt │ │ │ ├── NoSpecialCharacterValidator.kt │ │ │ ├── NumberOnlyValidator.kt │ │ │ ├── RequiredValidator.kt │ │ │ └── VtlValidator.kt │ └── res │ │ └── values │ │ ├── attrs.xml │ │ └── strings.xml │ └── test │ ├── java │ └── co │ │ └── kyash │ │ └── vtl │ │ ├── testing │ │ └── RxImmediateSchedulerRule.kt │ │ └── validators │ │ ├── AlphabetOnlyValidatorTest.kt │ │ ├── AsciiOnlyValidatorTest.kt │ │ ├── EmailValidatorTest.kt │ │ ├── HiraganaOnlyValidatorTest.kt │ │ ├── KatakanaOnlyValidatorTest.kt │ │ ├── MinLengthValidatorTest.kt │ │ ├── NoSpecialCharacterValidatorTest.kt │ │ ├── NumberOnlyValidatorTest.kt │ │ └── RequiredValidatorTest.kt │ └── resources │ └── robolectric.properties ├── settings.gradle └── versions.gradle /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | jobs: 3 | build: 4 | docker: 5 | - image: circleci/android:api-30 6 | 7 | working_directory: ~/repo 8 | 9 | environment: 10 | JVM_OPTS: -Xmx3200m 11 | GRADLE_OPTS: '-Dorg.gradle.parallel=false -Dorg.gradle.daemon=false' 12 | resource_class: large 13 | 14 | steps: 15 | - checkout 16 | - restore_cache: 17 | key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }} 18 | 19 | - run: 20 | name: Download Dependencies 21 | command: ./gradlew androidDependencies 22 | 23 | - save_cache: 24 | paths: 25 | - ~/.gradle 26 | key: jars-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }} 27 | 28 | - run: 29 | name: Run Tests 30 | command: ./gradlew testDebug 31 | 32 | - store_artifacts: 33 | path: app/build/outputs 34 | destination: outputs 35 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | # Overview 2 | - 3 | 4 | # Links 5 | - -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | close #ISSUE_NUMBER 2 | 3 | # Overview 4 | - 5 | 6 | # Links 7 | - 8 | 9 | # Screenshots 10 | 11 | Before | After 12 | :--: | :--: 13 | | 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .idea/* 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | 10 | # not ignore 11 | !.idea/codeStyleSettings.xml 12 | 13 | fabric.properties -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ValidatableTextInputLayout 2 | 3 | [![Build Status](https://circleci.com/gh/Kyash/validatable-textinput-layout.svg?style=shield)](https://circleci.com/gh/Kyash/validatable-textinput-layout/tree/master) 4 | [![JitPack](https://jitpack.io/v/Kyash/validatable-textinput-layout.svg)](https://jitpack.io/#Kyash/validatable-textinput-layout) 5 | 6 | ValidatableTextInputLayout is the view which extended TextInputLayout to validate the input text easily. 7 | 8 | ## Download 9 | 10 | ### Project build.gradle 11 | 12 | ```groovy 13 | allprojects { 14 | repositories { 15 | ... 16 | maven { url "https://jitpack.io" } 17 | } 18 | } 19 | ``` 20 | 21 | ### App build.gradle 22 | 23 | ```groovy 24 | dependencies { 25 | ... 26 | compile 'com.github.Kyash:validatable-textinput-layout:LATEST_VERSION' 27 | } 28 | ``` 29 | 30 | `LATEST_VERSION` is [![JitPack](https://jitpack.io/v/Kyash/validatable-textinput-layout.svg)](https://jitpack.io/#Kyash/validatable-textinput-layout) which supports AndroidX. 31 | If you still use Support Library, please use version `0.3.0`. 32 | 33 | ## Basic usage 34 | You can use as same as `TextInputLayout` in layout xml. 35 | 36 | ### Layout 37 | `trigger` attribute defines the timing of the text field validation. 38 | 39 | ```xml 40 | 45 | 46 | 51 | 52 | 53 | ``` 54 | 55 | ### Kotlin 56 | Register Validator class to `ValidatableTextInputLayout` 57 | For example, `RequiredValidator` shows error when the field is empty. 58 | 59 | ```kotlin 60 | private fun initValidator() { 61 | binding.firstName.register(RequiredValidator(getString(R.string.validation_error_required))) 62 | } 63 | ``` 64 | 65 | That's it. It works as below. 66 | 67 | ![required_validator.gif](art/required_validator.gif) 68 | 69 | ## Triggers 70 | There are 2 types of the validation trigger attributes. 71 | 72 | Attribute | Description 73 | :--: | :-- 74 | text_changed | Validate immediately when the text is changed. 75 | focus_changed | Validate when the focus is changed 76 | 77 | ## Validators 78 | This library provides some common validators 79 | 80 | Validator | Description 81 | :--: | :--: 82 | RequiredValidator | ![required_validator.gif](art/required_validator.gif) 83 | EmailValidator | ![email_validator.gif](art/email_validator.gif) 84 | NumberOnlyValidator | Number only 85 | AsciiOnlyValidator | ![ascii_validator.gif](art/ascii_validator.gif) 86 | AlphabetOnlyValidator | Alphabet character only 87 | HiraganaOnlyValidator | Jananese Hieragana character only 88 | KatakanaOnlyValidator | Japanese Katakana character only 89 | 90 | 91 | 92 | ## Custom validator 93 | You can create the custom validator by using `VtlValidator`. 94 | Since `VtlValidator` uses RxJava2, it can handle async logic like API as well! 95 | 96 | [MaterialDesignColorsValidator](https://github.com/Kyash/validatable-textinput-layout/blob/master/example/src/main/java/co/kyash/vtl/example/validators/MaterialDesignColorsValidator.kt) is example to get data via API and validate the input value. 97 | 98 | ```kotlin 99 | class MaterialDesignColorsValidator( 100 | private val api: MaterialDesignColorsApi, 101 | private val context: Context 102 | ) : VtlValidator { 103 | 104 | override fun validateAsCompletable(context: Context, text: String?): Completable { 105 | return api.all() 106 | .onErrorResumeNext { Single.error(VtlValidationFailureException(context.getString(R.string.validation_error_server))) } 107 | .flatMapCompletable { list -> 108 | if (text?.trim() != null) { 109 | list.filter { it == text.trim().toLowerCase() } 110 | .forEach { return@flatMapCompletable Completable.complete() } 111 | } 112 | return@flatMapCompletable Completable.error(VtlValidationFailureException(getErrorMessage())) 113 | } 114 | } 115 | 116 | override fun validate(text: String?): Boolean { 117 | throw UnsupportedOperationException("Sync method is not arrowed because this validation uses async API response.") 118 | } 119 | 120 | override fun getErrorMessage(): String { 121 | return context.getString(R.string.validation_error_colors) 122 | } 123 | } 124 | ``` 125 | 126 | ![custom_validator.gif](art/custom_validator.gif) 127 | 128 | ## Contributing 129 | We are always welcome your contribution! 130 | If you find a bug or want to add new feature, please raise issue. 131 | 132 | ## License 133 | 134 | ``` 135 | Copyright 2018 Kyash 136 | 137 | Licensed under the Apache License, Version 2.0 (the "License"); 138 | you may not use this file except in compliance with the License. 139 | You may obtain a copy of the License at 140 | 141 | http://www.apache.org/licenses/LICENSE-2.0 142 | 143 | Unless required by applicable law or agreed to in writing, software 144 | distributed under the License is distributed on an "AS IS" BASIS, 145 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 146 | See the License for the specific language governing permissions and 147 | limitations under the License. 148 | ``` 149 | -------------------------------------------------------------------------------- /art/ascii_validator.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kyash/validatable-textinput-layout/85bd0715df374ba1c0a7adf7505169fd4faa5556/art/ascii_validator.gif -------------------------------------------------------------------------------- /art/custom_validator.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kyash/validatable-textinput-layout/85bd0715df374ba1c0a7adf7505169fd4faa5556/art/custom_validator.gif -------------------------------------------------------------------------------- /art/email_validator.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kyash/validatable-textinput-layout/85bd0715df374ba1c0a7adf7505169fd4faa5556/art/email_validator.gif -------------------------------------------------------------------------------- /art/required_validator.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kyash/validatable-textinput-layout/85bd0715df374ba1c0a7adf7505169fd4faa5556/art/required_validator.gif -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | import dependencies.Depends 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | mavenCentral() 7 | gradlePluginPortal() 8 | maven { url "https://jitpack.io" } 9 | } 10 | dependencies { 11 | classpath Depends.GradlePlugin.android 12 | classpath Depends.GradlePlugin.kotlin 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | google() 19 | mavenCentral() 20 | maven { url "https://jitpack.io" } 21 | } 22 | } 23 | 24 | task clean(type: Delete) { 25 | delete rootProject.buildDir 26 | } 27 | -------------------------------------------------------------------------------- /buildSrc/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /buildSrc/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | `kotlin-dsl` 3 | } 4 | repositories { 5 | mavenCentral() 6 | } 7 | -------------------------------------------------------------------------------- /buildSrc/src/main/java/dependencies/Depends.kt: -------------------------------------------------------------------------------- 1 | package dependencies 2 | 3 | @Suppress("unused") 4 | object Depends { 5 | object GradlePlugin { 6 | const val android = "com.android.tools.build:gradle:7.0.0" 7 | const val kotlin = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Kotlin.version}" 8 | } 9 | 10 | object Test { 11 | const val junit = "junit:junit:4.13.2" 12 | const val testRunner = "androidx.test:runner:1.4.0" 13 | const val mockitoKotlin = "com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0" 14 | const val robolectric = "org.robolectric:robolectric:4.6.1" 15 | 16 | object Espresso { 17 | const val core = "androidx.test.espresso:espresso-core:3.1.0-alpha4" 18 | const val intents = "androidx.test.espresso:espresso-intents:3.1.0-alpha4" 19 | } 20 | } 21 | 22 | object AndroidX { 23 | const val appCompat = "androidx.appcompat:appcompat:1.0.0" 24 | const val recyclerView = "androidx.recyclerview:recyclerview:1.0.0" 25 | const val cardView = "androidx.cardview:cardview:1.0.0" 26 | const val design = "com.google.android.material:material:1.1.0-alpha01" 27 | } 28 | 29 | object Kotlin { 30 | const val version = "1.5.21" 31 | } 32 | 33 | object Stetho { 34 | const val version = "1.5.0" 35 | const val core = "com.facebook.stetho:stetho:$version" 36 | const val okhttp = "com.facebook.stetho:stetho-okhttp3:$version" 37 | } 38 | 39 | object Crashlytics { 40 | const val core = "com.crashlytics.sdk.android:crashlytics:2.8.0@aar" 41 | } 42 | 43 | object Retrofit { 44 | private const val version = "2.9.0" 45 | const val core = "com.squareup.retrofit2:retrofit:$version" 46 | const val converterMoshi = "com.squareup.retrofit2:converter-moshi:$version" 47 | const val adapterRxJava3 = "com.squareup.retrofit2:adapter-rxjava3:$version" 48 | } 49 | 50 | object Kotshi { 51 | private const val version = "1.0.6" 52 | const val api = "se.ansman.kotshi:api:$version" 53 | const val compiler = "se.ansman.kotshi:compiler:$version" 54 | } 55 | 56 | object Rx { 57 | const val RxJava = "io.reactivex.rxjava3:rxjava:3.0.13" 58 | const val RxAndroid = "io.reactivex.rxjava3:rxandroid:3.0.0" 59 | const val RxKotlin = "io.reactivex.rxjava3:rxkotlin:3.0.1" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /buildSrc/src/main/java/dependencies/Versions.kt: -------------------------------------------------------------------------------- 1 | package dependencies 2 | 3 | private object Versions { 4 | val androidCompileSdkVersion = 30 5 | val androidTargetSdkVersion = 30 6 | val androidMinSdkVersion = 19 7 | } 8 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /example/build.gradle: -------------------------------------------------------------------------------- 1 | import dependencies.Depends 2 | import dependencies.Versions 3 | 4 | apply plugin: 'com.android.application' 5 | apply plugin: 'kotlin-android' 6 | apply plugin: 'kotlin-kapt' 7 | apply from: "${rootDir.absolutePath}/versions.gradle" 8 | 9 | def versionMajor = 1 10 | def versionMinor = 0 11 | def versionPatch = 0 12 | 13 | android { 14 | compileSdkVersion Versions.androidCompileSdkVersion 15 | dataBinding.enabled = true 16 | 17 | defaultConfig { 18 | applicationId "co.kyash.vtl.sample" 19 | minSdkVersion Versions.androidMinSdkVersion 20 | targetSdkVersion Versions.androidTargetSdkVersion 21 | versionCode versionMajor * 10000 + versionMinor * 100 + versionPatch 22 | versionName "$versionMajor.$versionMinor.$versionPatch" 23 | } 24 | buildTypes { 25 | debug { 26 | applicationIdSuffix '.debug' 27 | versionNameSuffix "-debug" 28 | } 29 | release { 30 | debuggable false 31 | zipAlignEnabled true 32 | minifyEnabled false 33 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 34 | signingConfig signingConfigs.debug 35 | } 36 | } 37 | testOptions { 38 | unitTests.returnDefaultValues = true 39 | } 40 | lintOptions { 41 | lintConfig file('lint.xml') 42 | textReport true 43 | textOutput 'stdout' 44 | } 45 | } 46 | 47 | dependencies { 48 | implementation project(':library') 49 | 50 | //==================== Support Library ==================== 51 | implementation Depends.AndroidX.appCompat 52 | implementation Depends.AndroidX.design 53 | implementation Depends.AndroidX.cardView 54 | 55 | //==================== Network ==================== 56 | implementation Depends.Retrofit.core 57 | implementation Depends.Retrofit.converterMoshi 58 | implementation Depends.Retrofit.adapterRxJava3 59 | 60 | //==================== Structure ==================== 61 | implementation Depends.Kotshi.api 62 | kapt Depends.Kotshi.compiler 63 | 64 | implementation Depends.Rx.RxJava 65 | implementation Depends.Rx.RxAndroid 66 | implementation Depends.Rx.RxKotlin 67 | 68 | //==================== Debug ==================== 69 | implementation(Depends.Crashlytics.core) { 70 | transitive = true 71 | } 72 | 73 | //==================== Debug ==================== 74 | implementation Depends.Stetho.core 75 | implementation Depends.Stetho.okhttp 76 | 77 | //==================== Test ==================== 78 | testImplementation Depends.Test.junit 79 | testImplementation Depends.Test.mockitoKotlin 80 | testImplementation Depends.Test.robolectric 81 | androidTestImplementation Depends.Test.testRunner 82 | androidTestImplementation Depends.Test.Espresso.core 83 | androidTestImplementation Depends.Test.Espresso.intents 84 | } 85 | -------------------------------------------------------------------------------- /example/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /example/src/androidTest/java/co/kyash/vtl/MainActivityTest.kt: -------------------------------------------------------------------------------- 1 | package co.kyash.vtl 2 | 3 | import android.content.Intent 4 | import android.support.test.espresso.intent.Intents 5 | import android.support.test.espresso.intent.matcher.IntentMatchers 6 | import android.support.test.espresso.intent.rule.IntentsTestRule 7 | import android.support.test.filters.LargeTest 8 | import co.kyash.vtl.example.MainActivity 9 | import org.junit.Rule 10 | import org.junit.Test 11 | 12 | @LargeTest 13 | class MainActivityTest { 14 | 15 | @get:Rule 16 | private val activityTestRule = IntentsTestRule(MainActivity::class.java, true, false) 17 | 18 | @Test 19 | fun launch() { 20 | // when 21 | activityTestRule.launchActivity(Intent()) 22 | 23 | // then 24 | Intents.intended(IntentMatchers.hasComponent(MainActivity::class.java.name)) 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /example/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /example/src/main/java/co/kyash/vtl/example/App.kt: -------------------------------------------------------------------------------- 1 | package co.kyash.vtl.example 2 | 3 | import android.app.Application 4 | import com.facebook.stetho.Stetho 5 | 6 | class App : Application() { 7 | 8 | override fun onCreate() { 9 | super.onCreate() 10 | setUpStetho() 11 | } 12 | 13 | private fun setUpStetho() { 14 | Stetho.initializeWithDefaults(this) 15 | } 16 | } -------------------------------------------------------------------------------- /example/src/main/java/co/kyash/vtl/example/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package co.kyash.vtl.example 2 | 3 | import android.os.Bundle 4 | import android.util.Log 5 | import android.view.View 6 | import android.widget.Toast 7 | import androidx.appcompat.app.AppCompatActivity 8 | import androidx.databinding.DataBindingUtil 9 | import co.kyash.vtl.ValidatableView 10 | import co.kyash.vtl.example.api.MaterialDesignColorsApi 11 | import co.kyash.vtl.example.databinding.ActivityMainBinding 12 | import co.kyash.vtl.example.validators.MaterialDesignColorsValidator 13 | import co.kyash.vtl.validators.AsciiOnlyValidator 14 | import co.kyash.vtl.validators.EmailValidator 15 | import co.kyash.vtl.validators.NumberOnlyValidator 16 | import co.kyash.vtl.validators.RequiredValidator 17 | import com.crashlytics.android.Crashlytics 18 | import com.facebook.stetho.okhttp3.StethoInterceptor 19 | import com.squareup.moshi.Moshi 20 | import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers 21 | import io.reactivex.rxjava3.core.Completable 22 | import io.reactivex.rxjava3.core.Flowable 23 | import io.reactivex.rxjava3.disposables.CompositeDisposable 24 | import io.reactivex.rxjava3.schedulers.Schedulers 25 | import okhttp3.OkHttpClient 26 | import retrofit2.Retrofit 27 | import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory 28 | import retrofit2.converter.moshi.MoshiConverterFactory 29 | 30 | 31 | class MainActivity : AppCompatActivity() { 32 | 33 | private lateinit var binding: ActivityMainBinding 34 | 35 | private val validatableViewsForTriggerTextChanged: ArrayList = ArrayList() 36 | 37 | private val validatableViewsForTriggerFocusChanged: ArrayList = ArrayList() 38 | 39 | private val validatableViewsForButtonEnable: ArrayList = ArrayList() 40 | 41 | private val compositeDisposable = CompositeDisposable() 42 | 43 | private val api = Retrofit.Builder() 44 | .baseUrl("https://raw.githubusercontent.com") 45 | .addConverterFactory(MoshiConverterFactory.create(Moshi.Builder().build())) 46 | .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) 47 | .client(OkHttpClient.Builder().addNetworkInterceptor(StethoInterceptor()).build()) 48 | .build() 49 | .create(MaterialDesignColorsApi::class.java) 50 | 51 | override fun onCreate(savedInstanceState: Bundle?) { 52 | super.onCreate(savedInstanceState) 53 | 54 | binding = DataBindingUtil.setContentView(this, R.layout.activity_main) 55 | 56 | initValidators() 57 | 58 | binding.submit.setOnClickListener(this::onSubmitClick) 59 | binding.submit2.setOnClickListener(this::onSubmit2Click) 60 | binding.submit3.setOnClickListener(this::onSubmit3Click) 61 | } 62 | 63 | private fun initValidators() { 64 | // Example 1 65 | validatableViewsForTriggerTextChanged.addAll(arrayOf( 66 | binding.firstName.register(RequiredValidator(getString(R.string.validation_error_required))), 67 | binding.lastName.register(RequiredValidator(getString(R.string.validation_error_required))), 68 | binding.email.register(EmailValidator(getString(R.string.validation_error_email))), 69 | binding.numberOnly.register(NumberOnlyValidator(getString(R.string.validation_error_number_only))), 70 | binding.asciiOnly.register(AsciiOnlyValidator(getString(R.string.validation_error_ascii_only))) 71 | )) 72 | 73 | // Example 2 74 | validatableViewsForTriggerFocusChanged.addAll(arrayOf( 75 | binding.email2.register(EmailValidator(getString(R.string.validation_error_email))) 76 | )) 77 | 78 | // Example 3 79 | binding.colors.register(MaterialDesignColorsValidator(api, this)) 80 | 81 | // Example 4 82 | validatableViewsForButtonEnable.addAll(arrayOf( 83 | binding.firstName2.register(RequiredValidator(getString(R.string.validation_error_required))), 84 | binding.lastName2.register(RequiredValidator(getString(R.string.validation_error_required))) 85 | )) 86 | val validations: List> = validatableViewsForButtonEnable.flatMap { it.validationFlowables } 87 | Flowable.zip(validations) { Any() } 88 | .subscribeOn(Schedulers.computation()) 89 | .observeOn(AndroidSchedulers.mainThread()) 90 | .doOnError({ binding.submit3.isEnabled = false }) 91 | .retry() // non-terminated stream 92 | .subscribe({ binding.submit3.isEnabled = true }, { }) 93 | } 94 | 95 | 96 | private fun onSubmitClick(@Suppress("UNUSED_PARAMETER") view: View) { 97 | val validations: List = validatableViewsForTriggerTextChanged.map { it.validateAsCompletable() } 98 | validate(validations) 99 | } 100 | 101 | private fun onSubmit2Click(@Suppress("UNUSED_PARAMETER") view: View) { 102 | val validations: List = validatableViewsForTriggerFocusChanged.map { it.validateAsCompletable() } 103 | validate(validations) 104 | } 105 | 106 | private fun onSubmit3Click(@Suppress("UNUSED_PARAMETER") view: View) { 107 | Toast.makeText(this, R.string.validation_success, Toast.LENGTH_SHORT).show() 108 | } 109 | 110 | private fun validate(validations: List) { 111 | compositeDisposable.clear() 112 | 113 | compositeDisposable.add( 114 | Completable.mergeDelayError(validations) 115 | .subscribeOn(Schedulers.computation()) 116 | .observeOn(AndroidSchedulers.mainThread()) 117 | .subscribe({ 118 | Log.d("MainActivity", "Validation cleared.") 119 | Toast.makeText(this, R.string.validation_success, Toast.LENGTH_SHORT).show() 120 | }, { throwable -> 121 | Log.e("MainActivity", "Validation error occurred.", throwable) 122 | Toast.makeText(this, R.string.validation_error_occurred, Toast.LENGTH_SHORT).show() 123 | }) 124 | ) 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /example/src/main/java/co/kyash/vtl/example/api/MaterialDesignColorsApi.kt: -------------------------------------------------------------------------------- 1 | package co.kyash.vtl.example.api 2 | 3 | import io.reactivex.rxjava3.core.Single 4 | import retrofit2.http.GET 5 | 6 | interface MaterialDesignColorsApi { 7 | 8 | @GET("/Kyash/validatable-textinput-layout/master/json/colors.json") 9 | fun all(): Single> 10 | 11 | } 12 | -------------------------------------------------------------------------------- /example/src/main/java/co/kyash/vtl/example/validators/MaterialDesignColorsValidator.kt: -------------------------------------------------------------------------------- 1 | package co.kyash.vtl.example.validators 2 | 3 | import android.content.Context 4 | import co.kyash.vtl.VtlValidationFailureException 5 | import co.kyash.vtl.example.R 6 | import co.kyash.vtl.example.api.MaterialDesignColorsApi 7 | import co.kyash.vtl.validators.VtlValidator 8 | import io.reactivex.rxjava3.core.Completable 9 | import io.reactivex.rxjava3.core.Single 10 | 11 | class MaterialDesignColorsValidator( 12 | private val api: MaterialDesignColorsApi, 13 | private val context: Context 14 | ) : VtlValidator { 15 | 16 | override fun validateAsCompletable(context: Context, text: String?): Completable { 17 | return api.all() 18 | .onErrorResumeNext { Single.error(VtlValidationFailureException(context.getString(R.string.validation_error_server))) } 19 | .flatMapCompletable { list -> 20 | if (text?.trim() != null) { 21 | list.filter { it == text.trim().toLowerCase() } 22 | .forEach { return@flatMapCompletable Completable.complete() } 23 | } 24 | return@flatMapCompletable Completable.error(VtlValidationFailureException(getErrorMessage())) 25 | } 26 | } 27 | 28 | override fun validate(text: String?): Boolean { 29 | throw UnsupportedOperationException("sync method is not arrowed because this validation uses async API response.") 30 | } 31 | 32 | override fun getErrorMessage(): String { 33 | return context.getString(R.string.validation_error_colors) 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /example/src/main/res/drawable/btn_accent.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /example/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /example/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | 23 | 26 | 27 | 32 | 33 | 36 | 37 | 41 | 42 | 52 | 53 | 54 | 58 | 59 | 63 | 64 | 65 | 66 | 67 | 71 | 72 | 76 | 77 | 78 | 79 | 80 | 85 | 86 | 90 | 91 | 92 | 93 | 94 | 98 | 99 | 103 | 104 | 105 | 106 | 107 | 111 | 112 | 116 | 117 | 118 | 119 |