(This page has no text content)
Reactive Programming with Kotlin By Alex Sullivan Copyright ©2019 Razeware LLC. Notice of Rights All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner. Notice of Liability This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software. Trademarks All trademarks and registered trademarks appearing in this book are the property of their own respective owners. Reactive Programming with Kotlin raywenderlich.com 2
Dedications "To my wonderful partner Pallavi, without whom I would have never been able to start this undertaking. Your support and encouragement mean the world to me." — Alex Sullivan Reactive Programming with Kotlin raywenderlich.com 3
About the Author Alex Sullivan is an author of this book. Alex is a mobile developer who works at Thoughtbot in Boston, where he enjoys reactive programming, experimenting with different programming languages, and tinkering with fun approaches to building mobile applications. In his spare time, Alex enjoys traveling and relaxing with his partner, binging unhealthy amounts of Netflix and reading. Alex hopes to one day find a cat he's not allergic to and rant about bracket placement to him or her. About the Editors Joe Howard is the final pass editor for this book. Joe is a former physicist that studied computational particle physics using parallel Fortran simulations. He gradually shifted into systems engineering and then ultimately software engineering around the time of the release of the iOS and Android SDKs. He's been a mobile software developer on iOS and Android since 2009, working primarily at two agencies in Boston, MA since 2011. He's now the Pillar Lead for raywenderlich.com. Twitter: @orionthewake. Vijay Sharma is the final pass editor of this book. Vijay is a husband, a father and a senior mobile engineer. Based out of Canada's capital, Vijay has worked on dozens of apps for both Android and iOS. When not in front of his laptop, you can find him in front of a TV, behind a book, or chasing after his kids. You can reach out to him on Twitter @vijaysharm or on LinkedIn @vijaysharm Manda Frederick is the editor of this book. She has been involved in publishing for over ten years through various creative, educational, medical and technical print and digital publications, and is thrilled to bring her experience to the raywenderlich.com family as Managing Editor. In her free time, you can find her at the climbing gym, backpacking in the backcountry, hanging with her dog, working on poems, playing guitar and exploring breweries. Reactive Programming with Kotlin raywenderlich.com 4
Victoria Gonda is a tech editor for this book. Victoria is a software developer working mostly on Android apps. When she's not traveling to speak at conferences, she works remotely from Chicago. Her interest in tech started while studying computer science and dance production in college. In her spare time, you can find Victoria relaxing with a book, her partner, and her pets. You can connect with her on Twitter at @TTGonda. Ellen Shapiro is a tech editor for this book. Ellen is an iOS developer for Bakken & Bæck's Amsterdam office who also occasionally writes Android apps. She has worked in her spare time to help bring iOS songwriting app Hum to life. She’s also developed several independent applications through her personal company, Designated Nerd Software. When she's not writing code, she's usually tweeting about it at @DesignatedNerd. Amanjeet Singh is a tech editor for this book. Amanjeet is an Android Engineer in Delhi, India and an open source enthusiast. As a developer he always tries to build apps with optimized performance and good architectures which can be used on a large scale. Currently Android Engineer at 1mg, he helps in building apps for one of the leading healthcare platform in India. Also a technical editor and author in android team at raywenderlich.com. Matei Suica is a tech editor for this book. Matei is a software developer that dreams about changing the world with his work. From his small office in Romania, Matei is trying to create an App that will help millions. When the laptop lid closes, he likes to go to the gym and read. You can find him on Twitter or LinkedIn: @mateisuica Reactive Programming with Kotlin raywenderlich.com 5
About the Artist Vicki Wenderlich is the designer and artist of the cover of this book. She is Ray’s wife and business partner. She is a digital artist who creates illustrations, game art and a lot of other art or design work for the tutorials and books on raywenderlich.com. When she’s not making art, she loves hiking, a good glass of wine and attempting to create the perfect cheese plate. Reactive Programming with Kotlin raywenderlich.com 6
Acknowldegments We'd also like to thank the RxSwift: Reactive Programming with Swift authors, whose work served as the basis for parts of this book: • Scott Gardner has been developing iOS apps since 2010, Swift since the day it was announced, and RxSwift since before version 1. He's authored several video courses, tutorials, and articles on iOS app development, presented at numerous conferences, meetups, and online events, and this is his second book. Say hello to Scott on Twitter at @scotteg. • Junior Bontognali has been developing on iOS since the first iPhone and joined the RxSwift team in the early development stage. Based in Switzerland, when he's not eating cheese or chocolate, he's doing some cool stuff in the mobile space, without denying to work on other technologies. Other than that he organizes tech events, speaks and blogs. Say hello to Junior on Twitter at @bontoJR. • Florent Pillet has been developing for mobile platforms since the last century and moved to iOS on day 1. He adopted reactive programming before Swift was announced and has been using RxSwift in production since 2015. A freelance developer, Florent also uses Rx on Android and likes working on tools for developers like the popular NSLogger when he's not contracting for clients worldwide. Say hello to Florent on Twitter at @fpillet. • Marin Todorov is one of the founding members of the raywenderlich.com team and has worked on seven of the team's books. Besides crafting code, Marin also enjoys blogging, teaching, and speaking at conferences. He happily open-sources code. You can find out more about Marin at www.underplot.com. Reactive Programming with Kotlin raywenderlich.com 7
Table of Contents: Overview Book License 18........................................................... Book Source Code & Forums 19................................... What You Need 20....................................................... Book Updates 21.......................................................... About the Cover 22....................................................... Section I: Getting Started with RxJava 23.................... Chapter 1: Hello, RxJava! 24.................................. Chapter 2: Observables 45.................................... Chapter 3: Subjects 63.......................................... Chapter 4: Observables & Subjects in Practice 79 Section II: Operators & Best Practices 100................... Chapter 5: Filtering Operators 102......................... Chapter 6: Filtering Operators in Practice 120....... Chapter 7: Transforming Operators 133................. Chapter 8: Transforming Operators in Practice 149........................................................... Chapter 9: Combining Operators 163.................... Chapter 10: Combining Operators in Practice 185. Chapter 11: Time-Based Operators 203................ Section III: Intermediate RxJava 223............................ Reactive Programming with Kotlin raywenderlich.com 8
Chapter 12: Error Handling in Practice 224............ Chapter 13: Intro to Schedulers 244...................... Chapter 14: Flowables & Backpressure 262.......... Chapter 15: Testing RxJava Code 277................... Chapter 16: Creating Custom Reactive Extensions 294...................................................... Section IV: RxJava Community Cookbook 311............. Chapter 17: RxBindings 312................................... Chapter 18: Retrofit 331......................................... Chapter 19: RxPreferences 349............................. Chapter 20: RxPermissions 363............................ Section V: Putting It All Together 377........................... Chapter 21: RxJava & Jetpack 378........................ Chapter 22: Building a Complete RxJava App 395 Conclusion 423............................................................ Reactive Programming with Kotlin raywenderlich.com 9
Table of Contents: Extended Book License 18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book Source Code & Forums 19. . . . . . . . . . . . . . . . . . . . . . What You Need 20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book Updates 21. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Cover 22. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section I: Getting Started with RxJava 23. . . . . . . . . . . . . Chapter 1: Hello, RxJava! 24. . . . . . . . . . . . . . . . . . . . . . . . . RxJava and RxKotlin 24. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction to asynchronous programming 26. . . . . . . . . . . . . . . . . . . . Foundations of RxJava 33. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . App architecture 40. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . RxAndroid and RxBinding 41. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing RxJava 42. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Community 42. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 43. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 44. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 2: Observables 45. . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 45. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What is an observable? 46. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lifecycle of an observable 47. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating observables 48. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Subscribing to observables 49. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Disposing and terminating 53. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The create operator 54. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating observable factories 57. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using other observable types 58. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 61. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reactive Programming with Kotlin raywenderlich.com 10
Key points 62. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 3: Subjects 63. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 63. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What are subjects? 65. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with publish subjects 65. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with behavior subjects 68. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with replay subjects 71. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with async subjects 73. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with the RxRelay library 75. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 76. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 77. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 78. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 4: Observables & Subjects in Practice 79. . . . Getting started 80. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using a BehaviorSubject in a ViewModel 81. . . . . . . . . . . . . . . . . . . . . . . Adding photos 82. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Communicating with other views via subjects 87. . . . . . . . . . . . . . . . . . . Creating a custom observable 91. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Review: Single, Maybe, Completable 92. . . . . . . . . . . . . . . . . . . . . . . . . . Using Single in the app 95. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 98. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 99. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section II: Operators & Best Practices 100. . . . . . . . . . . . Chapter 5: Filtering Operators 102. . . . . . . . . . . . . . . . . . . Getting started 102. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ignoring operators 102. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Skipping operators 107. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Taking operators 111. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Distinct operators 114. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge: Create a phone number lookup 117. . . . . . . . . . . . . . . . . . . . . Key points 119. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reactive Programming with Kotlin raywenderlich.com 11
Where to go from here? 119. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 6: Filtering Operators in Practice 120. . . . . . . . . Improving the Combinestagram project 121. . . . . . . . . . . . . . . . . . . . . . . Challenge 132. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 132. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 132. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 7: Transforming Operators 133. . . . . . . . . . . . . . . Getting started 133. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Transforming elements 133. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Transforming inner observables 136. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Observing events 142. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 146. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 147. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 148. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 8: Transforming Operators in Practice 149. . . . Getting started with GitFeed 149. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fetching data from the web 151. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Transforming the response 152. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Processing the response 153. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Persisting objects to disk 155. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding a last-modified header 156. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 160. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 162. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 162. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 9: Combining Operators 163. . . . . . . . . . . . . . . . Getting started 163. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Prefixing and concatenating 164. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Merging 168. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combining elements 171. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Triggers 175. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Switches 179. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reactive Programming with Kotlin raywenderlich.com 12
Combining elements within a sequence 180. . . . . . . . . . . . . . . . . . . . . . . Challenge: The zip case 183. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 184. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 184. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 10: Combining Operators in Practice 185. . . . . Getting started 185. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preparing the EONET API class 186. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Add events into the mix 188. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Combining events and categories 191. . . . . . . . . . . . . . . . . . . . . . . . . . . . Downloading in parallel 194. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wiring up the days seek bar 199. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge: Adding a progress bar 201. . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 202. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 202. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 11: Time-Based Operators 203. . . . . . . . . . . . . . Getting started 204. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Buffering operators 204. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Time-shifting operators 215. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Timer operators 218. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 221. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 221. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section III: Intermediate RxJava 223. . . . . . . . . . . . . . . . . . Chapter 12: Error Handling in Practice 224. . . . . . . . . . . . Getting started 224. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Managing errors 226. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handle errors with catch 227. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Catching errors 230. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Retrying on error 232. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Errors as objects 237. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 241. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 243. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reactive Programming with Kotlin raywenderlich.com 13
Where to go from here? 243. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 13: Intro to Schedulers 244. . . . . . . . . . . . . . . . . . What is a scheduler? 245. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting up the project 245. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Switching schedulers 248. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pitfalls 253. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Best practices and built-in schedulers 257. . . . . . . . . . . . . . . . . . . . . . . . Key points 261. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 261. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 14: Flowables & Backpressure 262. . . . . . . . . . . Backpressure 262. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Buffering danger! 264. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Natural backpressure 265. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction to Flowables 266. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Backpressure strategies 268. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Flowables, Observables, Processors and Subjects — Oh, My! 272. . . . . Key points 276. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 276. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 15: Testing RxJava Code 277. . . . . . . . . . . . . . . . Getting started 277. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction to TestObserver 280. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using a TestScheduler 281. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Injecting schedulers 285. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Trampoline scheduler 286. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using subjects with mocked data 287. . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing ColorViewModel 289. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 293. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 293. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 16: Creating Custom Reactive Extensions 294. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 294. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reactive Programming with Kotlin raywenderlich.com 14
Extending a framework class 296. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wiring the extension up 297. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Wrapping the locations API 300. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The lift and compose functions 305. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing your custom reactive extension 307. . . . . . . . . . . . . . . . . . . . . . . Key points 309. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 310. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section IV: RxJava Community Cookbook 311. . . . . . . . Chapter 17: RxBindings 312. . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 313. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Extending ValueAnimator to be reactive 314. . . . . . . . . . . . . . . . . . . . . . . Using RxBindings with Android widgets 315. . . . . . . . . . . . . . . . . . . . . . . Fetching colors from an API 323. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Displaying an information dialog 325. . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 329. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 330. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 330. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 18: Retrofit 331. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 331. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Recap of Retrofit 332. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Including Rx adapters 334. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a JSON object 335. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Updating the JSON 339. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Retrieving JSON 344. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 347. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 348. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 19: RxPreferences 349. . . . . . . . . . . . . . . . . . . . . . Getting started 350. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using SharedPreferences 352. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Listening for preference updates 353. . . . . . . . . . . . . . . . . . . . . . . . . . . . Using RxPreferences 355. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reactive Programming with Kotlin raywenderlich.com 15
Subscribing to preference changes 356. . . . . . . . . . . . . . . . . . . . . . . . . . Saving custom objects 358. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 362. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 362. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 20: RxPermissions 363. . . . . . . . . . . . . . . . . . . . . Getting started 364. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Requesting the location permission 365. . . . . . . . . . . . . . . . . . . . . . . . . . Using RxPermissions 367. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Requesting another permission 370. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reading from external storage 371. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Writing the weather to external storage 372. . . . . . . . . . . . . . . . . . . . . . . Reacting to orientation changes 374. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 376. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 376. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section V: Putting It All Together 377. . . . . . . . . . . . . . . . . Chapter 21: RxJava & Jetpack 378. . . . . . . . . . . . . . . . . . . Getting started 379. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . RxJava and Room 381. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reacting to database changes 384. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Updating individual items 385. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Starting the app with cached data 387. . . . . . . . . . . . . . . . . . . . . . . . . . . Paging data in 388. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 394. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 394. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 22: Building a Complete RxJava App 395. . . . Introducing QuickTodo 395. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Architecting the application 396. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Task model 398. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Task Dao 399. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Task repository 401. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Replacing callbacks with observables 409. . . . . . . . . . . . . . . . . . . . . . . . Reactive Programming with Kotlin raywenderlich.com 16
Editing tasks 412. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 420. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 422. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion 423. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reactive Programming with Kotlin raywenderlich.com 17
LBook License By purchasing Reactive Programming with Kotlin, you have the following license: • You are allowed to use and/or modify the source code in Reactive Programming with Kotlin in as many apps as you want, with no attribution required. • You are allowed to use and/or modify all art, images and designs that are included in Reactive Programming with Kotlin in as many apps as you want, but must include this attribution line somewhere inside your app: “Artwork/images/designs: from Reactive Programming with Kotlin, available at www.raywenderlich.com”. • The source code included in Reactive Programming with Kotlin is for your personal use only. You are NOT allowed to distribute or sell the source code in Reactive Programming with Kotlin without prior authorization. • This book is for your personal use only. You are NOT allowed to sell this book without prior authorization, or distribute it to friends, coworkers or students; they would need to purchase their own copies. All materials provided with this book are provided on an “as is” basis, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action or contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. All trademarks and registered trademarks appearing in this guide are the properties of their respective owners. raywenderlich.com 18
BBook Source Code & Forums This book comes with the source code for the starter and completed projects for each chapter. These resources are shipped with the digital edition you downloaded from https://store.raywenderlich.com/products/reactive-programming-with-kotlin. We’ve also set up an official forum for the book at forums.raywenderlich.com. This is a great place to ask questions about the book or to submit any errors you may find. raywenderlich.com 19
WWhat You Need To follow along with the tutorials in this book, you’ll need the following: • A PC running Windows 10 or a recent Linux such as Ubuntu 18.04 LTS, or a Mac running the latest point release of macOS Mojave or later: You’ll need one of these to be able to install the latest versions of IntelliJ IDEA and Android Studio. • IntelliJ IDEA Community 2019.1 or later: IntelliJ IDEA is the IDE upon which Android Studio is based, and it's used in the book to look at pure Kotlin projects that demonstrate techniques in RxJava. You can download the latest version of IntelliJ IDEA Community for free here: https://www.jetbrains.com/idea/ • JDK 8 or later: You'll need a Java Development Kit installed for use with IntelliJ IDEA projects (Android Studio will use its own version of the JDK). You can download the Oracle JDK from here: https://www.oracle.com/technetwork/java/javase/ downloads/index.html • Android Studio 3.3.2 or later: Android Studio is the main development tool for Android. You can download the latest version of Android Studio for free here: https://developer.android.com/studio • An intermediate level knowledge of Kotlin and Android development. This book is about learning RxJava specifically; to understand the rest of the project code and how the accompanying demo projects work you will need at least an intermediate understanding of Kotlin and the Android SDK. All the Android sample projects in this book will work just fine in an Android emulator bundled with Android Studio, or you can also use a physical Android device. raywenderlich.com 20
Comments 0
Loading comments...
Reply to Comment
Edit Comment