<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Liviu Coman on Medium]]></title>
        <description><![CDATA[Stories by Liviu Coman on Medium]]></description>
        <link>https://medium.com/@liviucoman?source=rss-bec66a7522c2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*3bqNsEUXhD56EqngK4l91Q.jpeg</url>
            <title>Stories by Liviu Coman on Medium</title>
            <link>https://medium.com/@liviucoman?source=rss-bec66a7522c2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 01:05:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@liviucoman/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to handle asynchronous operations with Redux in Swift]]></title>
            <link>https://medium.com/swlh/how-to-handle-asynchronous-operations-with-redux-in-swift-495591d9df84?source=rss-bec66a7522c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/495591d9df84</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Liviu Coman]]></dc:creator>
            <pubDate>Sun, 01 Sep 2019 09:44:33 GMT</pubDate>
            <atom:updated>2019-09-04T14:31:03.927Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MM8LYY1XVd4vjIXllJAq0Q.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@_imkiran?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Sai Kiran Anagani</a> on <a href="https://unsplash.com/search/photos/technology?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>All apps today need at some point to talk to a remote resource like a server or database. These are async operations, meaning they get executed on a secondary thread and return at some later time on the main thread.</p><p>Let’s say we want to connect to such a remote resource in order to fetch details about a certain movie. The model we want to get back is this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a07dd0ce9feb8fcafed44175dd8a4d4/href">https://medium.com/media/8a07dd0ce9feb8fcafed44175dd8a4d4/href</a></iframe><p>And — to keep in line with functional code we’ve used in the past — we’re going to write a function to return that model with the help of a callback, like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d500bbb64a61472bb5d2c914b2ad05de/href">https://medium.com/media/d500bbb64a61472bb5d2c914b2ad05de/href</a></iframe><p>Redux however is focused on sync operations. Reducers are pure functions with no side effects, the Store dispatches actions on the main thread, etc. This is what makes code deterministic, which means it’s easier to test and simpler to understand.</p><p>How then can we create a remote layer that works well with Redux?</p><p>First, let’s define what a good solution would look like:</p><ul><li>it should be testable</li><li>it should be mockable</li><li>it should be loosely coupled with other parts of the app</li></ul><p>A good definition for all of the above already exists in the wider Redux ecosystem and it’s called a <a href="https://redux.js.org/advanced/middleware">Middleware</a>.</p><p>Essentially a middleware is an entity that allows you to perform side effects when a certain action passes through the Store. Those side effects can range from logging to the console, sending fire-and-forget stats as well as triggering new actions to the Store.</p><p>That last property of a Middleware is what we’ll be focusing on.</p><p>The system we want to build will listen to a particular Action, call the api to obtain a new Movie, and finally dispatch an Action containing the result.</p><p>For start, we’ll need to define what a Middleware looks like. As usual, we’re going to define it as a type alias for a function type.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5b6a0bbdd4a9a160cf1d4d2b2c46cc5/href">https://medium.com/media/c5b6a0bbdd4a9a160cf1d4d2b2c46cc5/href</a></iframe><p>If the concepts of ReduxState, Action, etc, are new to you, feel free to checkout my <a href="https://medium.com/swlh/how-to-implement-redux-in-swift-a2a0e7b7f10e">two</a> <a href="https://medium.com/@liviucoman/functional-swiftui-with-redux-5f4a8c96c0f3">other</a> posts about Redux in Swift.</p><p>Then we’ll need to enhance our initial Store to allow it to handle Middlewares.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f41800e904b2e0ce5e653618d473dae/href">https://medium.com/media/6f41800e904b2e0ce5e653618d473dae/href</a></iframe><p>What we’ve essentially done is added an array of Middlewares the Store needs to be initialised with and a way to actually trigger them once a new state gets computed.</p><p>With that preparation work behind us, we can focus on the actual Actions we need to intercept and then dispatch.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52bad6996bc1fa4e0428f22eecb68cff/href">https://medium.com/media/52bad6996bc1fa4e0428f22eecb68cff/href</a></iframe><p>You see we’ve split the MovieAction enum in a very granular way.</p><p>The <strong>start</strong> action marks the beginning of a remote fetch. This should be dispatched from a Props entity.</p><p>The <strong>success</strong> action marks the end of a remote fetch that has gone well. This will be dispatched by the Middleware we’re write.</p><p>The <strong>end</strong> action marks the end of a remote fetch that hasn’t gone well. This too will be dispatched by the new Middleware.</p><p>Finally, we can use the rules above to create the new movieMiddleware:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/138a80c85e4be94c42d860997207aca4/href">https://medium.com/media/138a80c85e4be94c42d860997207aca4/href</a></iframe><p>To tie it all up, this is how we’d add this to our Store.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fd8c7bf6a82304955c6840850973c1b/href">https://medium.com/media/5fd8c7bf6a82304955c6840850973c1b/href</a></iframe><p>The beauty of this is that it allows us to define our whole app business logic with nothing but Reducers and Middlewares. For example, a more mature movie app might have the following list of middlewares:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c047db175e3c655d0316c239348da066/href">https://medium.com/media/c047db175e3c655d0316c239348da066/href</a></iframe><p>And that’s it!</p><p>This article is one of a multi-part series exploring State, UI and Redux and how we can leverage knowledge of these topics to write more modular and testable code in both iOS and Android.</p><ul><li><a href="https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">How to make your Views a function of State in Kotlin</a></li><li><a href="https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217">How to decouple Views from their State in Kotlin</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079">How to implement Redux in Kotlin Part 1: the basics</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-2-real-world-example-8c9f1fa10e87">How to implement Redux in Kotlin Part 2: real world example</a></li><li><a href="https://medium.com/swlh/how-to-implement-redux-in-swift-a2a0e7b7f10e">How to implement Redux in Swift</a></li><li><a href="https://medium.com/@liviucoman/functional-swiftui-with-redux-5f4a8c96c0f3">Functional SwiftUI with Redux</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=495591d9df84" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/how-to-handle-asynchronous-operations-with-redux-in-swift-495591d9df84">How to handle asynchronous operations with Redux in Swift</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Functional SwiftUI with Redux]]></title>
            <link>https://medium.com/swlh/functional-swiftui-with-redux-5f4a8c96c0f3?source=rss-bec66a7522c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f4a8c96c0f3</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Liviu Coman]]></dc:creator>
            <pubDate>Sat, 10 Aug 2019 15:53:24 GMT</pubDate>
            <atom:updated>2019-09-01T09:39:51.528Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BiopWRcn6D8I_thawOu3hA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@patrikward?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Patrick Ward</a> on <a href="https://unsplash.com/search/photos/mac?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In a previous article on <a href="https://medium.com/swlh/how-to-implement-redux-in-swift-a2a0e7b7f10e">Redux in Swift</a> we saw how we can build a really basic counter app using SwiftUI and Redux, from scratch.</p><p>Here’s the UI code we ended up with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d2f496d89881e74f8ab0bbb19755f18/href">https://medium.com/media/8d2f496d89881e74f8ab0bbb19755f18/href</a></iframe><p>SwiftUI and Redux work really well together and the benefits are immediate: the declarative syntax helps with readability and the strict unidirectional data flow makes code easier to test and reason about.</p><p>However, SwiftUI is still new and everyone’s still exploring different ways of using it to change their codebases for the better.</p><p><a href="https://medium.com/@dimillian">Thomas Ricouard</a> has a more nuanced and advanced take on <a href="https://medium.com/better-programming/swiftui-and-redux-clean-code-and-small-independent-components-6f46a5eb46b3">SwiftUI</a> and <a href="https://medium.com/@Killectro">DJ Mitchell</a> has a really interesting approach to <a href="https://medium.com/grailed-engineering/modeling-your-view-models-as-functions-65b58525717f">view models as functions</a>.</p><p>Learning from each of them inspired me to look at my initial design and improve on it.</p><p>The first problem I wanted to solve is there’s a lot of boilerplate involved. Each view needs a reference to the Store and a props variable. This both decreases the signal-to-noise ratio in the codebase and distracts from the main responsibilities I think a view should have: rendering and handling user interactions.</p><p>The second problem I wanted to solve is the view is tightly coupled with Redux. Ideally I’d want the views to be agnostic of any business layer details in order to achieve a higher level of decoupling.</p><h3>Reducing boilerplate</h3><p>To get rid of boilerplate I took a step back and removed the Store from the CounterView, reverted it to its bare minimum and removed any functionality it might’ve had, with the assumption that I’ll add it back later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea7d8d9d75f49f527bc7cc27cdbdc0ad/href">https://medium.com/media/ea7d8d9d75f49f527bc7cc27cdbdc0ad/href</a></iframe><p>I still wanted all potential views to get updated by the Store, so I created a special type of view that wraps the Store as an <a href="https://developer.apple.com/documentation/swiftui/environmentobject">EnvironmentObject</a> rather than the initial <a href="https://developer.apple.com/documentation/swiftui/objectbinding">ObjectBinding</a> and called it ConnectedView.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/832342aeb7ba93c9468e016c3981253a/href">https://medium.com/media/832342aeb7ba93c9468e016c3981253a/href</a></iframe><p>Then I placed the ConnectedView at the topmost layer in the view hierarchy of the SceneDelegate. In this way all children of ConnectedView will be automatically updated by the Store.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0447366c227610a3cf20574d137eae96/href">https://medium.com/media/0447366c227610a3cf20574d137eae96/href</a></iframe><p>Finally, the initial CounterView didn’t really have a reason to be a struct anymore so I converted it to a free function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/750575f145d658d3d1ce1fd72e4c1a74/href">https://medium.com/media/750575f145d658d3d1ce1fd72e4c1a74/href</a></iframe><p>Amazingly, the SceneDelegate from above remains exactly the same.</p><h3>Adding back functionality whilst retaining decoupling</h3><p>Here is where it gets interesting. I took the initial “props” parameter — which was just an integer value — and converted it to a series of functions that either return a value or promise to perform some action. To group this functionality together I defined it as a new type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e958d9fbb842d22fcd330c42eeb10fd/href">https://medium.com/media/0e958d9fbb842d22fcd330c42eeb10fd/href</a></iframe><p>Here the type-alias acts as a protocol. It defines how an abstract “CounterProps” entity should look like but omits any implementation details. This keeps in line with the Inversion of Dependency principle of SOLID.</p><p>Now what remains is to plug CounterProps in CounterView:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7d9b625ee6871997893adfa359339c2/href">https://medium.com/media/d7d9b625ee6871997893adfa359339c2/href</a></iframe><p>A CounterProps implementation that makes use of the Store looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d4fe55f06df8253f71032590ff5c14a/href">https://medium.com/media/3d4fe55f06df8253f71032590ff5c14a/href</a></iframe><p>Which is just ready to be plugged in the view at SceneDelegate level to complete the mission:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3900f14213118eb2295fc7cf74262282/href">https://medium.com/media/3900f14213118eb2295fc7cf74262282/href</a></iframe><p>Finally here’s the full final design so you can compare it with the initial one.</p><p>It’s split into the files or domains each entity would logically fit into.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c0f7bb34752ce53bfd0cab4c6b9843c/href">https://medium.com/media/0c0f7bb34752ce53bfd0cab4c6b9843c/href</a></iframe><h3>Conclusions</h3><p>The initial design is definitely shorter and to the point. You might find that that’s what works best for you.</p><p>The final design however improves on it by getting rid of a lot of boilerplate down the line as well as decoupling the views from the business logic. The advantage of that is that if you ever want to move towards Elm or MVVM or any other pattern, you’d be able to do so without affecting the UI layer. Which in the end leads to a less fragile and more resilient system.</p><p>This article is one of a multi-part series exploring State, UI and Redux and how we can leverage knowledge of these topics to write more modular and testable code in both iOS and Android.</p><ul><li><a href="https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">How to make your Views a function of State in Kotlin</a></li><li><a href="https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217">How to decouple Views from their State in Kotlin</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079">How to implement Redux in Kotlin Part 1: the basics</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-2-real-world-example-8c9f1fa10e87">How to implement Redux in Kotlin Part 2: real world example</a></li><li><a href="https://medium.com/swlh/how-to-implement-redux-in-swift-a2a0e7b7f10e">How to implement Redux in Swift</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f4a8c96c0f3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/functional-swiftui-with-redux-5f4a8c96c0f3">Functional SwiftUI with Redux</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How we developed our new video platform Rukkaz as a cross-platform mobile app]]></title>
            <link>https://medium.com/superawesome-engineering/how-we-developed-our-new-video-platform-rukkaz-as-a-cross-platform-mobile-app-6001b4fd9b13?source=rss-bec66a7522c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6001b4fd9b13</guid>
            <category><![CDATA[kotlin-multiplatform]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[rukkaz]]></category>
            <category><![CDATA[streaming-video]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Liviu Coman]]></dc:creator>
            <pubDate>Tue, 06 Aug 2019 09:24:08 GMT</pubDate>
            <atom:updated>2019-08-06T10:13:45.521Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WARKl9-_uIvV-PF1joaKRg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@charlesdeluvio?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Charles 🇵🇭</a> on <a href="https://unsplash.com/search/photos/developer-working?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><a href="https://www.superawesome.com/2019/07/16/causing-a-rukkaz-our-video-platform-for-kids-and-family-content-creators/">We’ve recently unveiled our plans for Rukkaz</a>: a video platform designed for family creators, responsible advertisers and kids. Rukkaz is built with privacy-by-design and safe community as first principles (we talked about our guiding principles in <a href="https://bringtherukkaz.com/2019/07/17/why-we-are-building-rukkaz/">an earlier post</a>).</p><p>While developing Rukkaz natively for iOS and Android we realised we were writing the same functionalities twice, sometimes with different outcomes and inconsistencies across the platforms, resulting in loss of time, as well as headaches for the team.</p><p>This platform-specific approach was also having an effect on the team dynamics: we started to see ourselves as platform specialists rather than one team focused on delivering the best Rukkaz experience possible. So we switched to the same principles <a href="http://bit.ly/kidtech-aws-infra">used elsewhere in our kidtech stack</a>, and focused on cross-platform development.</p><h3>We chose to develop Rukkaz using Kotlin MultiPlatform</h3><p><a href="https://kotlinlang.org/docs/reference/multiplatform.html">Kotlin MultiPlatform</a> (MLP) is a project that extends the <a href="https://kotlinlang.org/">Kotlin</a> language and enables developers to cross-compile to Objective-C on iOS and interoperate natively with all the libraries and frameworks in the iOS ecosystem.</p><p>We found Kotlin MLP very lean and noninvasive, as it allows us to reuse all the native code we’ve developed for iOS (either in Obj-C or Swift) with zero effort, enabling us to defer the choice of rewriting valuable pre-existing business logic to the last responsible moment.</p><p>With Kotlin MLP we aim to have a completely shared codebase for all the key libraries making up Rukkaz, including eventing, logging, persistence, networking, and — most importantly — all the logic around and protection of the user’s privacy. To date, more than 50% of our codebase is shared across platforms, resulting in a massive saving of development time, and in a fast time to market for Rukkaz.</p><p>User interfaces, where platforms differ the most, are still platform specific. We are aware this is just the beginning of our journey, and we’ll reassess if we need to introduce a shared view layer in the future.</p><p>Since native Android development pivoted from Java to <a href="https://kotlinlang.org/">Kotlin</a> two years ago, our team has decided to embrace this new language. That choice gave us a headstart on Kotlin MultiPlatform as the whole team had developed a deep familiarity with the language, resulting in an extremely short learning curve when compared to other options, even for <a href="https://www.infoq.com/articles/kotlin-multiplatform-ios-developers/">iOS developers</a>.</p><p>Being responsible for delivering a feature end-to-end on both platforms with a good chunk of shared code has changed our mindset, turning platform experts into Rukkaz enthusiasts focused on making the internet safer for kids.</p><h3>Known shortcomings of Kotlin MLP</h3><p>There are a good number of libraries out there that handle the basic things you might need, like <a href="https://ktor.io/">Ktor</a> or <a href="https://github.com/Kotlin/kotlinx.serialization">KotlinX Serialization</a>, but the ecosystem of cross-platform libraries available today is not as rich.</p><p><a href="https://stackoverflow.com/questions/53169064/how-to-add-java-library-to-kotlin-native">Not all Java libraries</a> are supported out of the box, mostly because they depend on the Android SDK. Kotlin MultiPlatform compiles to Objective-C, and some functionalities like generics or suspended functions/coroutines are not 100% ready yet.</p><p>We consider both issues minor, as we’ve been able to work around them by creating Kotlin MultiPlatform wrappers around native libraries or functionalities.</p><p>We’re aware we’re amongst the early adopters of Kotlin Multiplatform, and we’re confident that our experience will get better as the number of companies embracing it grows, and the <a href="https://android-developers.googleblog.com/2019/05/kotlin-is-everywhere-join-global-event.html">Kotlin-Everywhere community</a> gets bigger.</p><h3>How we evaluated all cross-platform technologies</h3><p>We limited our study to the technologies with the largest adoption, defined as the number of StackOverflow questions, GitHub repositories, Google trends, and companies endorsing the technology.</p><p>We ended up shortlisting six technologies, namely: React Native, Flutter, NativeScript, Ionic, Xamarin and Kotlin Multiplatform (MLP).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9L_3yyamOBwWktzsln25wA.png" /></figure><p>Each cross-platform technology was then benchmarked against these four criteria:</p><ul><li>Quality: Does this enable us to produce the same results as with native?</li><li>Learning curve: Does it leverage existing skill sets in the team or company?</li><li>Team cohesion: Does it allow us to move together as a team in the same direction?</li><li>Hiring: Can we tap into a good pool of candidates to expand our development team?</li></ul><p>Each technology in our shortlist came with its own trade-offs. In the spirit of failing fast, we focussed on identifying the shortcomings of each option and determining whether those represented a show stopper for our specific case.</p><p>Here’s a short summary of the reasons why we discarded each technology.</p><p>The main red flag on <a href="https://facebook.github.io/react-native/">React Native</a> was the experiences of <a href="https://medium.com/airbnb-engineering/sunsetting-react-native-1868ba28e30a">AirBnB’s</a> and <a href="https://adtmag.com/articles/2018/07/10/abandon-react-native.aspx">Udacity’s</a> engineering teams, and how they ended up maintaining three codebases as opposed to one — which largely defeats the purpose of embracing a cross-platform technology.</p><p>On top of that, we couldn’t tap into our team’s existing knowledge — while at SuperAwesome we use JavaScript extensively, we’re geared towards an Angular ecosystem.</p><p>We found <a href="https://www.nativescript.org/">NativeScript</a> lacked both <a href="https://docs.nativescript.org/guides/integration-with-existing-ios-and-android-apps/extend-existing-android-app?_ga=2.99957016.763145308.1563475317-1019445790.1563475317">mature integration with native codebases</a> as well as a large enough developer pool.</p><p>We think a deep integration with native is a must-have feature for any cross-platform technology to achieve the same qualitative results as with native in specific cases. For this same reason we discarded <a href="https://ionicframework.com/">Ionic</a>, which allows for hybrid web-based app development only.</p><p>We decided against <a href="https://flutter.dev/">Flutter</a> and <a href="https://visualstudio.microsoft.com/xamarin/">Xamarin</a> because they would introduce a new tech stack to the company (based on Dart or .Net), in which we had no experience. Flutter also came with the con of having a small pool of candidates, which would have caused delays on hiring.</p><p>Do you want to help us solve problems like these to make the internet safer for kids? <a href="https://www.superawesome.com/careers/">We’re always looking for engineers</a> to join the mission!</p><p><em>Originally published at </em><a href="https://www.superawesome.com/2019/08/06/how-we-developed-our-new-video-platform-rukkaz-as-a-cross-platform-mobile-app/"><em>https://www.superawesome.com</em></a><em> on August 6, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6001b4fd9b13" width="1" height="1" alt=""><hr><p><a href="https://medium.com/superawesome-engineering/how-we-developed-our-new-video-platform-rukkaz-as-a-cross-platform-mobile-app-6001b4fd9b13">How we developed our new video platform Rukkaz as a cross-platform mobile app</a> was originally published in <a href="https://medium.com/superawesome-engineering">SuperAwesome Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to implement Redux in Swift]]></title>
            <link>https://medium.com/swlh/how-to-implement-redux-in-swift-a2a0e7b7f10e?source=rss-bec66a7522c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2a0e7b7f10e</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Liviu Coman]]></dc:creator>
            <pubDate>Sat, 03 Aug 2019 13:55:50 GMT</pubDate>
            <atom:updated>2019-08-05T21:48:57.017Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BiopWRcn6D8I_thawOu3hA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@patrikward?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Patrick Ward</a> on <a href="https://unsplash.com/search/photos/mac?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In my previous <a href="https://medium.com/swlh/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079">series of articles</a> I explored how to implement the Redux pattern in Kotlin, on Android. The reality is that the same can be applied in Swift, on iOS, without much hassle.</p><p>Of course, there are already a few libraries out there that implement a unidirectional data flow. One that I’ve tried is <a href="https://github.com/ReSwift/ReSwift">ReSwift</a>. But the good thing about Redux is that it’s pretty simple to implement and thus you can easily roll out your own version, one that’s better suited to your needs.</p><p>This is what we’re going to do in this article, using Swift5 and XCode 11 Beta 4. Which means we’re going to use some of the new features in Swift 5 — like <a href="https://developer.apple.com/documentation/swiftui">SwiftUI</a> — to make our code even better. The example we’ll finally implement is the classic counter app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*sXsYeVnthqU70pSoQC2zjQ.png" /></figure><p>Before we can build it, we’ll first need to create a few building blocks.</p><h3>Building blocks</h3><p>Let’s start from the very abstract and move our way down to the concrete.</p><p><a href="https://redux.js.org/introduction/three-principles">Redux</a> operates with a few basic elements: the Action, the State, the Reducer and the Store, with each one playing a different, distinct, part.</p><p>We can define the Action and the State as nothing more than two empty marker protocols:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70e90f010c81b68c6b4c6e6a8a0f87e3/href">https://medium.com/media/70e90f010c81b68c6b4c6e6a8a0f87e3/href</a></iframe><p>The Reducer is just a function that takes in an Action and a State in order to return a new State.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/669d0bdec7db9953f0b32ccb9b2d8fd0/href">https://medium.com/media/669d0bdec7db9953f0b32ccb9b2d8fd0/href</a></iframe><p>Reducers are the only entities allowed to change the app’s state. They play an important role in handling the app’s business logic.</p><p>Finally, there’s the Store. We want our simple Store to have three basic characteristics:</p><ul><li>to hold the only reference to the whole app State</li><li>to intercept actions and pass them to Reducers</li><li>to notify listeners of State changes</li></ul><p>Thus, an abstract Store will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70a15dc293a21896fbb57ad6bbe9abe9/href">https://medium.com/media/70a15dc293a21896fbb57ad6bbe9abe9/href</a></iframe><p>A few key points here:</p><p>We’ve made the Store implement Swift5’s new <a href="https://developer.apple.com/documentation/swiftui/bindableobject">BindableObject</a> protocol. This will allow us to remove a lot of the subscription and notification boilerplate down the line.</p><p>Secondly, we’ve defined the Store as being generic of a type “S conforming to ReduxState” so we can reuse this code with any State we might want, in different apps.</p><p>A default simple implementation might look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0dcadf96e7fe8e1eb2d691b943e1ca3/href">https://medium.com/media/d0dcadf96e7fe8e1eb2d691b943e1ca3/href</a></iframe><p>Notice we’ve hidden away the mutable state property to avoid any temptation to ever change it manually.</p><p>Notice also the <a href="https://developer.apple.com/documentation/combine/passthroughsubject">PassthroughSubject</a> called willChange. This comes from another new Apple framework, called <a href="https://developer.apple.com/documentation/combine">Combine</a>, and is the only requirement the BindableObject protocol imposes on its implementers. Using it we can send out an update each time the internal State changes.</p><h3>Implementation</h3><p>Now that we have our building blocks in place, it’s time to use them to create the logic for our simple counter app.</p><p>In order to do all of the above, we’ll need a State to hold the counter value and two actions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8f0c009261de7e8372ce2613f4ea500/href">https://medium.com/media/a8f0c009261de7e8372ce2613f4ea500/href</a></iframe><p>So far, so good. Next we’ll also need to add business logic in the mix by defining a CounterStateReducer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51d12b875c4fc3ec1cb548c0165aa622/href">https://medium.com/media/51d12b875c4fc3ec1cb548c0165aa622/href</a></iframe><p>Notice the Reducer always returns a <strong><em>new instance</em></strong> of CounterState if the increment or decrement actions pass through. This is a key point for all Reducers: they must never simply mutate existing state, but rather enforce immutability by returning new State instances.</p><p>Side effects of having such strict rules are that most reducers will look similar to the one above and that they’re quite straight forward to test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c19ffa90657dafcf0ca9a667744d38ed/href">https://medium.com/media/c19ffa90657dafcf0ca9a667744d38ed/href</a></iframe><p>Finally, we can initialise our app’s Store with all the entities we’ve built:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9034963fe0d486f8a164d2e7eb4b2e05/href">https://medium.com/media/9034963fe0d486f8a164d2e7eb4b2e05/href</a></iframe><p>Since we’ve built the DefaultStore to also be generic, we can use it with different states in different apps and it would all work the same way.</p><h3>The app</h3><p>Finally we get to the app side. As seen in the previous screenshot, it’s pretty simple: two buttons, one to increment and one to decrement, and a label presenting the current counter value.</p><p>The basic code for it is shown below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63e408fce5ddfcbccfc9ecbf2cf4752a/href">https://medium.com/media/63e408fce5ddfcbccfc9ecbf2cf4752a/href</a></iframe><p>Of course, it doesn’t yet do anything. We first need to connect it to the Store by adding an <a href="https://developer.apple.com/documentation/swiftui/objectbinding">ObjectBinding</a> reference to it. ObjectBinding is the reverse of BindableObject, which our Store implements. SwiftUI will automatically link these two together so that each time the Store’s internal PassthroughSubject calls send(), the View will be redrawn.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bac3a15b0dc450f17d67614070c1ad57/href">https://medium.com/media/bac3a15b0dc450f17d67614070c1ad57/href</a></iframe><p>Next, in order to update the value of the counter Text we can create a computed property, called “props”, that binds to the current Store State. The name “props” is pretty common in React and ReactNative to describe the properties of a certain view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13f4ea78d9867ccd4e8d44a36021f656/href">https://medium.com/media/13f4ea78d9867ccd4e8d44a36021f656/href</a></iframe><p>Finally, we can replace the empty actions our increment and decrement buttons have right now with appropriate calls to the store’s dispatch method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b93d9a07ecef5d34966ba87400fdd98d/href">https://medium.com/media/b93d9a07ecef5d34966ba87400fdd98d/href</a></iframe><p>And that’s all there is to it. When taken together, the Store, State, Action and Reducer code as well as the CounterView code form a unidirectional system.</p><p>The View dispatches Actions to the Store following a user interaction, the Store intercepts these Actions and produces a new State with the help of a Reducer and finally the View gets notified of changes and redraws itself.</p><p>This article is one of a multi-part series exploring State, UI and Redux and how we can leverage knowledge of these topics to write more modular and testable code in both iOS and Android.</p><ul><li><a href="https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">How to make your Views a function of State in Kotlin</a></li><li><a href="https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217">How to decouple Views from their State in Kotlin</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079">How to implement Redux in Kotlin Part 1: the basics</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-2-real-world-example-8c9f1fa10e87">How to implement Redux in Kotlin Part 2: real world example</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2a0e7b7f10e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/how-to-implement-redux-in-swift-a2a0e7b7f10e">How to implement Redux in Swift</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to implement Redux in Kotlin Part 2: real world example]]></title>
            <link>https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-2-real-world-example-8c9f1fa10e87?source=rss-bec66a7522c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c9f1fa10e87</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Liviu Coman]]></dc:creator>
            <pubDate>Sun, 14 Jul 2019 19:43:54 GMT</pubDate>
            <atom:updated>2019-07-15T11:57:08.018Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wyxuq21keffc5b0d_lMkUw.jpeg" /></figure><p>This is the second article in a series about implementing the Redux pattern in Kotlin.</p><p>In the <a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079">first part</a> we built all of the main Redux entities that help power a simple counter app from the ground up.</p><p>That’s a really convenient way of getting into thinking about State, Reducers, unidirectional data flow, etc, but it’s not as useful as a real world example. With that I mean code that more closely resembles what we’d write on a day to day basis. That’s why in this post we’re going to look at how we can apply the Redux pattern to an app we’ve started in <a href="https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">this article</a> and continued in <a href="https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217">this one</a>.</p><p>Feel free to first check those out, but as a reminder let’s see where we’re at.</p><p>This is the view we want to show on screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*upzDPstqQP4grKd04f6gJw.png" /></figure><p>And this is all the code that we have so far, neatly decoupled:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ffac8b3d54e92ca960d342bc91f987b/href">https://medium.com/media/2ffac8b3d54e92ca960d342bc91f987b/href</a></iframe><p>How do we add Redux to the architecture we currently have?</p><p>The good news is that it’s pretty decoupled as it is. There’s bits we won’t need to touch at all, like the ViewStates and the redraw or binder methods. Those are things that pertain to the UI layer more than anything else, whilst Redux is squarely focused on the business layer. Which means that where we need to focus is on the Repository and the ViewModel. We can replace them with our Store.</p><p>Before we can do that, however, we need to start just as we did in the counter app example, by defining how our State and Actions will look like.</p><h3>Building blocks</h3><p>Let’s lay out our first State:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b143b181536b66cde58be8954d3cd2b2/href">https://medium.com/media/b143b181536b66cde58be8954d3cd2b2/href</a></iframe><p>Our contact State is pretty simple and resembles the ContactDTO we defined as part of our Data layer. Indeed, one option would be to have the ContactState hold a reference to a ContactDTO directly. I don’t like this approach too much, since it gives our app’s State knowledge of data layer objects and thus couples it tightly together.</p><p>Going one step further, we can imagine our app holds a number of contacts, not just one. We want to hold them all in memory, so we can use a structure like a List, Set or a Map to defined all of the contacts in our app. That can be our global app state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75a90f7a635cf838d8416d4ea86b5b47/href">https://medium.com/media/75a90f7a635cf838d8416d4ea86b5b47/href</a></iframe><p>Finally, in a real world app, errors of different kinds do happen. It’s important we acknowledge that in our State and there’s various ways we can do that. One way is we can treat all errors as global events. We can show a popup or toast or any other type of error message when that happens. And we can define our ErrorState in relation to our AppState like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4b697946288599ab2bad56c1594fff6/href">https://medium.com/media/c4b697946288599ab2bad56c1594fff6/href</a></iframe><p>Now that we’re satisfied with our AppState, we can define our Actions.</p><p>Looking at the State and at what our contacts app needs to do, we can infer a number of actions:</p><ul><li>we want to signal to the Store a new contact has been loaded</li><li>we want to signal we’ve added or removed a contact from favourites</li><li>and finally we want to signal if there’s been an error</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e52500252e8133562cf6b558fd6aa38e/href">https://medium.com/media/e52500252e8133562cf6b558fd6aa38e/href</a></iframe><h3>Mutating state</h3><p>With our State and Actions in place, just as before, we can write our reducers — the business logic units that will take the place of the ViewModel. In this case we have three reducers to write, one for each state: ContactState, ErrorState and AppState.</p><p>ErrorStateReducer is the simplest one so we’ll start with that. Generally, we want to return a new ErrorState any time the GeneralError action passes through and null otherwise.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96093672dea13e4976b10cc6e50c7d3a/href">https://medium.com/media/96093672dea13e4976b10cc6e50c7d3a/href</a></iframe><p>ContactStateReducer is a bit trickier so let’s break it down action by action.</p><p>When the LoadedContact action passes through, we want to either add or replace a new entry in the contactState Map.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70242cd99981d31c3f89e62638d7f7f8/href">https://medium.com/media/70242cd99981d31c3f89e62638d7f7f8/href</a></iframe><p>Conversely, when the ToggleFavourite action passes through, we want to check if a contact already exists in the Map, and if so, toggle the isFavourite flag to either true or false.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/560f06c2907f12b76b81cf39b94b9ae9/href">https://medium.com/media/560f06c2907f12b76b81cf39b94b9ae9/href</a></iframe><p>Finally we can get to the AppStateReducer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/260c3e76f3b1d3a5ec9ed081d82e93f6/href">https://medium.com/media/260c3e76f3b1d3a5ec9ed081d82e93f6/href</a></iframe><p>This one’s a bit different than all other reducers we’ve written so far. It’s called a top-level reducer and it’s useful when you have a complex state tree. What it does is it always returns a new instance of the State and delegates creating each sub state to other individual reducers. This is a nice way of separating concerns.</p><h3>Usage</h3><p>With State, Actions and Reducers out of the way, we can focus on the last part — getting rid of ViewModels, Repositories, etc. To do that we have to go to our two Interactor implementations: LoadContactInteractor and HandleFavouriteContactInteractor. In both these cases we can replace ViewModel references to direct Store and Api references, like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/624b41e5151990eeccd7166d35023687/href">https://medium.com/media/624b41e5151990eeccd7166d35023687/href</a></iframe><p>Finally, we need to change our ContactComponent and remove the listener it had on the ViewModel with a StoreSubscriber:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38fecaf53d6354a5e14404d4a74019da/href">https://medium.com/media/38fecaf53d6354a5e14404d4a74019da/href</a></iframe><p>And that’s basically it. Now we’ve further improved our initial decoupled architecture. We’ve grouped all our business logic into easily testable Reducers, we’ve made sense of all our app’s state by keeping a single copy of it in the Store and we’ve made reasoning about the entire system that much easier by applied a strict unidirectional data flow.</p><p>For reference, the final code we’ve come to is this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a11a633500bf4f34a81712e956bfdf66/href">https://medium.com/media/a11a633500bf4f34a81712e956bfdf66/href</a></iframe><p>This article is one of a multi-part series exploring State, UI and Redux and how we can leverage knowledge of these topics to write more modular and testable code.</p><ul><li><a href="https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">How to make your Views a function of State in Kotlin</a></li><li><a href="https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217">How to decouple Views from their State in Kotlin</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079">How to implement Redux in Kotlin Part 1: the basics</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-2-real-world-example-8c9f1fa10e87">How to implement Redux in Kotlin Part 2: real world example</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c9f1fa10e87" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to implement Redux in Kotlin Part 1: the basics]]></title>
            <link>https://medium.com/swlh/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079?source=rss-bec66a7522c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/db2854613079</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Liviu Coman]]></dc:creator>
            <pubDate>Sun, 14 Jul 2019 19:43:15 GMT</pubDate>
            <atom:updated>2019-08-04T11:50:33.195Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wyxuq21keffc5b0d_lMkUw.jpeg" /></figure><p>During my past <a href="https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">two</a> <a href="https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217">articles</a> we saw how we can look at views as a function of state and how we can decouple our application’s business logic from the views themselves in order to create more modular and SOLID code.</p><p>Throughout those articles, I mentioned Redux quite a lot — and how it inspired me to think about views as local stores of some state. That’s no accident. In the past few years, Redux has been fundamental about changing the way I think about big front-end systems and has been my default tool for making sense and simplifying complicated problems.</p><p>The beauty of Redux is that it solves one problem really well, and it does so by being dead simple.</p><p>The problem: how do you organise and make sense of your app’s state and business logic?</p><p>The solution: enforce a strict unidirectional data flow and a single source of truth for all your app’s state.</p><p>For all its simplicity though, a lot of Redux tutorials can be daunting, especially if you’re used to traditional front end patterns like MVVM, MVP or MVC. On top of all the new concepts you have to familiarise yourself with, like Store, Action or Reducer, by far the hardest thing is to get in the mindset of unidirectional data flow.</p><p>This is what this two part series aims to explore. First, we’ll create all the building blocks for applying the Redux pattern in the classic counter app. We won’t stop there, though. Redux is meant to simplify complex systems. The kinds that both junior and experienced engineers find hard getting their heads around. So in the second part we’ll apply Redux to a real life example: the contacts app we developed in the first two articles.</p><h3>Building blocks</h3><p>Let’s begin by getting an idea of the simple counter app we want to build:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x-29HclnPl_YzwVOGv_sLQ.png" /></figure><p>and the code we start out with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c02aeef7c5b1c842c0f5082f37dafd8c/href">https://medium.com/media/c02aeef7c5b1c842c0f5082f37dafd8c/href</a></iframe><p>We want to get to a point where, using the Redux pattern, we can increment and decrement the counter by using the two buttons available to us.</p><p>In order to do that, we can begin with the first fundamental concept in Redux, the State.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b499b8c7d36e33e8d2c385973212a34e/href">https://medium.com/media/b499b8c7d36e33e8d2c385973212a34e/href</a></iframe><p>In simple terms, State is defined as the overall data and values that make your app work. In the second part of this series we’ll explore more advanced state trees but for now our counter app’s State is just an integer value. We can wrap that value inside a data class and call it our CounterState.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb57a731bab0fed4ce934920d8db5b72/href">https://medium.com/media/cb57a731bab0fed4ce934920d8db5b72/href</a></iframe><p>Now we need a way to operate on this State. This is where Redux starts showing its way of thinking. I mentioned earlier that Redux enforces, by design, a unidirectional data flow. What does that mean? To answer that question let us look at how Redux <strong>doesn’t</strong> want us to change State.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70fe5480097d49b721b910e85ae36c91/href">https://medium.com/media/70fe5480097d49b721b910e85ae36c91/href</a></iframe><p>The above is, obviously, code that works. But is has drawbacks, chief amongst them being that we’re letting our click listeners manipulate state directly and also dictate business logic by incrementing and decrementing values.</p><p>That makes the whole Activity harder to test and adds additional responsibilities to our code.</p><p>Redux solves this by introducing a new concept, called the Action.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94da98d1593eecad6db5464e1957ce57/href">https://medium.com/media/94da98d1593eecad6db5464e1957ce57/href</a></iframe><p>An Action is an intent we use in Redux to signal a state change. It’s an indirect way of specifying how we want our whole system to change. For our simple example, we need three CounterActions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cce1ce2b4d48169c24d42904fb1505e3/href">https://medium.com/media/cce1ce2b4d48169c24d42904fb1505e3/href</a></iframe><p>So our code should become something similar to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f83b98a11fe28e8dd5e8dd42c2e74ffd/href">https://medium.com/media/f83b98a11fe28e8dd5e8dd42c2e74ffd/href</a></iframe><p>Of course, we don’t have all the building blocks in place right now. What does it mean to dispatch an Action? And how does anything change on screen when we do that?</p><p>Before we can answer those questions, we need to introduce another concept, called the Reducer.</p><h3>Mutating state</h3><p>Okay, so we’ve talked about State and we’ve talked about Actions. Clearly, those two entities need to be linked somehow. The way we link them is through a Reducer. A Reducer is simply a pure function that takes in an old State and an Action and returns a new State. Reducers are the worker bees of Redux. They are the only way through which we change State in our system and thus drive all the business logic of our app. There should be no exceptions to this rule.</p><p>We can define a generic Reducer as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cfb52457a616609b0f6098322e876b5/href">https://medium.com/media/0cfb52457a616609b0f6098322e876b5/href</a></iframe><p>And from that we can create a CounterStateReducer to:</p><ul><li>create a new default CounterState when the Init action passes through</li><li>create a new incremented CounterState when the Increment action passes through</li><li>create a new decremented CounterState when the Decrement action passes through</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c24df10ba5b08762160542f5af5e08b/href">https://medium.com/media/1c24df10ba5b08762160542f5af5e08b/href</a></iframe><p>As promised, we’ve moved all of the business logic and all of the state mutation associated with our CounterActivity into this specialised entity we’ve implemented as a free function. Not only have we separated concerns pretty well, but we can unit test this in a straight forward manner.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f47e2f103f4bcc27a8fc81459d7a349b/href">https://medium.com/media/f47e2f103f4bcc27a8fc81459d7a349b/href</a></iframe><p>The power to unit test complex pieces of business logic in a simple way is one of the things I like about Redux.</p><p>Okay — we’ve learned about State, Actions and Reducers — how do all of these fit together? What binds them? The answer is the last piece of the Redux puzzle, the Store.</p><h3>Tying it all together</h3><p>First of all, what is a Store in Redux?</p><p>In short, it’s the single source of truth for your whole application. Meaning:</p><ul><li>it alone holds a reference to the whole application State</li><li>it alone can change State when an Action gets dispatched through it</li><li>and it alone notifies subscribers when there’s been a State change</li></ul><p>Following this definition and looking from the outside, we’ll need a way to dispatch Actions to the Store and a way to register subscribers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70ac2cd0ecdfb66bf758167b56a9ef49/href">https://medium.com/media/70ac2cd0ecdfb66bf758167b56a9ef49/href</a></iframe><p>Dispatching is clear. Let’s think about Store subscribers. They need to be an entity that can be injected with the most up to date State. The simplest Subscriber is just a function that takes in State and returns Unit.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4081344c08bb26552470a0db9e7a43f1/href">https://medium.com/media/4081344c08bb26552470a0db9e7a43f1/href</a></iframe><p>and thus our Store definition transitions to this, where we’ve also bound it to a generic parameter of type State.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38f628bc1e2b635b6a0eefbd17613e4c/href">https://medium.com/media/38f628bc1e2b635b6a0eefbd17613e4c/href</a></iframe><p>From here we can create the actual concrete implementation of Store, which we’ll name DefaultStore, and start filling in the details.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94e7f1c17b0c013c41766b9d1debcce6/href">https://medium.com/media/94e7f1c17b0c013c41766b9d1debcce6/href</a></iframe><p>Adding or removing subscribers is pretty simple and we can start with that. We’ll need a data structure to hold them all. A Set or List will do just fine.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2822e292e77b123f6b48a8b29b56cc33/href">https://medium.com/media/2822e292e77b123f6b48a8b29b56cc33/href</a></iframe><p>In order to dispatch actions we’ll first need to create an internal reference to our state and initialise it with an injected value:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0b8b81520ac3f44dd32596dc198843e/href">https://medium.com/media/b0b8b81520ac3f44dd32596dc198843e/href</a></iframe><p>Once we’ve done that, we can use a Reducer to implement the dispatch method. Here’s how that looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8aa1d3adfc2c0463a9ce33e5a8cb9a93/href">https://medium.com/media/8aa1d3adfc2c0463a9ce33e5a8cb9a93/href</a></iframe><p>And this is how the whole DefaultStore looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2b316804ef3de17b0ffcd18b8e8639e/href">https://medium.com/media/f2b316804ef3de17b0ffcd18b8e8639e/href</a></iframe><p>We’ve built a fully functioning Store in under 25 lines of code. That’s pretty amazing. Now it’s time to use it.</p><h3>Usage</h3><p>First, we can put our Store in a Dependency Injection system as a singleton. This is important, because we only want one Store per app.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d007b9991b958c5373721c1c7f5f88f/href">https://medium.com/media/6d007b9991b958c5373721c1c7f5f88f/href</a></iframe><p>We can obviously use Dagger2 or any other DI system that’s available to us.</p><p>Next we can replace the decrement and increment button click listeners with calls to dispatch actions to the Store:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57bd40adb1ab3e3af4bf3cd1027d7dac/href">https://medium.com/media/57bd40adb1ab3e3af4bf3cd1027d7dac/href</a></iframe><p>and finally we can subscribe to the Store and render the screen each time the State changes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95bca4c291e020a41220497bf7c70563/href">https://medium.com/media/95bca4c291e020a41220497bf7c70563/href</a></iframe><p>With this setup we can clearly see the unidirectional data flow that Redux prefers. Any change we want to effect on the state we do so by dispatching an Action to the Store. Anything we want to render on screen only happens when the Store tells us State has changed, via the Subscriber.</p><p>To sum up, let’s look at all the code we’ve written so far:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04eca6386aac21e40274aa9f6d75e5a3/href">https://medium.com/media/04eca6386aac21e40274aa9f6d75e5a3/href</a></iframe><p>A fully functioning Redux-powered app in less than 100 lines of code!</p><p>This article is one of a multi-part series exploring State, UI and Redux and how we can leverage knowledge of these topics to write more modular and testable code.</p><ul><li><a href="https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">How to make your Views a function of State in Kotlin</a></li><li><a href="https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217">How to decouple Views from their State in Kotlin</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079">How to implement Redux in Kotlin Part 1: the basics</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-2-real-world-example-8c9f1fa10e87">How to implement Redux in Kotlin Part 2: real world example</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db2854613079" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079">How to implement Redux in Kotlin Part 1: the basics</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to decouple Views from their State]]></title>
            <link>https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217?source=rss-bec66a7522c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fcf59192217</guid>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Liviu Coman]]></dc:creator>
            <pubDate>Sun, 30 Jun 2019 17:43:12 GMT</pubDate>
            <atom:updated>2019-07-15T11:56:57.020Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to decouple Views from their State in Kotlin</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wyxuq21keffc5b0d_lMkUw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@sapegin?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Artem Sapegin</a> on <a href="https://unsplash.com/search/photos/react?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In my <a href="https://medium.com/@liviucoman/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">previous post</a> I showed how we can structure our Views so that they’re easier to reason about and more deterministic by thinking about them as a function of state. I did that by ensuring two things:</p><p>First, the View’s own state — text draw on screen, selected buttons, loaded images — only gets updated through a redraw method.</p><p>Second, the View only interacts with the outside world through an Interactor.</p><p>That’s already a huge step up, in my opinion.</p><p>There’s room to improve even further, though, if we think about the number of responsibilities the view has.</p><p>I’m a big fan of the Single Responsibility Principle, which generally states that a class should have one and only one reason to change. Looked at from that lens, our view has three responsibilities:</p><ul><li>Ensure it renders correctly on screen. The ContactView is pretty simple and static, but more advanced views can have animations, effects, etc.</li><li>Assign the values it gets from a ViewState to the corresponding subview fields</li><li>And connect the Interactor with the corresponding button click listeners</li></ul><p>There’s a further problem if we look at it from a business or functional perspective. What if we want to reuse the same view with different data sources? Or what if we want to reuse the same view but have it perform different actions in different parts of an app? How can we achieve that?</p><p>Surely there are many good answers to the above two questions. I’m going to show a way that I’ve been tinkering with in my spare time that’s influenced by the way Components in React Native interact with State in Redux.</p><p>First, let’s recap how we laid out our View:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ndUi72ffO3Xyo0Z8Z3UFtw.png" /></figure><p>And what code we have so far:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15f2ec5712c778c019662e07111ca0e0/href">https://medium.com/media/15f2ec5712c778c019662e07111ca0e0/href</a></iframe><h4>Decoupling responsibilities: redrawing</h4><p>How do we start to break up this View, then?</p><p>We can start by taking the first responsibility and abstracting it away to its core components.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/210348722ecd2f12534f43633b93a975/href">https://medium.com/media/210348722ecd2f12534f43633b93a975/href</a></iframe><p>We can then provide a default implementation for a ContactView, based off the work we just did.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bde3da2256097fe46e3832d8a6b2bb6/href">https://medium.com/media/3bde3da2256097fe46e3832d8a6b2bb6/href</a></iframe><p>In a way, we’re back where we first started. But what’s more important is the above View can focus on just displaying information on screen in a nice way, whether by animating the followButton or highlighting the phoneTextView.</p><p>But how can the view actually do anything at all now? How can it even redraw itself?</p><p>Well, we can start by redefining the original ContactView.ViewState as its own independent entity. It no longer is the view state of a particular view but rather a view state that represents some form of contact information.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fa228f2d9a3f1087712d7084c24342d/href">https://medium.com/media/5fa228f2d9a3f1087712d7084c24342d/href</a></iframe><p>The information within stays the same and the abstraction on top of business layer models that this view state represents stays the same.</p><p>Here is where it gets interesting! We can take the ContactView and the ContactViewState — two independent entities at this point — and join them via a third one: redraw as a function from view state to view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28e1065777436e55a34fd225d073c48f/href">https://medium.com/media/28e1065777436e55a34fd225d073c48f/href</a></iframe><p>The above is really powerful.</p><p>By moving the redraw method outside of the view we’ve solved two problems in one go: we’ve removed the extra responsibility from the ContactView and we’ve also opened the possibility of reusing ContactView in multiple parts of our app. All we need to do is define different redraw methods, that take different view states, and join them with the same view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54b02519aca781e8baafe8231239290e/href">https://medium.com/media/54b02519aca781e8baafe8231239290e/href</a></iframe><p>What’s more, testing UI components with Espresso is still as straight forward as before, only instead of testing the view’s behaviour we are testing a particular redraw function in relation to a view and a state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b85d02253e117d9e008d21f13d551164/href">https://medium.com/media/b85d02253e117d9e008d21f13d551164/href</a></iframe><h4>Decoupling responsibilities: interaction</h4><p>At this point we’ve simplified our view tremendously and created a suite of redraw functions to meet our needs. But how can we make the view load itself or perform some sort of action?</p><p>Let’s go back to our initial Interactor:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f8aac6668ea9c87cd8df3860d86a8de/href">https://medium.com/media/6f8aac6668ea9c87cd8df3860d86a8de/href</a></iframe><p>It’s clear that it has two responsibilities: loading a contact and adding / removing it from a list of favourites. We want our code to adhere to the Single Responsibility Principle as much as possible so we can start by creating two independent Interactors, one for each responsibility.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77bc2186a6bf798d281f5f04bba49e4f/href">https://medium.com/media/77bc2186a6bf798d281f5f04bba49e4f/href</a></iframe><p>We can apply exactly the same principle as with the redraw methods to create a series of binding methods. Each method will bind some Interactor to some view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7391ddad4a8af65b68d47a428f4c8c68/href">https://medium.com/media/7391ddad4a8af65b68d47a428f4c8c68/href</a></iframe><p>Once more, when we test all of this, it’s not a view’s interaction we’re testing, but the relationship between an Interactor and a view. As an added benefit, the tests themselves become simpler and more concise.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/296dc9db56c76f57ac390e09396d50cb/href">https://medium.com/media/296dc9db56c76f57ac390e09396d50cb/href</a></iframe><p>One last thing about interactors: they’re petty generic.</p><p>One one side, that’s really good. We’re totally shielding our views from any business layer knowledge like contact ids, etc.</p><p>One the other side, it’s not so great because we don’t exactly know what contact to load or to follow anymore.</p><p>How do we solve that? As usual, there’s many options. We defined interactors as interfaces so maybe their implementation can hold those details. Whilst that’s totally valid, I like to go for a more functional approach, where the actual implementation of these interactor interfaces is a function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1c3461cf68837b25951a80b573814b8/href">https://medium.com/media/b1c3461cf68837b25951a80b573814b8/href</a></iframe><p>Or, to be more precise, a function that returns an anonymous implementation of an interactor, based off some view model and a contact id. The implementation may vary, of course, but the general idea is the function acts as a factory that correctly implements the interactor and connects it to the broader code base.</p><h4>Bringing it all together</h4><p>Finally let’s bring all of these together. We have our views, our interactors, our independent redraw methods and our independent bind methods.</p><p>We can start by placing our interactors into an easily accessible Dependency Injection object. We can place repositories, view models, etc in here as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c5d362ae8ebc8c06cb2423971016407/href">https://medium.com/media/4c5d362ae8ebc8c06cb2423971016407/href</a></iframe><p>The above example is pretty basic but it’ll do the job. You can use Dagger 2 and other similar DI frameworks to achieve a better result.</p><p>Finally in an Activity or Fragment we can join the views with the interactors and the redraw methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c23cfa4479fe217cf5d0f2830841282b/href">https://medium.com/media/c23cfa4479fe217cf5d0f2830841282b/href</a></iframe><p>The above is alright and if we want, we can leave it at that. But it also puts a lot of responsibility on the Activity to hook up all these pieces — a responsibility that can quickly grow out of hand when we add multiple views.</p><p>Coming back to the beginning of the article, where I mentioned React Native and Redux, there’s a concept there called a connected component. It’s basically the result of connecting a normal component — essentially our view — to the wider redux environment. We can take that concept and create a ContactComponent of our own:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09d317434f4d0e8e7f084dcea8330731/href">https://medium.com/media/09d317434f4d0e8e7f084dcea8330731/href</a></iframe><p>We’ve essentially moved all of that responsibility that we placed inside the Activity into another function that acts as a factory for our view.</p><p>Finally, the Activity just becomes this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25194fad04fd3bec56778cbb18849ffb/href">https://medium.com/media/25194fad04fd3bec56778cbb18849ffb/href</a></iframe><p>To sum up, this is most of the code that we’ve written so far:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/456d546495c65fd20f66f7ef5f1f5e46/href">https://medium.com/media/456d546495c65fd20f66f7ef5f1f5e46/href</a></iframe><p>This article is one of a multi-part series exploring State, UI and Redux and how we can leverage knowledge of these topics to write more modular and testable code.</p><ul><li><a href="https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">How to make your Views a function of State in Kotlin</a></li><li><a href="https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217">How to decouple Views from their State in Kotlin</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079">How to implement Redux in Kotlin Part 1: the basics</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-2-real-world-example-8c9f1fa10e87">How to implement Redux in Kotlin Part 2: real world example</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fcf59192217" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217">How to decouple Views from their State</a> was originally published in <a href="https://medium.com/zero-equals-false">Zero Equals False</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to make your Views a function of State in Kotlin]]></title>
            <link>https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6?source=rss-bec66a7522c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f95a3b130cd6</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Liviu Coman]]></dc:creator>
            <pubDate>Sun, 23 Jun 2019 08:50:55 GMT</pubDate>
            <atom:updated>2019-07-15T11:56:45.864Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8yz3hkO3ynHV2qYGARynJg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@sapegin?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Artem Sapegin</a> on <a href="https://unsplash.com/search/photos/react?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>One of the more interesting changes that has happened in the last few years is the appearance of state management libraries like Redux, Flux or MobX. This has generated a healthy debate amongst developers about — essentially — how to best make sense of the ever growing complexity in software.</p><p>Redux &amp; co are there to help you make sense and cut down the complexity that arises from state and state changes. Redux specifically does that by enforcing a few simple rules:</p><ul><li>there is only one source of truth, the state</li><li>that state is immutable</li><li>new state can only be created by pure functions called reducers as a result of some previous action</li></ul><p>In mobile, a lot of state resides in the UI layer, sometimes explicit, sometimes implicit. Things like the text value of a label, the enabled or disabled state of a button or the hidden or visible status of an image are all essentially expressions of some state in the system.</p><p>Unfortunately, all too often I see UI code that looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbf371d80e68a17d46e3e066279cfbd4/href">https://medium.com/media/fbf371d80e68a17d46e3e066279cfbd4/href</a></iframe><p>This is not bad in and of itself. It does what it’s supposed to do and you might argue that’s good enough. And indeed, for a simple view, this doesn’t stand out as bad. But there’s a subtle problem: it mixes business layer state with view layer state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26da0bd70f82f362328cc21e92271bb8/href">https://medium.com/media/26da0bd70f82f362328cc21e92271bb8/href</a></iframe><p>Every time we write code like this we’re making our system harder to reason about. In the above example, a single button’s selected or unselected state depends on a variety of factors, spread out throughout the code. Clearly it will only get worse the more complex the view gets and at one point it will stop scaling. When that happens, the cognitive load needed to properly understand all possible code paths becomes too great.</p><p>In this post I’m going to show the way I think about UI state. The fact that I mentioned Redux at the start isn’t a coincidence. I’m going to take some best practices and learnings from there in order to build a View as a function of its state. In this way it’ll be easier to reason about, more deterministic and simpler to test.</p><p>For start, let’s get a visual idea of what the view will look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ndUi72ffO3Xyo0Z8Z3UFtw.png" /></figure><p>You can see it’s supposed to display a user’s profile image and name, as well as a telephone number and a follow button to indicate favourite users. If you tap on the button you can add or remove a user from your favourites. If you tap on the phone number, you can start a phone call with your contact.</p><p>Before we jump any further, let’s write out how this view will look like in code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe2c045fb0e03cf4a05d96f9d7f30888/href">https://medium.com/media/fe2c045fb0e03cf4a05d96f9d7f30888/href</a></iframe><p>Now, I said above one of the key pillars of Redux is that state is immutable and can only be created by pure functions called reducers. Essentially, this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a8dabb1e8083fb1265ca8a395a5a9e7/href">https://medium.com/media/6a8dabb1e8083fb1265ca8a395a5a9e7/href</a></iframe><p>Does that mean that building views as a function of state implies creating a new instance each time something changes? On mobile, that would be a very expensive thing to do. What we can do, instead, is think about the full state of the view and redraw it only based on that. So if we look at the layout above, what state do we have? At it’s simplest, it’s this:</p><ul><li>a URI resource for the contact’s profile image</li><li>a string for the user name</li><li>a string for the phone number</li><li>and a local image resource for the favourites button</li></ul><p>We can write it in code like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be402273a5d4b408813506db5de28c27/href">https://medium.com/media/be402273a5d4b408813506db5de28c27/href</a></iframe><p>In the above example, the view tells the outside world how it thinks its state should look like, using Inversion of Control.</p><p>The state is also tailor made for the view. It contains only the information directly needed by the view in order to render itself and nothing more.</p><p>With that in place, it’s just a matter of creating a method that redraws the view using a given ViewState:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a325be7a7ccd07e13db482b5d187a90/href">https://medium.com/media/0a325be7a7ccd07e13db482b5d187a90/href</a></iframe><p>You can see we’ve ended up with a list of simple, deterministic, assignments. There is no other code in the view that changes what gets shown on screen.</p><p>Testing this with Espresso or any other UI testing framework becomes extremely easy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f709c73f0ec4d09d6e1394d7fd3da4e6/href">https://medium.com/media/f709c73f0ec4d09d6e1394d7fd3da4e6/href</a></iframe><p>In fact, we’ve also decoupled a lot of business logic that previously existed in the View by letting the ViewState deal with converting some data layer object into a format that’s appropriate for the View. We can also unit test that separately.</p><p>However, in the initial example we could also add or remove a contact from our favourites list, with the corresponding change in UI.</p><p>In order to re-enable that functionality <strong>and</strong> keep the deterministic redraw mechanism in place, we can add a new interface, called Interactor:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41a1755f510daed6b9bbe0bf2840cc9c/href">https://medium.com/media/41a1755f510daed6b9bbe0bf2840cc9c/href</a></iframe><p>Essentially, this mechanism allows the View to tell the outside world (Activities, Fragments, ViewModels, etc) that it would want some sort of operation performed. In practice this can look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5bae20a3c36bc0b7b862a183cb618bae/href">https://medium.com/media/5bae20a3c36bc0b7b862a183cb618bae/href</a></iframe><p>From here on, the view delegates these responsibilities to whatever class implements the Interactor interface. As before, testing the Interactor becomes extremely easy using both Espresso and Mockito.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6228d9dcef4f34c1ec69643973872e3/href">https://medium.com/media/e6228d9dcef4f34c1ec69643973872e3/href</a></iframe><p>In conclusion, we’ve created a ContactView that redraws as a function of some state. We’ve done that by grouping all redraw operations in one method and moving all other logic outside of the view, as part of an Interactor.</p><p>The full code of our view looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/617704569835c6284af452f1a016395d/href">https://medium.com/media/617704569835c6284af452f1a016395d/href</a></iframe><p>And now that we have the full picture of all the changes we did to our initial example, we can see an unintended good side-effect is that we’re in a much better position to handle the complexity of our view as it grows and changes.</p><p>If we need to add more subviews like address fields, workspace, etc, we only need to change a few lines in the redraw method. Likewise, if we need to extend the capabilities of the view, we only need to add more methods to the Interactor.</p><p>This article is one of a multi-part series exploring State, UI and Redux and how we can leverage knowledge of these topics to write more modular and testable code.</p><ul><li><a href="https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">How to make your Views a function of State in Kotlin</a></li><li><a href="https://medium.com/zero-equals-false/how-to-decouple-views-from-their-state-5fcf59192217">How to decouple Views from their State in Kotlin</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-1-the-basics-db2854613079">How to implement Redux in Kotlin Part 1: the basics</a></li><li><a href="https://medium.com/@liviucoman/how-to-implement-redux-in-kotlin-part-2-real-world-example-8c9f1fa10e87">How to implement Redux in Kotlin Part 2: real world example</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f95a3b130cd6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/how-to-make-your-views-a-function-of-state-in-kotlin-f95a3b130cd6">How to make your Views a function of State in Kotlin</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scalable data layer architecture for mobile apps]]></title>
            <link>https://medium.com/@liviucoman/scalable-data-layer-architecture-for-mobile-apps-57335f4613fd?source=rss-bec66a7522c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/57335f4613fd</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Liviu Coman]]></dc:creator>
            <pubDate>Sun, 02 Jun 2019 16:12:49 GMT</pubDate>
            <atom:updated>2019-06-23T10:26:00.961Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z68F4SMRusBjJ7Q8ox4Waw.jpeg" /></figure><p>At the heart of most modern applications sits some type of data layer. It’s there to help us connect to the network and get new information, store information locally or allow the user to send information to the cloud.</p><p>From the onset we can see that there’s a lot of weight placed on getting the data layer right.</p><p>But what does right mean? There’s probably as many answers as there are people, but for me it boils down to three key pillars:</p><ul><li>It has to be agnostic to what technology is used behind the scenes</li><li>it should provide a unified api for both local and remote operations</li><li>it has to be easily testable</li></ul><p>In this article I’m going to present my take on building such a system. I’m going to use data from a fictional movie database as a starting point and focus on getting information about a movie and the actors that played in it.</p><p>We’ll start off top to bottom and define a marker interface called Model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e5bfa8b5f7289e7d3f266340ec65fb9/href">https://medium.com/media/8e5bfa8b5f7289e7d3f266340ec65fb9/href</a></iframe><p>From there we can extend Movie and Actor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f43bc213818d95c603ab4c135c99d454/href">https://medium.com/media/f43bc213818d95c603ab4c135c99d454/href</a></iframe><p>Just these two should be enough to define the basic model space needed by our movie data layer.</p><p>Notice I’m not adding any methods to load, save or parse data. I’m a big fan of the Single Responsibility Principle, and I believe all those operations need to be carried out by some other entity. That entity we can call the Api.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f61dc02c24f34d0370a6578e48bc94b/href">https://medium.com/media/1f61dc02c24f34d0370a6578e48bc94b/href</a></iframe><p>However, the above, in and of itself, doesn’t provide any useful information. What methods should an Api contain? One way to go about it is look at what endpoints are exposed by a fictional cinema backend when it comes to getting information about a movie and its actors.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/700557bb9c561d3210ae61eddb98135b/href">https://medium.com/media/700557bb9c561d3210ae61eddb98135b/href</a></iframe><p>We can see we have three endpoints of interest. One approach in designing our own data layer is to mimic these endpoints. Our Api interface suddenly becomes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52666d0efddbc855e15f2d3bbfb83beb/href">https://medium.com/media/52666d0efddbc855e15f2d3bbfb83beb/href</a></iframe><p>I’ve seen this approach multiple times in many data layers.</p><p>However it falls short on some accounts.</p><p>What happens when we want to add a new endpoint? We’ll need to go back and change our interface definition as well as any implementations.</p><p>What happens when the details of one endpoint change? We’ll need to go back and change the implementations or even the interface.</p><p>What happens when the above becomes full of too many methods? We’ll need to split it into sub-apis that will have a ripple effect on our whole code base.</p><p>Clearly it’s a good approach only when you don’t expect too many changes in the future. But there’s a way to solve this issue in a more elegant way, by adhering to both the Single Responsibility Principle as well as the Interface Segregation Principle of SOLID.</p><p>Basically, we can rewrite our Api as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f0671a11e13a3c204a3126659a601c8/href">https://medium.com/media/7f0671a11e13a3c204a3126659a601c8/href</a></iframe><p>We’ve defined our Api entity as something that can be called, meaning it can do some form of action. Next we can generalise the type of inputs and outputs a typical api call can have by using generics:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5534b571bfa2af3bfb5de290b89ca7c7/href">https://medium.com/media/5534b571bfa2af3bfb5de290b89ca7c7/href</a></iframe><p>Starting from this one general Api definition we can define as many specific types of apis as we need to drive our business logic.</p><p>We’ll define them as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4dc75e23b078b94326bed01e9229d96c/href">https://medium.com/media/4dc75e23b078b94326bed01e9229d96c/href</a></iframe><p>The naming we used provides clear meaning about their intention. What it doesn’t do is give any clear insight into what sort of operations might be performed inside them: network operations, disk I/O, database, etc.</p><p>In order to unlock this, we’ll need to define two additional constructs: the Connection and the Parser.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d64cdf578e339b95abce2398cd8919c4/href">https://medium.com/media/d64cdf578e339b95abce2398cd8919c4/href</a></iframe><p>These can be seen as lower level helpers for any apis that we will implement. They split data gathering and data processing into two distinct operations:</p><ul><li>one that connects to any type of data source (remote, local, etc) and returns a raw string</li><li>one that parses any raw string (xml, JSON, binary, etc) into a meaningful output, like an Actor or Movie model.</li></ul><p>This separation is really important. In a lot of data layers out there it’s not enforced, meaning you might be tied in to a particular parser or format.</p><p>Starting from these two generic definitions we can go and specialise them even further, so they’ll cater to our different needs.</p><p>For example, an HttpConnection could look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30c2179c6eae2131a2a86299848f33b3/href">https://medium.com/media/30c2179c6eae2131a2a86299848f33b3/href</a></iframe><p>Notice a pattern in what we’re doing. We’re always going from the more generic and abstract, such as Connection, to the more specific, such as HttpConnection. Equally valid here would be a SocketConnection or SQLConnection, etc.</p><p>We can do the same thing for the parser. JSON is one of the most widely used formats these days:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b35a57a30ab165d3bc67e12a6434d37/href">https://medium.com/media/4b35a57a30ab165d3bc67e12a6434d37/href</a></iframe><p>but equally easy we can add a XMLParser or BinaryParser, etc.</p><p>In all of the above cases we’re still very much in the realm of abstractions. Neither HttpConnection, SocketConnection or JSONParser should specify how those operation actually happen and what underlying technologies are being used, whether it’s OkHttp, Moshi, Realm or Firebase, etc.</p><p>All of these details we can push even further away into actual classes that interact with and isolate 3rd party dependencies:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c7c8b4469ddb793580c7965341ff636/href">https://medium.com/media/8c7c8b4469ddb793580c7965341ff636/href</a></iframe><p>Now, finally, we can use all that we’ve so far defined to start implementing our actual Apis and drive some real business logic.</p><p>Let’s start by creating a concrete SearchMoviesApi.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b470abe8539f6a4a52540db7ddf5d592/href">https://medium.com/media/b470abe8539f6a4a52540db7ddf5d592/href</a></iframe><p>From the get go we inject a HttpConnection type and a Parser into the constructor. It makes sense since any api that would want to connect to a remote source will need that capability.</p><p>Inside this Api we can then define a HttpRequest specific to it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89cbfa8915c6a92a282a7bbc2a6c78bc/href">https://medium.com/media/89cbfa8915c6a92a282a7bbc2a6c78bc/href</a></iframe><p>Finally, the main body of the call method will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67d2843871f3912629f73d116111357f/href">https://medium.com/media/67d2843871f3912629f73d116111357f/href</a></iframe><p>We can do the same thing with the rest of the apis we would want to implement.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b7b419d71b7d5f6de9da4c9847e8f9b/href">https://medium.com/media/7b7b419d71b7d5f6de9da4c9847e8f9b/href</a></iframe><p>And finally, on the app side, we can set them up and use them like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a96bafdf828449e4803d241b6b66f916/href">https://medium.com/media/a96bafdf828449e4803d241b6b66f916/href</a></iframe><p><strong>Conclusion</strong></p><p>When building this cinema data layer you’ve noticed how we’ve gone from very abstract components to concrete implementations, always from top to bottom. That’s because we’ve been working all along in a layered architecture inspired by Robert C. Martin’s <a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html">clean architecture</a>. In such an architecture, code lives in different layers. Each layer depends only on the layer above and does not have any knowledge of the layer below. In this case, we’ll just have three layers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/359/1*GX9YKTq4IKoTBElYQCGiWQ.png" /></figure><ul><li><strong>Entities</strong>: this layer contains all the system wide business rules.</li><li><strong>Use cases</strong>: this layer takes the entities defined previously and specialises them for different purposes.</li><li><strong>3rd parties</strong>: finally in this layer we’ll relegate all of the actual low-level platform implementation details</li></ul><p>More specifically, if we were to map out the interfaces and classes we’ve defined, this is how it’ll look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/359/1*bgh8Y9y2at0LjiKpQM0sRw.png" /></figure><p>This layered approach ties in and facilitates the three key pillars I presented when we set out to create the data layer.</p><p>Let’s review them and see how we stack up:</p><ul><li>It has to be agnostic to what technology is used behind the scenes</li></ul><p>Since we’ve used inversion of dependency liberally throughout the codebase, higher level classes don’t directly depend on lower level classes, they both depend on common abstractions.</p><p>Thus, our RemoteSearchMovieApi depends on HttpConnection or Parser interfaces without knowing how these are implemented (OkHttp, Retrofit, Moshi, Gson, etc).</p><p>The whole idea behind thinking about code like this is that it leads to a system that’s more resilient to change. One day you can use OkHttp as your network provider and and the next switch it to Retrofit with minimal changes on the codebase. The same goes for switching between JSON and XML formats, etc.</p><ul><li>it should provide a unified api for both local and remote operations</li></ul><p>On the app side, we’ve shown that we can write code that connects to local or remote data sources using the same common interface.</p><ul><li>it has to be easily testable</li></ul><p>Finally, we’ve also eased testing. For low level classes such as OkHttpConnection or MoshiMovieListParser we can write unit tests in isolation and for higher level classes we can write integration tests, swapping different combinations of connections, parsers or any other dependencies they might need.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57335f4613fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>