<?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 Orit Malki on Medium]]></title>
        <description><![CDATA[Stories by Orit Malki on Medium]]></description>
        <link>https://medium.com/@orit.malki?source=rss-27d8376c42c0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_Y6Qm8xeJijJ3G3A7VPZNw.jpeg</url>
            <title>Stories by Orit Malki on Medium</title>
            <link>https://medium.com/@orit.malki?source=rss-27d8376c42c0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 21:32:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@orit.malki/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[Compose TextField — Taming the Beast]]></title>
            <link>https://medium.com/meliopayments/compose-textfield-taming-the-beast-f863385aa6c3?source=rss-27d8376c42c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/f863385aa6c3</guid>
            <category><![CDATA[material3]]></category>
            <category><![CDATA[textfield]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[compose]]></category>
            <category><![CDATA[melioengineering]]></category>
            <dc:creator><![CDATA[Orit Malki]]></dc:creator>
            <pubDate>Thu, 05 Oct 2023 07:19:28 GMT</pubDate>
            <atom:updated>2023-10-05T07:20:16.529Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*k8Y-FVOGRsketFtblFSiKA.png" /></figure><h3>Compose TextField — Taming the Beast</h3><h3><strong>3 challenges of Compose’s TextField and how to work around them</strong></h3><p>One of the most commonly used UI components for text input of any application is the TextField, or in its most basic version, the EditText, or Material Design’s TextInputLayout + TextInputEditText. Although this sounds like a pretty simple and straightforward component, it has a lot of complexity. And today’s Modern Android development ecosystem adds special challenges to it.</p><p>If you’re an Android developer that wants to migrate or write your first project in Compose framework, this article is for you. Compose is a very powerful tool, but, at the same time, a very young framework that keeps evolving constantly. This article will focus on three challenges, or pitfalls, that Compose’s TextField component presents and how we can work around them. If you’re searching for solutions and bumped into this article, it might save you some time and effort.</p><h3><strong>Background</strong></h3><p>My team and I develop Melio’s Android application, which has many form screens for users to fill. So naturally, TextField is one of the corner-stone UI components that are used in the app. As we worked through migrating our app to use Compose, we decided to use a Material 3 component — the <strong>TextField. </strong>Little did we know then, that this component will present its own challenges.</p><p>It’s a simple-looking component, easy to theme and handle. Yet we quickly noticed it presented some buggy behaviors.</p><h3><strong>First Pitfall: State management</strong></h3><p>Always, and especially when using TextField in a reactive stream environment like <strong>Flow</strong>, use <strong><em>remember {}</em></strong> for the textField value. <br>Preferably not just for its current string, but for the whole TextFieldValue object, like so:</p><pre>@Composable<br>fun CustomTextInputComponent(<br>  textValue: String<br>  onTextChanged: (String) -&gt; Unit,<br>) {<br>  var textFieldValue by remember {<br>      mutableStateOf(TextFieldValue(text = textValue))<br>  }<br>  TextField(<br>    value = textFieldValue,<br>    onValueChange = { newValue -&gt;<br>      textFieldValue = newValue<br>      onTextChanged(newValue.text)<br>    }<br>  )<br>}</pre><p>I’ll explain.</p><p>The use of <em>remember</em> for the text value, is the recommended way by Jetpack documentation. As reactive streams work in an asynchronous manner, they can get the input out of sync. Its most noticeable effect is the unstable cursor position while typing:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/1*RSYgP42nhoGoBcBVd9bH2w.gif" /><figcaption>cursor position gets out of sync</figcaption></figure><p>Using a local Compose State will keep the input in sync, as a single synchronic source is channeling the input into the component. Much has already been written about it in this <a href="https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5">article</a>.</p><p>Using <em>remember</em> caches the value set from outside the TextField between compositions. In order for this state to update on user input as well, we update it on each change in <em>onValueChange()</em>.</p><p>If you look carefully, you’ll also notice that this piece of code contains a bug. Several, actually.</p><p>When creating a new instance of TextFieldValue, the <em>selection </em>argument that represents the current cursor’s position, defaults to <em>TextRange.Zero</em>.</p><p>This means, that when the TextField value is updated externally, the cursor position will default to the text start, and any additional user input will come before the initial text:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*l-O97Kf9VgngJCtIduigvg.gif" /><figcaption>Cursor position is being reset to zero when switching fields</figcaption></figure><p>In order to address that, we can just init the <em>selection</em> value of the TextFieldValue to the text length.</p><p>That way, the local state will remember not only the text itself, but also the position of the cursor:</p><pre>var textFieldValue by remember { <br>    mutableStateOf(TextFieldValue(text = textValue, selection = TextRange(textValue.length))) <br>}</pre><p>In the current setup, we only support setting the text once from the outside. Afterwards, the state updates every time the text in the field is changed by the user’s input.</p><p>But what if at some point we would want to update the textField from an external state again?</p><p>For example, If we have a button that needs to clear a field full of text, and its onClick() method triggers a state update in the viewModel, like so:</p><pre>textFieldStateFlow.emit(“”)</pre><p>Then the update won’t propagate to the UI, since the field uses our remembered state (remember?) and not the one set from outside after the initial call.</p><p>To fix this, we need to manage the compose cached State as a <strong>single source of truth</strong> for both input sources (user input and programmatic input), by adding the following lines:</p><pre>//inside CustomTextInputComponent()<br><br>var textFieldValue by remember { mutableStateOf(TextFieldValue(text = textValue, selection = TextRange(textValue.length))) }<br><br>//update the state with text set from outside on each state change:<br> textFieldValue = TextFieldValue(text = textValue, selection = TextRange(textValue.length))</pre><p>Note that in order to avoid a potential infinite loop on this implementation, the dataflow to the textField state must be <strong>unidirectional</strong> from the viewModel to the UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*QdgFXWeHcWLMdpnBCPRKIw.png" /><figcaption>remember local state as single source of truth for the TextField</figcaption></figure><h3>Second Pitfall: The label animation cannot be disabled</h3><p>Unlike TextInputLayout, Jetpack Compose’s TextField imposes its design and doesn’t provide the option to disable the animation of the label from the underline to the top when the field gets focus.</p><p>Although it’s a nice animation, it’s not necessarily desired in all situations, and it’s a reasonable request to be able to disable it. Also, our design may require presenting the placeholder text alongside the label when the field is not focused, like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*f4tB4nnSfrYNSrHhD8oShA.png" /></figure><p>Turns out this isn’t possible as well with current API:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*_Fxz2zN0oAe5b_qMJhZwyw.gif" /><figcaption>Impossible to present label together with placeholder when the TextField unfocused</figcaption></figure><p>Looking at the source code, this is the way its built:</p><p>The placeholder is a composable being drawn as part of the DecorationBox — CommonDecorationBox of the BasicTextField, upon which the TextField is built. The CommonDecorationBox component features a built-in transition that cannot be disabled from the outside.</p><p>As we can see, if we want to present both the label and the placeholder simultaneously when the field is empty and unfocused, we simply can’t.</p><p>I simplified some of the source code, but it basically boils down to:</p><pre>val placeholderOpacity by transition.animateFloat(<br>…<br>) {<br>    when (it) {<br>        InputPhase.Focused -&gt; 1f<br>        //this line makes it impossible to show label and placeholder simultaneously<br>        InputPhase.UnfocusedEmpty -&gt; if (showLabel) 0f else 1f<br>        InputPhase.UnfocusedNotEmpty -&gt; 0f<br>     }<br>}</pre><p>It seems the easiest way to work around it is to set the label to null and use our own custom label composable instead, but then we’ll have to handle its state theming and coloring ourselves.<br>Another option is to implement our own decoration box, but that will be an overhead as well.</p><p>I anticipate that on next updates Google will give the developers more control over basic attributes like this.</p><h3><strong>Third Pitfall: Height changes on focus change</strong></h3><p>When implementing a form screen using a Column of TextFields, a very un-smooth transition is shown when switching focus from one empty field to another. You can see that height of the underline changes during the animation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/1*i8b5W9wfWE42ZQUGy_58Dg.gif" /><figcaption>Unsmooth tansition, the TextField changes its height when focus changes.</figcaption></figure><p>I drilled down to the source code trying to understand this issue better.</p><p>It originated again in the decoration box applied to the BasicTextField within the TextField.</p><p>There are two types of decoration boxes, one for each type of Material 3 textField styles — Filled and Outlined:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*luV3qxs4Tzm6KYt-xBj5AA.png" /><figcaption>Filled Vs. Outlined</figcaption></figure><p>The default type used is TextFieldDefaults.DecorationBox, which is based on the Filled textField style.</p><p>The internal implementation uses a <em>CommonDecorationBox </em>Composable, which handles each type a bit differently.</p><p>When using a TextFieldType.Outlined decoration box, this bug does not happen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*4MmtAalwL9iz9v-y1Zif4Q.gif" /><figcaption>Outlined TextField’s height stays constant on focus change</figcaption></figure><p>I tried to compare the implementation of the two types. Filled type uses a <em>TextFieldLayout </em>whereas<em> Outlined </em>type uses <em>OutlinedTextFieldLayout. </em>I noticed a main difference between the two: <em>OutlinedTextFieldLayout </em>specifies a measuring policy that takes further into account label changing dimensions during its transition, whereas TextFieldLayout does not.</p><p>I did not go any further than this, but it looks like a reasonable suspect for why the animation of the other type looks junky. So the workaround we chose together with our product designer was to use a BasicTextField with an OutlinedTextFieldDefaults.DecorationBox.</p><p>I <a href="https://issuetracker.google.com/issues/286260340">issued a bug</a> on Google’s issue tracker. As per writing these lines, it has progressed from P4 to P3 on their priority, so let’s hope it will be fixed on upcoming versions.</p><p>When discussing the above bugs at different threads of the issue tracker, it looks like they are in the process of fully re-writing the TextField component.</p><h3>What’s next</h3><p>I think a core UI component such as the TextField needs to become more mature in terms of robustness and flexibility before we can safely include it in our toolbox. But that’s a part of using a new framework — you get to help shape its evolution through feedback.</p><p>Looking forward to the next updates of Material 3!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3qSivDZI-3fRph6erM7GtQ.png" /><figcaption><a href="https://bit.ly/jobs-medium">Visit our career website</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f863385aa6c3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/meliopayments/compose-textfield-taming-the-beast-f863385aa6c3">Compose TextField — Taming the Beast</a> was originally published in <a href="https://medium.com/meliopayments">Melio’s R&amp;D blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Decoupling Android Navigation Logic with VIVA Pattern]]></title>
            <link>https://medium.com/meliopayments/decoupling-android-navigation-logic-with-viva-pattern-8a3a12afa8d2?source=rss-27d8376c42c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a3a12afa8d2</guid>
            <category><![CDATA[navigation]]></category>
            <category><![CDATA[viper]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[flow]]></category>
            <category><![CDATA[melioengineering]]></category>
            <dc:creator><![CDATA[Orit Malki]]></dc:creator>
            <pubDate>Sun, 19 Jun 2022 11:32:55 GMT</pubDate>
            <atom:updated>2023-08-01T09:24:53.023Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*dVKsfME2aHVqLhT3GPIoow.png" /></figure><p>At Melio, we are building a mobile SDK. Among its core functionalities, our SDK provides different screen flows that can be assembled as needed, no matter who is using them. They can be used as building blocks for constructing Melio’s own mobile app, or any other app that needs to include Melio functionalities.</p><p>As an Android Tech Lead, one of my first tasks was architecting Melio’s Android SDK and mobile app, in accordance with our Head of Mobile development team.</p><p>One of the main challenges we faced when building our mobile SDK, was designing a navigation pattern that would support the modularity we were striving for — making our screen components agnostic to the navigation logic between them.<br>This required decoupling UI components from navigation business logic. This is not a trivial task in Android, as navigating from one screen to another is mechanically done by UI components themselves (Activity/Fragment).</p><h3><strong>Who are we building for?</strong></h3><p>It was important to us to lay the groundwork for other partners and developers to use what we’re building. <br>Let’s take for example, an e-commerce app that will incorporate Melio’s checkout ability. This app plans to use Melio’s mobile SDK to enable businesses to pay in their preferred payment method, at which point a quick authentication may be desired that our mobile SDK should seamlessly provide.<br>The authentication flow will then require and include, UI, logic, and of course — internal navigation between the different screens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*z442lCxlhSRmIEemEhMvQg.png" /><figcaption>Pay checkout leading to authentication flow</figcaption></figure><p>The SDK should also be able to expose a single screen out of a flow. <br>For example, using only a single screen out of an onboarding flow to update a company’s billing address. In that case, this screen-unit should be agnostic to the navigation logic to/from it.</p><p>In order to achieve this, we needed to be able to dictate navigation logic <strong>from outside</strong> our screen-units. <br>But how could we approach this challenge?</p><h3><strong>Examining existing patterns</strong></h3><p>After reviewing different architectural design patterns to use for our screen-units, VIPER seemed like an interesting fit, as it has a good separation of navigation logic and mechanics.</p><p>In <strong>VIPER</strong>, each screen unit consists of a <strong>V</strong>iew, <strong>I</strong>nteractor, <strong>P</strong>resenter, <strong>E</strong>ntity and <strong>R</strong>outer. The router’s job is the mechanics of navigation, whereas the presenter’s job is to hold the logic for a navigation action.</p><p>Although it seemed enticing, in terms of separation of concerns, this pattern presented a dependency issue for us — each VIPER screen-unit is still aware of its navigation logic. Therefore, it couldn’t provide a complete solution for exposing an independent screen out of a flow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*ZlJHBsJfTOL8tC4-f_77dA.png" /><figcaption>We can see here that each screen/VIPER unit is coupled with navigation logic from it.</figcaption></figure><p>It also presented other disadvantages for us:</p><p>1. As an MVP-based pattern, the state of the presenter is not preserved through configuration changes, like in Android’s ViewModel class.</p><p>2. The presenter controls the view directly. If we considered using Jetpack Compose on some of our screens, it would be more suitable to maintain a state in ViewModel that lets the passive view merely observe and react.</p><p>So while we wanted to make our project Compose-ready, and not worry about configuration change state-loss, we found ourselves drawn to the MVVM pattern.</p><p><strong>Ok, but what does it have to do with navigation? </strong><br>While examining the <strong>MVVM </strong>pattern, we realized something. If the view can be decoupled from the business logic by being totally passive, only reacting to state changes triggered by LiveData / StateFlow Then why not use that same technique to decouple navigation logic as well?</p><p><strong>So we decided to build a coroutine-SharedFlow based mechanism for posting navigation events.</strong></p><h3><strong>Our solution for decoupling navigation from screen-units</strong></h3><p>With this goal in mind, we realized that we needed to differentiate between two key types of navigation events:</p><h4>Flow-level navigation (navigation from fragment)</h4><p>Melio’s mobile SDK offers a set of predefined screen-flows, each of which consist of a single activity that navigates different fragments using the Jetpack Navigation graph.</p><p>Inspired by VIPER and MVVM, I came up with a pattern combining the benefits of both patterns, named <strong>VIVA</strong>.<br>In VIVA, each screen-unit consists of the following components:<br><strong>V</strong>iew (Fragment), <strong>I</strong>nteractor (acts as a data provider), <strong>V</strong>iewModel, and a new addition of an <strong>A</strong>ctions sealed class. <br>It also has its own dependency injection component, creating a totally decoupled unit.</p><p>A contract and a generic BaseVIVAFragment class are used to bind those screen-unit-elements together, as such:</p><pre>abstract class BaseVIVAFragment&lt;V : Contract.View, <br>         VM : Contract.ViewModel&gt; <br>: Fragment(), Contract.View</pre><p>Here is an example of a contract for a simple SignUp screen-unit:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b1c1bdb74e495fcc9e17161a18a804f/href">https://medium.com/media/9b1c1bdb74e495fcc9e17161a18a804f/href</a></iframe><p>Let’s dive into the navigation aspects of the pattern…</p><p>Note: the <strong>Action </strong>sealed class specifies events that require navigation to another screen. This class corresponds to the NavigationEvent interface, which will allow us to use this Action as a payload for posting a navigation event.</p><p>The CoordinatorEvent is the actual event we post when a screen needs to navigate.<br>In the NavigateFromFragment data class, we pass the relevant Action (that implements NavigationEvent interface), containing all relevant information for navigation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebacd3210272c9b47ed4be1d29dcf0b2/href">https://medium.com/media/ebacd3210272c9b47ed4be1d29dcf0b2/href</a></iframe><p>For the event-firing mechanism, we use a CoordinatorEventBus object that holds a SharedFlow of CoordinatorEvents. It can be used from anywhere in the code. <br>It has a <strong>send</strong> function, to be used by the producer screen-unit:</p><pre>fun send(event: CoordinatorEvent)</pre><p>and a <strong>consume</strong> function, with a consume block to be executed when the flow is collected, to be used by the consumer coordinator/activity:</p><pre>fun consumeEvents(consume: (T) -&gt; Unit)</pre><p>So, when the fragment requires navigation, it’s ViewModel simply fires a navigation event, like so:</p><pre>CoordinatorEventBus.send(CoordinatorEvent.NavigateFromFragment<br>      (<br>       action =   <br>          SignUpFragmentContract.Action.CreateAccountCompleted(user)<br>      )<br>)</pre><p>The fragment is not aware of any navigation logic or destinations, it simply signals that it needs to navigate.</p><p>The consumer activity consumes this event from a lifecycle-scoped coroutine, which looks like this (simplified):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10bbdda3c9184e77c45f84863bbbe98d/href">https://medium.com/media/10bbdda3c9184e77c45f84863bbbe98d/href</a></iframe><p>See how we can clearly view where each navigation event came from, and can easily follow the navigation logic for each screen unit!</p><p>If we need to take a certain screen out of the flow, and use it independently in a different context, it should be simple since it has no dependencies outside of its own self-provided DI component.</p><p>For Application-level navigation, we use a Coordinator class that triggers and coordinates the different screen-flows exposed by the SDK. When consuming events in the App-level Coordinator, it works similarly to the above example, only the events being consumed are of type NavigateFromActivity.</p><h4><strong>App-level navigation (navigation from activity)</strong></h4><p>The pattern described in this section is used for <strong>reacting</strong> to navigation events. To initiate navigation inside activities, we use a state-driven variation of this pattern (currently out of the scope of this article).</p><p>With the App-level pattern, the Coordinator works the logic and mechanics of the navigation, and the flow-activities merely post an “I’m done!” event.</p><p>Inside the NavigateFromActivity event, you can see that the activity is being passed as a property. That is in order for the Coordinator to have control over the manner and timing of starting a new activity and finishing the previous one. This is very useful for smooth transitions and animations.</p><p>Here we can see an overview of our navigation pattern:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*wewMwYvUvnSj9CWNILEAog.png" /></figure><h3>Final thoughts</h3><p>Like every pattern, VIVA has its pros and cons. It suited our needs well when building an SDK with strong decoupling in mind, but it may not fit every project.</p><p><strong>Pros:</strong></p><ul><li>Screen-flows are agnostic to navigation logic</li><li>Screen-units are independent and can be used as stand-alones</li><li>Project is Compose-Ready</li><li>State is preserved through configuration changes</li><li>Clear separation of concerns with contract-binded units</li></ul><p><strong>Cons:</strong></p><ul><li>Many classes are needed for each screen-unit and flow-unit (see tip below)</li><li>It may be overkill for a smaller non-SDK project — but it’s definitely worth considering adopting the event-bus pattern part for decoupling the navigation</li></ul><blockquote>Tip: to generate a starter code for each screen unit, a cool tool that I found is Hygen — it allows you to create templates and auto-generate them. So in a matter of seconds, you can start working on a new screen with the VIVA pattern. The templates can be included in .git, so other team members can adapt the pattern consistently and easily.</blockquote><p>I hope that next time you need to build a navigation pattern, you’ll consider VIVA for decoupling and clear visual layout of navigation logic.</p><p>If you have any questions or comments please feel free to add below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3qSivDZI-3fRph6erM7GtQ.png" /><figcaption><a href="https://bit.ly/jobs-medium">Visit our career website</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a3a12afa8d2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/meliopayments/decoupling-android-navigation-logic-with-viva-pattern-8a3a12afa8d2">Decoupling Android Navigation Logic with VIVA Pattern</a> was originally published in <a href="https://medium.com/meliopayments">Melio’s R&amp;D blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>