<?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 Julio Mendoza on Medium]]></title>
        <description><![CDATA[Stories by Julio Mendoza on Medium]]></description>
        <link>https://medium.com/@j1c1m1b1?source=rss-869a553d6781------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*OPnBN7Cnf_SJ3bM3uzX72Q.jpeg</url>
            <title>Stories by Julio Mendoza on Medium</title>
            <link>https://medium.com/@j1c1m1b1?source=rss-869a553d6781------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 02:47:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@j1c1m1b1/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[Unidirectional Data Flow in for Android UIs]]></title>
            <link>https://medium.com/swlh/unidirectional-data-flow-in-for-android-uis-5edd1386f40d?source=rss-869a553d6781------2</link>
            <guid isPermaLink="false">https://medium.com/p/5edd1386f40d</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Julio Mendoza]]></dc:creator>
            <pubDate>Fri, 12 Feb 2021 20:01:28 GMT</pubDate>
            <atom:updated>2021-02-15T15:50:15.955Z</atom:updated>
            <content:encoded><![CDATA[<h3>Unidirectional Data Flow for Android UIs</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*P6Z3qqMIauO2azTP" /><figcaption>Photo by <a href="https://unsplash.com/@marjan_blan?utm_source=medium&amp;utm_medium=referral">Марьян Блан | @marjanblan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This article is co-authored with <a href="https://medium.com/u/7d293be6a5a4">Hans-Martin Schuller</a>, my Android development <em>brolleague</em> at HSE.</p><h3>Where is the code?</h3><p><a href="https://github.com/j1c1m1b1/UDF">Here</a></p><h3>Inspiration</h3><p>The ideas explored in this article are based on multiple sources that I recommend checking before diving any further:</p><ul><li><a href="https://www.youtube.com/watch?v=A2xyPZyoFUo">Etienne Caron — Simple MVI Architecture for Android</a></li><li><a href="https://www.youtube.com/watch?v=0IKHxjkgop4">Managing State with RxJava by Jake Wharton</a></li><li><a href="https://hackernoon.com/android-unidirectional-flow-with-livedata-bf24119e747">Android Unidirectional Flow with LiveData</a></li><li><a href="https://lordraydenmk.github.io//2021/modelling-ui-state/">Modeling UI State on Android</a></li></ul><h3>Introduction</h3><p>One of the main tasks of the <a href="https://www.hse.de/">HSE</a> Android app team is to rebuild the company’s <a href="https://play.google.com/store/apps/details?id=de.hse24">Android app</a>: taking old code as a reference and replace it with new code — a recurring Android development nightmare. Reading the old code made us ask the some questions I have asked myself before:</p><ul><li>How can different possible reactions to user inputs be written in a comprehensible manner, in order to make the code readable, robust and scalable?</li><li>How can we avoid having multiple flags in our Activities and Fragments that represent how the UI should look like at a given point in time?</li></ul><p>These questions are intended to be solved by the following snippets of code, heavily inspired by the sources listed at the top of the article.</p><p>There are multiple ways to achieve the proper display of information to the users and react to the input, the Android framework has evolved at an exponential rate in all directions, an example of this is are the <em>Lifecycle</em> library and specifically <em>ViewModel</em>s and <em>LiveData</em>.</p><p><em>ViewModels</em> are often used as direct intermediaries between user events over UI widgets and data sources, because of their tight connection to the lifecycle of their host <em>Fragments</em> and <em>Activities</em>.</p><p>Although there is a huge advantage of having <em>ViewModels</em> that receive user inputs and expose new data from these interactions, the generated code is often messy and difficult to translate into the clear differentiated states that the UI has to represent. An over-generalized user interaction in an Android app often looks something like this:</p><ul><li>The user navigates to a Fragment or Activity</li><li>Data starts to load from some data source</li><li>An indication of progress is shown to the user</li><li>Data is loaded</li><li>The UI is rendered</li><li>The user interacts with a widget in the view component</li><li>The input of the interaction (explicit or implicit) is sent to the data source to be operated on and obtain new information</li><li>An indication of progress is shown to the user…</li></ul><p>So on and so forth. It is easy to extract a common pattern from this, in fact it is so common that that’s exactly the reason LCE models exist.</p><h3>Components</h3><p>We use 3 basic elements that can model the unidirectional cycle of user interactions, data obtention and information rendering.</p><ul><li><strong>State</strong>: Represents a single state of the UI. It can be short or long lived.</li><li><strong>Action</strong>: Implements the method that takes: the input from an event, the contents of the previous state, and combines them through some business logic into a different state.</li><li><strong>UI Event</strong>: Represents an input given implicitly or explicitly from the user.</li></ul><p>Please note the order of these components. Normally the implementation tends to make the most sense when it is developed in the way these elements are listed.</p><p>Also of importance is the suspend modifier of the <em>perform</em> function, denoting the asynchronous execution of a transaction.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7458d5983bf684a8a7b364a26f92b023/href">https://medium.com/media/7458d5983bf684a8a7b364a26f92b023/href</a></iframe><h3>The event processor/state publisher</h3><p>In this case we are using a <a href="https://kotlinlang.org/docs/reference/coroutines/channels.html"><em>Channel</em></a> that sends UI Events that is consumed as a <a href="https://kotlinlang.org/docs/reference/coroutines/flow.html"><em>Flow</em></a> and then can be transformed: first into an Action Flow and then to a State Flow by performing each emitted action. This resulting Flow then can be collected to modify the UI according to the new resulting state.</p><p>The engine of the state machine is composed by an extension function on the UI Events <em>Channel</em> field. The implementation can be summarized as follows:</p><ul><li>The Events <em>Channel</em> is consumed as a <em>Flow</em></li><li>Transform the Events <em>Flow</em> into an Actions <em>Flow</em> by calling <em>toAction()</em><strong><em> </em></strong>on each emitted Event</li><li>Transform the Actions <em>Flow</em> into a States <em>Flow</em> by calling <em>perform()</em> on each emitted action</li><li>Collect the obtained states by passing each one in the <em>collectionHandler</em> lambda parameter</li></ul><p>There are some Flow transformation operations to ensure the correct sequential and distinct emission of the resulting states to be collected in the collector lambda parameter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54d62e01e2d8f9dd1a9acd46ef96d686/href">https://medium.com/media/54d62e01e2d8f9dd1a9acd46ef96d686/href</a></iframe><p>The previous state needs to be provided as this state machine doesn’t keep tabs on the previous states and delegates this functionality to its implementation.</p><p>Because the state machine is an <em>interface</em>, it doesn’t make sense for the <em>Channel</em> to be initialized since it has to be overridden by the implementation.</p><p>The choice of using an <em>interface</em> was made because this implementation is not limited to Android applications, and in the example project from where this code is extracted, the interface is declared in Kotlin only module.</p><p>The next steps describe the implementation of this state machine and its components in an Android module. In the example project the <em>presentation</em> module that hosts the state machine <em>interface </em>and the base and concrete components is included into an Android app module as a dependency.</p><h3>The Abstract ViewModel</h3><p>The logical place for the abstract parts of the state machine to be implemented is a <em>ViewModel</em>, because we have multiple suspending functions being called and the <em>ViewModel</em> provides the <em>viewModelScope</em> where it is safe to execute this suspending methods, since it is tied directly to the <em>ViewModel</em> lifecycle.</p><p>However if we are to re-use the <em>ViewModel</em> implementation we might as well create an <em>abstract</em> <em>ViewModel</em> and handle the specifics of each case scenario in children <em>ViewModels</em>. Here you can see how this <em>abstract</em> <em>ViewModel</em> is declared:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86019354da4c842dc06bf3c2a60fe8c5/href">https://medium.com/media/86019354da4c842dc06bf3c2a60fe8c5/href</a></iframe><p><em>LiveData</em> is being used here to host the states that are received after dispatching the events. Making the new states be available in the <em>observingBlock</em> of the <em>observe</em> function declared at the bottom of the class.</p><p>It is necessary to provide an initial state for the state machine to work, since it is necessary for actions to be performed. This was chosen over having a nullable value for the <em>perform()</em> method in the <em>Action</em> interface, because of the cyclic nature of the paradigm to be applied here: from a state some events can be triggered, this events converted into states by transforming actions, taking the optional output of the previous state. A <em>null</em> state in this cycle just doesn’t make sense<em>.</em></p><h3>Implementations</h3><p>All the elements necessary for the state machine to function are in place, now we just have to add the specific implementations.</p><h4>Implemented Components</h4><p>We are making use of sealed classes in order to be able to represent the different states, actions and events of our simple example project. Since sealed classes support nesting we can be as specific as we want.</p><h4>States</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca02e3623c64d75e9e2bc33665a7b985/href">https://medium.com/media/ca02e3623c64d75e9e2bc33665a7b985/href</a></iframe><h4>Events</h4><p>They are simple containers of inputs from the UI as parameters.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40fc5ca1e41496d89fe47e6c5bc9e7b9/href">https://medium.com/media/40fc5ca1e41496d89fe47e6c5bc9e7b9/href</a></iframe><h4>Actions</h4><p>As you can see there is 1 to 1 mapping from event to action, which makes sense since we want a user event to trigger one transformation and get one event containing all the new UI information to be displayed.</p><p><em>Actions</em> tend to be larger since they are links to the execution of the business logic, taking the previous and the inputs from the events as parameters for the transactions under the hood. This won’t be a problem anymore when Kotlin 1.5 arrives, because <em>sealed</em> classes are going to be allowed to be implemented in different files than their declaration.</p><p>Here we are emulating the business logic of the app by calling a random delay on both the <em>SendSuccess</em> and <em>SendError</em> actions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df3958a18195c5703c668cd3f1931cd9/href">https://medium.com/media/df3958a18195c5703c668cd3f1931cd9/href</a></iframe><h4>The ViewModel</h4><p>Now that our components are done, the ViewModel can be implemented allowing us to declare the initial state and dispatch both events.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb4df3127981a38f1a13da91735f418f/href">https://medium.com/media/fb4df3127981a38f1a13da91735f418f/href</a></iframe><h4>MainActivity</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f1bef8468f2eaf6c294cd7d798fdd93/href">https://medium.com/media/4f1bef8468f2eaf6c294cd7d798fdd93/href</a></iframe><p>The activity code is now reduced to the initialization of the layout elements and the observing of the state change, that is hooked with a <em>when</em> statement.</p><p>In this way the logic of rendering the fragments and activities can be isolated to the current state that is being handled. No weird flags necessary.</p><h3>Testing</h3><p>How can we ensure that the states are being correctly generated after the dispatch of an event? We created a testing process that can be described as follows:</p><ul><li>Define an initial or previous state</li><li>Define the expected resulting states</li><li>Dispatch the event to be tested</li><li>Transform it into an action and perform it</li><li>Assert the equality of generated states with the expected states</li></ul><p>Here is the implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae6f55caeb0709305e255d756b6a1df0/href">https://medium.com/media/ae6f55caeb0709305e255d756b6a1df0/href</a></iframe><p>The implementation of this function looks very familiar, this is because it is a minimized version of the <em>UiEventsProcessor</em><strong><em>.</em></strong></p><p>This function will validate that the expected states are exactly the same ones that are generated after calling the <em>perform() </em>function on the Action generated by the dispatched UI Event. The complexity of the tests that use this function lie in the definition of the initial and expected states.</p><p>Below are some examples of the over-simplistic approach of the implementation of the example project, but a variation of the method declared above is used in the tests for the production application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f100d742a169ff798e0d296a3c798fba/href">https://medium.com/media/f100d742a169ff798e0d296a3c798fba/href</a></iframe><p>In this example the final and more complex expected states are generated by the <em>successOperationSimulator</em> and <em>erroneousOperationSimulator</em>, encapsulations of the simulated business logic the application uses. It is evident that in order to have proper tests these kinds of encapsulations should be provided to both the test and the real application modules, but this is another topic.</p><h3>Conclusion</h3><p>There is no silver bullet for Android apps implementation, there are multiple valid approaches to the complexities of the parts that make an Android application, but having specific division of the states of the UI has worked very well in the reconstruction of HSE, and has allowed to provide control over a part of the Android implementation that has always posed a problem to multiple developers.</p><p>Thanks for reading and Kotlin your problems away!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5edd1386f40d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/unidirectional-data-flow-in-for-android-uis-5edd1386f40d">Unidirectional Data Flow in for Android UIs</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[Shipbook Logging with Timber in Android]]></title>
            <link>https://medium.com/@j1c1m1b1/shipbook-logging-with-timber-in-android-bdb44199b5e6?source=rss-869a553d6781------2</link>
            <guid isPermaLink="false">https://medium.com/p/bdb44199b5e6</guid>
            <category><![CDATA[timber]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Julio Mendoza]]></dc:creator>
            <pubDate>Thu, 05 Sep 2019 09:02:43 GMT</pubDate>
            <atom:updated>2019-09-05T09:02:43.008Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZK73SJUhlYG1I_ju26oyoA.jpeg" /></figure><p>If you are reading this I think you are using the awesome <a href="https://www.shipbook.io/">Shipbook</a> to get useful information about logs in your Android apps.</p><h3>Dependencies</h3><p>Just add the dependency in your Android app or library modules dependency .gradle files:</p><pre>implementation ‘io.shipbook.shipbooksdk:shipbooksdk:&lt;Shipbook version&gt;’</pre><pre>implementation &#39;com.jakewharton.timber:timber:&lt;Timber version&gt;&#39;</pre><h3>The Application File</h3><p>After you have successfully Gradle synced your project, initialise your Shipbook configuration (as specified in the <a href="https://www.shipbook.io/android-doc">docs</a>) inside the onCreate() method of your application class.</p><pre>ShipBook.start(this,&quot;YOUR_APP_ID&quot;, &quot;YOUR_APP_KEY&quot;)</pre><h3>Configuration with Timber</h3><p>Create a class extending Timber.Tree in this sample it is called ShipbookTree:</p><pre><strong>class </strong>ShipBookTree: Timber.Tree() {<br>    <strong>override fun </strong>log(priority: Int, tag: String?, message: String, t: Throwable?) {<br>        io.shipbook.shipbooksdk.Log.message(tag, message, Severity.fromInt(priority), t)<br>    }<br>}</pre><p>Be sure to use the correct import when using Log : import io.shipbook.shipbooksdh.Log. You can customise your Tree however you like, particularly to avoid sending useless <em>info</em> and <em>debug</em> priority messages to Shipbook.</p><h3>Planting the Tree</h3><p>Be sure to plant the Timber tree <strong>after</strong> Shipbook has been initialised:</p><pre>Timber.plant(ShipBookTree())</pre><h3>You are done!</h3><p>I hope this guide was helpful in anyway. Enjoy your logging!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bdb44199b5e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>