<?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 Davide Lorenzi on Medium]]></title>
        <description><![CDATA[Stories by Davide Lorenzi on Medium]]></description>
        <link>https://medium.com/@davide.lorenzi?source=rss-15cce78bcf1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cKyXHNKzFfwZUlLh</url>
            <title>Stories by Davide Lorenzi on Medium</title>
            <link>https://medium.com/@davide.lorenzi?source=rss-15cce78bcf1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 00:04:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@davide.lorenzi/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[Type erasure in Swift: a real example]]></title>
            <link>https://medium.com/wise-engineering/type-erasure-in-swift-a-real-example-8c1864881f65?source=rss-15cce78bcf1------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c1864881f65</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Davide Lorenzi]]></dc:creator>
            <pubDate>Tue, 21 Jan 2020 09:30:23 GMT</pubDate>
            <atom:updated>2021-06-07T15:31:42.379Z</atom:updated>
            <content:encoded><![CDATA[<p>A practical case where hiding the type becomes useful</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BHBz_35BxVYsgha6A84EZQ.jpeg" /></figure><p>There are many articles about type erasure, and I’m sure that for everyone it’s a fascinating concept the first time you see it. <br>Yet, it can be a bit challenging to understand how it works at the beginning, and to visualise some real practical usages of it.<br>This article tries to help in that: it shows an example of type erasure on real production code.<br>We won’t cover too much theory about it, but mainly how we got to this implementation given our needs.</p><h4>Introduction</h4><p>One of the current big features in our iOS app is the ability to support 3rd-party profiles, which we called Multi User Access (MUA).<br>Each profile that the user may get access to comes with a set of permissions, which tells which kind of operations the user can actually perform on the given profile.<br>Some examples of the operations controlled by a permission are: start a new transfer, initiate the verification process, open a new balance account, edit profile information, and so on.<br>They are not related to a specific part of our product, but can touch anything.<br>Worth noting that more permissions will be also added in the future, along new features being released.</p><h4>Requirements</h4><p>For the above reason, we wanted to be able to define and group them by domain. <br>For example, we have:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45d898dc2fa0dc8469fe4521efdee52e/href">https://medium.com/media/45d898dc2fa0dc8469fe4521efdee52e/href</a></iframe><p>The <em>Permission</em> protocol is just a marker, with the only added requirement of conforming to <em>Equatable</em>: for any two given permissions in the same group (same type), we want them to be comparable.</p><p>Then we have some permissions: <em>TransferPermission</em> and <em>BalancePermission</em> are just some examples among many other permissions we have<em>. <br></em>Giving they are all implemented as <em>enum</em>, we got the <em>Equatable</em> conformance for free.<br>We also need somehow to be able to compare across different permission types, for example <em>TransferPermission.create</em> with <em>BalancePermission.withdraw. </em>[<strong>requirement 1</strong>]</p><p>For each of our profiles we need to store a set of these permissions too [<strong>requirement 2</strong>]: this will let us compare its content with a given permission (ie. <em>BalancePermission.create</em>) to know if we have actually been granted the permission for that specific operation.</p><p>Another important thing we want to have is the independence of these permission types we declare. As mentioned above, we’ll add new permissions in the future, and we don’t want to keep adding new glueing logic each time that happens. [<strong>requirement 3</strong>] <br>This means, <em>BalancePermission</em> (and other components too) needs to be completely unaware of <em>TransferPermission </em>and vice-versa<em>, </em>for example.<br>We will add/remove permissions as we want, but will only need to operate on those.</p><p>If we try to declare a Set (or an Array) of <em>Permission</em> on the <em>Profile</em> type, we’ll immediately be rewarded with the notorious error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kRvxzIXiOR6a58b36WCpuw.png" /></figure><p>The error is telling us exactly what is happening: the <em>Permission</em> protocol, extending the <em>Equatable</em>, has constraints on <em>Self</em>.<br> This is not the path to follow, then.</p><h4>Type erasure to the rescue</h4><p>A simple solution is to create a non-generic concrete type, that will not rely on any <em>associatedType</em> linked to any protocol (this is the reason for the previous error, in fact, acting like a ‘generic’ protocol).<br>This is what we came up with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc0d8af3280c510a23c0516a7f1ef220/href">https://medium.com/media/cc0d8af3280c510a23c0516a7f1ef220/href</a></iframe><ul><li>1: we still need to keep a copy of the original permission that we’ll need to compare against to, but we can’t use the ‘generic’ protocol <em>Permission</em>. Hence, we keep it as an <em>Any</em> type</li><li>2: the <em>init() </em>function is generic (note: not the<em> WrappedPermission</em> type itself!) on its parameter, to ensure we only pass a concrete <em>Permission</em> type. This is required to be able to instantiate the component. At compilation time, in fact, the caller will know which concrete type is being passed to this initialiser. Without this, we would get the same error we shown above (about “<em>Self or associatedType requirements</em>”)</li><li>3: this is the function that we’ll later invoke from within a <em>Profile</em>: as above, this function is generic on the parameter that it takes, which is the <em>Permission</em> we want to compare with the one we keep as a reference. We need this to (i) ensure that we are passing a valid <em>Permission </em>and to make the compiler happy, and (ii) to try to cast the internal permission to the same type <em>U. </em>Worth a note that this doesn’t have to be the same concrete type as the one passed in the <em>init()</em>. I actually gave it a different name (<em>U</em> instead of <em>T</em>) to highlight this fact, even though it is not required</li><li>4. combining the previous steps, and by knowing that the <em>Permission</em> protocol requires the <em>Equatable</em> conformance, we can easily apply the equality operator to check whether the two permissions are the same.</li></ul><p>The points 1 and 3 are where the type erasure really happens: the <em>associatedType </em>is removed from the public interface (1) allowing us to pass around the <em>WrappedPermission</em>,<em> </em>while at the same time keeping our functionality by moving the ‘generic’ on a function level (3).</p><p>The missing bit is the protocol that will be implemented by the <em>Profile</em> type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7f6470c15ac8a6f36606608f8373c26/href">https://medium.com/media/a7f6470c15ac8a6f36606608f8373c26/href</a></iframe><p>It’s easy to see how this generic function is similar to the previous ones we saw on the <em>WrappedPermission</em> type, and actually required for the same reason: to be able to capture the ‘generic’ type of the <em>Permission</em>.<br>The <em>Profile</em> type will also hold a reference to a list of <em>WrappedPermission </em>he has been granted access to: its implementation of this function searches among the granted permissions until it finds the one we are looking for, eventually, returning false otherwise.</p><p>All our initial requirements have been satisfied: a <em>Profile</em> can hold a list of heterogeneous permission types through the usage of <em>WrappedPermission</em>, while at the same time keeping the comparison functionality, without the need of defining any relationship and/or logic between them.<br>This means that in the future we can easily add new types conforming to the <em>Permission </em>protocol: these will just work with our ecosystem, without the need to update any other component to work with the newly created permission.</p><p>Finally, this is how it looks on the caller side:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6b90e63a9780c712b8e47f07c1d093e/href">https://medium.com/media/d6b90e63a9780c712b8e47f07c1d093e/href</a></iframe><p>P.S. Interested to join us? We’re hiring. Check out our <a href="https://www.wise.jobs/search/?t=engineering&amp;sort=oldest&amp;utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=engineering_blog">open Engineering roles</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c1864881f65" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wise-engineering/type-erasure-in-swift-a-real-example-8c1864881f65">Type erasure in Swift: a real example</a> was originally published in <a href="https://medium.com/wise-engineering">Wise Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing a Flow on iOS]]></title>
            <link>https://medium.com/wise-engineering/flow-an-evolution-of-coordinator-3-of-3-cc727129982c?source=rss-15cce78bcf1------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc727129982c</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[mobile]]></category>
            <dc:creator><![CDATA[Davide Lorenzi]]></dc:creator>
            <pubDate>Wed, 28 Aug 2019 16:03:19 GMT</pubDate>
            <atom:updated>2021-06-07T15:36:43.263Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to write unit tests</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RdZB8hkVWdTv9YHQYH79YQ.png" /></figure><p>This article is the last one of the series about Flow, and focuses on unit testing.<br>Be sure to check the previous articles to fully understand this one: <a href="https://medium.com/@davide.lorenzi/flow-an-evolution-of-coordinator-1-of-3-c8dcf11770f2">building blocks</a> and <a href="https://medium.com/@davide.lorenzi/flow-an-evolution-of-coordinator-2-of-3-d9f4bb23596e">example of Flow implementation</a>.</p><p>Given a <em>Flow</em> is basically a container that coordinates the presentation of different view controllers, and that may eventually start sub-<em>Flows</em>, what we want to be testable is:</p><ul><li>which is the presented view controller</li><li>which is the active sub-flow</li></ul><p>Since any sub-<em>Flow</em> is still a normal <em>Flow</em>, we can write unit tests against it too, and check for the same conditions (almost like a recursion). This way, it’s easy to test all the view controllers that are being presented.</p><p>The second point, in particular, is important for another reason: by being able to reason about a sub-<em>Flow</em> as a whole component (with a particular <em>FlowResultType</em>), in fact, we don’t have to rely anymore on any particular view controller that can appear in that sub-<em>Flow</em>. If some logic inside the sub-<em>Flow</em> will change in the future (to maybe switch the initial view controller to be presented), all our tests will still be good. That is, we do not rely on any implementation detail of the sub-<em>Flow</em>, but rather on its interface.</p><p>Let’s see how the <a href="https://medium.com/@davide.lorenzi/flow-an-evolution-of-coordinator-2-of-3-d9f4bb23596e">examples shown in the previous part</a> could be tested.</p><h4><strong>Simple Flow with a single VC</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ac6bcce53352c57d028f9ee0ebdc441/href">https://medium.com/media/1ac6bcce53352c57d028f9ee0ebdc441/href</a></iframe><p>This is pretty straightforward. After creating the <em>Flow</em> we want to test, <em>SimpleModalFlow</em>, we set its <em>viewControllerPresenterFactory</em> to be the mocked version(1). The <em>ViewControllerPresenter</em> that this factory will generate are mocked versions which won’t physically present any view controller on the screen, but simply keep a queue of them. <br>Also, to test when the <em>flowFinished(result:dismisser:)</em> is being called, we set the <em>FlowHandler</em> callbacks to save the result passed as a parameter (2).</p><p>What these unit tests are doing is:</p><ul><li>(3) ensuring that the <em>started</em> callback is called</li><li>(4) checking that the presented view controller is of a specific type</li><li>(5) checking that the FlowHandler finished callback is called with the expected flow result, when the delegate function is triggered</li></ul><h4><strong>Flow with different VCs + a sub-Flow</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1fd9630c9612c0e58013730eadc263c/href">https://medium.com/media/a1fd9630c9612c0e58013730eadc263c/href</a></iframe><p>The code in this case is pretty much the same as the previous example. We can still test which view controller is presented at any given time by doing the very same operations seen before. The only possible difference is that as we have more view controllers here, we may have a slightly different way to simulate how their presentation is triggered.</p><p>The main difference is that we have a sub-<em>Flow </em>being<em> </em>launched, in this case: testing this is still really simple. We simply check the <em>flowInspectable</em> property for the <em>activeSubFlowResultType</em>, and compare it against to the <em>FlowResultType</em> that we expect to be presented.</p><p>With this version of <em>FlowInspectable</em> implementation, we don’t get to know which <em>Flow</em> instance is actually launched. This means that if our <em>Flow</em> under test has, for example, an A/B test check, and at runtime can decide between 2 different sub-<em>Flows</em> to launch (both of them must have the same <em>FlowResultType</em> though: they must be doing the same thing, and be compatible), we won’t be able to detect this detail. (check the <a href="https://medium.com/@davide.lorenzi/flow-an-evolution-of-coordinator-1-of-3-c8dcf11770f2">first article</a>, <em>FlowInspectable</em> section).</p><p>One important thing to note is that all these tests are synchronous: we don’t have to add any timed expectation, which will make our tests much slower (imagine if you would have to wait just 1sec for each test containing a view controller you are presenting).</p><h4><strong>Conclusion</strong></h4><p>With this final part, we have seen how to write unit tests for the Flow. In particular, we know how to check about the presented view controller, and also if there is any sub-Flow which is currently being launched by the Flow we are testing. Finally, we can test when a flow completes its execution and test that the result provided is correct.</p><p>In this series we started with a real-world problem, and what were the benefits of solving it in a more elegant and composable way.<br>After providing a more formal definition of our requirements, we implemented all the required building blocks.<br>We shown how to use them in order to build more complex Flows, and how to eventually compose them.<br>Finally, we saw how to write unit tests against our newly build Flows, to ensure they are really doing what is expected.</p><p>As all these components are actively used in our project, they are constantly tweaked and improved to add more functionalities and to cover more scenarios and use cases.<br>I will therefore try to keep this series up to date with the actual implementation of these components.<br>At the same time, all kind of feedback is very appreciated :)</p><p>P.S. Interested to join us? We’re hiring. Check out our <a href="https://www.wise.jobs/search/?t=engineering&amp;sort=oldest&amp;utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=engineering_blog">open Engineering roles</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc727129982c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wise-engineering/flow-an-evolution-of-coordinator-3-of-3-cc727129982c">Testing a Flow on iOS</a> was originally published in <a href="https://medium.com/wise-engineering">Wise Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementation of a Flow on iOS]]></title>
            <link>https://medium.com/wise-engineering/flow-an-evolution-of-coordinator-2-of-3-d9f4bb23596e?source=rss-15cce78bcf1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9f4bb23596e</guid>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Davide Lorenzi]]></dc:creator>
            <pubDate>Wed, 28 Aug 2019 16:02:18 GMT</pubDate>
            <atom:updated>2019-08-30T09:05:31.110Z</atom:updated>
            <content:encoded><![CDATA[<h4>Examples of implementation and composition of Flows</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1S4HWC2vSHPleGs6xLn8Mw.jpeg" /></figure><p>This article builds on information covered in the <a href="https://medium.com/@davide.lorenzi/flow-an-evolution-of-coordinator-1-of-3-c8dcf11770f2">first writing</a> of this series.</p><p>After having had a look at the main building blocks of a Flow, we can now see how we can implement them. All the examples shown here are built around fictional view controllers: their only requirement is to have a way to communicate back the result that they can produce. In a real world scenario, these results will be much more complex components: the data they produce could be a complex object, a DTO, or whatever you want. There is no limitation.</p><p>Also, I’m used to have a <em>Flow</em> create a VIPER module (maybe through an external factory, which contains and links together all the dependencies): it will get a reference to the view controller to present; the <em>Flow</em> will also be set as the Router. In fact, in practical scenarios, the <em>Flow</em> most of the times implements the Router interface of different VIPER modules, one for each module it needs to present.</p><p>If you use different design patterns, that is fine. Your logic to tie together all the components will eventually be slightly different, but what won’t change is how to operate a <em>Flow</em> component.</p><h4><strong>Simple Flow with a single VC to present</strong></h4><p>Let’s start from the a very basic example. We have a <em>ButtonViewController</em> (code not show here, but can be found in the <a href="https://github.com/davidelorenzitw/Flow">GitHub project</a>) which is simply showing a button. This view controller has a delegate property which is called when the button is pressed.</p><p>The example is really simple, but is all we need to show how to launch the <em>Flow</em>, how to handle the responses from any view controller that we presented, and finally how to pass back the flow result to the object which started it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/009c49b13b73fec3e65acd93e649bd31/href">https://medium.com/media/009c49b13b73fec3e65acd93e649bd31/href</a></iframe><ul><li>(1) we defined the result type for our <em>Flow</em> to be <em>SimpleModalFlowResult</em>, an enum with a single case (for what we want to demonstrate, this is enough)</li><li>(2) we have a factory, through which obtain a <em>ViewControllerPresenter</em></li><li>(3) after creating the view controller that we want to present and setting the delegate, we use the <em>ViewControllerPresenter</em> to let it present the view controller.</li><li>(4) we get back a <em>ViewControllerDismisser</em>, which we keep in a private var</li><li>(5) on the completion we also invoke the <em>flowStarted()</em> function on the <em>FlowHandler</em></li><li>(6) the <em>terminate()</em> function is dismissing the view controller presented in a non-animated way. This may be required sometimes when we want to terminate the Flow prematurely from the outside, without waiting for it to finish</li><li>(7) once the delegate function is triggered, we invoke the <em>flowFinished(result:dismisser:)</em> function on the <em>FlowHandler</em>, passing the final flow result along with the <em>ViewControllerDismisser</em> responsible to unwind the UI state</li></ul><p>To launch this Flow, all we need to do is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f450458895f02c0307828cf1175f5118/href">https://medium.com/media/f450458895f02c0307828cf1175f5118/href</a></iframe><p>Here we are also using the <em>FlowController</em> to automatically manage the <em>Flow</em>, so we don’t have to manually keep a strong reference to it while it’s being presented. This is not strictly necessary in this example, but it just shows how we can use a <em>FlowController</em> even outside of a <em>Flow</em> (and not only for sub-Flows).</p><h4><strong>Flow with different VCs presented + a sub-Flow</strong></h4><p>Let’s see a more complex scenario, where a <em>Flow</em> has to present more than a single view controller. I’ll omit a lot of code not strictly relevant to understand how the overall process works (could find the complete version in the <a href="https://github.com/davidelorenzitw/Flow">GitHub project</a>).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7107c886dca0fac3f0de1bdea18fd48/href">https://medium.com/media/b7107c886dca0fac3f0de1bdea18fd48/href</a></iframe><p>The <em>start()</em> function is doing is pretty much the same as the previous example: presenting a view controller and setting the delegate. (1)</p><p>What changes are the actions we perform when the callback is called. (2)</p><p>We launch a new <em>Flow</em> (through the <em>FlowController</em>, which takes care of holding a strong reference until it has finished). (3)<br>On the <em>onCompleted</em> callback we switch over the result of the sub-Flow. (4)<br>In case of <em>.error</em>, we let the main <em>Flow</em> terminates with a <em>.canceled(reason:)</em> result. (5)<br>In case of <em>.success</em> , we present another view controller (6). When it will call the <em>confirmResult()</em> callback (7), we will dismiss it (8), and on the completion we will let the main Flow finishes with a <em>.completed(success: true)</em> result. This is done by calling the <em>flowFinished(result:dismisser:)</em> function on the <em>FlowHandler (9)</em>.</p><p>Let’s take a look at a part of the code inside the sub-Flow that we are here launching:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd66b546ae121229af13a15f140662bb/href">https://medium.com/media/fd66b546ae121229af13a15f140662bb/href</a></iframe><p>This shows one of the places where this <em>SimpleNavigationFlow</em> may terminates. It’s important to see that we always inform the caller through the <em>FlowHandler</em> functions: in this case using the <em>flowFinished(result:dismisser)</em>. <br>From this Flow’s point of view, in fact, we have no idea if it is being launched as a normal Flow (ie. from a view controller) or as a sub-Flow (what is happening in our specific case).</p><p>This example may look complex at first, but this is exactly what I wanted to show: how to handle different kind of tasks and how to combine them, depending on our needs:</p><ul><li>present a single view controller</li><li>launch a new sub-<em>Flow</em></li><li>either terminates the main <em>Flow</em>, or present another view controller</li><li>terminates the main <em>Flow</em></li></ul><p>All this without knowing at all what the sub-<em>Flow</em> is doing internally, but simply knowing its <em>FlowResultType</em>.</p><h4><strong>Build your Flow</strong></h4><p>With the examples shown above, it should now be easier to know how to create a <em>Flow</em>. The main things to remember about it are:</p><ul><li>use <em>ViewControllerPresenter</em> when presenting a view controller. Keep the reference of the <em>ViewControllerDismisser</em> to be able to later dismiss it.</li><li>communicate back to the caller through the <em>FlowHandler</em>, by invoking its <em>flowStarted()</em> and <em>flowFinished(result:dismisser:)</em> functions. The <em>ViewControllerDismisser</em> to pass here is the one which unwind what the <em>Flow</em> presented. In most of the cases it will be the one related to the first view controller that we presented, but it may be not. Up to you.</li><li>do not unwind the UI from within the Flow before calling <em>flowFinished(result:dismisser:) </em>: it should be responsibility of the caller to handle this. This will make the same <em>Flow</em> be reusable in different places, with different requirements (ie. dismiss the <em>Flow</em> vs. do not dismiss the <em>Flow</em> but leave it on the stack, or dismiss it animated vs dismiss it not animated).</li></ul><h4><strong>Real-world case: TransferWise</strong></h4><p>We are starting to use this approach in many new components that we are building. The Profile Flows (both the Personal and the Business one) are built upon this idea, for example.</p><p>The Business Flow is even launching the Personal Flow as a sub-Flow. It is also launching other sub-Flows along with standalone view controllers. It is presented in 5 different places throughout the app: all of them need to be handled differently (completely dismiss the flow when finished, either animated or not; or leave it on the navigation stack so the user can eventually come back; or perform some extra custom actions once finished). All of this has been easily achieved because of the properties of the Flow components.</p><p>The Onboarding Flow (shown when a new user create a new account) is composed, among the others, to launch the Business Flow as a sub-Flow. To make this happens, not a single line of code has been changed in the Business Flow, because of the modularity and how the components stick together.</p><h4><strong>Conclusion</strong></h4><p>We have seen how creating some Flows by putting together the different building blocks. We have seen a simple example, where only a view controller is being presented. We have also seen a more complex case, which handles multiple view controllers and a sub-Flow as well. With these examples, we have shown basically all you need to start create any Flow that you may need. The concepts shown here, in fact, could be reused on a much larger/different Flow that you may need: the core ideas and interactions between the parts are still the same.</p><p>In the <a href="https://medium.com/@davide.lorenzi/flow-an-evolution-of-coordinator-3-of-3-cc727129982c">next and final part</a> we’ll take a look at how to write unit tests for the Flow components that we just created.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9f4bb23596e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wise-engineering/flow-an-evolution-of-coordinator-2-of-3-d9f4bb23596e">Implementation of a Flow on iOS</a> was originally published in <a href="https://medium.com/wise-engineering">Wise Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flow: an evolution of Coordinator for iOS]]></title>
            <link>https://medium.com/wise-engineering/flow-an-evolution-of-coordinator-1-of-3-c8dcf11770f2?source=rss-15cce78bcf1------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8dcf11770f2</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[Davide Lorenzi]]></dc:creator>
            <pubDate>Wed, 28 Aug 2019 16:00:59 GMT</pubDate>
            <atom:updated>2021-06-07T15:33:46.857Z</atom:updated>
            <content:encoded><![CDATA[<h4>Defining Flows to allow composition of smaller ones to create a more complex Flow</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*jgzyZbJdDFoeLlaTasCcRQ.jpeg" /></figure><h4>Introduction</h4><p>We have already seen many articles about Coordinators, each one bringing a different flavour. The core idea, though, remains the same.</p><blockquote>A Coordinator is a component which handles the presentation of different view controllers.</blockquote><p>This way we can decouple view controllers from knowing about each other (increasing reusability also).<br>At the same time, a Coordinator encapsulates logically a set of view controllers, creating a new module, which is easier to reason about.</p><p>At TransferWise, we already use the concept of flow throughout the company, which is a sequence of steps (screens and user interactions) to achieve a final result.<br>For example, the Transfer Flow is in charge of gathering all the data in order to send a payment from A to B: this encapsulates a set of different operations.</p><p>A flow can be started from various places within the app: for example, the Transfer Flow can be started from the ‘New transfer’ button on the main screen, from a previous transfer by repeating it or from the Balance Account.</p><p>Sometimes you’ll need to nest another flow inside the parent flow to achieve your goal.<br>For example, the Transfer Flow has to eventually start the Profile Flow, to get more information about the user, before proceeding with its original intent.</p><h4><strong>Requirements for a Flow</strong></h4><p>With all of these criteria listed above, the rough requirements for a coordinator — what I call a flow — are these:</p><ul><li>a component that encapsulates and performs an activity that provides a final result as output</li><li>flexibility and customisability in presenting Flows from different places in the app</li><li>an easy way to chain and nest flows to create more complex ones</li><li>possibility to interchange between different Flows that have the same result type (ie. think of A/B testing, or if a feature is implemented in different ways depending the country we are in)</li></ul><p>For these flows, we also want to have:</p><ul><li>easy way of testing</li><li>have a practical way of handling their lifecycle</li></ul><blockquote>An important note to make is that I wanted all this to NOT be tied to any particular design/architecture pattern.<br>In some examples, I may sometimes refer to some VIPER components (which is what we mainly use at TransferWise), but the Flow as a concept does not depend on any particular design pattern.</blockquote><h4><strong>Related parts</strong></h4><p>This series is split in 3 parts for easier understanding, broken down to:</p><ul><li>first (this): introduction + building blocks</li><li>second (<a href="https://medium.com/@davide.lorenzi/flow-an-evolution-of-coordinator-2-of-3-d9f4bb23596e">here</a>): examples on how to use a Flow</li><li>third (<a href="https://medium.com/@davide.lorenzi/flow-an-evolution-of-coordinator-3-of-3-cc727129982c">here</a>): examples on how to write unit tests for a Flow</li></ul><blockquote>All the code shown here is already being used in production code. This works great for us, but there are of course possible improvements or things that may work better in a slightly different way for you.</blockquote><h3><strong>Building blocks</strong></h3><h4>Flow</h4><p>A Flow can be seen as a module which, once started, is expected to terminate producing a result. The caller should make no assumptions on how the <em>Flow</em> is working: this way, we can really make it reusable in different scenarios.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b0ed41cfed78cb9cf41df72317d4001/href">https://medium.com/media/6b0ed41cfed78cb9cf41df72317d4001/href</a></iframe><ul><li>(1) each <em>Flow</em> has a specific <em>FlowResultType</em>. This could be an enum, for example (with associated values, eventually), but it’s totally up to you</li><li>(2) a <em>FlowHandler&lt;T&gt;</em> is acting like a delegate, and is responsible to provide the result of the <em>Flow</em> and also to let the caller handle the dismissal of the flow itself</li><li>(3)<em> start()</em> and <em>terminate()</em>, to operate the Flow</li><li>the interface is clean of any other parameter, which the <em>Flow</em> protocol doesn’t need to know. They are in fact implementation details.</li></ul><h4><strong>FlowHandler</strong></h4><p>Through the <em>FlowHandler</em>, a <em>Flow</em> communicates back to the caller when it starts or finishes, providing also the result and a way to dismiss what the <em>Flow</em> presented.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8d9bdb148250ecf4eec62c79065c061/href">https://medium.com/media/d8d9bdb148250ecf4eec62c79065c061/href</a></iframe><p>This is just a wrapper, initialised passing two callbacks:</p><ul><li>(1) <em>started</em>: called when the <em>Flow</em> starts</li><li>(2)<em> finished</em>: called when the <em>Flow</em> completes. It has 2 parameters: the flow result T, and the <em>ViewControllerDismisser</em> (to let us handle the dismissal)</li></ul><h4><strong>ViewControllerPresenter &amp; Dismisser</strong></h4><p>These components are used to abstract how the view controllers are presented/dismissed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74892f64d537c19a79cc06da9732a2fd/href">https://medium.com/media/74892f64d537c19a79cc06da9732a2fd/href</a></iframe><p>With these interfaces we get a couple of benefits:</p><ul><li>inside the <em>Flow</em>, we don’t have to care about any presentation logic (UIKit), since that is abstracted. In bigger flows especially, this results in a much easier (and less error prone) way to switch between all the VCs we need to present (imagine if some of the view controllers are presented modally, and some pushed on the navigation view controller instead)</li><li>let the object that started the <em>Flow</em> decide how to dismiss it (animated/not animated), and if dismiss it at all</li><li>make the <em>Flow</em> more reusable, even as a sub-Flow. Since all that a <em>FlowHandler&lt;T&gt;</em> knows about is a <em>ViewControllerDismisser</em>, it’s simple to reuse a <em>Flow</em> when we have different constraints (more on this later)</li></ul><p>We also have a factory which provides us these components:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e30bc87cc700461421a9c276ee85391/href">https://medium.com/media/3e30bc87cc700461421a9c276ee85391/href</a></iframe><p>To be able to write synchronously unit test, this factory can be injected in the <em>Flow</em>.<br>This way, we can provide a mocked version that won’t actually present any view controller on screen, but simply put them in an array. Our unit tests could then simply check that array and assert any required condition.</p><h4>FlowInspectable</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c139f87a76469b914ec06685f0864e4f/href">https://medium.com/media/c139f87a76469b914ec06685f0864e4f/href</a></iframe><p>This only exposes the type of the sub-Flow active at any given moment.</p><blockquote>Note: we could eventually make this protocol expose the instance itself, rather than the Flow result type, if we want to be able to have more precise unit tests.</blockquote><h4><strong>FlowController (for sub-flows)</strong></h4><p>Finally, for when we need to handle sub-Flows, we have an additional component which can help us write less and more robust code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24db46d043f9367c093988a9b4f9a1b7/href">https://medium.com/media/24db46d043f9367c093988a9b4f9a1b7/href</a></iframe><p>It implements the <em>FlowInspectable</em> protocol, and adds a function. Its signature contains all the parameters required by the <em>FlowHandler</em>, which this <em>FlowController</em> is creating and assigning to the <em>Flow</em> passed as parameter.<br>Its responsibility is to keep a strong reference to the <em>Flow</em> being presented, and release it when it completes.</p><blockquote>This could have been done manually by the object launching a new flow, but I found it a bit harder to master/remember for new people approaching the Flow idea, and more error prone too.</blockquote><h4><strong>Conclusion</strong></h4><p>What we did cover in this first article: <br>- the definition of our problem<br>- the actual features we want to implement<br>- the basic building blocks and their purpose to create flows</p><p>You can find the complete project containing all these codes <a href="https://github.com/davidelorenzitw/Flow">here</a>.</p><p>In the <a href="https://medium.com/@davide.lorenzi/flow-an-evolution-of-coordinator-2-of-3-d9f4bb23596e">second part</a> we’ll see a concrete example of how to create a flow from scratch.</p><p>P.S. Interested to join us? We’re hiring. Check out our <a href="https://www.wise.jobs/search/?t=engineering&amp;sort=oldest&amp;utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=engineering_blog">open Engineering roles</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8dcf11770f2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wise-engineering/flow-an-evolution-of-coordinator-1-of-3-c8dcf11770f2">Flow: an evolution of Coordinator for iOS</a> was originally published in <a href="https://medium.com/wise-engineering">Wise Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>