├── .gitignore ├── .idea ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── RxOperators ├── .gitignore ├── README.md ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── freeankit │ │ └── rxkotlinoperators │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── freeankit │ │ │ └── rxkotlinoperators │ │ │ ├── ApplicationClass.kt │ │ │ ├── model │ │ │ ├── ApiUser.kt │ │ │ ├── Bike.kt │ │ │ ├── DateModel.java │ │ │ └── User.kt │ │ │ ├── ui │ │ │ ├── OperatorsActivity.kt │ │ │ ├── RxBinding │ │ │ │ └── RxLoginScreenActivity.kt │ │ │ ├── RxOperators │ │ │ │ ├── AsyncSubjectOperatorActivity.kt │ │ │ │ ├── BehaviorSubjectActivity.kt │ │ │ │ ├── CompletableObserverOperatorActivity.kt │ │ │ │ ├── DisposableOperatorActivity.kt │ │ │ │ ├── FlowableOperatorActivity.kt │ │ │ │ ├── RepeatWhenOperatorActivity.kt │ │ │ │ ├── SimpleOperatorActivity.kt │ │ │ │ ├── SingleObserverOperatorActivity.kt │ │ │ │ ├── ThrottleFirstOperatorActivity.kt │ │ │ │ ├── ThrottleLastOperatorActivity.kt │ │ │ │ ├── TimerOperatorActivity.kt │ │ │ │ ├── combiningOperators │ │ │ │ │ ├── CombineLatestOperatorActivity.kt │ │ │ │ │ ├── MergeOperatorActivity.kt │ │ │ │ │ └── ZipOperatorActivity.kt │ │ │ │ ├── conditionalOperators │ │ │ │ │ └── AmbOperatorActivity.kt │ │ │ │ ├── connectableOperators │ │ │ │ │ ├── PublishSubjectOperatorActivity.kt │ │ │ │ │ ├── ReplayOperatorActivity.kt │ │ │ │ │ └── ReplaySubjectOperatorActivity.kt │ │ │ │ ├── creatingOperators │ │ │ │ │ ├── DeferOperatorActivity.kt │ │ │ │ │ └── IntervalOperatorActivity.kt │ │ │ │ ├── errorHandlingOperators │ │ │ │ │ └── RetryOperatorActivity.kt │ │ │ │ ├── filteringOperators │ │ │ │ │ ├── DebounceOperatorActivity.kt │ │ │ │ │ ├── DistinctOperatorActivity.kt │ │ │ │ │ ├── FilterOperatorActivity.kt │ │ │ │ │ ├── SkipOperatorActivity.kt │ │ │ │ │ └── TakeOperatorActivity.kt │ │ │ │ ├── mathematicalOperators │ │ │ │ │ ├── ConcatOperatorActivity.kt │ │ │ │ │ └── ReduceOperatorActivity.kt │ │ │ │ ├── transformingOperators │ │ │ │ │ ├── BufferOperatorActivity.kt │ │ │ │ │ ├── FlatMapOperatorActivity.kt │ │ │ │ │ ├── MapOperatorActivity.kt │ │ │ │ │ ├── ScanOperatorActivity.kt │ │ │ │ │ └── WindowOperatorActivity.kt │ │ │ │ └── utilityOperators │ │ │ │ │ └── DelayOperatorActivity.kt │ │ │ ├── RxPagination │ │ │ │ ├── PaginationActivity.kt │ │ │ │ └── PaginationAdapter.kt │ │ │ └── SelectionActivity.kt │ │ │ └── utils │ │ │ ├── Constant.kt │ │ │ └── Utils.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_example_operator.xml │ │ ├── activity_main.xml │ │ ├── activity_operators.xml │ │ ├── activity_rx_login_screen.xml │ │ ├── activity_selection.xml │ │ └── layout_adapter_item.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── freeankit │ └── rxkotlinoperators │ └── ExampleUnitTest.kt ├── _config.yml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .DS_Store 5 | /build 6 | /captures 7 | .externalNativeBuild 8 | /.idea/ 9 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RxKotlinOperators 2 | 3 | Learn RxKotlin with simple coding examples 4 | 5 | 6 | ![GitHub stars](https://img.shields.io/github/stars/badges/shields.svg?style=social&label=Stars) | 7 | [![GitHub followers](https://img.shields.io/github/followers/espadrine.svg?style=social&label=Follow)](https://github.com/AnkitDroidGit) 8 | 9 | 10 | 11 | # Migration from RxKotlin 1.0 to RxKotlin 2.0 12 | 13 | To allow having RxKotlin 1 and RxKotlin 2 side-by-side, RxKotlin 2 is under the maven coordinates io.reactivex.rxjava2:rxjava:2.x.y and classes are accessible below io.reactivex. 14 | Users switching from 1.x to 2.x have to re-organize their imports, but carefully. 15 | 16 | ## Using RxKotlin 2.0 Library in your application 17 | 18 | Add this in your build.gradle 19 | 20 | `compile 'io.reactivex.rxjava2:rxjava:2.1.1'` 21 | 22 | If you are using RxAndroid also, then add the following 23 | 24 | `compile 'io.reactivex.rxjava2:rxandroid:2.0.1'` 25 | 26 | # Quick Look on few changes done in RxKotlin2 over RxKotlin1 27 | 28 | ### RxJava1/RxKotlin1 -> RxJava2/RxKotlin2 29 | 30 | * `onCompleted` -> `onComplete` - without the trailing d 31 | * `Func1` -> `Function` 32 | * `Func2` -> `BiFunction` 33 | * `CompositeSubscription` -> `CompositeDisposable` 34 | * `limit` operator has been removed - Use `take` in RxKotlin2 35 | 36 | ....... and so on 37 | 38 | # RxOperators : 39 | 40 | * `map()` -> Transform the items emitted by an Observable by applying a function to each item 41 | * `zip()` -> Combine the emissions of multiple Observables together via a specified function and emit single items for each combination based on the results of this function 42 | * `take()` -> Emit only the first n items emitted by an Observable 43 | * `timer()` -> Create an Observable that emits a particular item after a given delay 44 | * `flatMap()` -> Transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable 45 | * `interval()` -> Create an Observable that emits a sequence of integers spaced by a given time interval 46 | * `debounce()` -> Only emit an item from an Observable if a particular time span has passed without it emitting another item 47 | * `Single Observer` -> A Single is something like an Observable, but instead of emitting a series of values — anywhere from none at all to an infinite number — it always either emits one value or an error notification. 48 | * `reduce()` -> Apply a function to each item emitted by an Observable, sequentially, and emit the final value 49 | * `buffer()` -> Periodically gather items emitted by an Observable into bundles and emit these bundles rather than emitting the items one at a time 50 | * `filter()` -> Emit only those items from an Observable that pass a predicate test 51 | * `skip()` -> Suppress the first n items emitted by an Observable 52 | * `scan()` -> Apply a function to each item emitted by an Observable, sequentially, and emit each successive value 53 | * `replay()` -> Ensure that all observers see the same sequence of emitted items, even if they subscribe after the Observable has begun emitting items 54 | * `concat()` -> Emit the emissions from two or more Observables without interleaving them 55 | * `merge()` -> Combine multiple Observables into one by merging their emissions 56 | * `defer()` -> The Defer operator waits until an observer subscribes to it, and then it generates an Observable, typically with an Observable factory function. It does this afresh for each subscriber, so although each subscriber may think it is subscribing to the same Observable, in fact each subscriber gets its own individual sequence. 57 | * `distinct()` -> Suppress duplicate items emitted by an Observable 58 | * `Replay Subject` -> Replays events (in a configurable bounded or unbounded manner) to current and late Observers. 59 | * `Publish Subject` -> Subject that, once an Observer has subscribed, emits all subsequently observed items to the subscriber 60 | * `Behaviour Subject` -> Subject that emits the most recent item it has observed and all subsequent observed items to each subscribed Observer. 61 | * `Aync Subject` -> A Subject that emits the very last value followed by a completion event or the received error to Observers. 62 | * `Throttle First` -> Emit the first items emitted by an Observable within periodic time intervals 63 | * `amb()` -> Given two or more source Observables, emits all of the items from the first of these Observables to emit an item 64 | * `Catch` -> Recover from an onError notification by continuing the sequence without error 65 | * `Retry` -> If a source Observable emits an error, resubscribe to it in the hopes that it will complete without error 66 | 67 | * Coming More 68 | ## TODO 69 | 70 | Adding more operator examples 71 | 72 | ## Highlights : 73 | * [SimpleOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/SimpleOperatorActivity.kt) - Using `Simple` operator 74 | * [MapOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/transformingOperators/MapOperatorActivity.kt) - Using `Map` operator 75 | * [ZipOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/combiningOperators/ZipOperatorActivity.kt) - Using `Zip` observer 76 | * [DisposableOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/DisposableOperatorActivity.kt) - Using `Disposable` operator 77 | * [FlatMapOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/transformingOperators/FlatMapOperatorActivity.kt) - Using `FlatMap` Operator 78 | * [IntervalOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/creatingOperators/IntervalOperatorActivity.kt) - Using `Interval` Operator 79 | * [TakeOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/filteringOperators/TakeOperatorActivity.kt) - Using `Take` Operator 80 | * [TimerOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/TimerOperatorActivity.kt) - Using `Timer` Operator 81 | * [DebounceOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/filteringOperators/DebounceOperatorActivity.kt) - Using `Debounce` Operator 82 | * [SingleObserverOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/SingleObserverOperatorActivity.kt) - Using `Single Observer` Operator 83 | * [FlowableOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/FlowableOperatorActivity.kt) - Using `Flowable` 84 | * [ReduceOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/mathematicalOperators/ReduceOperatorActivity.kt) -Using `Reduce` Operator 85 | * [BufferOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/transformingOperators/BufferOperatorActivity.kt) -Using `Buffer` Operator 86 | * [FilterOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/filteringOperators/FilterOperatorActivity.kt) -Using `Filter` Operator 87 | * [SkipOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/filteringOperators/SkipOperatorActivity.kt) -Using `Skip` Operator 88 | * [ScanOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/transformingOperators/ScanOperatorActivity.kt) -Using `Scan` Operator 89 | * [ReplayOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/connectableOperators/ReplayOperatorActivity.kt) -Using `Replay` Operator 90 | * [ConcatOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/mathematicalOperators/ConcatOperatorActivity.kt) -Using `Concat` Operator 91 | * [MergeOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/combiningOperators/MergeOperatorActivity.kt) -Using `Merge` Operator 92 | * [DeferOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/creatingOperators/DeferOperatorActivity.kt) -Using `Defer` Operator 93 | * [DistinctOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/filteringOperators/DistinctOperatorActivity.kt) -Using `Distinct` Operator 94 | * [ReplaySubjectOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/connectableOperators/ReplaySubjectOperatorActivity.kt) -Using `Replay Subject` Operator 95 | * [PublishSubjectOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/connectableOperators/PublishSubjectOperatorActivity.kt) -Using `Publish Subject` Operator 96 | * [BehaviorSubjectActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/BehaviorSubjectActivity.kt) -Using `Behavior Subject` Operator 97 | * [AsyncSubjectOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/AsyncSubjectOperatorActivity.kt) -Using `Aync Subject` Operator 98 | * [ThrottleFirstOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/ThrottleFirstOperatorActivity.kt) -Using `Throttle First` Operator 99 | * [ThrottleLastOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/ThrottleLastOperatorActivity.kt) -Using `Throttle Last` Operator 100 | * [WindowOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/transformingOperators/WindowOperatorActivity.kt) -Using `Window` Operator 101 | * [AmbOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/conditionalOperators/AmbOperatorActivity.kt) -Using `Amb` Operator 102 | * [DelayOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/utilityOperators/DelayOperatorActivity.kt) -Using `Delay` Operator 103 | * [CombineLatestOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/combiningOperators/CombineLatestOperatorActivity.kt) 104 | * [CompletableObserverOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/CompletableObserverOperatorActivity.kt) 105 | * [RepeatWhenOperatorActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/RepeatWhenOperatorActivity.kt) 106 | * Coming More 107 | 108 | 109 | 110 | 111 | # RxBinding 112 | 113 | Rx is powerful because we can compose transformations. What that means is that we can have reusable, safe and more functional code that simply plugs into your code. 114 | 115 | As an example let’s say we’re making a login screen with an email and a password… 116 | 117 | Consider the following rules that we want for our email addresses 118 | 119 | - Length should be greater than 6 120 | - Should have a correct email pattern 121 | - Let the user know if any of the above fails 122 | - On each key stroke, verify 123 | 124 | 125 | ## Highlights : 126 | * [RxLoginScreenActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxBinding/RxLoginScreenActivity.kt) - Login Screen using RxBinding 127 | 128 | 129 | # Pagination And Lazy loading using RxKotlin 130 | 131 | 132 | ## Highlights : 133 | * [PaginationActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxPagination/PaginationActivity.kt) - Activity containing recyclerview and data. 134 | * [PaginationAdapter](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxPagination/PaginationAdapter.kt) - Adapter containing layout item 135 | 136 | 137 | 138 | ### Contact - Let's connect to learn together 139 | - [Twitter](https://twitter.com/KumarAnkitRKE) 140 | - [Github](https://github.com/AnkitDroidGit) 141 | - [LinkedIn](https://www.linkedin.com/in/kumarankitkumar/) 142 | - [Facebook](https://www.facebook.com/freeankit) 143 | - [Slack](https://ankitdroid.slack.com) 144 | - [Stackoverflow](https://stackoverflow.com/users/3282461/android) 145 | - [Android App](https://play.google.com/store/apps/details?id=com.freeankit.ankitprofile) 146 | 147 | 148 | ### License 149 | 150 | Copyright 2017 Ankit Kumar 151 | 152 | Licensed under the Apache License, Version 2.0 (the "License"); 153 | you may not use this file except in compliance with the License. 154 | You may obtain a copy of the License at 155 | 156 | http://www.apache.org/licenses/LICENSE-2.0 157 | 158 | Unless required by applicable law or agreed to in writing, software 159 | distributed under the License is distributed on an "AS IS" BASIS, 160 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 161 | See the License for the specific language governing permissions and 162 | limitations under the License. 163 | -------------------------------------------------------------------------------- /RxOperators/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /RxOperators/README.md: -------------------------------------------------------------------------------- 1 | # RxKotlinOperators 2 | 3 | Learn RxKotlin with simple coding examples 4 | 5 | 6 | # Migration from RxKotlin 1.0 to RxKotlin 2.0 7 | 8 | To allow having RxKotlin 1 and RxKotlin 2 side-by-side, RxKotlin 2 is under the maven coordinates io.reactivex.rxjava2:rxjava:2.x.y and classes are accessible below io.reactivex. 9 | Users switching from 1.x to 2.x have to re-organize their imports, but carefully. 10 | 11 | ## Using RxKotlin 2.0 Library in your application 12 | 13 | Add this in your build.gradle 14 | 15 | `compile 'io.reactivex.rxjava2:rxjava:2.1.1'` 16 | 17 | If you are using RxAndroid also, then add the following 18 | 19 | `compile 'io.reactivex.rxjava2:rxandroid:2.0.1'` 20 | 21 | # Quick Look on few changes done in RxKotlin2 over RxKotlin1 22 | 23 | ### RxJava1/RxKotlin1 -> RxJava2/RxKotlin2 24 | 25 | * `onCompleted` -> `onComplete` - without the trailing d 26 | * `Func1` -> `Function` 27 | * `Func2` -> `BiFunction` 28 | * `CompositeSubscription` -> `CompositeDisposable` 29 | * `limit` operator has been removed - Use `take` in RxKotlin2 30 | 31 | ....... and so on 32 | 33 | # RxOperators : 34 | 35 | * `map()` -> Transform the items emitted by an Observable by applying a function to each item 36 | * `zip()` -> Combine the emissions of multiple Observables together via a specified function and emit single items for each combination based on the results of this function 37 | * `take()` -> Emit only the first n items emitted by an Observable 38 | * `timer()` -> Create an Observable that emits a particular item after a given delay 39 | * `flatMap()` -> Transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable 40 | * `interval()` -> Create an Observable that emits a sequence of integers spaced by a given time interval 41 | * `debounce()` -> Only emit an item from an Observable if a particular time span has passed without it emitting another item 42 | * `Single Observer` -> A Single is something like an Observable, but instead of emitting a series of values — anywhere from none at all to an infinite number — it always either emits one value or an error notification. 43 | * `reduce()` -> Apply a function to each item emitted by an Observable, sequentially, and emit the final value 44 | * `buffer()` -> Periodically gather items emitted by an Observable into bundles and emit these bundles rather than emitting the items one at a time 45 | * `filter()` -> Emit only those items from an Observable that pass a predicate test 46 | * `skip()` -> Suppress the first n items emitted by an Observable 47 | * `scan()` -> Apply a function to each item emitted by an Observable, sequentially, and emit each successive value 48 | * `replay()` -> Ensure that all observers see the same sequence of emitted items, even if they subscribe after the Observable has begun emitting items 49 | * `concat()` -> Emit the emissions from two or more Observables without interleaving them 50 | * `merge()` -> Combine multiple Observables into one by merging their emissions 51 | * `defer()` -> The Defer operator waits until an observer subscribes to it, and then it generates an Observable, typically with an Observable factory function. It does this afresh for each subscriber, so although each subscriber may think it is subscribing to the same Observable, in fact each subscriber gets its own individual sequence. 52 | * `distinct()` -> Suppress duplicate items emitted by an Observable 53 | * `Replay Subject` -> Replays events (in a configurable bounded or unbounded manner) to current and late Observers. 54 | * `Publish Subject` -> Subject that, once an Observer has subscribed, emits all subsequently observed items to the subscriber 55 | * `Behaviour Subject` -> Subject that emits the most recent item it has observed and all subsequent observed items to each subscribed Observer. 56 | * `Aync Subject` -> A Subject that emits the very last value followed by a completion event or the received error to Observers. 57 | * `Throttle First` -> Emit the first items emitted by an Observable within periodic time intervals 58 | * `amb()` -> Given two or more source Observables, emits all of the items from the first of these Observables to emit an item 59 | * `Catch` -> Recover from an onError notification by continuing the sequence without error 60 | * `Retry` -> If a source Observable emits an error, resubscribe to it in the hopes that it will complete without error 61 | 62 | * Coming More 63 | ## TODO 64 | 65 | Adding more operator examples 66 | 67 | ## Highlights : 68 | * [SimpleOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/SimpleOperatorActivity.kt) - Using `Simple` operator 69 | * [MapOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/transformingOperators/MapOperatorActivity.kt) - Using `Map` operator 70 | * [ZipOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/combiningOperators/ZipOperatorActivity.kt) - Using `Zip` observer 71 | * [DisposableOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/DisposableOperatorActivity.kt) - Using `Disposable` operator 72 | * [FlatMapOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/transformingOperators/FlatMapOperatorActivity.kt) - Using `FlatMap` Operator 73 | * [IntervalOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/creatingOperators/IntervalOperatorActivity.kt) - Using `Interval` Operator 74 | * [TakeOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/filteringOperators/TakeOperatorActivity.kt) - Using `Take` Operator 75 | * [TimerOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/TimerOperatorActivity.kt) - Using `Timer` Operator 76 | * [DebounceOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/filteringOperators/DebounceOperatorActivity.kt) - Using `Debounce` Operator 77 | * [SingleObserverOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/SingleObserverOperatorActivity.kt) - Using `Single Observer` Operator 78 | * [FlowableOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/FlowableOperatorActivity.kt) - Using `Flowable` 79 | * [ReduceOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/mathematicalOperators/ReduceOperatorActivity.kt) -Using `Reduce` Operator 80 | * [BufferOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/transformingOperators/BufferOperatorActivity.kt) -Using `Buffer` Operator 81 | * [FilterOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/filteringOperators/FilterOperatorActivity.kt) -Using `Filter` Operator 82 | * [SkipOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/filteringOperators/SkipOperatorActivity.kt) -Using `Skip` Operator 83 | * [ScanOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/transformingOperators/ScanOperatorActivity.kt) -Using `Scan` Operator 84 | * [ReplayOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/connectableOperators/ReplayOperatorActivity.kt) -Using `Replay` Operator 85 | * [ConcatOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/mathematicalOperators/ConcatOperatorActivity.kt) -Using `Concat` Operator 86 | * [MergeOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/combiningOperators/MergeOperatorActivity.kt) -Using `Merge` Operator 87 | * [DeferOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/creatingOperators/DeferOperatorActivity.kt) -Using `Defer` Operator 88 | * [DistinctOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/filteringOperators/DistinctOperatorActivity.kt) -Using `Distinct` Operator 89 | * [ReplaySubjectOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/connectableOperators/ReplaySubjectOperatorActivity.kt) -Using `Replay Subject` Operator 90 | * [PublishSubjectOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/connectableOperators/PublishSubjectOperatorActivity.kt) -Using `Publish Subject` Operator 91 | * [BehaviorSubjectActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/BehaviorSubjectActivity.kt) -Using `Behavior Subject` Operator 92 | * [AsyncSubjectOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/AsyncSubjectOperatorActivity.kt) -Using `Aync Subject` Operator 93 | * [ThrottleFirstOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/ThrottleFirstOperatorActivity.kt) -Using `Throttle First` Operator 94 | * [ThrottleLastOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/ThrottleLastOperatorActivity.kt) -Using `Throttle Last` Operator 95 | * [WindowOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/transformingOperatorsWindowOperatorActivity.kt) -Using `Window` Operator 96 | * [AmbOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/conditionalOperators/AmbOperatorActivity.kt) -Using `Amb` Operator 97 | * [DelayOperatorActivity](https://github.com/AnkitDroidGit/RxJava-RxKotlin-Android/blob/master/app/src/main/java/com/freeankit/rxjava2samples/ui/operators/utilityOperators/DelayOperatorActivity.kt) -Using `Delay` Operator 98 | 99 | * Coming More 100 | 101 | 102 | ### Contact - Let's connect to learn together 103 | - [Twitter](https://twitter.com/KumarAnkitRKE) 104 | - [Github](https://github.com/AnkitDroidGit) 105 | - [LinkedIn](https://www.linkedin.com/in/kumarankitkumar/) 106 | - [Facebook](https://www.facebook.com/freeankit) 107 | - [Slack](https://ankitdroid.slack.com) 108 | - [Stackoverflow](https://stackoverflow.com/users/3282461/android) 109 | - [Android App](https://play.google.com/store/apps/details?id=com.freeankit.ankitprofile) 110 | 111 | 112 | ### License 113 | 114 | Copyright 2017 Ankit Kumar 115 | 116 | Licensed under the Apache License, Version 2.0 (the "License"); 117 | you may not use this file except in compliance with the License. 118 | You may obtain a copy of the License at 119 | 120 | http://www.apache.org/licenses/LICENSE-2.0 121 | 122 | Unless required by applicable law or agreed to in writing, software 123 | distributed under the License is distributed on an "AS IS" BASIS, 124 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125 | See the License for the specific language governing permissions and 126 | limitations under the License. 127 | -------------------------------------------------------------------------------- /RxOperators/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 27 9 | defaultConfig { 10 | applicationId "com.freeankit.rxjava2samples" 11 | minSdkVersion 19 12 | targetSdkVersion 27 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | compileOptions { 24 | targetCompatibility 1.8 25 | sourceCompatibility 1.8 26 | } 27 | } 28 | 29 | dependencies { 30 | implementation fileTree(dir: 'libs', include: ['*.jar']) 31 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 32 | implementation 'com.android.support:recyclerview-v7:27.1.1' 33 | implementation 'com.android.support:design:27.1.1' 34 | implementation 'com.android.support:appcompat-v7:27.1.1' 35 | implementation 'com.android.support:animated-vector-drawable:27.1.1' 36 | implementation 'com.android.support.constraint:constraint-layout:1.1.0' 37 | 38 | //rxJava2 39 | implementation 'io.reactivex.rxjava2:rxjava:2.1.9' 40 | implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' 41 | 42 | //Rx Binding 43 | implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1' 44 | implementation 'com.jakewharton.rxbinding2:rxbinding-support-v4:2.1.1' 45 | implementation 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7:2.1.1' 46 | implementation 'com.jakewharton.rxbinding2:rxbinding-design:2.1.1' 47 | } 48 | -------------------------------------------------------------------------------- /RxOperators/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 | -------------------------------------------------------------------------------- /RxOperators/src/androidTest/java/com/freeankit/rxkotlinoperators/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators 2 | 3 | import android.support.test.InstrumentationRegistry 4 | import android.support.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("com.freeankit.rxjava2samples", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /RxOperators/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ApplicationClass.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators 2 | 3 | /** 4 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/12/2017 (MM/DD/YYYY ) 5 | */ 6 | class ApplicationClass { 7 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/model/ApiUser.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.model 2 | 3 | /** 4 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/12/2017 (MM/DD/YYYY ) 5 | */ 6 | data class ApiUser(var login: String, 7 | var id: Int, 8 | var url: String) -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/model/Bike.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.model 2 | 3 | import io.reactivex.Observable 4 | 5 | /** 6 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 11/01/2018 (MM/DD/YYYY ) 7 | */ 8 | class Bike { 9 | private var brand: String? = null 10 | 11 | fun setBrand(brand: String) { 12 | this.brand = brand 13 | } 14 | 15 | fun brandDeferObservable(): Observable { 16 | return Observable.defer { Observable.just(brand!!) } 17 | } 18 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/model/DateModel.java: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.model; 2 | 3 | /** 4 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 15/12/2017 (MM/DD/YYYY ) 5 | */ 6 | 7 | public class DateModel implements Comparable { 8 | private Integer date; 9 | private Integer value; 10 | 11 | public DateModel(int date, int value) { 12 | this.date = date; 13 | this.value = value; 14 | } 15 | 16 | @Override 17 | public int compareTo(DateModel o) { 18 | return value.compareTo(o.value); 19 | } 20 | 21 | public Integer getDate() { 22 | return date; 23 | } 24 | 25 | public void setDate(Integer date) { 26 | this.date = date; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/model/User.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.model 2 | 3 | /** 4 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/12/2017 (MM/DD/YYYY ) 5 | */ 6 | public class User() { 7 | var id: Int = 0 8 | var login: String = "" 9 | var url: String = "" 10 | var isFollowing: Boolean = false 11 | 12 | @JvmOverloads constructor(githubUser: ApiUser) : this() { 13 | this.id = githubUser.id 14 | this.login = githubUser.login 15 | this.url = githubUser.url 16 | } 17 | 18 | 19 | override fun toString(): String { 20 | return "User{" + 21 | "id=" + id + 22 | ", login='" + login + '\'' + 23 | ", url='" + url + '\'' + 24 | ", isFollowing=" + isFollowing + 25 | '}' 26 | } 27 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/OperatorsActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import android.support.v7.app.AppCompatActivity 6 | import android.view.View 7 | import com.freeankit.rxkotlinoperators.R 8 | import com.freeankit.rxkotlinoperators.ui.RxOperators.* 9 | import com.freeankit.rxkotlinoperators.ui.RxOperators.combiningOperators.MergeOperatorActivity 10 | import com.freeankit.rxkotlinoperators.ui.RxOperators.combiningOperators.ZipOperatorActivity 11 | import com.freeankit.rxkotlinoperators.ui.RxOperators.connectableOperators.PublishSubjectOperatorActivity 12 | import com.freeankit.rxkotlinoperators.ui.RxOperators.connectableOperators.ReplayOperatorActivity 13 | import com.freeankit.rxkotlinoperators.ui.RxOperators.connectableOperators.ReplaySubjectOperatorActivity 14 | import com.freeankit.rxkotlinoperators.ui.RxOperators.creatingOperators.DeferOperatorActivity 15 | import com.freeankit.rxkotlinoperators.ui.RxOperators.creatingOperators.IntervalOperatorActivity 16 | import com.freeankit.rxkotlinoperators.ui.RxOperators.errorHandlingOperators.RetryOperatorActivity 17 | import com.freeankit.rxkotlinoperators.ui.RxOperators.filteringOperators.* 18 | import com.freeankit.rxkotlinoperators.ui.RxOperators.mathematicalOperators.ConcatOperatorActivity 19 | import com.freeankit.rxkotlinoperators.ui.RxOperators.mathematicalOperators.ReduceOperatorActivity 20 | import com.freeankit.rxkotlinoperators.ui.RxOperators.transformingOperators.* 21 | import com.freeankit.rxkotlinoperators.ui.RxOperators.utilityOperators.DelayOperatorActivity 22 | 23 | /** 24 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/12/2017 (MM/DD/YYYY ) 25 | */ 26 | class OperatorsActivity : AppCompatActivity() { 27 | override fun onCreate(savedInstanceState: Bundle?) { 28 | super.onCreate(savedInstanceState) 29 | setContentView(R.layout.activity_operators) 30 | } 31 | 32 | fun startSimpleActivity(view: View) { 33 | startActivity(Intent(this@OperatorsActivity, SimpleOperatorActivity::class.java)) 34 | } 35 | 36 | fun startMapActivity(view: View) { 37 | startActivity(Intent(this@OperatorsActivity, MapOperatorActivity::class.java)) 38 | } 39 | 40 | fun startZipActivity(view: View) { 41 | startActivity(Intent(this@OperatorsActivity, ZipOperatorActivity::class.java)) 42 | } 43 | 44 | fun startDisposableActivity(view: View) { 45 | startActivity(Intent(this@OperatorsActivity, DisposableOperatorActivity::class.java)) 46 | } 47 | 48 | fun startTakeActivity(view: View) { 49 | startActivity(Intent(this@OperatorsActivity, TakeOperatorActivity::class.java)) 50 | } 51 | 52 | fun startTimerActivity(view: View) { 53 | startActivity(Intent(this@OperatorsActivity, TimerOperatorActivity::class.java)) 54 | } 55 | 56 | fun startFlatMapActivity(view: View) { 57 | startActivity(Intent(this@OperatorsActivity, FlatMapOperatorActivity::class.java)) 58 | } 59 | 60 | fun startIntervalActivity(view: View) { 61 | startActivity(Intent(this@OperatorsActivity, IntervalOperatorActivity::class.java)) 62 | } 63 | 64 | fun startDebounceActivity(view: View) { 65 | startActivity(Intent(this@OperatorsActivity, DebounceOperatorActivity::class.java)) 66 | } 67 | 68 | fun startSingleObserverActivity(view: View) { 69 | startActivity(Intent(this@OperatorsActivity, SingleObserverOperatorActivity::class.java)) 70 | } 71 | 72 | fun startCompletableObserverActivity(view: View) { 73 | startActivity(Intent(this@OperatorsActivity, CompletableObserverOperatorActivity::class.java)) 74 | } 75 | 76 | fun startFlowableActivity(view: View) { 77 | startActivity(Intent(this@OperatorsActivity, FlowableOperatorActivity::class.java)) 78 | } 79 | 80 | fun startReduceActivity(view: View) { 81 | startActivity(Intent(this@OperatorsActivity, ReduceOperatorActivity::class.java)) 82 | } 83 | 84 | fun startBufferActivity(view: View) { 85 | startActivity(Intent(this@OperatorsActivity, BufferOperatorActivity::class.java)) 86 | } 87 | 88 | fun startFilterActivity(view: View) { 89 | startActivity(Intent(this@OperatorsActivity, FilterOperatorActivity::class.java)) 90 | } 91 | 92 | fun startSkipActivity(view: View) { 93 | startActivity(Intent(this@OperatorsActivity, SkipOperatorActivity::class.java)) 94 | } 95 | 96 | fun startScanActivity(view: View) { 97 | startActivity(Intent(this@OperatorsActivity, ScanOperatorActivity::class.java)) 98 | } 99 | 100 | fun startReplayActivity(view: View) { 101 | startActivity(Intent(this@OperatorsActivity, ReplayOperatorActivity::class.java)) 102 | } 103 | 104 | fun startConcatActivity(view: View) { 105 | startActivity(Intent(this@OperatorsActivity, ConcatOperatorActivity::class.java)) 106 | } 107 | 108 | fun startMergeActivity(view: View) { 109 | startActivity(Intent(this@OperatorsActivity, MergeOperatorActivity::class.java)) 110 | } 111 | 112 | fun startDeferActivity(view: View) { 113 | startActivity(Intent(this@OperatorsActivity, DeferOperatorActivity::class.java)) 114 | } 115 | 116 | fun startDistinctActivity(view: View) { 117 | startActivity(Intent(this@OperatorsActivity, DistinctOperatorActivity::class.java)) 118 | } 119 | 120 | fun startReplaySubjectActivity(view: View) { 121 | startActivity(Intent(this@OperatorsActivity, ReplaySubjectOperatorActivity::class.java)) 122 | } 123 | 124 | fun startPublishSubjectActivity(view: View) { 125 | startActivity(Intent(this@OperatorsActivity, PublishSubjectOperatorActivity::class.java)) 126 | } 127 | 128 | fun startBehaviorSubjectActivity(view: View) { 129 | startActivity(Intent(this@OperatorsActivity, BehaviorSubjectActivity::class.java)) 130 | } 131 | 132 | fun startAsyncSubjectActivity(view: View) { 133 | startActivity(Intent(this@OperatorsActivity, AsyncSubjectOperatorActivity::class.java)) 134 | } 135 | 136 | fun startThrottleFirstActivity(view: View) { 137 | startActivity(Intent(this@OperatorsActivity, ThrottleFirstOperatorActivity::class.java)) 138 | } 139 | 140 | fun startThrottleLastActivity(view: View) { 141 | startActivity(Intent(this@OperatorsActivity, ThrottleLastOperatorActivity::class.java)) 142 | } 143 | 144 | fun startWindowActivity(view: View) { 145 | startActivity(Intent(this@OperatorsActivity, WindowOperatorActivity::class.java)) 146 | } 147 | 148 | fun startDelayActivity(view: View) { 149 | startActivity(Intent(this@OperatorsActivity, DelayOperatorActivity::class.java)) 150 | } 151 | 152 | fun startRetryActivity(view: View) { 153 | startActivity(Intent(this@OperatorsActivity, RetryOperatorActivity::class.java)) 154 | } 155 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxBinding/RxLoginScreenActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxBinding 2 | 3 | import android.support.v7.app.AppCompatActivity 4 | import android.os.Bundle 5 | import android.util.Patterns 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.jakewharton.rxbinding2.widget.RxTextView 8 | import io.reactivex.Observable 9 | import io.reactivex.ObservableTransformer 10 | import io.reactivex.Single 11 | import io.reactivex.android.schedulers.AndroidSchedulers 12 | import kotlinx.android.synthetic.main.activity_rx_login_screen.* 13 | import java.util.concurrent.TimeUnit 14 | 15 | class RxLoginScreenActivity : AppCompatActivity() { 16 | 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_rx_login_screen) 20 | 21 | RxTextView.afterTextChangeEvents(editTextEmail) 22 | .skipInitialValue() 23 | .map { 24 | emailWrapper.error = null 25 | it.view().text.toString() 26 | } 27 | .debounce(1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()) 28 | .compose(lengthGreaterThanSix) 29 | .compose(verifyEmailPattern) 30 | .compose(retryWhenError { 31 | emailWrapper.error = it.message 32 | }) 33 | .subscribe() 34 | 35 | RxTextView.afterTextChangeEvents(editTextPassword) 36 | .skipInitialValue() 37 | .map { 38 | passwordWrapper.error = null 39 | it.view().text.toString() 40 | } 41 | .debounce(1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()) 42 | .compose(lengthGreaterThanSix) 43 | .compose(retryWhenError { 44 | passwordWrapper.error = it.message 45 | }) 46 | .subscribe() 47 | } 48 | private inline fun retryWhenError(crossinline onError: (ex: Throwable) -> Unit): ObservableTransformer = ObservableTransformer { observable -> 49 | observable.retryWhen { errors -> 50 | errors.flatMap { 51 | onError(it) 52 | Observable.just("") 53 | } 54 | 55 | } 56 | } 57 | 58 | private val lengthGreaterThanSix = ObservableTransformer { observable -> 59 | observable.flatMap { 60 | Observable.just(it).map { it.trim() } // - abcdefg - | 61 | .filter { it.length > 6 } 62 | .singleOrError() 63 | .onErrorResumeNext { 64 | if (it is NoSuchElementException) { 65 | Single.error(Exception("Length must be greater than 6")) 66 | } else { 67 | Single.error(it) 68 | } 69 | } 70 | .toObservable() 71 | 72 | 73 | } 74 | } 75 | 76 | private val verifyEmailPattern = ObservableTransformer { observable -> 77 | observable.flatMap { 78 | Observable.just(it).map { it.trim() } 79 | .filter { 80 | Patterns.EMAIL_ADDRESS.matcher(it).matches() 81 | } 82 | .singleOrError() 83 | .onErrorResumeNext { 84 | if (it is NoSuchElementException) { 85 | Single.error(Exception("Email not valid")) 86 | } else { 87 | Single.error(it) 88 | } 89 | } 90 | .toObservable() 91 | } 92 | } 93 | 94 | 95 | 96 | } 97 | -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/AsyncSubjectOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observer 9 | import io.reactivex.disposables.Disposable 10 | import io.reactivex.subjects.AsyncSubject 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/02/2018 (MM/DD/YYYY ) 15 | */ 16 | class AsyncSubjectOperatorActivity :AppCompatActivity(){ 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeAsyncSubjectOperator() }) 22 | } 23 | 24 | /* An AsyncSubject emits the last value (and only the last value) emitted by the source 25 | * Observable, and only after that source Observable completes. (If the source Observable 26 | * does not emit any values, the AsyncSubject also completes without emitting any values.) 27 | */ 28 | private fun executeAsyncSubjectOperator() { 29 | 30 | val source = AsyncSubject.create() 31 | 32 | source.subscribe(getFirstObserver()) // it will emit only 4 and onComplete 33 | 34 | source.onNext(1) 35 | source.onNext(2) 36 | source.onNext(3) 37 | 38 | /* 39 | * it will emit 4 and onComplete for second observer also. 40 | */ 41 | source.subscribe(getSecondObserver()) 42 | 43 | source.onNext(4) 44 | source.onComplete() 45 | 46 | } 47 | 48 | private fun getFirstObserver(): Observer { 49 | return object : Observer { 50 | 51 | override fun onSubscribe(d: Disposable) { 52 | Log.d(Constant().TAG, " First onSubscribe : " + d.isDisposed) 53 | } 54 | 55 | override fun onNext(value: Int) { 56 | textView.append(" First onNext : value : $value") 57 | textView.append(Constant().LINE_SEPARATOR) 58 | Log.d(Constant().TAG, " First onNext value : $value") 59 | } 60 | 61 | override fun onError(e: Throwable) { 62 | textView.append(" First onError : " + e.message) 63 | textView.append(Constant().LINE_SEPARATOR) 64 | Log.d(Constant().TAG, " First onError : " + e.message) 65 | } 66 | 67 | override fun onComplete() { 68 | textView.append(" First onComplete") 69 | textView.append(Constant().LINE_SEPARATOR) 70 | Log.d(Constant().TAG, " First onComplete") 71 | } 72 | } 73 | } 74 | 75 | private fun getSecondObserver(): Observer { 76 | return object : Observer { 77 | 78 | override fun onSubscribe(d: Disposable) { 79 | textView.append(" Second onSubscribe : isDisposed :" + d.isDisposed) 80 | Log.d(Constant().TAG, " Second onSubscribe : " + d.isDisposed) 81 | textView.append(Constant().LINE_SEPARATOR) 82 | } 83 | 84 | override fun onNext(value: Int) { 85 | textView.append(" Second onNext : value : $value") 86 | textView.append(Constant().LINE_SEPARATOR) 87 | Log.d(Constant().TAG, " Second onNext value : $value") 88 | } 89 | 90 | override fun onError(e: Throwable) { 91 | textView.append(" Second onError : " + e.message) 92 | textView.append(Constant().LINE_SEPARATOR) 93 | Log.d(Constant().TAG, " Second onError : " + e.message) 94 | } 95 | 96 | override fun onComplete() { 97 | textView.append(" Second onComplete") 98 | textView.append(Constant().LINE_SEPARATOR) 99 | Log.d(Constant().TAG, " Second onComplete") 100 | } 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/BehaviorSubjectActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observer 9 | import io.reactivex.disposables.Disposable 10 | import io.reactivex.subjects.BehaviorSubject 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 30/01/2018 (MM/DD/YYYY ) 15 | */ 16 | class BehaviorSubjectActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeBehaviorSubjectOperator() }) 22 | } 23 | 24 | /* When an observer subscribes to a BehaviorSubject, it begins by emitting the item most 25 | * recently emitted by the source Observable (or a seed/default value if none has yet been 26 | * emitted) and then continues to emit any other items emitted later by the source Observable(s). 27 | * It is different from Async Subject as async emits the last value (and only the last value) 28 | * but the Behavior Subject emits the last and the subsequent values also. 29 | */ 30 | private fun executeBehaviorSubjectOperator() { 31 | 32 | val source = BehaviorSubject.create() 33 | 34 | source.subscribe(getFirstObserver()) // it will get 1, 2, 3, 4 and onComplete 35 | 36 | source.onNext(1) 37 | source.onNext(2) 38 | source.onNext(3) 39 | 40 | /* 41 | * it will emit 3(last emitted), 4 and onComplete for second observer also. 42 | */ 43 | source.subscribe(getSecondObserver()) 44 | 45 | source.onNext(4) 46 | source.onComplete() 47 | } 48 | 49 | 50 | private fun getFirstObserver(): Observer { 51 | return object : Observer { 52 | 53 | override fun onSubscribe(d: Disposable) { 54 | Log.d(Constant().TAG, " First onSubscribe : " + d.isDisposed) 55 | } 56 | 57 | override fun onNext(value: Int) { 58 | textView.append(" First onNext : value : " + value!!) 59 | textView.append(Constant().LINE_SEPARATOR) 60 | Log.d(Constant().TAG, " First onNext value : " + value) 61 | } 62 | 63 | override fun onError(e: Throwable) { 64 | textView.append(" First onError : " + e.message) 65 | textView.append(Constant().LINE_SEPARATOR) 66 | Log.d(Constant().TAG, " First onError : " + e.message) 67 | } 68 | 69 | override fun onComplete() { 70 | textView.append(" First onComplete") 71 | textView.append(Constant().LINE_SEPARATOR) 72 | Log.d(Constant().TAG, " First onComplete") 73 | } 74 | } 75 | } 76 | 77 | private fun getSecondObserver(): Observer { 78 | return object : Observer { 79 | 80 | override fun onSubscribe(d: Disposable) { 81 | textView.append(" Second onSubscribe : isDisposed :" + d.isDisposed) 82 | Log.d(Constant().TAG, " Second onSubscribe : " + d.isDisposed) 83 | textView.append(Constant().LINE_SEPARATOR) 84 | } 85 | 86 | override fun onNext(value: Int) { 87 | textView.append(" Second onNext : value : " + value!!) 88 | textView.append(Constant().LINE_SEPARATOR) 89 | Log.d(Constant().TAG, " Second onNext value : " + value) 90 | } 91 | 92 | override fun onError(e: Throwable) { 93 | textView.append(" Second onError : " + e.message) 94 | textView.append(Constant().LINE_SEPARATOR) 95 | Log.d(Constant().TAG, " Second onError : " + e.message) 96 | } 97 | 98 | override fun onComplete() { 99 | textView.append(" Second onComplete") 100 | textView.append(Constant().LINE_SEPARATOR) 101 | Log.d(Constant().TAG, " Second onComplete") 102 | } 103 | } 104 | } 105 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/CompletableObserverOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Completable 9 | import io.reactivex.CompletableObserver 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | import java.util.concurrent.TimeUnit 15 | 16 | /** 17 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 19/12/2017 (MM/DD/YYYY ) 18 | */ 19 | class CompletableObserverOperatorActivity : AppCompatActivity() { 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | 23 | setContentView(R.layout.activity_example_operator) 24 | 25 | btn.setOnClickListener({ executeCompletableObserverOperator() }) 26 | } 27 | 28 | /* 29 | * simple example using CompletableObserver 30 | */ 31 | private fun executeCompletableObserverOperator() { 32 | val completable = Completable.timer(1000, TimeUnit.MILLISECONDS) 33 | 34 | completable 35 | .subscribeOn(Schedulers.io()) 36 | // Be notified on the main thread 37 | .observeOn(AndroidSchedulers.mainThread()) 38 | .subscribe(getCompletableObserver()) 39 | 40 | 41 | 42 | } 43 | 44 | private fun getCompletableObserver(): CompletableObserver { 45 | return object : CompletableObserver { 46 | override fun onSubscribe(d: Disposable) { 47 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 48 | } 49 | 50 | override fun onComplete() { 51 | textView.append(" onComplete") 52 | textView.append(Constant().LINE_SEPARATOR) 53 | Log.d(Constant().TAG, " onComplete") 54 | } 55 | 56 | override fun onError(e: Throwable) { 57 | textView.append(" onError : " + e.message) 58 | textView.append(Constant().LINE_SEPARATOR) 59 | Log.d(Constant().TAG, " onError : " + e.message) 60 | } 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/DisposableOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.os.SystemClock 5 | import android.support.v7.app.AppCompatActivity 6 | import android.util.Log 7 | import android.view.View 8 | import com.freeankit.rxkotlinoperators.R 9 | import com.freeankit.rxkotlinoperators.utils.Constant 10 | import io.reactivex.Observable 11 | import io.reactivex.android.schedulers.AndroidSchedulers 12 | import io.reactivex.disposables.CompositeDisposable 13 | import io.reactivex.observers.DisposableObserver 14 | import io.reactivex.schedulers.Schedulers 15 | import kotlinx.android.synthetic.main.activity_example_operator.* 16 | 17 | /** 18 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 11/12/2017 (MM/DD/YYYY ) 19 | */ 20 | class DisposableOperatorActivity : AppCompatActivity() { 21 | private val disposables = CompositeDisposable() 22 | override fun onCreate(savedInstanceState: Bundle?) { 23 | super.onCreate(savedInstanceState) 24 | setContentView(R.layout.activity_example_operator) 25 | 26 | btn.setOnClickListener({ executeDisposableOperator() }) 27 | } 28 | 29 | /* 30 | * Example to understand how to use disposables. 31 | * disposables is cleared in onDestroy of this activity. 32 | */ 33 | private fun executeDisposableOperator() { 34 | progress.visibility = View.VISIBLE 35 | disposables.add(sampleObservable() 36 | // Run on a background thread 37 | .subscribeOn(Schedulers.io()) 38 | // Be notified on the main thread 39 | .observeOn(AndroidSchedulers.mainThread()) 40 | .subscribeWith(object : DisposableObserver() { 41 | override fun onComplete() { 42 | 43 | textView.append(" onComplete") 44 | textView.append(Constant().LINE_SEPARATOR) 45 | Log.d(Constant().TAG, " onComplete") 46 | progress.visibility = View.GONE 47 | } 48 | 49 | override fun onError(e: Throwable) { 50 | textView.append(" onError : " + e.message) 51 | textView.append(Constant().LINE_SEPARATOR) 52 | Log.d(Constant().TAG, " onError : " + e.message) 53 | progress.visibility = View.GONE 54 | } 55 | 56 | override fun onNext(value: String) { 57 | textView.append(" onNext : value : " + value) 58 | textView.append(Constant().LINE_SEPARATOR) 59 | Log.d(Constant().TAG, " onNext value : " + value) 60 | progress.visibility = View.GONE 61 | } 62 | })) 63 | } 64 | 65 | private fun sampleObservable(): Observable { 66 | return Observable.defer { 67 | // Do some long running operation 68 | SystemClock.sleep(2000) 69 | Observable.just("Firebase", "Fabric", "Analytics", "Github", "LinkedIn") 70 | } 71 | } 72 | 73 | override fun onDestroy() { 74 | super.onDestroy() 75 | disposables.clear() // do not send event after activity has been destroyed 76 | } 77 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/FlowableOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Flowable 9 | import io.reactivex.SingleObserver 10 | import io.reactivex.disposables.Disposable 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | 14 | /** 15 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 20/12/2017 (MM/DD/YYYY ) 16 | */ 17 | class FlowableOperatorActivity : AppCompatActivity() { 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | setContentView(R.layout.activity_example_operator) 21 | 22 | btn.setOnClickListener({ executeFlowableOperator() }) 23 | } 24 | 25 | /* 26 | * simple example using Flowable 27 | */ 28 | private fun executeFlowableOperator() { 29 | 30 | val observable = Flowable.just(1, 2, 3, 4) 31 | observable 32 | .reduce(50) { t1, t2 -> t1 + t2 } 33 | .subscribe(getObserver()) 34 | 35 | } 36 | 37 | 38 | private fun getObserver(): SingleObserver { 39 | 40 | return object : SingleObserver { 41 | 42 | 43 | override fun onSubscribe(d: Disposable) { 44 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 45 | } 46 | 47 | override fun onSuccess(value: Int) { 48 | textView.append(" onSuccess : value : $value") 49 | textView.append(Constant().LINE_SEPARATOR) 50 | Log.d(Constant().TAG, " onSuccess : value : $value") 51 | } 52 | 53 | override fun onError(e: Throwable) { 54 | textView.append(" onError : " + e.message) 55 | textView.append(Constant().LINE_SEPARATOR) 56 | Log.d(Constant().TAG, " onError : " + e.message) 57 | } 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/RepeatWhenOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observer 9 | import io.reactivex.disposables.Disposable 10 | import io.reactivex.subjects.PublishSubject 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | 14 | /** 15 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 28/02/2018 (MM/DD/YYYY) 16 | */ 17 | class RepeatWhenOperatorActivity : AppCompatActivity() { 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | setContentView(R.layout.activity_example_operator) 21 | 22 | btn.setOnClickListener({ executeRepeatWhenOperator() }) 23 | } 24 | 25 | private fun executeRepeatWhenOperator() { 26 | val source = PublishSubject.create() 27 | 28 | source.subscribe(getFirstObserver()) // it will get 1, 2, 3, 4 and onComplete 29 | 30 | source.onNext(1) 31 | source.onNext(2) 32 | source.onNext(3) 33 | // 34 | // /* 35 | // * it will emit 4 and onComplete for second observer also. 36 | // */ 37 | // source.subscribe(getSecondObserver()) 38 | // 39 | // source.onNext(4) 40 | // source.onComplete() 41 | 42 | 43 | // source.doOnError(err -> showConnectionDialog()) 44 | // source.retryWhen(retryHandler -> retryHandler.flatMap(nothing -> retrySubject.asObservable())) 45 | // source.subscribe(result -> getSecondObserver()) 46 | source.onComplete() 47 | } 48 | 49 | private fun getFirstObserver(): Observer { 50 | return object : Observer { 51 | 52 | override fun onSubscribe(d: Disposable) { 53 | Log.d(Constant().TAG, " First onSubscribe : " + d.isDisposed) 54 | } 55 | 56 | override fun onNext(value: Int) { 57 | textView.append(" First onNext : value : " + value!!) 58 | textView.append(Constant().LINE_SEPARATOR) 59 | Log.d(Constant().TAG, " First onNext value : " + value) 60 | } 61 | 62 | override fun onError(e: Throwable) { 63 | textView.append(" First onError : " + e.message) 64 | textView.append(Constant().LINE_SEPARATOR) 65 | Log.d(Constant().TAG, " First onError : " + e.message) 66 | } 67 | 68 | override fun onComplete() { 69 | textView.append(" First onComplete") 70 | textView.append(Constant().LINE_SEPARATOR) 71 | Log.d(Constant().TAG, " First onComplete") 72 | } 73 | } 74 | } 75 | 76 | private fun getSecondObserver(): Observer { 77 | return object : Observer { 78 | 79 | override fun onSubscribe(d: Disposable) { 80 | textView.append(" Second onSubscribe : isDisposed :" + d.isDisposed) 81 | Log.d(Constant().TAG, " Second onSubscribe : " + d.isDisposed) 82 | textView.append(Constant().LINE_SEPARATOR) 83 | } 84 | 85 | override fun onNext(value: Int) { 86 | textView.append(" Second onNext : value : " + value!!) 87 | textView.append(Constant().LINE_SEPARATOR) 88 | Log.d(Constant().TAG, " Second onNext value : " + value) 89 | } 90 | 91 | override fun onError(e: Throwable) { 92 | textView.append(" Second onError : " + e.message) 93 | textView.append(Constant().LINE_SEPARATOR) 94 | Log.d(Constant().TAG, " Second onError : " + e.message) 95 | } 96 | 97 | override fun onComplete() { 98 | textView.append(" Second onComplete") 99 | textView.append(Constant().LINE_SEPARATOR) 100 | Log.d(Constant().TAG, " Second onComplete") 101 | } 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/SimpleOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import android.view.View 7 | import com.freeankit.rxkotlinoperators.R 8 | import com.freeankit.rxkotlinoperators.utils.Constant 9 | import io.reactivex.Observable 10 | import io.reactivex.Observer 11 | import io.reactivex.android.schedulers.AndroidSchedulers 12 | import io.reactivex.disposables.Disposable 13 | import io.reactivex.schedulers.Schedulers 14 | import kotlinx.android.synthetic.main.activity_example_operator.* 15 | 16 | /** 17 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/12/2017 (MM/DD/YYYY ) 18 | */ 19 | class SimpleOperatorActivity : AppCompatActivity() { 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_example_operator) 23 | 24 | btn.setOnClickListener({ executeSimpleOperator() }) 25 | } 26 | 27 | /* 28 | * simple example to emit two value one by one 29 | */ 30 | private fun executeSimpleOperator() { 31 | progress.visibility = View.VISIBLE 32 | getObservable() 33 | // Run on a background thread 34 | .subscribeOn(Schedulers.io()) 35 | // Be notified on the main thread 36 | .observeOn(AndroidSchedulers.mainThread()) 37 | .subscribe(getObserver()) 38 | } 39 | 40 | private fun getObservable(): Observable { 41 | return Observable.just("RxJava", "RxAndroid") 42 | } 43 | 44 | private fun getObserver(): Observer { 45 | return object : Observer { 46 | 47 | override fun onSubscribe(d: Disposable) { 48 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 49 | } 50 | 51 | override fun onNext(value: String) { 52 | textView.append(" onNext : value : " + value) 53 | textView.append("\n") 54 | Log.d(Constant().TAG, " onNext : value : " + value) 55 | progress.visibility = View.GONE 56 | } 57 | 58 | override fun onError(e: Throwable) { 59 | textView.append(" onError : " + e.message) 60 | textView.append("\n") 61 | Log.d(Constant().TAG, " onError : " + e.message) 62 | progress.visibility = View.GONE 63 | } 64 | 65 | override fun onComplete() { 66 | textView.append(" onComplete") 67 | textView.append("\n\n") 68 | Log.d(Constant().TAG, " onComplete") 69 | progress.visibility = View.GONE 70 | } 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/SingleObserverOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Single 9 | import io.reactivex.SingleObserver 10 | import io.reactivex.disposables.Disposable 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 18/12/2017 (MM/DD/YYYY ) 15 | */ 16 | class SingleObserverOperatorActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeSingleObserverOperator() }) 22 | } 23 | 24 | /* 25 | * simple example using SingleObserver 26 | */ 27 | private fun executeSingleObserverOperator() { 28 | Single.just("Ankit") 29 | .subscribe(getSingleObserver()) 30 | 31 | } 32 | 33 | private fun getSingleObserver(): SingleObserver { 34 | return object : SingleObserver { 35 | override fun onSubscribe(d: Disposable) { 36 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 37 | } 38 | 39 | override fun onSuccess(value: String) { 40 | textView.append(" onNext : value : " + value) 41 | textView.append(Constant().LINE_SEPARATOR) 42 | Log.d(Constant().TAG, " onNext value : " + value) 43 | } 44 | 45 | override fun onError(e: Throwable) { 46 | textView.append(" onError : " + e.message) 47 | textView.append(Constant().LINE_SEPARATOR) 48 | Log.d(Constant().TAG, " onError : " + e.message) 49 | } 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/ThrottleFirstOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | import java.util.concurrent.TimeUnit 15 | 16 | /** 17 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 15/02/2018 (MM/DD/YYYY ) 18 | */ 19 | class ThrottleFirstOperatorActivity : AppCompatActivity() { 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_example_operator) 23 | 24 | btn.setOnClickListener({ executeThrottleFirstOperator() }) 25 | } 26 | 27 | 28 | /* 29 | * Using throttleFirst() -> if the source Observable has emitted no items since 30 | * the last time it was sampled, the Observable that results from this operator 31 | * will emit no item for that sampling period. 32 | */ 33 | private fun executeThrottleFirstOperator() { 34 | 35 | getObservable() 36 | .throttleFirst(500, TimeUnit.MILLISECONDS) 37 | // Run on a background thread 38 | .subscribeOn(Schedulers.io()) 39 | // Be notified on the main thread 40 | .observeOn(AndroidSchedulers.mainThread()) 41 | .subscribe(getObserver()) 42 | } 43 | 44 | private fun getObservable(): Observable { 45 | return Observable.create { emitter -> 46 | // send events with simulated time wait 47 | Thread.sleep(0) 48 | emitter.onNext(1) // deliver 49 | emitter.onNext(2) // skip 50 | Thread.sleep(505) 51 | emitter.onNext(3) // deliver 52 | Thread.sleep(99) 53 | emitter.onNext(4) // skip 54 | Thread.sleep(100) 55 | emitter.onNext(5) // skip 56 | emitter.onNext(6) // skip 57 | Thread.sleep(305) 58 | emitter.onNext(7) // deliver 59 | Thread.sleep(510) 60 | emitter.onComplete() 61 | } 62 | } 63 | 64 | private fun getObserver(): Observer { 65 | return object : Observer { 66 | 67 | override fun onSubscribe(d: Disposable) { 68 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 69 | } 70 | 71 | override fun onNext(value: Int) { 72 | textView.append(" onNext : ") 73 | textView.append(Constant().LINE_SEPARATOR) 74 | textView.append(" value : " + value) 75 | textView.append(Constant().LINE_SEPARATOR) 76 | Log.d(Constant().TAG, " onNext ") 77 | Log.d(Constant().TAG, " value : " + value) 78 | } 79 | 80 | override fun onError(e: Throwable) { 81 | textView.append(" onError : " + e.message) 82 | textView.append(Constant().LINE_SEPARATOR) 83 | Log.d(Constant().TAG, " onError : " + e.message) 84 | } 85 | 86 | override fun onComplete() { 87 | textView.append(" onComplete") 88 | textView.append(Constant().LINE_SEPARATOR) 89 | Log.d(Constant().TAG, " onComplete") 90 | } 91 | } 92 | } 93 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/ThrottleLastOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | import java.util.concurrent.TimeUnit 15 | 16 | /** 17 | *@author by Ankit Kumar (ankitdroiddeveloper@gmail.com) on 2/17/18 (MM/DD/YYYY ) 18 | **/ 19 | class ThrottleLastOperatorActivity : AppCompatActivity() { 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_example_operator) 23 | 24 | btn.setOnClickListener({ executeThrottleLastOperator() }) 25 | } 26 | 27 | /* 28 | * Using throttleLast() -> emit the most recent items emitted by an Observable within 29 | * periodic time intervals, so here it will emit 2, 6 and 7 as we have simulated it to be the 30 | * last the element in the interval of 500 millis 31 | */ 32 | private fun executeThrottleLastOperator() { 33 | 34 | getObservable() 35 | .throttleLast(500, TimeUnit.MILLISECONDS) 36 | // Run on a background thread 37 | .subscribeOn(Schedulers.io()) 38 | // Be notified on the main thread 39 | .observeOn(AndroidSchedulers.mainThread()) 40 | .subscribe(getObserver()) 41 | } 42 | 43 | 44 | private fun getObservable(): Observable { 45 | return Observable.create { emitter -> 46 | // send events with simulated time wait 47 | Thread.sleep(0) 48 | emitter.onNext(1) // skip 49 | emitter.onNext(2) // deliver 50 | Thread.sleep(505) 51 | emitter.onNext(3) // skip 52 | Thread.sleep(99) 53 | emitter.onNext(4) // skip 54 | Thread.sleep(100) 55 | emitter.onNext(5) // skip 56 | emitter.onNext(6) // deliver 57 | Thread.sleep(305) 58 | emitter.onNext(7) // deliver 59 | Thread.sleep(510) 60 | emitter.onComplete() 61 | } 62 | } 63 | 64 | private fun getObserver(): Observer { 65 | return object : Observer { 66 | 67 | override fun onSubscribe(d: Disposable) { 68 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 69 | } 70 | 71 | override fun onNext(value: Int) { 72 | textView.append(" onNext : ") 73 | textView.append(Constant().LINE_SEPARATOR) 74 | textView.append(" value : " + value!!) 75 | textView.append(Constant().LINE_SEPARATOR) 76 | Log.d(Constant().TAG, " onNext ") 77 | Log.d(Constant().TAG, " value : " + value) 78 | } 79 | 80 | override fun onError(e: Throwable) { 81 | textView.append(" onError : " + e.message) 82 | textView.append(Constant().LINE_SEPARATOR) 83 | Log.d(Constant().TAG, " onError : " + e.message) 84 | } 85 | 86 | override fun onComplete() { 87 | textView.append(" onComplete") 88 | textView.append(Constant().LINE_SEPARATOR) 89 | Log.d(Constant().TAG, " onComplete") 90 | } 91 | } 92 | } 93 | 94 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/TimerOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | import java.util.concurrent.TimeUnit 15 | 16 | /** 17 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 12/12/2017 (MM/DD/YYYY ) 18 | */ 19 | class TimerOperatorActivity : AppCompatActivity() { 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_example_operator) 23 | 24 | btn.setOnClickListener({ executeTimerOperator() }) 25 | } 26 | 27 | /* 28 | * simple example using timer to do something after 2 second 29 | */ 30 | private fun executeTimerOperator() { 31 | getObservable() 32 | // Run on a background thread 33 | .subscribeOn(Schedulers.io()) 34 | // Be notified on the main thread 35 | .observeOn(AndroidSchedulers.mainThread()) 36 | .subscribe(getObserver()) 37 | 38 | } 39 | 40 | private fun getObservable(): Observable { 41 | return Observable.timer(2, TimeUnit.SECONDS) 42 | } 43 | 44 | private fun getObserver(): Observer { 45 | return object : Observer { 46 | 47 | override fun onSubscribe(d: Disposable) { 48 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 49 | } 50 | 51 | override fun onNext(value: Long) { 52 | textView.append(" onNext : value : " + value) 53 | textView.append(Constant().LINE_SEPARATOR) 54 | Log.d(Constant().TAG, " onNext : value : " + value) 55 | } 56 | 57 | override fun onError(e: Throwable) { 58 | textView.append(" onError : " + e.message) 59 | textView.append(Constant().LINE_SEPARATOR) 60 | Log.d(Constant().TAG, " onError : " + e.message) 61 | } 62 | 63 | override fun onComplete() { 64 | textView.append(" onComplete") 65 | textView.append(Constant().LINE_SEPARATOR) 66 | Log.d(Constant().TAG, " onComplete") 67 | } 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/combiningOperators/CombineLatestOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.combiningOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import com.freeankit.rxkotlinoperators.R 6 | import kotlinx.android.synthetic.main.activity_example_operator.* 7 | 8 | /** 9 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 23/03/2018 (MM/DD/YYYY) 10 | */ 11 | class CombineLatestOperatorActivity : AppCompatActivity() { 12 | override fun onCreate(savedInstanceState: Bundle?) { 13 | super.onCreate(savedInstanceState) 14 | setContentView(R.layout.activity_example_operator) 15 | 16 | btn.setOnClickListener({ 17 | //executeCombineLatestOperator() }) 18 | }) 19 | } 20 | 21 | // private fun executeCombineLatestOperator() { 22 | // val builder = StringBuilder() 23 | // //combine two observables 24 | // //notice the sizes of Observables and the output 25 | // 26 | // 27 | // Observable.combineLatest( 28 | // getBoyNames(), 29 | // getRollNumber(), 30 | // { s, i -> s + " -> " + i }) 31 | // .observeOn(AndroidSchedulers.mainThread()) 32 | // .subscribe(object : Observer { 33 | // internal lateinit var d: Disposable 34 | // 35 | // override fun onSubscribe(d: Disposable) { 36 | // this.d = d 37 | // } 38 | // 39 | // override fun onNext(s: String) { 40 | // builder.append(s).append(Constant().LINE_SEPARATOR) 41 | // } 42 | // 43 | // override fun onError(e: Throwable) { 44 | // Log.e(Constant().TAG, "onError: ", e) 45 | // if (!d.isDisposed) 46 | // d.isDisposed 47 | // } 48 | // 49 | // override fun onComplete() { 50 | // textView.text = builder.toString() 51 | // if (!d.isDisposed) 52 | // d.isDisposed 53 | // } 54 | // }) 55 | // } 56 | // 57 | // private fun getBoyNames(): Observable { 58 | // return Observable.fromArray("JP", "Bala", "Sahan", "Ankit") 59 | // } 60 | // 61 | // private fun getRollNumber(): Observable { 62 | // return Observable.fromArray(12, 15, 63, 15, 54, 92) 63 | // } 64 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/combiningOperators/MergeOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.combiningOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.disposables.Disposable 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 10/01/2018 (MM/DD/YYYY ) 15 | */ 16 | class MergeOperatorActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeMergeOperator() }) 22 | } 23 | 24 | /* 25 | * Using merge operator to combine Observable : merge does not maintain 26 | * the order of Observable. 27 | * It will emit all the 7 values may not be in order 28 | * Ex - "A1", "B1", "A2", "A3", "A4", "B2", "B3" - may be anything 29 | */ 30 | 31 | private fun executeMergeOperator() { 32 | val aStrings = arrayOf("A1", "A2", "A3", "A4") 33 | val bStrings = arrayOf("B1", "B2", "B3") 34 | 35 | val aObservable = Observable.fromArray(*aStrings) 36 | val bObservable = Observable.fromArray(*bStrings) 37 | 38 | Observable.merge(aObservable, bObservable) 39 | .subscribe(getObserver()) 40 | 41 | } 42 | 43 | private fun getObserver(): Observer { 44 | return object : Observer { 45 | 46 | override fun onSubscribe(d: Disposable) { 47 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 48 | } 49 | 50 | override fun onNext(value: String) { 51 | textView.append(" onNext : value : " + value) 52 | textView.append(Constant().LINE_SEPARATOR) 53 | Log.d(Constant().TAG, " onNext : value : " + value) 54 | } 55 | 56 | override fun onError(e: Throwable) { 57 | textView.append(" onError : " + e.message) 58 | textView.append(Constant().LINE_SEPARATOR) 59 | Log.d(Constant().TAG, " onError : " + e.message) 60 | } 61 | 62 | override fun onComplete() { 63 | textView.append(" onComplete") 64 | textView.append(Constant().LINE_SEPARATOR) 65 | Log.d(Constant().TAG, " onComplete") 66 | } 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/combiningOperators/ZipOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.combiningOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import android.view.View 7 | import com.freeankit.rxkotlinoperators.R 8 | import com.freeankit.rxkotlinoperators.model.User 9 | import com.freeankit.rxkotlinoperators.utils.Constant 10 | import com.freeankit.rxkotlinoperators.utils.Utils 11 | import io.reactivex.Observable 12 | import io.reactivex.Observer 13 | import io.reactivex.android.schedulers.AndroidSchedulers 14 | import io.reactivex.disposables.Disposable 15 | import io.reactivex.functions.BiFunction 16 | import io.reactivex.schedulers.Schedulers 17 | import kotlinx.android.synthetic.main.activity_example_operator.* 18 | 19 | /** 20 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/12/2017 (MM/DD/YYYY ) 21 | */ 22 | class ZipOperatorActivity : AppCompatActivity() { 23 | override fun onCreate(savedInstanceState: Bundle?) { 24 | super.onCreate(savedInstanceState) 25 | setContentView(R.layout.activity_example_operator) 26 | 27 | btn.setOnClickListener({ executeZipOperator() }) 28 | } 29 | 30 | /* 31 | * Here we are getting two user list 32 | * One, the list of Kotlin fans 33 | * Another one, the list of Java fans 34 | * Then we are finding the list of users who loves both 35 | */ 36 | private fun executeZipOperator() { 37 | progress.visibility = View.VISIBLE 38 | Observable.zip, List, List>(getKotlinFansObservable(), getJavaFansObservable(), 39 | BiFunction, List, List> { kotlinFans, javaFans -> Utils().filterUserWhoLovesBoth(kotlinFans, javaFans) }) 40 | // Run on a background thread 41 | .subscribeOn(Schedulers.io()) 42 | // Be notified on the main thread 43 | .observeOn(AndroidSchedulers.mainThread()) 44 | .subscribe(getObserver()) 45 | } 46 | 47 | private fun getKotlinFansObservable(): Observable> { 48 | return Observable.create> { e -> 49 | if (!e.isDisposed) { 50 | e.onNext(Utils().getUserListWhoLovesKotlin()) 51 | e.onComplete() 52 | } 53 | } 54 | } 55 | 56 | private fun getJavaFansObservable(): Observable> { 57 | return Observable.create> { e -> 58 | if (!e.isDisposed) { 59 | e.onNext(Utils().getUserListWhoLovesJava()) 60 | e.onComplete() 61 | } 62 | } 63 | } 64 | 65 | private fun getObserver(): Observer> { 66 | return object : Observer> { 67 | 68 | override fun onSubscribe(d: Disposable) { 69 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 70 | } 71 | 72 | override fun onNext(userList: List) { 73 | textView.append(" onNext") 74 | textView.append(Constant().LINE_SEPARATOR) 75 | for (user in userList) { 76 | textView.append(" loginName : " + user.login) 77 | textView.append(Constant().LINE_SEPARATOR) 78 | } 79 | Log.d(Constant().TAG, " onNext : " + userList.size) 80 | progress.visibility = View.GONE 81 | } 82 | 83 | override fun onError(e: Throwable) { 84 | textView.append(" onError : " + e.message) 85 | textView.append(Constant().LINE_SEPARATOR) 86 | Log.d(Constant().TAG, " onError : " + e.message) 87 | progress.visibility = View.GONE 88 | } 89 | 90 | override fun onComplete() { 91 | textView.append(" onComplete") 92 | textView.append(Constant().LINE_SEPARATOR) 93 | Log.d(Constant().TAG, " onComplete") 94 | progress.visibility = View.GONE 95 | } 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/conditionalOperators/AmbOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.conditionalOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import com.freeankit.rxkotlinoperators.R 6 | import kotlinx.android.synthetic.main.activity_example_operator.* 7 | 8 | /** 9 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 25/02/2018 (MM/DD/YYYY) 10 | */ 11 | class AmbOperatorActivity : AppCompatActivity() { 12 | 13 | override fun onCreate(savedInstanceState: Bundle?) { 14 | super.onCreate(savedInstanceState) 15 | setContentView(R.layout.activity_example_operator) 16 | 17 | btn.setOnClickListener({ executeAmbOperator() }) 18 | } 19 | 20 | private fun executeAmbOperator() { 21 | // TODO 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/connectableOperators/PublishSubjectOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.connectableOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observer 9 | import io.reactivex.disposables.Disposable 10 | import io.reactivex.subjects.PublishSubject 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 29/01/2018 (MM/DD/YYYY ) 15 | */ 16 | class PublishSubjectOperatorActivity:AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executePublishSubjectOperator() }) 22 | } 23 | 24 | 25 | /* PublishSubject emits to an observer only those items that are emitted 26 | * by the source Observable, subsequent to the time of the subscription. 27 | */ 28 | private fun executePublishSubjectOperator() { 29 | val source = PublishSubject.create() 30 | 31 | source.subscribe(getFirstObserver()) // it will get 1, 2, 3, 4 and onComplete 32 | 33 | source.onNext(1) 34 | source.onNext(2) 35 | source.onNext(3) 36 | 37 | /* 38 | * it will emit 4 and onComplete for second observer also. 39 | */ 40 | source.subscribe(getSecondObserver()) 41 | 42 | source.onNext(4) 43 | source.onComplete() 44 | 45 | } 46 | 47 | private fun getFirstObserver(): Observer { 48 | return object : Observer { 49 | 50 | override fun onSubscribe(d: Disposable) { 51 | Log.d(Constant().TAG, " First onSubscribe : " + d.isDisposed) 52 | } 53 | 54 | override fun onNext(value: Int) { 55 | textView.append(" First onNext : value : " + value!!) 56 | textView.append(Constant().LINE_SEPARATOR) 57 | Log.d(Constant().TAG, " First onNext value : " + value) 58 | } 59 | 60 | override fun onError(e: Throwable) { 61 | textView.append(" First onError : " + e.message) 62 | textView.append(Constant().LINE_SEPARATOR) 63 | Log.d(Constant().TAG, " First onError : " + e.message) 64 | } 65 | 66 | override fun onComplete() { 67 | textView.append(" First onComplete") 68 | textView.append(Constant().LINE_SEPARATOR) 69 | Log.d(Constant().TAG, " First onComplete") 70 | } 71 | } 72 | } 73 | 74 | private fun getSecondObserver(): Observer { 75 | return object : Observer { 76 | 77 | override fun onSubscribe(d: Disposable) { 78 | textView.append(" Second onSubscribe : isDisposed :" + d.isDisposed) 79 | Log.d(Constant().TAG, " Second onSubscribe : " + d.isDisposed) 80 | textView.append(Constant().LINE_SEPARATOR) 81 | } 82 | 83 | override fun onNext(value: Int) { 84 | textView.append(" Second onNext : value : " + value!!) 85 | textView.append(Constant().LINE_SEPARATOR) 86 | Log.d(Constant().TAG, " Second onNext value : " + value) 87 | } 88 | 89 | override fun onError(e: Throwable) { 90 | textView.append(" Second onError : " + e.message) 91 | textView.append(Constant().LINE_SEPARATOR) 92 | Log.d(Constant().TAG, " Second onError : " + e.message) 93 | } 94 | 95 | override fun onComplete() { 96 | textView.append(" Second onComplete") 97 | textView.append(Constant().LINE_SEPARATOR) 98 | Log.d(Constant().TAG, " Second onComplete") 99 | } 100 | } 101 | } 102 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/connectableOperators/ReplayOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.connectableOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observer 9 | import io.reactivex.disposables.Disposable 10 | import io.reactivex.subjects.PublishSubject 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/01/2018 (MM/DD/YYYY ) 15 | */ 16 | class ReplayOperatorActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeReplayOperator() }) 22 | } 23 | 24 | 25 | /* Using replay operator, replay ensure that all observers see the same sequence 26 | * of emitted items, even if they subscribe after the Observable has begun emitting items 27 | */ 28 | private fun executeReplayOperator() { 29 | val source = PublishSubject.create() 30 | val connectableObservable = source.replay(3) // bufferSize = 3 to retain 3 values to replay 31 | connectableObservable.connect() // connecting the connectableObservable 32 | 33 | connectableObservable.subscribe(getFirstObserver()) 34 | 35 | source.onNext(1) 36 | source.onNext(2) 37 | source.onNext(3) 38 | source.onNext(4) 39 | source.onComplete() 40 | 41 | /* 42 | * it will emit 2, 3, 4 as (count = 3), retains the 3 values for replay 43 | */ 44 | connectableObservable.subscribe(getSecondObserver()) 45 | 46 | } 47 | 48 | private fun getFirstObserver(): Observer { 49 | return object : Observer { 50 | 51 | override fun onSubscribe(d: Disposable) { 52 | Log.d(Constant().TAG, " First onSubscribe : " + d.isDisposed) 53 | } 54 | 55 | override fun onNext(value: Int) { 56 | textView.append(" First onNext : value : " + value) 57 | textView.append(Constant().LINE_SEPARATOR) 58 | Log.d(Constant().TAG, " First onNext value : " + value) 59 | } 60 | 61 | override fun onError(e: Throwable) { 62 | textView.append(" First onError : " + e.message) 63 | textView.append(Constant().LINE_SEPARATOR) 64 | Log.d(Constant().TAG, " First onError : " + e.message) 65 | } 66 | 67 | override fun onComplete() { 68 | textView.append(" First onComplete") 69 | textView.append(Constant().LINE_SEPARATOR) 70 | Log.d(Constant().TAG, " First onComplete") 71 | } 72 | } 73 | } 74 | 75 | private fun getSecondObserver(): Observer { 76 | return object : Observer { 77 | 78 | override fun onSubscribe(d: Disposable) { 79 | textView.append(" Second onSubscribe : isDisposed :" + d.isDisposed) 80 | Log.d(Constant().TAG, " Second onSubscribe : " + d.isDisposed) 81 | textView.append(Constant().LINE_SEPARATOR) 82 | } 83 | 84 | override fun onNext(value: Int) { 85 | textView.append(" Second onNext : value : " + value!!) 86 | textView.append(Constant().LINE_SEPARATOR) 87 | Log.d(Constant().TAG, " Second onNext value : " + value) 88 | } 89 | 90 | override fun onError(e: Throwable) { 91 | textView.append(" Second onError : " + e.message) 92 | Log.d(Constant().TAG, " Second onError : " + e.message) 93 | } 94 | 95 | override fun onComplete() { 96 | textView.append(" Second onComplete") 97 | Log.d(Constant().TAG, " Second onComplete") 98 | } 99 | } 100 | } 101 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/connectableOperators/ReplaySubjectOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.connectableOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observer 9 | import io.reactivex.disposables.Disposable 10 | import io.reactivex.subjects.ReplaySubject 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 29/01/2018 (MM/DD/YYYY ) 15 | */ 16 | class ReplaySubjectOperatorActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeReplaySubjectOperator() }) 22 | } 23 | 24 | /* ReplaySubject emits to any observer all of the items that were emitted 25 | * by the source Observable, regardless of when the observer subscribes. 26 | */ 27 | private fun executeReplaySubjectOperator() { 28 | val source = ReplaySubject.create() 29 | 30 | source.subscribe(getFirstObserver()) // it will get 1, 2, 3, 4 31 | 32 | source.onNext(1) 33 | source.onNext(2) 34 | source.onNext(3) 35 | source.onNext(4) 36 | source.onComplete() 37 | 38 | /* 39 | * it will emit 1, 2, 3, 4 for second observer also as we have used replay 40 | */ 41 | source.subscribe(getSecondObserver()) 42 | 43 | } 44 | 45 | private fun getFirstObserver(): Observer { 46 | return object : Observer { 47 | 48 | override fun onSubscribe(d: Disposable) { 49 | Log.d(Constant().TAG, " First onSubscribe : " + d.isDisposed) 50 | } 51 | 52 | override fun onNext(value: Int) { 53 | textView.append(" First onNext : value : " + value!!) 54 | textView.append(Constant().LINE_SEPARATOR) 55 | Log.d(Constant().TAG, " First onNext value : " + value) 56 | } 57 | 58 | override fun onError(e: Throwable) { 59 | textView.append(" First onError : " + e.message) 60 | textView.append(Constant().LINE_SEPARATOR) 61 | Log.d(Constant().TAG, " First onError : " + e.message) 62 | } 63 | 64 | override fun onComplete() { 65 | textView.append(" First onComplete") 66 | textView.append(Constant().LINE_SEPARATOR) 67 | Log.d(Constant().TAG, " First onComplete") 68 | } 69 | } 70 | } 71 | 72 | private fun getSecondObserver(): Observer { 73 | return object : Observer { 74 | 75 | override fun onSubscribe(d: Disposable) { 76 | textView.append(" Second onSubscribe : isDisposed :" + d.isDisposed) 77 | Log.d(Constant().TAG, " Second onSubscribe : " + d.isDisposed) 78 | textView.append(Constant().LINE_SEPARATOR) 79 | } 80 | 81 | override fun onNext(value: Int) { 82 | textView.append(" Second onNext : value : " + value!!) 83 | textView.append(Constant().LINE_SEPARATOR) 84 | Log.d(Constant().TAG, " Second onNext value : " + value) 85 | } 86 | 87 | override fun onError(e: Throwable) { 88 | textView.append(" Second onError : " + e.message) 89 | textView.append(Constant().LINE_SEPARATOR) 90 | Log.d(Constant().TAG, " Second onError : " + e.message) 91 | } 92 | 93 | override fun onComplete() { 94 | textView.append(" Second onComplete") 95 | textView.append(Constant().LINE_SEPARATOR) 96 | Log.d(Constant().TAG, " Second onComplete") 97 | } 98 | } 99 | } 100 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/creatingOperators/DeferOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.creatingOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.model.Bike 8 | import com.freeankit.rxkotlinoperators.utils.Constant 9 | import io.reactivex.Observer 10 | import io.reactivex.disposables.Disposable 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 11/01/2018 (MM/DD/YYYY ) 15 | */ 16 | class DeferOperatorActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeDeferOperator() }) 22 | } 23 | 24 | 25 | /* 26 | * Defer used for Deferring Observable code until subscription in RxJava 27 | */ 28 | private fun executeDeferOperator() { 29 | val bike = Bike() 30 | 31 | val brandDeferObservable = bike.brandDeferObservable() 32 | 33 | bike.setBrand("Harley Davidson") // Even if we are setting the brand after creating Observable 34 | // we will get the brand as Harley Davidson. 35 | // If we had not used defer, we would have got null as the brand. 36 | 37 | brandDeferObservable.subscribe(getObserver()) 38 | 39 | } 40 | 41 | private fun getObserver(): Observer { 42 | return object : Observer { 43 | 44 | override fun onSubscribe(d: Disposable) { 45 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 46 | } 47 | 48 | override fun onNext(value: String) { 49 | textView.append(" onNext : value : " + value) 50 | textView.append(Constant().LINE_SEPARATOR) 51 | Log.d(Constant().TAG, " onNext : value : " + value) 52 | } 53 | 54 | override fun onError(e: Throwable) { 55 | textView.append(" onError : " + e.message) 56 | textView.append(Constant().LINE_SEPARATOR) 57 | Log.d(Constant().TAG, " onError : " + e.message) 58 | } 59 | 60 | override fun onComplete() { 61 | textView.append(" onComplete") 62 | textView.append(Constant().LINE_SEPARATOR) 63 | Log.d(Constant().TAG, " onComplete") 64 | } 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/creatingOperators/IntervalOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.creatingOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | import java.util.concurrent.TimeUnit 15 | 16 | /** 17 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 15/12/2017 (MM/DD/YYYY ) 18 | */ 19 | class IntervalOperatorActivity : AppCompatActivity() { 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_example_operator) 23 | 24 | btn.setOnClickListener({ executeIntervalOperator() }) 25 | } 26 | 27 | /* 28 | * simple example using interval to run task at an interval of 2 sec 29 | * which start immediately 30 | */ 31 | private fun executeIntervalOperator() { 32 | getObservable() 33 | // Run on a background thread 34 | .subscribeOn(Schedulers.io()) 35 | // Be notified on the main thread 36 | .observeOn(AndroidSchedulers.mainThread()) 37 | .subscribe(getObserver()) 38 | 39 | } 40 | 41 | private fun getObservable(): Observable { 42 | return Observable.interval(1, 2, TimeUnit.SECONDS) 43 | } 44 | 45 | private fun getObserver(): Observer { 46 | return object : Observer { 47 | 48 | override fun onSubscribe(d: Disposable) { 49 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 50 | } 51 | 52 | override fun onNext(value: Long) { 53 | textView.append(" onNext : value : " + value) 54 | textView.append(Constant().LINE_SEPARATOR) 55 | Log.d(Constant().TAG, " onNext : value : " + value) 56 | } 57 | 58 | override fun onError(e: Throwable) { 59 | textView.append(" onError : " + e.message) 60 | textView.append(Constant().LINE_SEPARATOR) 61 | Log.d(Constant().TAG, " onError : " + e.message) 62 | } 63 | 64 | override fun onComplete() { 65 | textView.append(" onComplete") 66 | textView.append(Constant().LINE_SEPARATOR) 67 | Log.d(Constant().TAG, " onComplete") 68 | } 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/errorHandlingOperators/RetryOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.errorHandlingOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | import java.util.concurrent.TimeUnit 15 | 16 | /** 17 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 22/04/2018 (MM/DD/YYYY) 18 | */ 19 | class RetryOperatorActivity : AppCompatActivity() { 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_example_operator) 23 | 24 | btn.setOnClickListener({ executeRetryOperator() }) 25 | } 26 | 27 | 28 | // if a source Observable emits an error, resubscribe to it in the hopes that it will complete without error 29 | 30 | private fun executeRetryOperator() { 31 | getObservable() 32 | .retry() 33 | // .retryWhen(source -> source.delay(100, TimeUnit.MILLISECONDS)) 34 | .debounce(500, TimeUnit.MILLISECONDS) 35 | // Run on a background thread 36 | .subscribeOn(Schedulers.io()) 37 | // Be notified on the main thread 38 | .observeOn(AndroidSchedulers.mainThread()) 39 | .subscribe(getObserver()) 40 | } 41 | 42 | private fun getObservable(): Observable { 43 | return Observable.create { emitter -> 44 | // send events with simulated time wait 45 | emitter.onNext(1) // skip 46 | Thread.sleep(400) 47 | emitter.onNext(2) // deliver 48 | Thread.sleep(505) 49 | emitter.onNext(3) // skip 50 | Thread.sleep(100) 51 | emitter.onNext(4) // deliver kj h ihioh 52 | Thread.sleep(605) /////////////// 53 | emitter.onNext(5) // deliver 54 | Thread.sleep(510) 55 | emitter.onComplete() 56 | } 57 | } 58 | 59 | 60 | private fun getObserver(): Observer { 61 | return object : Observer { 62 | override fun onSubscribe(d: Disposable) { 63 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 64 | } 65 | 66 | override fun onNext(value: Int) { 67 | textView.append(" onNext : value : $value") 68 | textView.append(Constant().LINE_SEPARATOR) 69 | Log.d(Constant().TAG, " onNext value : $value") 70 | } 71 | 72 | override fun onError(e: Throwable) { 73 | textView.append(" onError : " + e.message) 74 | textView.append(Constant().LINE_SEPARATOR) 75 | Log.d(Constant().TAG, " onError : " + e.message) 76 | } 77 | 78 | override fun onComplete() { 79 | textView.append(" onComplete") 80 | textView.append(Constant().LINE_SEPARATOR) 81 | Log.d(Constant().TAG, " onComplete") 82 | } 83 | } 84 | } 85 | 86 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/filteringOperators/DebounceOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.filteringOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | import java.util.concurrent.TimeUnit 15 | 16 | /** 17 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 15/12/2017 (MM/DD/YYYY ) 18 | */ 19 | class DebounceOperatorActivity : AppCompatActivity() { 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_example_operator) 23 | 24 | btn.setOnClickListener({ executeDebounceOperator() }) 25 | } 26 | 27 | /* 28 | * Using debounce() -> only emit an item from an Observable if a particular time-span has 29 | * passed without it emitting another item, so it will emit 2, 4, 5 as we have simulated it. 30 | */ 31 | private fun executeDebounceOperator() { 32 | getObservable() 33 | .debounce(500, TimeUnit.MILLISECONDS) 34 | // Run on a background thread 35 | .subscribeOn(Schedulers.io()) 36 | // Be notified on the main thread 37 | .observeOn(AndroidSchedulers.mainThread()) 38 | .subscribe(getObserver()) 39 | 40 | } 41 | 42 | private fun getObservable(): Observable { 43 | return Observable.create { emitter -> 44 | // send events with simulated time wait 45 | emitter.onNext(1) // skip 46 | Thread.sleep(400) 47 | emitter.onNext(2) // deliver 48 | Thread.sleep(505) 49 | emitter.onNext(3) // skip 50 | Thread.sleep(100) 51 | emitter.onNext(4) // deliver kj h ihioh 52 | Thread.sleep(605) /////////////// 53 | emitter.onNext(5) // deliver 54 | Thread.sleep(510) 55 | emitter.onComplete() 56 | } 57 | } 58 | 59 | 60 | private fun getObserver(): Observer { 61 | return object : Observer { 62 | override fun onSubscribe(d: Disposable) { 63 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 64 | } 65 | 66 | override fun onNext(value: Int) { 67 | textView.append(" onNext : value : " + value) 68 | textView.append(Constant().LINE_SEPARATOR) 69 | Log.d(Constant().TAG, " onNext value : " + value) 70 | } 71 | 72 | override fun onError(e: Throwable) { 73 | textView.append(" onError : " + e.message) 74 | textView.append(Constant().LINE_SEPARATOR) 75 | Log.d(Constant().TAG, " onError : " + e.message) 76 | } 77 | 78 | override fun onComplete() { 79 | textView.append(" onComplete") 80 | textView.append(Constant().LINE_SEPARATOR) 81 | Log.d(Constant().TAG, " onComplete") 82 | } 83 | } 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/filteringOperators/DistinctOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.filteringOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.disposables.Disposable 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 25/01/2018 (MM/DD/YYYY ) 15 | */ 16 | class DistinctOperatorActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeDistinctOperator() }) 22 | } 23 | 24 | /* 25 | * distinct() suppresses duplicate items emitted by the source Observable. 26 | */ 27 | private fun executeDistinctOperator() { 28 | 29 | getObservable() 30 | .distinct() 31 | .subscribe(getObserver()) 32 | } 33 | 34 | private fun getObservable(): Observable { 35 | return Observable.just(1, 2, 1, 1, 2, 3, 4, 6, 4) 36 | } 37 | 38 | 39 | private fun getObserver(): Observer { 40 | return object : Observer { 41 | 42 | override fun onSubscribe(d: Disposable) { 43 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 44 | } 45 | 46 | override fun onNext(value: Int) { 47 | textView.append(" onNext : value : " + value!!) 48 | textView.append(Constant().LINE_SEPARATOR) 49 | Log.d(Constant().TAG, " onNext value : " + value) 50 | } 51 | 52 | override fun onError(e: Throwable) { 53 | Log.d(Constant().TAG, " onError : " + e.message) 54 | } 55 | 56 | override fun onComplete() { 57 | Log.d(Constant().TAG, " onComplete") 58 | } 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/filteringOperators/FilterOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.filteringOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.disposables.Disposable 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 02/01/2018 (MM/DD/YYYY ) 15 | */ 16 | class FilterOperatorActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeFilterOperator() }) 22 | } 23 | 24 | /* 25 | * simple example by using filter operator to emit only even value 26 | * 27 | */ 28 | private fun executeFilterOperator() { 29 | 30 | Observable.just(1, 2, 3, 4, 5, 6) 31 | .filter { integer -> integer % 2 == 0 } 32 | .subscribe(getObserver()) 33 | } 34 | 35 | private fun getObserver(): Observer { 36 | return object : Observer { 37 | 38 | override fun onSubscribe(d: Disposable) { 39 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 40 | } 41 | 42 | override fun onNext(value: Int) { 43 | textView.append(" onNext : ") 44 | textView.append(Constant().LINE_SEPARATOR) 45 | textView.append(" value : " + value) 46 | textView.append(Constant().LINE_SEPARATOR) 47 | Log.d(Constant().TAG, " onNext ") 48 | Log.d(Constant().TAG, " value : " + value) 49 | } 50 | 51 | override fun onError(e: Throwable) { 52 | textView.append(" onError : " + e.message) 53 | textView.append(Constant().LINE_SEPARATOR) 54 | Log.d(Constant().TAG, " onError : " + e.message) 55 | } 56 | 57 | override fun onComplete() { 58 | textView.append(" onComplete") 59 | textView.append(Constant().LINE_SEPARATOR) 60 | Log.d(Constant().TAG, " onComplete") 61 | } 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/filteringOperators/SkipOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.filteringOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | 15 | /** 16 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 04/01/2018 (MM/DD/YYYY ) 17 | */ 18 | class SkipOperatorActivity : AppCompatActivity() { 19 | override fun onCreate(savedInstanceState: Bundle?) { 20 | super.onCreate(savedInstanceState) 21 | setContentView(R.layout.activity_example_operator) 22 | 23 | btn.setOnClickListener({ executeSkipOperator() }) 24 | } 25 | 26 | /* Using skip operator, it will not emit 27 | * the first 2 values. 28 | */ 29 | private fun executeSkipOperator() { 30 | getObservable() 31 | // Run on a background thread 32 | .subscribeOn(Schedulers.io()) 33 | // Be notified on the main thread 34 | .observeOn(AndroidSchedulers.mainThread()) 35 | .skip(2) 36 | .subscribe(getObserver()) 37 | } 38 | 39 | private fun getObservable(): Observable { 40 | return Observable.just(1, 2, 3, 4, 5) 41 | } 42 | 43 | private fun getObserver(): Observer { 44 | return object : Observer { 45 | 46 | override fun onSubscribe(d: Disposable) { 47 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 48 | } 49 | 50 | override fun onNext(value: Int) { 51 | textView.append(" onNext : value : " + value) 52 | textView.append(Constant().LINE_SEPARATOR) 53 | Log.d(Constant().TAG, " onNext value : " + value) 54 | } 55 | 56 | override fun onError(e: Throwable) { 57 | textView.append(" onError : " + e.message) 58 | textView.append(Constant().LINE_SEPARATOR) 59 | Log.d(Constant().TAG, " onError : " + e.message) 60 | } 61 | 62 | override fun onComplete() { 63 | textView.append(" onComplete") 64 | textView.append(Constant().LINE_SEPARATOR) 65 | Log.d(Constant().TAG, " onComplete") 66 | } 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/filteringOperators/TakeOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.filteringOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | 15 | /** 16 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 12/12/2017 (MM/DD/YYYY ) 17 | */ 18 | class TakeOperatorActivity : AppCompatActivity() { 19 | override fun onCreate(savedInstanceState: Bundle?) { 20 | super.onCreate(savedInstanceState) 21 | setContentView(R.layout.activity_example_operator) 22 | 23 | btn.setOnClickListener({ executeTakeOperator() }) 24 | } 25 | 26 | /* Using take operator, it only emits 27 | * required number of values. here only 3 out of 5 28 | */ 29 | private fun executeTakeOperator() { 30 | 31 | getObservable() 32 | // Run on a background thread 33 | .subscribeOn(Schedulers.io()) 34 | // Be notified on the main thread 35 | .observeOn(AndroidSchedulers.mainThread()) 36 | .take(3) 37 | .subscribe(getObserver()) 38 | } 39 | 40 | private fun getObservable(): Observable { 41 | return Observable.just(1, 2, 3, 4, 5) 42 | } 43 | 44 | private fun getObserver(): Observer { 45 | return object : Observer { 46 | override fun onSubscribe(d: Disposable) { 47 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 48 | } 49 | 50 | override fun onNext(value: Int) { 51 | textView.append(" onNext : value : " + value) 52 | textView.append(Constant().LINE_SEPARATOR) 53 | Log.d(Constant().TAG, " onNext value : " + value) 54 | } 55 | 56 | override fun onError(e: Throwable) { 57 | textView.append(" onError : " + e.message) 58 | textView.append(Constant().LINE_SEPARATOR) 59 | Log.d(Constant().TAG, " onError : " + e.message) 60 | } 61 | 62 | override fun onComplete() { 63 | textView.append(" onComplete") 64 | textView.append(Constant().LINE_SEPARATOR) 65 | Log.d(Constant().TAG, " onComplete") 66 | } 67 | } 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/mathematicalOperators/ConcatOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.mathematicalOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.disposables.Disposable 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 09/01/2018 (MM/DD/YYYY ) 15 | */ 16 | class ConcatOperatorActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeConcatOperator() }) 22 | } 23 | 24 | /* 25 | * Using concat operator to combine Observable : concat maintain 26 | * the order of Observable. 27 | * It will emit all the 7 values in order 28 | * here - first "A1", "A2", "A3", "A4" and then "B1", "B2", "B3" 29 | * first all from the first Observable and then 30 | * all from the second Observable all in order 31 | */ 32 | private fun executeConcatOperator() { 33 | val aStrings = arrayOf("A1", "A2", "A3", "A4") 34 | val bStrings = arrayOf("B1", "B2", "B3") 35 | 36 | val aObservable = Observable.fromArray(*aStrings) 37 | val bObservable = Observable.fromArray(*bStrings) 38 | 39 | Observable.concat(aObservable, bObservable) 40 | .subscribe(getObserver()) 41 | 42 | } 43 | 44 | private fun getObserver(): Observer { 45 | return object : Observer { 46 | 47 | override fun onSubscribe(d: Disposable) { 48 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 49 | } 50 | 51 | override fun onNext(value: String) { 52 | textView.append(" onNext : value : " + value) 53 | textView.append(Constant().LINE_SEPARATOR) 54 | Log.d(Constant().TAG, " onNext : value : " + value) 55 | } 56 | 57 | override fun onError(e: Throwable) { 58 | textView.append(" onError : " + e.message) 59 | textView.append(Constant().LINE_SEPARATOR) 60 | Log.d(Constant().TAG, " onError : " + e.message) 61 | } 62 | 63 | override fun onComplete() { 64 | textView.append(" onComplete") 65 | textView.append(Constant().LINE_SEPARATOR) 66 | Log.d(Constant().TAG, " onComplete") 67 | } 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/mathematicalOperators/ReduceOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.mathematicalOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.MaybeObserver 9 | import io.reactivex.Observable 10 | import io.reactivex.disposables.Disposable 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 26/12/2017 (MM/DD/YYYY ) 15 | */ 16 | class ReduceOperatorActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeReduceOperator() }) 22 | } 23 | 24 | private fun executeReduceOperator() { 25 | getObservable() 26 | .reduce { t1, t2 -> t1 + t2 } 27 | .subscribe(getObserver()) 28 | 29 | } 30 | 31 | private fun getObservable(): Observable { 32 | return Observable.just(1, 2, 3, 4) 33 | } 34 | 35 | private fun getObserver(): MaybeObserver { 36 | return object : MaybeObserver { 37 | override fun onSubscribe(d: Disposable) { 38 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 39 | } 40 | 41 | override fun onSuccess(value: Int) { 42 | textView.append(" onSuccess : value : " + value) 43 | textView.append(Constant().LINE_SEPARATOR) 44 | Log.d(Constant().TAG, " onSuccess : value : " + value) 45 | } 46 | 47 | override fun onError(e: Throwable) { 48 | textView.append(" onError : " + e.message) 49 | textView.append(Constant().LINE_SEPARATOR) 50 | Log.d(Constant().TAG, " onError : " + e.message) 51 | } 52 | 53 | override fun onComplete() { 54 | textView.append(" onComplete") 55 | textView.append(Constant().LINE_SEPARATOR) 56 | Log.d(Constant().TAG, " onComplete") 57 | } 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/transformingOperators/BufferOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.transformingOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.disposables.Disposable 11 | import kotlinx.android.synthetic.main.activity_example_operator.* 12 | 13 | /** 14 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 29/12/2017 (MM/DD/YYYY ) 15 | */ 16 | class BufferOperatorActivity : AppCompatActivity() { 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_example_operator) 20 | 21 | btn.setOnClickListener({ executeBufferOperator() }) 22 | } 23 | 24 | /* 25 | * simple example using buffer operator - bundles all emitted values into a list 26 | */ 27 | private fun executeBufferOperator() { 28 | 29 | val buffered = getObservable().buffer(3, 1) 30 | 31 | // 3 means, it takes max of three from its start index and create list 32 | // 1 means, it jumps one step every time 33 | // so the it gives the following list 34 | // 1 - one, two, three 35 | // 2 - two, three, four 36 | // 3 - three, four, five 37 | // 4 - four, five 38 | // 5 - five 39 | 40 | buffered.subscribe(getObserver()) 41 | } 42 | 43 | private fun getObservable(): Observable { 44 | return Observable.just("one", "two", "three", "four", "five") 45 | } 46 | 47 | private fun getObserver(): Observer> { 48 | return object : Observer> { 49 | 50 | override fun onSubscribe(d: Disposable) { 51 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 52 | } 53 | 54 | override fun onNext(stringList: List) { 55 | textView.append(" onNext size : " + stringList.size) 56 | textView.append(Constant().LINE_SEPARATOR) 57 | Log.d(Constant().TAG, " onNext : size :" + stringList.size) 58 | for (value in stringList) { 59 | textView.append(" value : " + value) 60 | textView.append(Constant().LINE_SEPARATOR) 61 | Log.d(Constant().TAG, " : value :" + value) 62 | } 63 | 64 | } 65 | 66 | override fun onError(e: Throwable) { 67 | textView.append(" onError : " + e.message) 68 | textView.append(Constant().LINE_SEPARATOR) 69 | Log.d(Constant().TAG, " onError : " + e.message) 70 | } 71 | 72 | override fun onComplete() { 73 | textView.append(" onComplete") 74 | textView.append(Constant().LINE_SEPARATOR) 75 | Log.d(Constant().TAG, " onComplete") 76 | } 77 | } 78 | } 79 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/transformingOperators/FlatMapOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.transformingOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | 15 | /** 16 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 13/12/2017 (MM/DD/YYYY ) 17 | */ 18 | class FlatMapOperatorActivity : AppCompatActivity() { 19 | override fun onCreate(savedInstanceState: Bundle?) { 20 | super.onCreate(savedInstanceState) 21 | setContentView(R.layout.activity_example_operator) 22 | 23 | btn.setOnClickListener({ executeFlatMapOperator() }) 24 | } 25 | 26 | /*Transform the items emitted by an Observable into Observables, 27 | then flatten the emissions from those into a single Observable*/ 28 | private fun executeFlatMapOperator() { 29 | getObservable() 30 | .subscribeOn(Schedulers.io()) 31 | // Be notified on the main thread 32 | .observeOn(AndroidSchedulers.mainThread()) 33 | .flatMap { integer -> multiplyInt(integer, 2) } 34 | .flatMap { integer -> multiplyInt(integer, 3) } 35 | .flatMap { integer -> multiplyInt(integer, 5) } 36 | .subscribe(getObserver()) 37 | 38 | 39 | } 40 | 41 | private fun getObservable(): Observable { 42 | return Observable.just(1, 2, 3, 4, 5) 43 | } 44 | 45 | private fun getObserver(): Observer { 46 | return object : Observer { 47 | override fun onSubscribe(d: Disposable) { 48 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 49 | } 50 | 51 | override fun onNext(value: Int) { 52 | textView.append(" onNext : value : " + value) 53 | textView.append(Constant().LINE_SEPARATOR) 54 | Log.d(Constant().TAG, " onNext value : " + value) 55 | } 56 | 57 | override fun onError(e: Throwable) { 58 | textView.append(" onError : " + e.message) 59 | textView.append(Constant().LINE_SEPARATOR) 60 | Log.d(Constant().TAG, " onError : " + e.message) 61 | } 62 | 63 | override fun onComplete() { 64 | textView.append(" onComplete") 65 | textView.append(Constant().LINE_SEPARATOR) 66 | Log.d(Constant().TAG, " onComplete") 67 | } 68 | } 69 | } 70 | 71 | private fun multiplyInt(integer: Int?, mulplier: Int): Observable { 72 | //simulating a heavy duty computational expensive operation 73 | for (i in 0..100) { 74 | } 75 | return Observable.just(integer!! * mulplier) 76 | } 77 | 78 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/transformingOperators/MapOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.transformingOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import android.view.View 7 | import com.freeankit.rxkotlinoperators.R 8 | import com.freeankit.rxkotlinoperators.model.ApiUser 9 | import com.freeankit.rxkotlinoperators.model.User 10 | import com.freeankit.rxkotlinoperators.utils.Constant 11 | import com.freeankit.rxkotlinoperators.utils.Utils 12 | import io.reactivex.Observable 13 | import io.reactivex.Observer 14 | import io.reactivex.android.schedulers.AndroidSchedulers 15 | import io.reactivex.disposables.Disposable 16 | import io.reactivex.schedulers.Schedulers 17 | import kotlinx.android.synthetic.main.activity_example_operator.* 18 | 19 | 20 | /** 21 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/12/2017 (MM/DD/YYYY ) 22 | */ 23 | class MapOperatorActivity : AppCompatActivity() { 24 | override fun onCreate(savedInstanceState: Bundle?) { 25 | super.onCreate(savedInstanceState) 26 | setContentView(R.layout.activity_example_operator) 27 | 28 | btn.setOnClickListener({ executeMapOperator() }) 29 | } 30 | 31 | /* 32 | * Here we are getting ApiUser Object from api server 33 | * then we are converting it into User Object because 34 | * may be our database support User Not ApiUser Object 35 | * Here we are using Map Operator to do that 36 | */ 37 | private fun executeMapOperator() { 38 | progress.visibility = View.VISIBLE 39 | getObservable() 40 | // Run on a background thread 41 | .subscribeOn(Schedulers.io()) 42 | // Be notified on the main thread 43 | .observeOn(AndroidSchedulers.mainThread()) 44 | .map { apiUser -> Utils().convertApiUserListToUserList(apiUser) } 45 | .subscribe(getObserver()) 46 | 47 | } 48 | 49 | private fun getObservable(): Observable> { 50 | return Observable.create> { e -> 51 | if (!e.isDisposed) { 52 | e.onNext(Utils().getApiUserList()) 53 | e.onComplete() 54 | } 55 | } 56 | } 57 | 58 | private fun getObserver(): Observer> { 59 | return object : Observer> { 60 | 61 | override fun onSubscribe(d: Disposable) { 62 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 63 | } 64 | 65 | override fun onNext(userList: List) { 66 | textView.append(" onNext") 67 | textView.append(Constant().LINE_SEPARATOR) 68 | for (user in userList) { 69 | textView.append(" login Name : " + user.login) 70 | textView.append(Constant().LINE_SEPARATOR) 71 | } 72 | Log.d(Constant().TAG, " onNext : " + userList.size) 73 | progress.visibility = View.GONE 74 | } 75 | 76 | override fun onError(e: Throwable) { 77 | textView.append(" onError : " + e.message) 78 | textView.append(Constant().LINE_SEPARATOR) 79 | Log.d(Constant().TAG, " onError : " + e.message) 80 | progress.visibility = View.GONE 81 | } 82 | 83 | override fun onComplete() { 84 | textView.append(" onComplete") 85 | textView.append(Constant().LINE_SEPARATOR) 86 | Log.d(Constant().TAG, " onComplete") 87 | progress.visibility = View.GONE 88 | } 89 | } 90 | } 91 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/transformingOperators/ScanOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.transformingOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | 15 | /** 16 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 05/01/2018 (MM/DD/YYYY ) 17 | */ 18 | class ScanOperatorActivity : AppCompatActivity() { 19 | override fun onCreate(savedInstanceState: Bundle?) { 20 | super.onCreate(savedInstanceState) 21 | setContentView(R.layout.activity_example_operator) 22 | 23 | btn.setOnClickListener({ executeScanOperator() }) 24 | } 25 | 26 | /* Using scan operator, it sends also the previous result */ 27 | private fun executeScanOperator() { 28 | getObservable() 29 | // Run on a background thread 30 | .subscribeOn(Schedulers.io()) 31 | // Be notified on the main thread 32 | .observeOn(AndroidSchedulers.mainThread()) 33 | .scan { int1, int2 -> int1 + int2 } 34 | .subscribe(getObserver()) 35 | } 36 | 37 | private fun getObservable(): Observable { 38 | return Observable.just(1, 2, 3, 4, 5) 39 | } 40 | 41 | private fun getObserver(): Observer { 42 | return object : Observer { 43 | 44 | override fun onSubscribe(d: Disposable) { 45 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 46 | } 47 | 48 | override fun onNext(value: Int) { 49 | textView.append(" onNext : value : " + value) 50 | textView.append(Constant().LINE_SEPARATOR) 51 | Log.d(Constant().TAG, " onNext value : " + value) 52 | } 53 | 54 | override fun onError(e: Throwable) { 55 | textView.append(" onError : " + e.message) 56 | textView.append(Constant().LINE_SEPARATOR) 57 | Log.d(Constant().TAG, " onError : " + e.message) 58 | } 59 | 60 | override fun onComplete() { 61 | textView.append(" onComplete") 62 | textView.append(Constant().LINE_SEPARATOR) 63 | Log.d(Constant().TAG, " onComplete") 64 | } 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/transformingOperators/WindowOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.transformingOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.android.schedulers.AndroidSchedulers 10 | import io.reactivex.functions.Consumer 11 | import io.reactivex.schedulers.Schedulers 12 | import kotlinx.android.synthetic.main.activity_example_operator.* 13 | import java.util.concurrent.TimeUnit 14 | 15 | /** 16 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 19/02/2018 (MM/DD/YYYY ) 17 | */ 18 | class WindowOperatorActivity : AppCompatActivity() { 19 | override fun onCreate(savedInstanceState: Bundle?) { 20 | super.onCreate(savedInstanceState) 21 | setContentView(R.layout.activity_example_operator) 22 | 23 | btn.setOnClickListener({ executeWindowOperator() }) 24 | } 25 | 26 | 27 | /* 28 | * Example using window operator -> It periodically 29 | * subdivide items from an Observable into 30 | * Observable windows and emit these windows rather than 31 | * emitting the items one at a time 32 | */ 33 | 34 | private fun executeWindowOperator() { 35 | Observable.interval(1, TimeUnit.SECONDS).take(12) 36 | .window(3, TimeUnit.SECONDS) 37 | .subscribeOn(Schedulers.io()) 38 | .observeOn(AndroidSchedulers.mainThread()) 39 | .subscribe(getConsumer()) 40 | } 41 | 42 | 43 | /* 44 | * Without Lambda 45 | * */ 46 | 47 | 48 | // fun getConsumer(): Consumer> { 49 | // return Consumer { observable -> 50 | // Log.d(Constant().TAG, "Sub Divide begin....") 51 | // textView.append("Sub Divide begin ....") 52 | // textView.append(Constant().LINE_SEPARATOR) 53 | // observable 54 | // .subscribeOn(Schedulers.io()) 55 | // .observeOn(AndroidSchedulers.mainThread()) 56 | // .subscribe(object : Consumer { 57 | // override fun accept(value: Long) { 58 | // Log.d(Constant().TAG, "Next:" + value) 59 | // textView.append("Next:" + value) 60 | // textView.append(Constant().LINE_SEPARATOR) 61 | // } 62 | // }) 63 | // } 64 | // } 65 | 66 | 67 | 68 | /* 69 | * With Lambda 70 | * */ 71 | 72 | private fun getConsumer(): Consumer> { 73 | return Consumer { observable -> 74 | Log.d(Constant().TAG, "Sub Divide begin....") 75 | textView.append("Sub Divide begin ....") 76 | textView.append(Constant().LINE_SEPARATOR) 77 | observable 78 | .subscribeOn(Schedulers.io()) 79 | .observeOn(AndroidSchedulers.mainThread()) 80 | .subscribe { value -> 81 | Log.d(Constant().TAG, "Next:" + value) 82 | textView.append("Next:" + value) 83 | textView.append(Constant().LINE_SEPARATOR) 84 | } 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxOperators/utilityOperators/DelayOperatorActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxOperators.utilityOperators 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import com.freeankit.rxkotlinoperators.R 7 | import com.freeankit.rxkotlinoperators.utils.Constant 8 | import io.reactivex.Observable 9 | import io.reactivex.Observer 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.Disposable 12 | import io.reactivex.schedulers.Schedulers 13 | import kotlinx.android.synthetic.main.activity_example_operator.* 14 | import java.util.concurrent.TimeUnit 15 | 16 | 17 | /** 18 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 20/02/2018 (MM/DD/YYYY) 19 | */ 20 | class DelayOperatorActivity : AppCompatActivity() { 21 | override fun onCreate(savedInstanceState: Bundle?) { 22 | super.onCreate(savedInstanceState) 23 | setContentView(R.layout.activity_example_operator) 24 | 25 | btn.setOnClickListener({ executeDelayOperator() }) 26 | } 27 | 28 | 29 | /* 30 | * simple example using delay to emit after 2 second 31 | */ 32 | private fun executeDelayOperator() { 33 | 34 | getObservable().delay(2, TimeUnit.SECONDS) 35 | // Run on a background thread 36 | .subscribeOn(Schedulers.io()) 37 | // Be notified on the main thread 38 | .observeOn(AndroidSchedulers.mainThread()) 39 | .subscribe(getObserver()) 40 | } 41 | 42 | private fun getObservable(): Observable { 43 | return Observable.just("Jitendra Kumar") 44 | } 45 | 46 | private fun getObserver(): Observer { 47 | return object : Observer { 48 | 49 | override fun onSubscribe(d: Disposable) { 50 | Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed) 51 | } 52 | 53 | override fun onNext(value: String) { 54 | textView.append(" onNext : value : " + value) 55 | textView.append(Constant().LINE_SEPARATOR) 56 | Log.d(Constant().TAG, " onNext : value : " + value) 57 | } 58 | 59 | override fun onError(e: Throwable) { 60 | textView.append(" onError : " + e.message) 61 | textView.append(Constant().LINE_SEPARATOR) 62 | Log.d(Constant().TAG, " onError : " + e.message) 63 | } 64 | 65 | override fun onComplete() { 66 | textView.append(" onComplete") 67 | textView.append(Constant().LINE_SEPARATOR) 68 | Log.d(Constant().TAG, " onComplete") 69 | } 70 | } 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxPagination/PaginationActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxPagination 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.support.v7.widget.LinearLayoutManager 6 | import android.support.v7.widget.RecyclerView 7 | import android.view.View 8 | import com.freeankit.rxkotlinoperators.R 9 | import io.reactivex.Flowable 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.CompositeDisposable 12 | import io.reactivex.processors.PublishProcessor 13 | import kotlinx.android.synthetic.main.activity_main.* 14 | import org.reactivestreams.Publisher 15 | import java.util.concurrent.TimeUnit 16 | 17 | 18 | class PaginationActivity : AppCompatActivity() { 19 | 20 | private var layoutManager: LinearLayoutManager? = null 21 | private var adapter: PaginationAdapter? = null 22 | private val compositeDisposable = CompositeDisposable() 23 | private var loading = false 24 | private var pageNumber = 0 25 | private val VISIBLE_THRESHOLD = 1 26 | private var lastVisibleItem: Int = 0 27 | private var totalItemCount: Int = 0 28 | 29 | private val paginator: PublishProcessor = PublishProcessor.create() 30 | 31 | override fun onCreate(savedInstanceState: Bundle?) { 32 | super.onCreate(savedInstanceState) 33 | 34 | setContentView(R.layout.activity_main) 35 | 36 | layoutManager = LinearLayoutManager(this) 37 | layoutManager!!.orientation = LinearLayoutManager.VERTICAL 38 | recyclerView.layoutManager = layoutManager 39 | adapter = PaginationAdapter() 40 | recyclerView.adapter = adapter 41 | setUpLoadMoreListener() 42 | subscribeForData() 43 | } 44 | 45 | private fun setUpLoadMoreListener() { 46 | recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { 47 | override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { 48 | super.onScrolled(recyclerView, dx, dy) 49 | totalItemCount = layoutManager?.itemCount!! 50 | lastVisibleItem = layoutManager?.findLastVisibleItemPosition()!! 51 | if (!loading && totalItemCount <= lastVisibleItem + VISIBLE_THRESHOLD) { 52 | pageNumber++ 53 | paginator.onNext(pageNumber) 54 | loading = true 55 | } 56 | } 57 | }) 58 | 59 | } 60 | 61 | override fun onDestroy() { 62 | super.onDestroy() 63 | compositeDisposable.clear() 64 | } 65 | 66 | private fun subscribeForData() { 67 | val disposable = paginator 68 | .onBackpressureDrop() 69 | .concatMap({ page -> 70 | loading = true 71 | progressBar.visibility = View.VISIBLE 72 | dataFromNetwork(page) 73 | }) 74 | .observeOn(AndroidSchedulers.mainThread()) 75 | .subscribe({ items -> 76 | adapter?.addData(items as MutableList) 77 | loading = false 78 | progressBar.visibility = View.INVISIBLE 79 | }) 80 | 81 | compositeDisposable.add(disposable) 82 | 83 | paginator.onNext(pageNumber) 84 | 85 | } 86 | 87 | private fun dataFromNetwork(page: Int): Publisher>? { 88 | return Flowable.just(true) 89 | .delay(2, TimeUnit.SECONDS) 90 | .map({ 91 | val items = ArrayList() 92 | for (i in 1..10) { 93 | items.add("ListItem " + (page * 10 + i)) 94 | } 95 | items 96 | }) 97 | 98 | } 99 | } 100 | 101 | 102 | -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxPagination/PaginationAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui.RxPagination 2 | 3 | import android.support.v7.widget.RecyclerView 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import android.widget.TextView 8 | import com.freeankit.rxkotlinoperators.R 9 | 10 | 11 | /** 12 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 04/04/2018 (MM/DD/YYYY) 13 | */ 14 | class PaginationAdapter : RecyclerView.Adapter() { 15 | 16 | private val items: MutableList = ArrayList() 17 | 18 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { 19 | val v = LayoutInflater.from(parent.context).inflate(R.layout.layout_adapter_item, parent, false) 20 | return ViewHolder(v) 21 | } 22 | 23 | override fun getItemCount(): Int { 24 | return items.size 25 | } 26 | 27 | override fun onBindViewHolder(holder: ViewHolder, position: Int) { 28 | holder.bind(items[position]) 29 | } 30 | 31 | fun addData(list: MutableList) { 32 | items.addAll(list) 33 | notifyDataSetChanged() 34 | } 35 | 36 | inner class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) { 37 | fun bind(content: String) { 38 | (itemView as TextView).text = content 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/SelectionActivity.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.ui 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import android.support.v7.app.AppCompatActivity 6 | import android.view.View 7 | import com.freeankit.rxkotlinoperators.ui.RxPagination.PaginationActivity 8 | import com.freeankit.rxkotlinoperators.R 9 | import com.freeankit.rxkotlinoperators.ui.RxBinding.RxLoginScreenActivity 10 | 11 | /** 12 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/12/2017 (MM/DD/YYYY ) 13 | */ 14 | class SelectionActivity : AppCompatActivity() { 15 | override fun onCreate(savedInstanceState: Bundle?) { 16 | super.onCreate(savedInstanceState) 17 | setContentView(R.layout.activity_selection) 18 | } 19 | 20 | fun startOperatorsActivity(view: View) { 21 | startActivity(Intent(this@SelectionActivity, OperatorsActivity::class.java)) 22 | } 23 | 24 | fun startRxBinding(view: View) { 25 | startActivity(Intent(this@SelectionActivity, RxLoginScreenActivity::class.java)) 26 | } 27 | 28 | fun startRxPagination(view: View) { 29 | startActivity(Intent(this@SelectionActivity, PaginationActivity::class.java)) 30 | } 31 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/utils/Constant.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.utils 2 | 3 | /** 4 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/12/2017 (MM/DD/YYYY ) 5 | */ 6 | class Constant { 7 | val TAG = "Tag" 8 | val LINE_SEPARATOR: String = "\n" 9 | } -------------------------------------------------------------------------------- /RxOperators/src/main/java/com/freeankit/rxkotlinoperators/utils/Utils.kt: -------------------------------------------------------------------------------- 1 | package com.freeankit.rxkotlinoperators.utils 2 | 3 | import com.freeankit.rxkotlinoperators.model.ApiUser 4 | import com.freeankit.rxkotlinoperators.model.User 5 | import java.util.ArrayList 6 | 7 | /** 8 | * @author Ankit Kumar (ankitdroiddeveloper@gmail.com) on 08/12/2017 (MM/DD/YYYY ) 9 | */ 10 | class Utils { 11 | fun convertApiUserListToUserList(apiUserList: List): List { 12 | val userList = ArrayList() 13 | 14 | for (apiUser in apiUserList) { 15 | val user = User() 16 | user.login = apiUser.login 17 | user.url = apiUser.url 18 | userList.add(user) 19 | } 20 | 21 | return userList 22 | } 23 | 24 | fun getApiUserList(): List { 25 | val apiUserList = ArrayList() 26 | 27 | val apiUserOne = ApiUser("Ankit", 1, "Kumar") 28 | apiUserList.add(apiUserOne) 29 | 30 | val apiUserTwo = ApiUser("JP", 2, "Kumar") 31 | apiUserList.add(apiUserTwo) 32 | 33 | val apiUserThree = ApiUser("Ankit", 3, "Prajapati") 34 | apiUserList.add(apiUserThree) 35 | 36 | return apiUserList 37 | } 38 | 39 | fun filterUserWhoLovesBoth(kotlinFans: List, javaFans: List): List { 40 | val userWhoLovesBoth = ArrayList() 41 | for (kotlinFan in kotlinFans) { 42 | javaFans 43 | .filter { kotlinFan.id == it.id } 44 | .forEach { userWhoLovesBoth.add(kotlinFan) } 45 | } 46 | return userWhoLovesBoth 47 | } 48 | 49 | fun getUserListWhoLovesKotlin(): List { 50 | val userList = ArrayList() 51 | 52 | val userOne = User() 53 | userOne.id = 1 54 | userOne.login = "Bala" 55 | userOne.url = "K" 56 | userList.add(userOne) 57 | 58 | val userTwo = User() 59 | userTwo.id = 2 60 | userTwo.login = "Ankit" 61 | userTwo.url = "Kumar" 62 | userList.add(userTwo) 63 | 64 | return userList 65 | } 66 | 67 | fun getUserListWhoLovesJava(): List { 68 | val userList = ArrayList() 69 | 70 | val userOne = User() 71 | userOne.id = 1 72 | userOne.login = "Bala" 73 | userOne.url = "K" 74 | userList.add(userOne) 75 | 76 | val userTwo = User() 77 | userTwo.id = 3 78 | userTwo.login = "Sandeep" 79 | userTwo.url = "Kumar" 80 | userList.add(userTwo) 81 | 82 | return userList 83 | } 84 | } -------------------------------------------------------------------------------- /RxOperators/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /RxOperators/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 | -------------------------------------------------------------------------------- /RxOperators/src/main/res/layout/activity_example_operator.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 |