<?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[bystevenp - Medium]]></title>
        <description><![CDATA[All things Steven - Medium]]></description>
        <link>https://medium.com/bystevenp?source=rss----162ba58a26ff---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>bystevenp - Medium</title>
            <link>https://medium.com/bystevenp?source=rss----162ba58a26ff---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 17:57:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/bystevenp" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Comment Trees and Recursion with RxJava]]></title>
            <link>https://medium.com/bystevenp/comment-trees-and-recursion-with-rxjava-d147a904610a?source=rss----162ba58a26ff---4</link>
            <guid isPermaLink="false">https://medium.com/p/d147a904610a</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[rxandroid]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Steven Pungdumri]]></dc:creator>
            <pubDate>Mon, 17 Jul 2017 17:13:43 GMT</pubDate>
            <atom:updated>2017-07-17T13:11:26.751Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*tYt9aHEaMP23vSmP7O9l0Q.jpeg" /><figcaption><a href="https://www.b2bnn.com/wp-content/uploads/2015/09/Commentbutton.jpg">Image source</a></figcaption></figure><p>Recently I’ve been working on a <a href="https://github.com/spung/hn">Hacker News client</a> as a way to brush up on some Android technologies and to have a canvas to work on some app design. The first screen I built would display the <a href="https://news.ycombinator.com/">top stories</a> on Hacker News. The second would be a <a href="https://news.ycombinator.com/item?id=14711153">detail screen</a> with the content of a story, and more importantly comments from the community.</p><p>It turns out comments are logically stored in n-ary trees in order to accommodate n-comments, each with any combination of n-deep child comments. Once we have our data structured properly, we can display the comments with some good old-fashioned preorder tree traversing.</p><p>Assuming in this iteration we only intend to fetch and display these comments, on the presentation side we simply need a list of items to display. Reducing the dataset to a list, we need some indicator of how much to indent them, so the user understands which parent comment a child comment is replying to.</p><h4>Constructing our tree</h4><p>Assume we have the following API:</p><pre>@GET(<strong>&quot;item/{id}.json&quot;</strong>)<br>Item getItem(@Path(<strong>&quot;id&quot;</strong>) <strong>long </strong>id);</pre><p>We can recursively fetch our comments and construct our tree:</p><pre><strong>void </strong>fetchChildren(Item item) {<br>  <strong>if </strong>(!item.hasChildren()) <strong>return</strong>;<br><br>  List&lt;Item&gt; children = <strong>new </strong>ArrayList&lt;&gt;(item.<strong>childIds</strong>.size());<br>  <strong>for </strong>(Long childId : item.<strong>childIds</strong>) {<br>    Item child = <strong>api</strong>.getItem(childId);<br>    children.add(child);<br>    fetchChildren(child);<br>  }<br><br>  item.<strong>children </strong>= children;<br>}</pre><h4>Creating a list to display</h4><p>Once we have our tree handy, we use recursion to traverse a comment tree to get a simple list to display:</p><pre>List&lt;Item&gt; preorderTraversal(List&lt;Item&gt; list, Item item) {<br>  list.add(item);<br><br>  <strong>if </strong>(item.hasChildren()) {<br>    <strong>for </strong>(Item child : item.<strong>children</strong>) {<br>      child.<strong>indent </strong>= item.<strong>indent </strong>+ 1;<br>      preorderTraversal(list, child);<br>    }<br>  }<br><br>  <strong>return </strong>list;<br>}</pre><p>Great, this works. Diving into some of the popularly used Android libraries today, I wondered what this would look like with RxJava.</p><h4>Recursive calls with RxJava</h4><p>Let’s redo our API call with Observables:</p><pre>@GET(<strong>&quot;item/{id}.json&quot;</strong>)<br>Observable&lt;Item&gt; getItem(@Path(<strong>&quot;id&quot;</strong>) <strong>long </strong>id);</pre><p>Now we construct our tree…<em>and</em> produce a list to display at the same time?</p><pre><strong>public </strong>Observable&lt;Item&gt; getComments(<strong>final </strong>Item parentItem) {<br>  <strong>if </strong>(parentItem.hasKids()) {<br>    <strong>return </strong>Observable.merge(<br>      Observable.just(parentItem),<br>      Observable.fromArray(parentItem.getKids())<br>        .flatMap(ids -&gt; getItems(ids))<br>        .flatMap(item -&gt; {<br>          item.indent = parentItem.indent + 1;<br>          <strong>return </strong>getComments(item);<br>        })<br>    );<br>  } <strong>else </strong>{<br>    <strong>return </strong>Observable.just(parentItem);<br>  }<br>}</pre><p>Wow, that’s so much more concise. Here we’re able to both fetch comments and combine them into a stream with the order we want to display them with, all in one go. You can see the preorder traversal part of this with the order of parameters in the Observable.merge() call — parent, then the recursive call on its children. From here we can call this method, subscribe to the observable returned, and populate comments as they’re fetched.</p><p>Really enjoying RxJava so far and thinking about things in streams. Looking forward to discovering other cool ways to apply it to new and existing code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d147a904610a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bystevenp/comment-trees-and-recursion-with-rxjava-d147a904610a">Comment Trees and Recursion with RxJava</a> was originally published in <a href="https://medium.com/bystevenp">bystevenp</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Architecture Components]]></title>
            <link>https://medium.com/bystevenp/android-architecture-components-49a5a24b1f38?source=rss----162ba58a26ff---4</link>
            <guid isPermaLink="false">https://medium.com/p/49a5a24b1f38</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Steven Pungdumri]]></dc:creator>
            <pubDate>Mon, 22 May 2017 19:06:31 GMT</pubDate>
            <atom:updated>2017-05-22T19:49:31.272Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*72kvc1ozR6bQ-eprnyR7gA.png" /><figcaption><a href="http://chrisz0rz.deviantart.com/art/Android-Background-302097493">Image source</a></figcaption></figure><p>For years now, many in the Android developer community have voiced frustration that Google hasn’t adequately offered guidelines on how to build robust, testable apps. Novice developers often find themselves with god activities and fragments that do much more than maintain the UI such as mucking around with business logic, making network calls, accessing the database, and much more. Over the years, developers have proposed design patterns that historically worked well for other frameworks. Today, some of the more popular approaches are <a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">MVC</a>, <a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter">MVP</a>, and <a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel">MVVM</a>. Developers often research, choose one, then stick with the guidelines of the architecture they picked or use a library with helper components to get started.</p><p>Google has now released a set of documentation with accompanying components to offer an opinion on how Android apps should be architected. Let’s take a brief dive into the library in its current state (<em>1.0.0-alpha1</em> at the time of writing).</p><h3><strong>Library Overview</strong></h3><ul><li><strong>LiveData </strong>— lifecycle aware, observable sources of data. When paired with a LifecycleOwner, LiveData can respond to lifecycle events like pausing updates during a stopped lifecycle.</li><li><strong>LifecycleOwner</strong> — interface implemented by activities and fragments to publish lifecycle events to any observers.</li><li><strong>LifecycleObserver</strong>— observe LifecycleOwners to respond to lifecycle events (implemented by LiveData).</li><li><strong>ViewModel</strong> — a layer to manage and provide data to a specific UI component.</li></ul><h3><strong>ViewModel</strong></h3><p>The ViewModel doesn’t modify views directly (in fact it shouldn’t know about the UI components at all), instead it offers up observables for changes, typically using instances of LiveData. It handles the logic needed to initialize, store, and retrieve data used in the UI. This can be shared across activities and fragments, and is safe from lifecycle events such as activity recreation on a screen rotation.</p><p><strong><em>They can destroy our Activities, but they can never destroy our data </em></strong>✊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/0*aOS1Yom1tyx9D8b1." /><figcaption>Illustrates how the ViewModel scope persists across activities and fragments being destroyed and recreated. Note: destroyed != finished. <a href="https://codelabs.developers.google.com/codelabs/android-lifecycles/img/8efa40941430fc4c.png">Image source</a></figcaption></figure><h3>LiveData</h3><p>LiveData provides data using the observer pattern, similar to other reactive libraries. In addition to this, they are lifecycle-aware and respond accordingly to the state of their active observers. For example, LiveData objects will send updates only when their corresponding LifecycleOwners are active (visible to the user), and stop when LifecycleOwners are inactive, since there’s no point updating the UI if it’s not visible to the user. They will also handle cleanup of references and observers automatically when the observed lifecycles are destroyed.</p><h3>LifecycleObserver</h3><p>Any object can now listen for lifecycle events by subscribing to them. Simply pass through the LifecycleOwner object to a component that implements LifecycleObserver and call:</p><pre>lifecycleOwner.getLifecycle().addObserver(this);</pre><p>From here, the LifecycleObserver can annotate methods to be called on lifecycle events:</p><pre>@OnLifecycleEvent(Lifecycle.EVENT.ON_START)<br>void startListening() { … }</pre><pre>@OnLifecycleEvent(Lifecycle.EVENT.ON_STOP)<br>void stopListening() { … }</pre><p>The following states are available to listen for: CREATED, DESTROYED, INITIALIZED, RESUMED, STARTED</p><p>At any point, the LifecycleObserver can also call</p><pre>lifecycleOwner.getLifecycle().getCurrentState()</pre><p>or</p><pre>lifecycleOwner.getLifecycle().getCurrentState().isAtLeast() </pre><p>and switch logic accordingly.</p><h3>Putting it Together</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*cSnab5YG8LbMjBWOTkQxyQ.png" /><figcaption><a href="https://developer.android.com/topic/libraries/architecture/images/final-architecture.png">Image source</a></figcaption></figure><p>The resulting app architecture should resemble this diagram. The <strong>Activity/Fragment</strong> layer only contains UI logic. The <strong>ViewModel</strong>(s) encapsulate model data surfaced by instances of observable <strong>LiveData</strong>. The ViewModel handles the actual source of that data with a reference to components in the <strong>Repository</strong> layer, which may be pulling from network, cache, database, or a combination of three. This post doesn’t dive into the Repository layer, but if you’re using SQLite in your app it’d be worth checking out <a href="https://developer.android.com/topic/libraries/architecture/room.html"><em>Room</em></a>, Google’s ORM library that provides an abstraction layer over SQLite, which was also recently released.</p><h3>Recap</h3><p>Google has responded to the the Android developer community’s ask for an opinion and direction on a clean way to architect Android apps. It comes with no surprise that it pulls ideas from existing libraries that have emerged and become popular (for good reason) over the years, since these ideas have been implemented, tested, and iterated on by the community.</p><p>It’s important to know that these architecture components and guidelines are not required by any means. They can be used altogether, in pieces alongside other existing libraries, or not at all. What’s great is we have another resource for building stable, testable apps.</p><p><strong>References:</strong></p><p><a href="https://developer.android.com/topic/libraries/architecture/index.html">Android Architecture Components | Android Developers</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49a5a24b1f38" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bystevenp/android-architecture-components-49a5a24b1f38">Android Architecture Components</a> was originally published in <a href="https://medium.com/bystevenp">bystevenp</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>