<?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 Burhan U Din Drabu on Medium]]></title>
        <description><![CDATA[Stories by Burhan U Din Drabu on Medium]]></description>
        <link>https://medium.com/@drabu?source=rss-acdf3766b574------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mouIJIKRj9dD6NXH7xgWEA.jpeg</url>
            <title>Stories by Burhan U Din Drabu on Medium</title>
            <link>https://medium.com/@drabu?source=rss-acdf3766b574------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 22:44:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@drabu/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[Build State-driven UI with clean architecture in android.]]></title>
            <link>https://medium.com/@drabu/build-state-driven-ui-with-mvvm-and-clean-architecture-in-android-d03003b206e7?source=rss-acdf3766b574------2</link>
            <guid isPermaLink="false">https://medium.com/p/d03003b206e7</guid>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[solid-principles]]></category>
            <category><![CDATA[android-ui]]></category>
            <dc:creator><![CDATA[Burhan U Din Drabu]]></dc:creator>
            <pubDate>Sat, 18 Jun 2022 11:59:09 GMT</pubDate>
            <atom:updated>2024-02-26T20:23:33.229Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RyVhS1FQpn1RnH_4" /><figcaption>Photo by <a href="https://unsplash.com/@xoforoct?utm_source=medium&amp;utm_medium=referral">EJ Strat</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We always strive to write code which is compliant with basic coding standards like SOLID, SLAP(Single Level Abstraction Principle), Law of Demeter etc. We know these principles are fundamental and they take care of scalability, maintainability and many other factors for the most part.</p><p>This article is just another attempt to make things easier when it comes to handling the UI of our application, so we start by first looking at a fundamental use case.</p><h3>The Problem Statement<strong>:</strong></h3><p>The fact that our UI can request data, we develop views which would show its states. If we take a look at a basic operation here are some of the basic states that we can land into:</p><ul><li><strong>Fetching data<br></strong>The state where you would be requesting data from your repo and the data is being fetched irrespective.</li><li><strong>Data fetched successfully<br></strong>The state where you get the data you requested and you update your UI accordingly.</li><li><strong>Data fetch failed</strong><br>Getting issues while fetching data is not something new, we always keep a UI which essentially gives a retry mechanism which helps users get back on track.</li></ul><p>There are some of the basic states we might incur when doing the most basic operation in android, however, things might get complicated when we have multiple data fetching in a single screen with different repositories.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6CAcpqZ7QEu0j5Bm" /><figcaption>Photo by <a href="https://unsplash.com/es/@amseaman?utm_source=medium&amp;utm_medium=referral">Andrew Seaman</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>It doesn&#39;t stop here, once you fetch the data-set that doesn’t mean we just take the data and update the UI. A lot of times the state of the data would itself have multiple states associated with it. Sounds confusing.</p><h3>Consider this example:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uYg-qvgv1vQNPACx" /><figcaption>Photo by <a href="https://unsplash.com/@iamjensrb?utm_source=medium&amp;utm_medium=referral">Jens Behrmann</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Considering we have an app that manages different types of skilled experts, now depending on the skill set, we take different inputs from the user and the same would down-steam in respective flows which means every piece of UI has different states(info). Consider these not at a screen level where you can put them into fragments but in a widget context where you have multiple sections tiled in a View.</p><p>These states can vary from loading, failure, success, retry and what not. Each states can have their own set of sub-states which would make it quite hard to maintain.</p><pre>expertViewModel.progressBarUI.observe(viewLifecycleOwner) { it -&gt;<br>    // show - hide progress bar<br>}<br><br>expertViewModel.expertDetailsUI.observe(viewLifecycleOwner) {<br>    // access binding and make updates to ui<br>}<br><br>expertViewModel.failureUI.observe(viewLifecycleOwner) {<br>    // show failure ui <br>}<br><br>... and it goes on and on....</pre><h3>What’s wrong with this approach:</h3><p>Here’s a list of some of the issues that we can find by looking at the code snippet above:</p><ul><li><strong>Open for modifications:<br></strong>A problem that you can tell just by looking at the code is it&#39;s open for modifications which simply means every time you add a new expert in the system. You’ll have to come back to this file and handle the case which incurs modifications in an existing file which one should always try to avoid.</li><li><strong>Poor Encapsulation</strong>:<br>If you look closely you will find the data being used here is open for any state to access. The type check is happening at the UI layer as the views are accessible at the same level. Would have been great if a state could have been able to <strong>access only</strong> the data it <strong>deals with.</strong></li><li><strong>Open View access</strong>:<br>When we define functions in the UI layer which are supposed to manipulate only a section of view can misuse and can modify the states of other widgets which not only makes it difficult to debug view issues but also makes your view venerable to being modified anywhere in the UI layer.</li><li><strong>Limitless LOC</strong>:<br>When our code is open for modifications then it&#39;s very hard to limit the lines of code in a file which ideally shouldn&#39;t be more than 500 Lines of Code. Consider the statement above, as experts keep adding to the system. Your LOC will keep increasing.</li><li><strong>Private functions chaos:<br></strong>when updating different parts of UI, we would for sure have multiple private functions associated with the primary function and this means you’ll be having multiple private functions for multiple states.</li></ul><h3><strong>What we can achieve:</strong></h3><p>Now, this is soothing and here’s how we can do this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EKAd9_ePiJYZZXccmdsqrw.png" /></figure><p>As <strong>clean</strong> as it looks, it is as equally <strong>easy</strong> to implement.</p><h3>How to make it work:</h3><p>So we’ll use just the basics of Polymorphism to achieve this by defining an abstract class here’s how we do it:</p><h4>1. Define an interface for states:</h4><p>we define a sealed interface and define the finite set of states that the UI can withhold.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PcwQxtmOYKnMErXIhmU8DA.png" /></figure><h4>2. Defining states:</h4><p>Each State would override updateUI function which would make the updates as it requires. Please note that here you can add private functions to ensure code maintainability and readability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dWcZTM_c4g2E0lU05LgO7g.png" /></figure><h4>3. Pushing States from ViewModel:</h4><p>Now Inside your view-model, depending upon the use case we would push the State and encapsulate data inside the same. This way States define the data they require and that’s how we encapsulate their data dealing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gWiFpzf-KhaMeEEBEImBgA.png" /></figure><h4>4: We just listen on the UI layer:</h4><p>At last, we just attach the observer and it takes care of routing the state to its respective implementations. That’s It!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qADyTkw2dPj-08RPTtnbSg.png" /></figure><h3>Conclusion:</h3><p>By doing all this you have not only made your code compliant to basic coding standards set by SOLID but you would enjoy the mechanics when adding more states to our UI.</p><p>Check out this example to see this approach in action.</p><p><a href="https://github.com/Drabu/state-driven-ui-android">GitHub - Drabu/state-driven-ui-android: 💥 A simple design were you separate different states of the UI in different classes which and the state. This not only helps in allocating single responsibility to classes but also makes you design open for extensions for when adding new states. The states also enclose data-set required for their operation.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d03003b206e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A much better approach to managing our repository with MVVM in android]]></title>
            <link>https://medium.com/@drabu/policy-based-repository-in-android-c6222f3e22c6?source=rss-acdf3766b574------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6222f3e22c6</guid>
            <category><![CDATA[solid-principles]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[repositories]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Burhan U Din Drabu]]></dc:creator>
            <pubDate>Mon, 13 Jun 2022 03:39:29 GMT</pubDate>
            <atom:updated>2022-06-19T12:35:03.208Z</atom:updated>
            <content:encoded><![CDATA[<p>As we scale our repository with multiple data sources, we always aim to make it more readable but simultaneously limit the lines of code, and build it in the <strong>best way possible</strong> that can make our system <strong>easy to scale</strong> and easy to <strong>maintain</strong>. This article is just another attempt to <strong>achieve this goal</strong> specifically the <strong>repository</strong> <strong>part</strong> which serves as a single source of truth.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eBGGRIg8jVwKcwH1" /><figcaption>Photo by <a href="https://unsplash.com/@brett_jordan?utm_source=medium&amp;utm_medium=referral">Brett Jordan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>A little sneak peek to get you excited:</h3><pre>/*<br>This policy would try to get data from the Network first<br>and then would fall back to other data sources.<br>*/</pre><pre>viewModel.requestData(Policy.NETWORK_FIRST)</pre><p>A simple approach where our UI layer requests data from the view model with <strong>policies</strong> depending upon its use case where policies are nothing but a queue of data sources which are called upon until the desired data is acquired. Policies need not always be a combination but can also be a single data source as well.</p><h3>The Pain point<strong>:</strong></h3><p>Considering the fact that we have multiple data sources which are primarily managed by our repository, it does get affected when we add new sources.</p><pre>if(getDataFromAPI().error())<br>   if(getDataFromDatabase().error())<br>        .... and it goes on and on.....</pre><p>This adds a lot of boilerplate to the repository and as the scope of our data-management increases, we have to define more methods in case we want to fall back to other data sources, and then have a method for each of the data sources separately.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*t1VkGiJHi3lg2zn4" /><figcaption>Photo by <a href="https://unsplash.com/@brett_jordan?utm_source=medium&amp;utm_medium=referral">Brett Jordan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3><strong>Issues to tackle:</strong></h3><p>Here are some of the issues that we may encounter if we tend to continue using our repository by defining methods with a fallback approach:</p><ul><li>It increases LOC as we keep adding new data sources.</li><li>Adds complexity to our repository.</li><li>Hardcoded fallback approaches restrict us from changing their behaviour at runtime.</li><li>Adding new sources of data modifies the existing repository which in turn violates the open-close principle.</li><li>Separate methods to handle permutations and combinations of data sources add clutter to our repository.</li><li>Redundant error handling with each data call in the repository leads to redundant code.</li></ul><h3><strong>What we can do:</strong></h3><p>Instead of defining the sources of data in the repository, We can have the sources composed with the right behaviour object. Sounds complicated? Let me simplify.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7dS_c8P2Z7ZSB4AH" /><figcaption>Photo by <a href="https://unsplash.com/@brett_jordan?utm_source=medium&amp;utm_medium=referral">Brett Jordan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We simply queue data sources inside the repository. The queue is popped until it&#39;s empty and the data is retrieved from any of the sources. In case we run out of data sources we update the UI accordingly and the resulting code looks something like this:</p><pre>/*We Queue the sources which would pop in the FIFO pattern*/<br>val policy = ArrayDeque(<em>arrayListOf</em>(APISource(), DatabaseSource()))</pre><h3>How does this help?</h3><p>Here are some of the perks of using this approach and how it makes your life easy while maintaining the scalability of your repository</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ctBy90u0Z9KbTDjFSbVM-w.jpeg" /></figure><p><strong>Less Redundant Code:</strong> Since your data source is now a queue, we can <strong>define multiple permutations </strong>of the queue by creating policies just by reordering the data sources in the <strong>queue</strong>.</p><p><strong>SOLID principle: </strong>We’re isolating data sources which makes our repository<strong> closed to modifications</strong> and makes it <strong>open for extensions</strong>. Anytime you need to add a <strong>new data source</strong> you just need to <strong>add it </strong>to the Queue. <strong>That’s it!</strong></p><p><strong>Efficient Error handling: </strong>becomes too smooth when dealing with multiple data sources which otherwise need to be handled individually.</p><pre>override fun onFailure() {<br>    if (arrayDeque.<em>isNotEmpty</em>())<br>        arrayDeque.pop().getData(this)<br><br>}</pre><p>In a nutshell, this approach keeps the basic principles of development in tact as policies would not only get rid of multiple if-else blocks but also makes our code complaint to single-responsibility and open-close principle.</p><p>Check out this example to see this approach in action.</p><p><a href="https://github.com/Drabu/policy-based-repo-android">GitHub - Drabu/policy-based-repo-android: 🚀Policy based pattern is simple approach where you can align your data sources in a simple queue and pop the queue untill you get the required data. You can rearrange order of the data-sources and label them as policies so now your UI can request data in policies and all of it without any spaghetti code</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6222f3e22c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Validate forms in android by FormValidator Library.]]></title>
            <link>https://medium.com/@drabu/validate-forms-in-android-by-formvalidator-library-eb6bb298ad77?source=rss-acdf3766b574------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb6bb298ad77</guid>
            <category><![CDATA[android-forms]]></category>
            <category><![CDATA[forms]]></category>
            <category><![CDATA[validation]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[edittext]]></category>
            <dc:creator><![CDATA[Burhan U Din Drabu]]></dc:creator>
            <pubDate>Sun, 12 May 2019 11:05:59 GMT</pubDate>
            <atom:updated>2022-06-13T18:41:46.394Z</atom:updated>
            <content:encoded><![CDATA[<h3>Validate forms in android</h3><p>How to validate forms in android with optional values and custom error messages without using any custom views.</p><p><strong>The problem with validation</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-JWwmeFCfFAFQhY4vgOCcw.jpeg" /><figcaption>When you are validating 20 edit texts one by one and you have 10 more to go!</figcaption></figure><p>Validating forms in android is such a tedious task and if you’re an android developer you can co-relate. I’ve been working in android native technology for about three years now and have been waiting for some cool library to solve this problem, but till date i couldn’t find one. I could find libraries that would do form validation but would replace the edit-text with some custom view having custom attributes which i don’t find feasible to implement.</p><h3><strong>Problem with existing libraries</strong></h3><blockquote>Change a chunk of existing code in my XML</blockquote><p>Although i love opensource libraries but i I can’t just replace all my edit text with some &lt;com.abc.def.EditText /&gt; as it adds up a lot of work on my desk as i am already done designing the view.</p><blockquote>Less or no error handling.</blockquote><p>We all love to follow the happy path but what if there is an issue while evaluating the form validation. There is very little or no error handling mechanism in form validation libraries i happened to check out.</p><blockquote>Setting errors on TextInputLayout</blockquote><p>Considering you have Edit Texts enclosed in Text Input Layouts, it really gets nasty as the libraries set errors to edit texts but you need them on Text Input Layouts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*npUERzvibrqxAM-SwKKLdQ.jpeg" /><figcaption>Can’t risk all of it!</figcaption></figure><p>And considering if the author removes the library or has a crucial unresolved bug then you are in deep trouble. You may have to replace those views again everywhere in your project which is again a nasty task to perform.</p><h3>So What to do?</h3><p>Now after all of waiting i finally figured out a way to resolve all of these issues and packed them in a library called <a href="https://github.com/Drabu/FormValidator"><strong>FormValidator</strong></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*1fHyZI08XQXWnDMTSN9-aw.jpeg" /><figcaption>It does the job and i simply love it!</figcaption></figure><p>A library that lets you do stuff with the following features</p><ul><li>Validate forms with in built Edit Text views and no custom views required.</li><li>Comes with error handling mechanism.</li><li>Can skip optional edit texts.</li><li>Fits well with setting errors with or without TextInputLayouts as it detects view automatically and sets error accordingly.</li><li>Enable / Disable errors on form after evaluation.</li><li>Custom error message.</li></ul><p><strong>All of this magic comes to action:</strong></p><pre>FormValidator.isFormFilled(mainLinearLayoutLL, this, optionalParams = intArrayOf(R.id.lastNameET)) </pre><h3><strong>How does it work?</strong></h3><p>The library takes a layout (ViewGroup) which contains your form and iterates the views inside the View Group. The library uses recursive approach to search edit text views and checks them individually and all of this happens in a worker thread.</p><pre>override fun onFormValidationTaskSuccess(isFormFilled: Boolean) {<br>        /*Here isFormFilled represents that weather the form is filled or not*/<br><br>        if (isFormFilled){<br>            Toast.makeText(this, &quot;Form filled&quot;, Toast.LENGTH_LONG).show()<br>        }else {<br>            Toast.makeText(this, &quot;Form is not yet filled, Please fill the form.&quot;, Toast.LENGTH_LONG).show()<br>        }<br><br>    }</pre><h3><strong>Why unbind the listener in onDestroy</strong>?</h3><p>Considering if you requesting for form validation and you set the error Enabled to true , and at the same time you exit the application.</p><p>FormValidator.clearFormValidator()</p><p>The FormValidator is checking forms in an asynchronous worker thread and once it finds an empty edit text it tries to set error while the view is destroyed already and that may result in an error. So to prevent this you need to unbind the listener and everything is in control.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*p9ut6pW6bL5likV1VFU4MA.png" /><figcaption>You are in control.</figcaption></figure><p>Feel free to checkout the library and you’re more than welcome to improvise. Thank you for your time.</p><p><a href="https://github.com/Drabu/FormValidator">Drabu/FormValidator</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb6bb298ad77" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>