├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── sys1yagi │ │ └── rxandroid │ │ └── observables │ │ └── PagingObservableTest.java │ └── main │ ├── AndroidManifest.xml │ ├── groovy │ └── com │ │ └── sys1yagi │ │ └── rxandroid │ │ ├── Application.java │ │ ├── activities │ │ ├── FormValidation2Activity.groovy │ │ ├── FormValidationActivity.groovy │ │ ├── ListViewPagingActivity.groovy │ │ ├── MainActivity.groovy │ │ ├── RssParseActivity.groovy │ │ ├── SimpleNetworkAccessActivity.groovy │ │ └── ToolbarActivity.groovy │ │ ├── events │ │ └── OnEditorActionEvent.groovy │ │ ├── fragments │ │ └── MenuListFragment.groovy │ │ ├── models │ │ └── Item.groovy │ │ ├── observables │ │ ├── EditTextObservable.groovy │ │ ├── FormValidator.groovy │ │ ├── HttpRequestObservable.groovy │ │ └── OnSubscribeEditTextInput.groovy │ │ ├── retrofit │ │ ├── AndroidArsenalService.groovy │ │ └── RssConverter.groovy │ │ └── tools │ │ └── GlobalBus.groovy │ └── res │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ ├── layout │ ├── activity_form_validation.xml │ ├── activity_form_validation2.xml │ ├── activity_listview_paging.xml │ ├── activity_main.xml │ ├── activity_rss_parse.xml │ ├── activity_simple_network_access.xml │ ├── fragment_menulist.xml │ └── toolbar.xml │ ├── menu │ └── menu_main.xml │ ├── values-v21 │ └── styles.xml │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── art └── form_validation.png ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | .idea 8 | *.iml -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rxandroid-usecase 2 | ================= 3 | 4 | RxJavaとかRxAndroidよくわからないので、よくある要求に対してRxAndroidを使って実装するとどうなるかを試すリポジトリです。RxJavaの使い方として正しいコードが書かれているとは限らないのでご注意ください。 5 | 6 | I don't understand RxJava or RxAndroid yet. I'll implement typical case of Android, using RxAndroid. (+o+) 7 | 8 | 9 | ## environment 10 | 11 | Will change rapidly... 12 | 13 | - Groovy 2.4.0-beta-3 14 | - rxandroid 0.23.0 15 | - multidex 1.0.0 16 | - swissknife 1.1.4 17 | - greenbot/EventBus 2.4.0 18 | - retrofit 1.8.0 19 | 20 | 21 | ## Form Validation 22 | 23 | 24 | ### require 25 | 26 | emailとpasswordを入力するフォームがある。submitボタンを押した時、それぞれのフォームの空チェックを行い、エラーの場合送信を中止し、エラー表示を行う。 27 | 28 | ![](./art/form_validation.png) 29 | 30 | ### implement 31 | 32 | まずは空チェックをして結果を返すObservableを生成するクラスを作る。 33 | `notEmpty()`はsubscribeされた時に、指定したTextViewが`empty`だとfalse, `!empty`だとtrueを送出するObservable。 success, failureを引数に受け取っているが、これは個別のバリデーション結果に対してアクションを起こしたい場合に指定する。 34 | 35 | ```groovy 36 | @CompileStatic 37 | class FormValidator { 38 | 39 | def static Observable notEmpty(TextView textView, 40 | Action0 success, 41 | Action0 failure) { 42 | return rx.Observable.create({ Subscriber subscriber -> 43 | if (TextUtils.isEmpty(textView.getText())) { 44 | if (failure != null) { 45 | failure.call() 46 | } 47 | subscriber.onNext(false) 48 | } else { 49 | if (success != null) { 50 | success.call() 51 | } 52 | subscriber.onNext(true) 53 | } 54 | subscriber.onCompleted() 55 | } as Observable.OnSubscribe) 56 | } 57 | } 58 | ``` 59 | 60 | `FormValidator`を使ってvalidation対象のEditTextからObservableを作る。それをconcatでつなげる。concatして作ったObservableに`reduce()`と`filter()`をかける。`reduce()`はconcatでつなげたObservableの結果を&して、全てのvalidationが通ったかを表すBooleanを送出する。`filter()`は`reduce()`の結果をそのまま返す。この時falseだったら後続の処理が呼ばれない。 61 | RxAndroidの`ViewObservable.clicks()`を使ってクリックイベントをもらい、その中でをsubscribeする。subscribeする時に渡すAction1が呼び出される時は全てのvalidtionがtrueで通過している状態になる。 62 | 63 | ```groovy 64 | @InjectView(R.id.edit_email) 65 | EditText email; 66 | 67 | @InjectView(R.id.text_email_error) 68 | TextView emailError; 69 | 70 | @InjectView(R.id.edit_password) 71 | EditText password; 72 | 73 | @InjectView(R.id.text_password_error) 74 | TextView passwordError; 75 | 76 | @InjectView(R.id.buttom_submit) 77 | Button submit; 78 | 79 | @Override 80 | protected void onCreate(Bundle savedInstanceState) { 81 | super.onCreate(savedInstanceState); 82 | setContentView(R.layout.activity_form_validation); 83 | 84 | def validators = prepareValidation() 85 | .reduce({ a, b -> a && b }) 86 | .filter({ a -> a }) 87 | 88 | ViewObservable 89 | .clicks(submit) 90 | .subscribe( 91 | { event -> 92 | //クリックイベントがくる度に 93 | //validatorsをsubscribeする 94 | validators.subscribe({ _ -> 95 | submit() 96 | }) 97 | }) 98 | } 99 | 100 | def Observable prepareValidation() { 101 | 102 | //emailをvalidationするObservableを作る 103 | Observable emailEmptyValidator = FormValidator 104 | .notEmpty(email, 105 | hideError(emailError), 106 | showError(emailError, "*Enter your e-mail address.")) 107 | 108 | //passwordをvalidationするObservableを作る 109 | Observable passwordEmptyValidator = FormValidator 110 | .notEmpty(password, 111 | hideError(passwordError), 112 | showError(passwordError, "*Enter your password.")) 113 | 114 | //concatで2つのObservableをつなげて返す 115 | return Observable.concat(emailEmptyValidator, passwordEmptyValidator) 116 | } 117 | ``` 118 | 119 | 補助的なメソッド達。 120 | Support methods. 121 | 122 | 123 | ```groovy 124 | def static Action0 hideError(TextView errorView) { 125 | return { 126 | errorView.setVisibility(View.GONE) 127 | } as Action0 128 | } 129 | 130 | def static Action0 showError(TextView errorView, String message) { 131 | return { 132 | errorView.setText(message) 133 | errorView.setVisibility(View.VISIBLE) 134 | } as Action0 135 | } 136 | ``` 137 | 138 | バリデーションの種類はFormValidatorにメソッドを追加していけばよい。それらをconcatで繋げればいくらでもバリデーションの処理を追加できる。FormValidatorは画面の実装とは独立しているので使いまわせる。 139 | 140 | ## Form validation 2 141 | 142 | ### require 143 | 144 | emailとpasswordを入力するフォームがある。emailとpasswordがinvalidな間、submitをdisabledにする。 145 | validationは以下の通り 146 | 147 | __email__ 148 | 149 | - 空ではない 150 | 151 | __password__ 152 | 153 | - 空ではない 154 | 155 | ### implement 156 | 157 | FormValidatorに`watchNotEmpty()`を追加する。`ViewObservable.text()`を使って指定したTextView(EditText)の内容の変化を監視する。`map`してOnTextChangeEventのTextViewの中身を空チェックするObservableを返却する。 158 | 159 | ```groovy 160 | class FormValidator { 161 | //... 162 | 163 | def static Observable watchNotEmpty(TextView textView, boolean emitInitialValue) { 164 | return ViewObservable.text(textView, emitInitialValue). 165 | map({ OnTextChangeEvent event -> 166 | return !TextUtils.isEmpty(event.text) 167 | }) 168 | } 169 | } 170 | ``` 171 | 172 | `FormValidator.watchNotEmpty()`を使ってバリデーションを実行するObservableを作成する。`Observable.combineLatest()`を使って2つのObservableを合成する。合成した結果を使って`submit`(Button)のenable/disableをセットする。 173 | 174 | ```groovy 175 | @InjectView(R.id.edit_email) 176 | EditText email; 177 | 178 | @InjectView(R.id.edit_password) 179 | EditText password; 180 | 181 | @InjectView(R.id.buttom_submit) 182 | Button submit; 183 | 184 | @Override 185 | protected void onCreate(Bundle savedInstanceState) { 186 | super.onCreate(savedInstanceState); 187 | setContentView(R.layout.activity_form_validation2); 188 | 189 | def emailEmptyObservable = FormValidator.watchNotEmpty(email, true); 190 | def passwordEmptyObservable = FormValidator.watchNotEmpty(password, true); 191 | 192 | Observable.combineLatest(emailEmptyObservable, passwordEmptyObservable, 193 | { a, b -> 194 | return a && b 195 | } as Func2) 196 | .subscribe( 197 | { Boolean isValid -> 198 | submit.setEnabled(isValid) 199 | }) 200 | 201 | ViewObservable 202 | .clicks(submit) 203 | .subscribe( 204 | { _ -> 205 | submit() 206 | }) 207 | } 208 | ``` 209 | 210 | `Observable.concat()`ではなく`Observable.combineLatest()`を使っている。concatは渡したObservableがonComplete()を呼び出さないと次のObservableの結果を送出し始めない。combineLatestは指定したObservableの両方の値がonNext()で出揃ったらFunc2が実行される。 211 | 212 | 213 | ## Simple Network Access 214 | 215 | TODO 216 | 217 | ## Rss Parse 218 | 219 | TODO 220 | 221 | 222 | ## ListView paging 223 | 224 | TODO 225 | 226 | ## View Binding 227 | 228 | TODO 229 | 230 | ## RxAndroid in Fragment 231 | 232 | TODO 233 | 234 | 235 | 236 | ## LICENSE 237 | 238 | ``` 239 | Copyright 2014 Toshihiro Yagi. 240 | 241 | Licensed under the Apache License, Version 2.0 (the "License"); 242 | you may not use this file except in compliance with the License. 243 | You may obtain a copy of the License at 244 | 245 | http://www.apache.org/licenses/LICENSE-2.0 246 | 247 | Unless required by applicable law or agreed to in writing, software 248 | distributed under the License is distributed on an "AS IS" BASIS, 249 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 250 | See the License for the specific language governing permissions and 251 | limitations under the License. 252 | ``` 253 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | maven { url 'https://maven.fabric.io/repo' } 5 | } 6 | dependencies { 7 | classpath 'me.champeau.gradle:gradle-groovy-android-plugin:0.3.4' 8 | classpath 'org.robolectric:robolectric-gradle-plugin:0.14.1' 9 | } 10 | } 11 | 12 | apply plugin: 'com.android.application' 13 | apply plugin: 'robolectric' 14 | 15 | android { 16 | compileSdkVersion 21 17 | buildToolsVersion "21.1.1" 18 | 19 | defaultConfig { 20 | applicationId "com.sys1yagi.rxandroid" 21 | minSdkVersion 16 22 | targetSdkVersion 18 23 | versionCode 1 24 | versionName "1.0" 25 | multiDexEnabled = true 26 | } 27 | 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_7 30 | targetCompatibility JavaVersion.VERSION_1_7 31 | } 32 | buildTypes { 33 | release { 34 | minifyEnabled false 35 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 36 | } 37 | } 38 | dexOptions { 39 | incremental true 40 | javaMaxHeapSize "2g" 41 | } 42 | packagingOptions { 43 | exclude 'META-INF/NOTICE.txt' 44 | exclude 'META-INF/LICENSE.txt' 45 | exclude 'META-INF/groovy-release-info.properties' 46 | } 47 | compileOptions { 48 | sourceCompatibility JavaVersion.VERSION_1_7 49 | targetCompatibility JavaVersion.VERSION_1_7 50 | } 51 | } 52 | 53 | apply plugin: 'me.champeau.gradle.groovy-android' 54 | 55 | dependencies { 56 | compile fileTree(dir: 'libs', include: ['*.jar']) 57 | compile 'org.codehaus.groovy:groovy:2.4.0-beta-3:grooid' 58 | compile 'com.android.support:appcompat-v7:21.0.2' 59 | compile 'com.android.support:cardview-v7:21.0.2' 60 | compile 'com.android.support:support-v4:21.0.2' 61 | compile 'com.android.support:multidex:1.0.0' 62 | compile 'com.arasthel:swissknife:1.1.4' 63 | compile 'com.android.support:multidex:1.0.0' 64 | compile 'io.reactivex:rxandroid:0.23.0' 65 | compile 'de.greenrobot:eventbus:2.4.0' 66 | compile 'com.squareup.retrofit:retrofit:1.8.0' 67 | compile 'com.squareup.okhttp:okhttp:2.1.0' 68 | compile 'com.squareup.okhttp:okhttp-urlconnection:2.1.0' 69 | compile 'org.jsoup:jsoup:1.7.3' 70 | 71 | androidTestCompile 'junit:junit:4.11' 72 | 73 | androidTestCompile('org.robolectric:robolectric:2.3') { 74 | exclude module: 'classworlds' 75 | exclude module: 'maven-artifact' 76 | exclude module: 'maven-artifact-manager' 77 | exclude module: 'maven-error-diagnostics' 78 | exclude module: 'maven-model' 79 | exclude module: 'maven-plugin-registry' 80 | exclude module: 'maven-profile' 81 | exclude module: 'maven-project' 82 | exclude module: 'maven-settings' 83 | exclude module: 'nekohtml' 84 | exclude module: 'plexus-container-default' 85 | exclude module: 'plexus-interpolation' 86 | exclude module: 'plexus-utils' 87 | exclude module: 'wagon-file' 88 | exclude module: 'wagon-http-lightweight' 89 | exclude module: 'wagon-http-shared' 90 | exclude module: 'wagon-provider-api' 91 | } 92 | } 93 | 94 | project.androidGroovy { 95 | options { 96 | sourceCompatibility = '1.7' 97 | targetCompatibility = '1.7' 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /usr/local/opt/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/sys1yagi/rxandroid/observables/PagingObservableTest.java: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.observables; 2 | 3 | import org.hamcrest.CoreMatchers; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.robolectric.RobolectricTestRunner; 8 | 9 | 10 | @RunWith(RobolectricTestRunner.class) 11 | public class PagingObservableTest { 12 | 13 | @Test 14 | public void a() { 15 | //TODO 16 | Assert.assertThat("aa", CoreMatchers.is("aa")); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 25 | 28 | 29 | 32 | 33 | 36 | 37 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/Application.java: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid; 2 | 3 | import android.content.Context; 4 | import android.support.multidex.MultiDexApplication; 5 | 6 | public class Application extends MultiDexApplication { 7 | 8 | @Override 9 | protected void attachBaseContext(Context base) { 10 | try { 11 | super.attachBaseContext(base); 12 | } catch (Exception e) { 13 | String vmName = System.getProperty("java.vm.name"); 14 | if (!vmName.startsWith("Java")) { 15 | throw e; 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/activities/FormValidation2Activity.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.activities 2 | 3 | import android.content.Context 4 | import android.content.Intent 5 | import android.os.Bundle 6 | import android.widget.Button 7 | import android.widget.EditText 8 | import android.widget.Toast 9 | import com.arasthel.swissknife.annotations.InjectView 10 | import com.sys1yagi.rxandroid.R 11 | import com.sys1yagi.rxandroid.observables.FormValidator 12 | import groovy.transform.CompileStatic 13 | import rx.Observable 14 | import rx.android.observables.ViewObservable 15 | import rx.functions.Func2 16 | 17 | @CompileStatic 18 | public class FormValidation2Activity extends ToolbarActivity { 19 | 20 | def static Intent createIntent(Context context) { 21 | new Intent(context, FormValidation2Activity.class) 22 | } 23 | 24 | @InjectView(R.id.edit_email) 25 | EditText email; 26 | 27 | @InjectView(R.id.edit_password) 28 | EditText password; 29 | 30 | @InjectView(R.id.buttom_submit) 31 | Button submit; 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_form_validation2); 37 | 38 | def emailEmptyObservable = FormValidator.watchNotEmpty(email, true); 39 | def passwordEmptyObservable = FormValidator.watchNotEmpty(password, true); 40 | 41 | Observable.combineLatest(emailEmptyObservable, passwordEmptyObservable, 42 | { a, b -> 43 | return a && b 44 | } as Func2) 45 | .subscribe( 46 | { Boolean isValid -> 47 | submit.setEnabled(isValid) 48 | }) 49 | 50 | ViewObservable 51 | .clicks(submit) 52 | .subscribe( 53 | { _ -> 54 | submit() 55 | }) 56 | } 57 | 58 | def submit() { 59 | Toast.makeText(this, "submit!", Toast.LENGTH_SHORT).show() 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/activities/FormValidationActivity.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.activities 2 | 3 | import android.content.Context 4 | import android.content.Intent 5 | import android.os.Bundle 6 | import android.view.View 7 | import android.view.inputmethod.EditorInfo 8 | import android.widget.Button 9 | import android.widget.EditText 10 | import android.widget.TextView 11 | import android.widget.Toast 12 | import com.arasthel.swissknife.annotations.InjectView 13 | import com.sys1yagi.rxandroid.R 14 | import com.sys1yagi.rxandroid.events.OnEditorActionEvent 15 | import com.sys1yagi.rxandroid.observables.EditTextObservable 16 | import com.sys1yagi.rxandroid.observables.FormValidator 17 | import groovy.transform.CompileStatic 18 | import rx.Observable 19 | import rx.android.observables.ViewObservable 20 | import rx.functions.Action0 21 | 22 | @CompileStatic 23 | public class FormValidationActivity extends ToolbarActivity { 24 | 25 | def static Intent createIntent(Context context) { 26 | new Intent(context, FormValidationActivity.class) 27 | } 28 | 29 | @InjectView(R.id.edit_email) 30 | EditText email; 31 | 32 | @InjectView(R.id.text_email_error) 33 | TextView emailError; 34 | 35 | @InjectView(R.id.edit_password) 36 | EditText password; 37 | 38 | @InjectView(R.id.text_password_error) 39 | TextView passwordError; 40 | 41 | @InjectView(R.id.buttom_submit) 42 | Button submit; 43 | 44 | def static Action0 hideError(TextView errorView) { 45 | return { 46 | errorView.setVisibility(View.GONE) 47 | } as Action0 48 | } 49 | 50 | def static Action0 showError(TextView errorView, String message) { 51 | return { 52 | errorView.setText(message) 53 | errorView.setVisibility(View.VISIBLE) 54 | } as Action0 55 | } 56 | 57 | @Override 58 | protected void onCreate(Bundle savedInstanceState) { 59 | super.onCreate(savedInstanceState); 60 | setContentView(R.layout.activity_form_validation); 61 | 62 | prepareEditorAction() 63 | def validators = prepareValidation() 64 | .reduce({ a, b -> a && b }) 65 | .filter({ a -> a }) 66 | 67 | ViewObservable 68 | .clicks(submit) 69 | .subscribe( 70 | { event -> 71 | validators.subscribe({ _ -> 72 | submit() 73 | }) 74 | }) 75 | } 76 | 77 | def submit() { 78 | Toast.makeText(this, "submit!", Toast.LENGTH_SHORT).show() 79 | } 80 | 81 | def prepareEditorAction() { 82 | EditTextObservable.editorAction(password).subscribe({ OnEditorActionEvent event -> 83 | if (event.actionId == EditorInfo.IME_ACTION_DONE) { 84 | submit.performClick() 85 | } 86 | }) 87 | } 88 | 89 | def Observable prepareValidation() { 90 | Observable emailEmptyValidator = FormValidator 91 | .notEmpty(email, 92 | hideError(emailError), 93 | showError(emailError, "*Enter your e-mail address.")) 94 | 95 | Observable passwordEmptyValidator = FormValidator 96 | .notEmpty(password, 97 | hideError(passwordError), 98 | showError(passwordError, "*Enter your password address.")) 99 | 100 | return Observable.concat(emailEmptyValidator, passwordEmptyValidator) 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/activities/ListViewPagingActivity.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.activities 2 | 3 | import android.content.Context 4 | import android.content.Intent 5 | import android.os.Bundle 6 | import android.widget.ListView 7 | import com.arasthel.swissknife.annotations.InjectView 8 | import com.sys1yagi.rxandroid.R 9 | 10 | public class ListViewPagingActivity extends ToolbarActivity { 11 | 12 | public static Intent createIntent(Context context) { 13 | Intent intent = new Intent(context, ListViewPagingActivity.class) 14 | return intent 15 | } 16 | 17 | @InjectView(R.id.list_view) 18 | ListView listView 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState) 23 | setContentView(R.layout.activity_listview_paging) 24 | 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/activities/MainActivity.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.activities 2 | 3 | import android.os.Bundle 4 | import android.support.v4.app.Fragment 5 | import android.support.v7.app.ActionBarActivity 6 | import android.support.v7.widget.Toolbar 7 | import android.view.LayoutInflater 8 | import android.view.Menu 9 | import android.view.MenuItem 10 | import android.view.View 11 | import android.view.ViewGroup 12 | import com.arasthel.swissknife.SwissKnife 13 | import com.arasthel.swissknife.annotations.InjectView 14 | import com.sys1yagi.rxandroid.MenuListFragment 15 | import com.sys1yagi.rxandroid.R 16 | import groovy.transform.CompileStatic 17 | 18 | @CompileStatic 19 | public class MainActivity extends ActionBarActivity { 20 | 21 | @InjectView(R.id.toolbar) 22 | Toolbar toolbar 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState) 27 | setContentView(R.layout.activity_main) 28 | SwissKnife.inject(this) 29 | setSupportActionBar(toolbar) 30 | 31 | if (savedInstanceState == null) { 32 | getSupportFragmentManager().beginTransaction() 33 | .add(R.id.container, MenuListFragment.newInstance()) 34 | .commit() 35 | } 36 | } 37 | 38 | @Override 39 | public boolean onCreateOptionsMenu(Menu menu) { 40 | getMenuInflater().inflate(R.menu.menu_main, menu) 41 | return true 42 | } 43 | 44 | @Override 45 | public boolean onOptionsItemSelected(MenuItem item) { 46 | int id = item.getItemId() 47 | 48 | if (id == R.id.action_settings) { 49 | return true 50 | } 51 | 52 | return super.onOptionsItemSelected(item) 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/activities/RssParseActivity.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid 2 | 3 | import android.content.Context 4 | import android.content.Intent 5 | import android.net.Uri 6 | import android.os.Bundle 7 | import android.view.View 8 | import android.widget.ArrayAdapter 9 | import android.widget.ListView 10 | import android.widget.ProgressBar 11 | import android.widget.TextView 12 | import com.arasthel.swissknife.annotations.InjectView 13 | import com.sys1yagi.rxandroid.activities.ToolbarActivity 14 | import com.sys1yagi.rxandroid.models.Item 15 | import com.sys1yagi.rxandroid.retrofit.AndroidArsenalService 16 | import com.sys1yagi.rxandroid.retrofit.RssConverter 17 | import groovy.transform.CompileStatic 18 | import retrofit.RestAdapter 19 | import rx.android.events.OnItemClickEvent 20 | import rx.android.observables.AndroidObservable 21 | import rx.android.observables.ViewObservable 22 | import rx.android.schedulers.AndroidSchedulers 23 | import rx.schedulers.Schedulers 24 | 25 | @CompileStatic 26 | public class RssParseActivity extends ToolbarActivity { 27 | 28 | public static Intent createIntent(Context context) { 29 | Intent intent = new Intent(context, RssParseActivity.class) 30 | return intent 31 | } 32 | 33 | @InjectView(R.id.list_view) 34 | ListView listView 35 | 36 | @InjectView(R.id.progress_bar) 37 | ProgressBar progressBar 38 | 39 | @InjectView(R.id.error_text) 40 | TextView errorText 41 | 42 | @Override 43 | protected void onCreate(Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState) 45 | setContentView(R.layout.activity_rss_parse) 46 | 47 | ArrayAdapter adapter = new ArrayAdapter( 48 | this as Context, 49 | android.R.layout.simple_list_item_1, 50 | android.R.id.text1, 51 | ); 52 | listView.setAdapter(adapter) 53 | 54 | RestAdapter restAdapter = new RestAdapter.Builder() 55 | .setEndpoint("http://feeds.feedburner.com/") 56 | .setConverter(new RssConverter()) 57 | .build() 58 | 59 | AndroidArsenalService androidArsenalService = restAdapter. 60 | create(AndroidArsenalService.class); 61 | 62 | AndroidObservable.bindActivity(this, 63 | androidArsenalService.androidArsenal() 64 | .subscribeOn(Schedulers.newThread()) 65 | .observeOn(AndroidSchedulers.mainThread())) 66 | .subscribe( 67 | { List items -> 68 | progressBar.setVisibility(View.GONE) 69 | listView.setVisibility(View.VISIBLE) 70 | items.each({ Item item -> 71 | adapter.add(item.title) 72 | }) 73 | ViewObservable.itemClicks(listView).subscribe({ OnItemClickEvent event -> 74 | String url = items.get(event.position).url 75 | Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)) 76 | startActivity(intent) 77 | }) 78 | 79 | }, 80 | { Throwable error -> 81 | progressBar.setVisibility(View.GONE) 82 | errorText.setVisibility(View.VISIBLE) 83 | errorText.setText(error.getMessage()) 84 | }) 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/activities/SimpleNetworkAccessActivity.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.activities 2 | 3 | import android.content.Context 4 | import android.content.Intent 5 | import android.os.Bundle 6 | import android.view.View 7 | import android.widget.ProgressBar 8 | import android.widget.TextView 9 | import com.arasthel.swissknife.annotations.InjectView 10 | import com.sys1yagi.rxandroid.R 11 | import com.sys1yagi.rxandroid.observables.HttpRequestObservable 12 | import rx.android.observables.AndroidObservable 13 | import rx.android.schedulers.AndroidSchedulers 14 | import rx.schedulers.Schedulers 15 | 16 | public class SimpleNetworkAccessActivity extends ToolbarActivity { 17 | 18 | public static final String ARGS_URL = "url" 19 | 20 | public static Intent createIntent(Context context, String url) { 21 | Intent intent = new Intent(context, SimpleNetworkAccessActivity.class) 22 | 23 | intent.putExtra(ARGS_URL, url) 24 | 25 | return intent 26 | } 27 | 28 | @InjectView(R.id.text_content) 29 | TextView content 30 | 31 | @InjectView(R.id.progress_bar) 32 | ProgressBar progressBar 33 | 34 | @InjectView(R.id.error_text) 35 | TextView errorText 36 | 37 | String url 38 | 39 | @Override 40 | protected void onCreate(Bundle savedInstanceState) { 41 | super.onCreate(savedInstanceState); 42 | setContentView(R.layout.activity_simple_network_access); 43 | 44 | url = getIntent().getStringExtra(ARGS_URL) 45 | 46 | AndroidObservable.bindActivity(this, 47 | HttpRequestObservable.get(url) 48 | .subscribeOn(Schedulers.newThread()) 49 | .observeOn(AndroidSchedulers.mainThread())) 50 | .subscribe( 51 | { String html -> 52 | progressBar.setVisibility(View.GONE) 53 | content.setVisibility(View.VISIBLE) 54 | content.setText(html) 55 | }, 56 | { Throwable error -> 57 | progressBar.setVisibility(View.GONE) 58 | errorText.setVisibility(View.VISIBLE) 59 | errorText.setText(error.getMessage()) 60 | }) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/activities/ToolbarActivity.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.activities 2 | 3 | import android.support.v7.app.ActionBarActivity 4 | import android.support.v7.widget.Toolbar 5 | import com.arasthel.swissknife.SwissKnife 6 | import com.sys1yagi.rxandroid.R 7 | 8 | class ToolbarActivity extends ActionBarActivity { 9 | 10 | @Override 11 | public void setContentView(int layoutResID) { 12 | super.setContentView(layoutResID) 13 | SwissKnife.inject(this) 14 | Toolbar toolbar = findViewById(R.id.toolbar) 15 | if (toolbar != null) { 16 | setSupportActionBar(toolbar) 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/events/OnEditorActionEvent.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.events 2 | 3 | import android.view.KeyEvent 4 | import android.widget.TextView 5 | 6 | public class OnEditorActionEvent { 7 | 8 | TextView view 9 | 10 | int actionId 11 | 12 | KeyEvent event 13 | 14 | OnEditorActionEvent(TextView view, int actionId, KeyEvent event) { 15 | this.view = view 16 | this.actionId = actionId 17 | this.event = event 18 | } 19 | 20 | public static OnEditorActionEvent create(final TextView view, int actionId, 21 | KeyEvent event) { 22 | return new OnEditorActionEvent(view, actionId, event) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/fragments/MenuListFragment.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid 2 | 3 | import android.app.Activity 4 | import android.content.Intent 5 | import android.os.Bundle 6 | import android.support.annotation.Nullable 7 | import android.support.v4.app.Fragment 8 | import android.view.LayoutInflater 9 | import android.view.View 10 | import android.view.ViewGroup 11 | import android.widget.ArrayAdapter 12 | import android.widget.ListView 13 | import com.arasthel.swissknife.SwissKnife 14 | import com.arasthel.swissknife.annotations.InjectView 15 | import com.sys1yagi.rxandroid.activities.FormValidation2Activity 16 | import com.sys1yagi.rxandroid.activities.FormValidationActivity 17 | import com.sys1yagi.rxandroid.activities.ListViewPagingActivity 18 | import com.sys1yagi.rxandroid.activities.SimpleNetworkAccessActivity 19 | import groovy.transform.CompileStatic 20 | import rx.Observable 21 | import rx.android.events.OnItemClickEvent 22 | import rx.android.observables.ViewObservable 23 | import rx.android.schedulers.AndroidSchedulers 24 | 25 | @CompileStatic 26 | class MenuListFragment extends Fragment { 27 | 28 | def static MenuListFragment newInstance() { 29 | return new MenuListFragment() 30 | } 31 | 32 | @InjectView(R.id.list_view) 33 | ListView listView 34 | 35 | @Override 36 | View onCreateView(LayoutInflater inflater, 37 | @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 38 | View view = inflater.inflate(R.layout.fragment_menulist, null) 39 | SwissKnife.inject(this, view) 40 | 41 | ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), 42 | android.R.layout.simple_list_item_1, android.R.id.text1); 43 | listView.setAdapter(adapter) 44 | 45 | Observable.from( 46 | "Simple Network Access in Activity", 47 | "Rss Parse in Activity", 48 | "Form validation 1 in Activity", 49 | "Form validation 2 in Activity", 50 | "ListView paging", 51 | //"View Binding", 52 | //"xxx in Fragment" 53 | ) 54 | .subscribeOn(AndroidSchedulers.mainThread()) 55 | .observeOn(AndroidSchedulers.mainThread()) 56 | .subscribe({ item -> 57 | adapter.add(item) 58 | }); 59 | ViewObservable.itemClicks(listView).subscribe({ OnItemClickEvent event -> 60 | int position = event.position 61 | actions[position].call(getActivity()) 62 | }) 63 | 64 | return view 65 | } 66 | 67 | def Closure[] actions = [ 68 | { 69 | Activity activity -> 70 | Intent intent = SimpleNetworkAccessActivity.createIntent(activity, 71 | "https://android-arsenal.com/details/1/1170") 72 | activity.startActivity(intent) 73 | 74 | }, 75 | { 76 | Activity activity -> 77 | Intent intent = RssParseActivity.createIntent(activity) 78 | activity.startActivity(intent) 79 | 80 | }, 81 | { 82 | Activity activity -> 83 | Intent intent = FormValidationActivity.createIntent(activity) 84 | activity.startActivity(intent) 85 | }, 86 | { 87 | Activity activity -> 88 | Intent intent = FormValidation2Activity.createIntent(activity) 89 | activity.startActivity(intent) 90 | }, 91 | { 92 | Activity activity -> 93 | Intent intent = ListViewPagingActivity.createIntent(activity) 94 | activity.startActivity(intent) 95 | } 96 | ] 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/models/Item.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.models 2 | 3 | class Item { 4 | 5 | String title 6 | 7 | String url 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/observables/EditTextObservable.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.observables 2 | 3 | import android.widget.EditText 4 | import com.sys1yagi.rxandroid.events.OnEditorActionEvent 5 | import groovy.transform.CompileStatic 6 | import rx.Observable 7 | 8 | @CompileStatic 9 | class EditTextObservable { 10 | 11 | private EditTextObservable() { 12 | } 13 | 14 | public static Observable editorAction(EditText input) { 15 | return Observable.create(new OnSubscribeEditTextInput(input)); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/observables/FormValidator.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.observables 2 | 3 | import android.text.TextUtils 4 | import android.widget.TextView 5 | import groovy.transform.CompileStatic 6 | import rx.Observable 7 | import rx.Subscriber 8 | import rx.android.events.OnTextChangeEvent 9 | import rx.android.observables.ViewObservable 10 | import rx.functions.Action0 11 | 12 | @CompileStatic 13 | class FormValidator { 14 | 15 | def static Observable notEmpty(TextView textView) { 16 | return notEmpty(textView, null, null) 17 | } 18 | 19 | def static Observable notEmpty(TextView textView, 20 | Action0 success, 21 | Action0 failure) { 22 | return Observable.create({ Subscriber subscriber -> 23 | if (TextUtils.isEmpty(textView.getText())) { 24 | if (failure != null) { 25 | failure.call() 26 | } 27 | subscriber.onNext(false) 28 | } else { 29 | if (success != null) { 30 | success.call() 31 | } 32 | subscriber.onNext(true) 33 | } 34 | subscriber.onCompleted() 35 | } as Observable.OnSubscribe) 36 | } 37 | 38 | def static Observable watchNotEmpty(TextView textView, boolean emitInitialValue) { 39 | return ViewObservable.text(textView, emitInitialValue). 40 | map({ OnTextChangeEvent event -> 41 | return !TextUtils.isEmpty(event.text) 42 | }) 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/observables/HttpRequestObservable.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.observables 2 | 3 | import com.squareup.okhttp.OkHttpClient 4 | import com.squareup.okhttp.Request 5 | import com.squareup.okhttp.Response 6 | import rx.Observable 7 | import rx.Subscriber 8 | 9 | class HttpRequestObservable { 10 | 11 | static OkHttpClient CLIENT = new OkHttpClient(); 12 | 13 | def static Observable get(String url) { 14 | return rx.Observable.create({ Subscriber subscriber -> 15 | Request request = new Request.Builder() 16 | .url(url) 17 | .build(); 18 | Response response = CLIENT.newCall(request).execute(); 19 | if (response.successful) { 20 | subscriber.onNext(response.body().string()) 21 | } else { 22 | subscriber.onError(new Exception(response.message())) 23 | } 24 | subscriber.onCompleted() 25 | } as Observable.OnSubscribe) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/observables/OnSubscribeEditTextInput.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.observables 2 | 3 | import android.view.KeyEvent 4 | import android.widget.EditText 5 | import android.widget.TextView 6 | import com.sys1yagi.rxandroid.events.OnEditorActionEvent 7 | import groovy.transform.CompileStatic 8 | import rx.Subscriber 9 | import rx.Subscription 10 | import rx.android.subscriptions.AndroidSubscriptions 11 | 12 | @CompileStatic 13 | class OnSubscribeEditTextInput implements rx.Observable.OnSubscribe { 14 | 15 | EditText input 16 | 17 | public OnSubscribeEditTextInput(EditText input) { 18 | this.input = input; 19 | } 20 | 21 | @Override 22 | void call(Subscriber subscriber) { 23 | input.setOnEditorActionListener(new TextView.OnEditorActionListener() { 24 | 25 | @Override 26 | boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 27 | subscriber.onNext(OnEditorActionEvent.create(v, actionId, event)) 28 | return false 29 | } 30 | }) 31 | 32 | final Subscription subscription = AndroidSubscriptions.unsubscribeInUiThread({ 33 | input.setOnEditorActionListener(null) 34 | }); 35 | 36 | subscriber.add(subscription) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/retrofit/AndroidArsenalService.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.retrofit 2 | 3 | import com.sys1yagi.rxandroid.models.Item 4 | import retrofit.http.GET 5 | import rx.Observable 6 | 7 | interface AndroidArsenalService { 8 | 9 | @GET("/Android_Arsenal") 10 | Observable> androidArsenal(); 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/retrofit/RssConverter.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.retrofit 2 | 3 | import com.sys1yagi.rxandroid.models.Item 4 | import groovy.transform.CompileStatic 5 | import org.jsoup.Jsoup 6 | import org.jsoup.nodes.Document 7 | import org.jsoup.nodes.Element 8 | import org.jsoup.parser.Parser 9 | import retrofit.converter.ConversionException 10 | import retrofit.converter.Converter 11 | import retrofit.mime.TypedInput 12 | import retrofit.mime.TypedOutput 13 | 14 | import java.lang.reflect.Type 15 | 16 | @CompileStatic 17 | public class RssConverter implements Converter { 18 | 19 | @Override 20 | Object fromBody(TypedInput body, Type type) throws ConversionException { 21 | def items = [] 22 | Document document = Jsoup.parse(body.in(), "utf-8", "", Parser.xmlParser()) 23 | 24 | document.select("item").collect({ Element child -> 25 | Item item = new Item() 26 | item.title = child.select("title").first().text() 27 | item.url = child.select("link").first().text() 28 | items.add(item) 29 | }) 30 | return items 31 | } 32 | 33 | @Override 34 | TypedOutput toBody(Object object) { 35 | return null 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/groovy/com/sys1yagi/rxandroid/tools/GlobalBus.groovy: -------------------------------------------------------------------------------- 1 | package com.sys1yagi.rxandroid.tools 2 | 3 | import de.greenrobot.event.EventBus 4 | import groovy.transform.CompileStatic 5 | 6 | @CompileStatic 7 | class GlobalBus { 8 | static EventBus INSTANCE = new EventBus() 9 | 10 | public static register(o) { 11 | INSTANCE.register(o) 12 | } 13 | 14 | public static unregister(o) { 15 | INSTANCE.unregister(o) 16 | } 17 | 18 | public static post(o) { 19 | INSTANCE.post(o) 20 | } 21 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sys1yagi/rxandroid-usecase/6fbb2fc64cf18d18c9a0c77ef8737f6878d9c8cf/app/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sys1yagi/rxandroid-usecase/6fbb2fc64cf18d18c9a0c77ef8737f6878d9c8cf/app/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sys1yagi/rxandroid-usecase/6fbb2fc64cf18d18c9a0c77ef8737f6878d9c8cf/app/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sys1yagi/rxandroid-usecase/6fbb2fc64cf18d18c9a0c77ef8737f6878d9c8cf/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_form_validation.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 15 | 16 | 20 | 21 | 28 | 38 | 39 | 43 | 50 | 59 | 60 |