<?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 Dhilip Seshadri Kumar on Medium]]></title>
        <description><![CDATA[Stories by Dhilip Seshadri Kumar on Medium]]></description>
        <link>https://medium.com/@dhilipsesh?source=rss-16629f51a284------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GXbosUvxD1rD9Tx-</url>
            <title>Stories by Dhilip Seshadri Kumar on Medium</title>
            <link>https://medium.com/@dhilipsesh?source=rss-16629f51a284------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 03:02:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhilipsesh/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[Defining Data Sources in Clean Architecture Android]]></title>
            <link>https://medium.com/swlh/defining-data-sources-in-clean-architecture-android-f0ee7cbc6634?source=rss-16629f51a284------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0ee7cbc6634</guid>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[datasource]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Dhilip Seshadri Kumar]]></dc:creator>
            <pubDate>Tue, 19 Jan 2021 07:45:48 GMT</pubDate>
            <atom:updated>2021-01-19T14:39:02.647Z</atom:updated>
            <content:encoded><![CDATA[<h3>Defining Data Sources in a Clean Architecture Implementation -Android</h3><p><strong>Prelude</strong>:</p><p>In my previous article (<a href="https://medium.com/swlh/my-take-on-clean-architecture-implementation-in-android-4959ae4e18a6">My take on clean architecture</a>), and (<a href="https://medium.com/swlh/design-together-clean-architecture-android-77624cf5d385">Let&#39;s design together with clean architecture</a>), I had provided my insights on Clean Architecture and how to go about designing it using an example app. I had a lot of follow-ups and questions regarding how and what we consider as a data source. So I am putting together this article to deep-dive into how we can define and refine our data sources. If you are wondering what Clean Architecture is, refer to this <a href="https://medium.com/swlh/my-take-on-clean-architecture-implementation-in-android-4959ae4e18a6">article</a>. Without further ado let us dig in.</p><p><strong>Data Sources</strong>:</p><p>A data source is where data originates, even though the concept is straightforward and simple, comprehending and identifying a data source can be somewhat tricky. Data sources are usually tied up to a repository as recommended in Android architecture components and communicate to the other layers of clean architecture through them. Identifying a data source plays a crucial role in implementing clean architecture in an effective manner.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*H-l-7mHdb8gEiAtqDLHq2A.png" /><figcaption>Data flow from the data source through the layers of Clean Architecture</figcaption></figure><p>Let&#39;s take a look at a few data sources that we come across often in the world of Android.</p><p><em>Broadcast Receivers</em>:</p><p>Broadcast receivers are a way to receive data from the android system or from other applications. A lot of times this is used in conjunction with Activity and associated with them. From the perspective of clean architecture, this is a data source that can obtain data. This data source can be tied up to the corresponding repository. A lot of times we have one broadcast receiver listening to various intent actions. That is fine as long as it makes logical sense to group them together and trickles them down to the corresponding repository.</p><p><em>AIDL &amp; Bound services</em>:</p><p>AIDL(Android Interface definition layer) is one of the mechanisms by which an application residing in one process can communicate with an application residing in another process. AIDL can be a data source through which the application can obtain data and communicate with other applications. From the perspective of clean architecture, this can be tied up to a repository and trickle down to various layers of the architecture.</p><p><em>Network I/O</em>:</p><p>This is a relatively straightforward definition of data source, where we obtain data through low-level networking such as sockets and streams. In the case of sockets, a single one can entertain many different message types so one should carefully go ahead to choose the corresponding repo to trickle down the stream through the layers.</p><p><em>File I/O</em>:</p><p>For some reason, we do not generally consider file operations to be a part of the data source, we generally associate it with utils that can be accessed by any module directly. To ensure we treat every data source consistently, the file I/O should be considered as a data source and can talk to the corresponding repo based on the type of handling it does.</p><p><em>Rest/Http API</em>:</p><p>Rest or HTTP endpoints are one of the most common types of data sources that are available. We generally tend to associate a repository for this based on what type of data it carters. These should just deal with obtaining data either using 3rd party SDKs such as retrofit or plain old HTTP calls. We should refrain from processing these data here.</p><p><em>Shared Preferences</em>:</p><p>If we think about it, shared preference is not the first thing that we associate with the data source, but it is as much as a data source as anything else and should be handled the same way. Any transformation of data should be avoided in this layer and it should purely be data retrieval at this layer.</p><p><em>Databases &amp; Real-Time DB</em>:</p><p>Databases are the traditional storehouses of data and it should come as a no-brainer that it is included as a part of the data source. This could house any data retrieval mechanisms such as old-style DAO’s or Realm handlers.</p><p>Real-Time Databases such as firebase or dynamo DB are no exceptions to this. They follow the same pattern. They are treated as a data source and streams of data can flow to other layers from here.</p><p><em>Data from Third-party libraries/SDK</em>:</p><p>Oftentimes we tend to include a lot of different SDKs and third-party libraries to ease our life. These cater to different use-cases. It could be a simple UI component or a complex map data provider. Depending on whether it provides data that is used by the application, it could be treated as a data source and could fit into the data layer of the clean architecture.</p><p><em>Data from StartActivityForResult</em>:</p><p>This is an interesting aspect when we think about it. At times we interact with other applications to obtain data using AIDL, Broadcast, and more frequently by starting an activity for a result. Things tend to get a little tricky when we do a start activity for result and try to map the data obtained as a data source. There are ways to ensure that this data can be cleanly mapped as a data source rather than handling as a part of an activity. This can be achieved through ActivityResultContract and decoupling them from the Activity.</p><p><em>Push Notifications</em>:</p><p>Push Notifications are yet another form of data that gets pushed down from the server. A lot of time, it is associated with immediate action from the user but sometimes can expect silent execution in the background. Nevertheless, this should be treated as another data source and the data should flow from here to the repo and the use-case where our business logic can process this effectively.</p><p><em>Android Data providers</em>:</p><p>Content providers are the other usual suspects, Application uses content providers to share the data across which are more static in nature. This again falls directly in the data source category, more obvious than a few others mentioned above.</p><p><strong>The Denouement:</strong></p><p>This approach of identifying data sources and wiring them up by layers of Clean Architecture can be applied to any solution on any scale. I have tasted quite a bit of success with this philosophy and have found the applications are a lot more scalable and a lot of code can be put to reuse, even across different applications. Though I have talked about defining data sources for Android, this can be applied to any OS where you would like to incorporate clean architecture.</p><p>Even though I have tried to be comprehensive, I am sure there are more that we can consider as a data source. Feel free to comment on any which you feel can be added as a data source.</p><p>In this data-hungry world, defining proper data sources becomes the fulcrum of creating good applications, and identifying the right data sources is Step 0.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0ee7cbc6634" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/defining-data-sources-in-clean-architecture-android-f0ee7cbc6634">Defining Data Sources in Clean Architecture Android</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[Architecting a VOIP Cross-Platform Mobile App using WebRTC with Unity 3D]]></title>
            <link>https://medium.com/swlh/architecting-a-voip-cross-platform-mobile-app-using-webrtc-with-unity-3d-a016d10c495f?source=rss-16629f51a284------2</link>
            <guid isPermaLink="false">https://medium.com/p/a016d10c495f</guid>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[cross-platform]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[webrtc]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Dhilip Seshadri Kumar]]></dc:creator>
            <pubDate>Sun, 19 Jul 2020 11:34:47 GMT</pubDate>
            <atom:updated>2020-07-19T11:34:47.753Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Prelude</strong>:</p><p>I realize that there are a lot of technologies involved in the title and it is there for a reason. The fascination towards P2P can not be downplayed. Ever since the advent of the internet and file-sharing applications, the voices of P2P communications have always been growing louder. And it is especially growing in popularity in recent pandemic times where we rely heavily on VOIP for remote communications. The recent surge in popularity has raised concerns about security and privacy. WebRTC remains one of the most secure ways for VOIP communication between two peers in an encrypted fashion and hence the drive to use this. Switching gears on to Unity, cross-platform development is again one of the trending topics which promise to achieve the goal of writing once and deploying everywhere. we don&#39;t generally associate Unity to develop mobile applications other than games, but there are inherent advantages to using Unity as a cross-platform development tool which we will analyze later in this article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aluzZxhRny_mFVAooS2Bbw.jpeg" /></figure><p><strong>WebRTC:</strong></p><p>For folks who are new to WebRTC, I will try to explain what it is and how it works very briefly. WebRTC is an open-source framework to establish real-time communication between peers.</p><p>WebRTC relies on two main server entities to get its initial handshake to establish between the peers. First is the STUN server which is used to obtain the public-facing IP address of the remote peers and next is the Signalling Server which initiates the handshake and establishes communications between the two parties. Once the initial handshake sequence is done the communication becomes P2P. For development purposes, there are many publically available STUN servers and Signalling servers, although I would recommend hosting open-source server projects in your own server setup. Keep in mind it would consume very less bandwidth as it is only used for the handshake.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*puNzFa8peiU8ACH8dRK6Rg.png" /></figure><p>Finally the dreaded component — “TURN server”. TURN server is a relay server that is used as a fallback mechanism when the remote peers are behind a symmetrical <a href="https://en.wikipedia.org/wiki/Network_address_translation">NAT</a> and it is difficult to obtain the public IP address using STUN. So, in this case, we do not use P2P which means the whole communication is relayed using the TURN server adding to the cost and bandwidth. But thankfully we could avoid using the TURN server for most of the use cases. There are open-source implementations of TURN available which can be deployed easily. We can also rely on readymade solutions provided by third-party vendors (please refer to the reference section).</p><p><strong>Unity as a Development Tool:</strong></p><p>Unity is not the first thing that comes to mind when we think about cross-platform development tools for mobile applications but it has its own merits. Unity proves to be useful if we plan to extend support to devices such as HoloLens, Mac apps, and especially when it comes to throwing in some AR capabilities to our VOIP application. It has an inbuilt physics ray-cast which helps in the placement of models over planes quite easily.</p><p>Unity also has third party WebRTC plugins that would help us achieve the same and of course we can develop both ARKit and ARCore features in Unity. We can easily achieve 75% common codebase and 25% specific to platforms.</p><p><strong>Application Development:</strong></p><p>So now that we have an ecosystem in place with the STUN server, Signalling Server, TURN server, Unity IDE, and WebRTC plugin the missing piece is how do we put it all together using a Mobile application and build a workable solution.</p><p>So to brainstorm the architecture and build a solution, there are two key aspects we should consider further.</p><ol><li>Exchange of keys between peers</li><li>Integrating the Push Notification Mechanism for a seamless experience</li></ol><p>WebRTC Plugin requires a unique identifier that needs to be exchanged between the callee and the caller. This is usually done outside of the scope of application. We could send a unique ID when we register a session with WebRTC. This is the unique ID that the other peers will be able to connect (Imagine this as a meeting room code). We can rely on iMessages or text messages to send the ID to other clients. We can register a URI schema so that clicking on the text messages can open our application directly.</p><p>Next is notifying the callee about receiving a call. There are scenarios when the host application could be in the background or be closed while it waits for clients to join in. So we need to ensure that we notify the host appropriately when the clients join. We could use the Push notification mechanism to inform the callee about incoming calls and also integrate the platform-specific call UI such as CallKit and so on. Push Notification can be achieved by a simple push notification server or firebase.</p><p>So now that we have thought through everything that needs to be in place, let us put those thoughts on to blocks in the application. The high-level system can be envisioned as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*lCsvrLWL4h3427B82GVZbg.png" /></figure><p><strong>The Denouement:</strong></p><p>Thus a simple and effective VOIP communication ecosystem can be envisioned by identifying the right components. WebRTC plays an increasingly major role in VOIP communication. It is easy and fun to integrate and extend various solutions from it. Unity’s physics engine is advanced and perfect to cater to various AR needs. Further information on all these components can be found in the references section below that can help to kickstart your development.</p><p><strong>References</strong>:</p><ol><li>WebRTC Unity Plugin — <a href="https://www.because-why-not.com/">https://www.because-why-not.com/</a></li><li>Signaling Server — <a href="https://github.com/because-why-not/awrtc_signaling">https://github.com/because-why-not/awrtc_signaling</a></li><li>STUN/TURN — <a href="https://github.com/coturn/coturn">https://github.com/coturn/coturn</a></li><li>TURN 3rd party — <a href="https://www.twilio.com/stun-turn">https://www.twilio.com/stun-turn</a></li><li>Push Notifications — <a href="https://firebase.google.com/products/cloud-messaging">https://firebase.google.com/products/cloud-messaging</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a016d10c495f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/architecting-a-voip-cross-platform-mobile-app-using-webrtc-with-unity-3d-a016d10c495f">Architecting a VOIP Cross-Platform Mobile App using WebRTC with Unity 3D</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[Let’s design together with Clean Architecture in Android]]></title>
            <link>https://medium.com/swlh/design-together-clean-architecture-android-77624cf5d385?source=rss-16629f51a284------2</link>
            <guid isPermaLink="false">https://medium.com/p/77624cf5d385</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[mobile-appdevelopment]]></category>
            <category><![CDATA[architecture-design]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Dhilip Seshadri Kumar]]></dc:creator>
            <pubDate>Wed, 24 Jun 2020 09:38:05 GMT</pubDate>
            <atom:updated>2020-06-24T21:33:11.644Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Prelude</strong>:</p><p>In my previous article (<a href="https://medium.com/swlh/my-take-on-clean-architecture-implementation-in-android-4959ae4e18a6">My take on clean architecture</a>), I had provided my insights on Clean Architecture on a bird’s-eye view. I had a lot of follow-ups and questions regarding the actual implementation. So I thought rather than pointing to some code in a GitHub repo why can’t we design together and incorporate the Clean Architecture philosophy? Hence this article. If you are wondering what Clean Architecture is, refer to this <a href="https://medium.com/swlh/my-take-on-clean-architecture-implementation-in-android-4959ae4e18a6">article</a>. Without further ado let us dig in.</p><p><strong>The Problem Statement:</strong></p><p>For this article let us architect &amp; design a simple Weather Application with the following requirements.</p><ul><li>Need to fetch location (Fetch from high accuracy GNSS receiver via Bluetooth, when unavailable fetch location from Android Location Manager)</li><li>Obtain Weather data ( Obtain weather data from various sources such as OpenWeather API, Accu Weather and so on based on user preference)</li><li>Display weather data alongside weather images and 7 days prediction</li></ul><p><strong>Thinking Blocks</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aG3G5N1q4dt0EjGnZqY6mA.jpeg" /></figure><p>To architect any application the first step would be to identify and envision the rudimentary blocks that would be used in the application. Let us go through each of the requirements and start creating blocks.</p><p><em>“Need to fetch location”</em>:</p><p>So let us begin by arriving at the data that is needed for the application to work. So our first block would be “Location”, hey but again we need location from two different sources so let us segregate them into “Android Location”, “GNSS Location”.</p><p><em>“Obtain Weather data”</em>:</p><p>So now that we have location blocks let us move on to the next requirement about the weather. So by repeating the same for Weather data we would have “OpenWeather” and “Yahoo Weather” blocks.</p><p><em>“Display weather data”</em>:</p><p>The next set of blocks to identify is to display to the user based on the requirements. So we would need an “Activity” and associated fragments. When we think of the data in UI, we would also think about the “Adapters” and “ViewModels” which are going to modify the data obtained to suit the UI needs.</p><p>So now we have arrived at various blocks which can be summarized in the diagram below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*vQ9ZndoGz1OAEa5XYW5txA.png" /><figcaption>Rudimentary blocks for the problem statement</figcaption></figure><p><strong>Layering the Blocks with Clean Architecture:</strong></p><p>Now that we have the basic blocks in place the next step would be to segregate them to align them with the pillars of Clean Architecture. Just to recap, the layers in Clean Architecture can be categorized into UILayer, Domain Layer, Repo Layer, and DataLayer. Let’s get started with the all-important data layer.</p><p><em>DataLayer:</em></p><p>In the blocks, we have identified it would make sense to place the data source blocks in this layer. The data blocks that build this application are AccuWeather API, OpenWeatherMap API, Android Location Listener, and GNSS Location Provider. Each of them provides a type of data that the application requires to consume, hence it makes perfect sense to map them to the data layer. These blocks are sometimes referred to as remote data. So the obvious next question would be, is there something called local data? Here the DataCache block would act as a local data source that would provide data in offline scenarios. Hence that would add to our Data layer as well.</p><p><em>Repo Layer:</em></p><p>It’s common for people to get the Repo layer intertwined with the data layer and have also come across people arguing that it should be merged into the data layer. One has to carefully distinguish them so that we can achieve low coupling and high cohesion. Our previous “thinking blocks” section did not result in a block for this layer and it is understandable as it was rudimentary. The repo layer for this problem statement would consist of two main blocks, “Location Repo” and “Weather Repo”. So these layers define what type of data would be obtained and would delegate the implementation to the corresponding concrete repo’s.</p><p><em>Domain Layer</em>:</p><p>The domain layer is where we place all our business logic. This is a very critical layer and in an ideal scenario should be designed in a way that can be reused across various use-cases. Use-cases are usually driven by a need and a mechanism by which the data can be consumed and put to use in a meaningful manner. This is the part of the application which is state aware and interacts with various sources to provide information useful for the application. For this reason, it is often referred to as interactors or use-cases.</p><p>These blocks are not the first thing you identify in a problem statement but then we eventually stumble on to them when we think about wiring our data. In our problem statement, the major use case is to fetch weather data. The UI layer might probably have no use of the location data by itself but then we know that it is needed to fetch weather data. The sequence of fetching the right location first and then obtaining weather data based on the location is handled by “WeatherUseCase” in the domain layer.</p><p>Though location data will not be directly consumed by the UI layer we could still make a “LocationUseCase” which would fetch the right location data from various sources and can be consumed by the WeatherUseCase. I know people are divided upon a use-case invoking another use-case but then my rationale would be re-use use-cases and have them stick to a single responsibility.</p><p><em>UI Layer</em>:</p><p>This is our presentation layer that is going to display all the good work done by other layers to the end-user. This layer not just deals with displaying UI but also deals with adapting the data to suit each of the screens. So as we identified earlier “Activities”, “Fragments” and “ViewModels” falls under this layer.</p><p>So now that we have visited all the pillars of Clean Architecture, let us take a look at how it all looks together when represented as a diagram.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ImhBGILfoyoCaKs6tltZNg.png" /></figure><p><strong>Wiring the blocks and Sequencing:</strong></p><p>Segregating blocks is just one part of the equation unless we wire them properly we still won’t align ourselves with the design philosophy of Clean Architecture. So to sequence and wire these blocks together we should understand the data flow and how each block interacts with each other. This is relatively easy now since we have separated the layers and have identified the data sources.</p><p>So as a rule of thumb the data requests go from left to right in the above diagram and the data flows from right to left. It is also essential to determine how the data flows between blocks inside each layer to design the right way. Let us try to sequence one particular request and come up with all interactions in that sequence.</p><p><em>ObtainWeather Sequence :</em></p><p>Let us start thinking through the sequence starting with the UI layer, so here a Weather Fragment associated with the Main Activity requests its corresponding ViewModel (WeatherViewModel) for weather data. The Weather View Model knows where to fetch the data and requests the data from the domain layer’s WeatherUseCase. The WeatherUseCase is where all the intelligence is built-in, it first requests LocationUseCase to fetch the best possible location data which in turn contacts the corresponding repo layer and obtains the highest possible accurate location and provides it to WeatherUseCase. Having obtained the location, the WeatherUseCase then talks to corresponding Weather Repos to obtain weather data from different sources and finally provides it to the ViewModel which requested it. The ViewModel in turn modifies the data to suit the needs of the UI and sends it across to the WeatherFragment to display.</p><p>The above thought process can be visualized by a sequence diagram below. Even though the calls might not be synchronous in real-time, it does a decent job of representing the flows in a way we can understand better. I have also ignored the DataCache sequence to avoid alternate flows and to keep it simple</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G4eLxeQm6neTq2_heTiqpA.png" /><figcaption>Basic Interactions between various blocks</figcaption></figure><p><strong>The Denouement:</strong></p><p>This approach of brainstorming blocks, segregating them on to layers, and wiring them up by layers of Clean Architecture can be applied to any solution on any scale. I have tasted quite a bit of success with this philosophy and have found the applications are a lot more scalable and a lot of code can be put to reuse, even across different applications.</p><p>At the end of the day, the design is something we always tweak to suit our application needs, do feel free to talk about any approaches you have taken to incorporate Clean Architecture philosophy in the comments section that might benefit the readers.</p><p>Once we get the backbone and data flow right for any application coding is just something we would savor.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77624cf5d385" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/design-together-clean-architecture-android-77624cf5d385">Let’s design together with Clean Architecture in Android</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[My take on Clean Architecture implementation in Android]]></title>
            <link>https://medium.com/swlh/my-take-on-clean-architecture-implementation-in-android-4959ae4e18a6?source=rss-16629f51a284------2</link>
            <guid isPermaLink="false">https://medium.com/p/4959ae4e18a6</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[architecture-components]]></category>
            <dc:creator><![CDATA[Dhilip Seshadri Kumar]]></dc:creator>
            <pubDate>Tue, 05 May 2020 15:39:23 GMT</pubDate>
            <atom:updated>2020-05-17T17:27:08.829Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Prelude:</strong></p><p>Clean architecture is a wonderful way to make sure your application has a strong foundation that can be made scalable to build upon. When done right, it helps in achieving the holy grail of designing once and building incrementing blocks on top of it without having nightmares about refactoring the architecture.</p><p>I decided to give Clean Architecture a try almost 1.5 years back in Android alongside Kotlin &amp; Koin and the experience was very heartening to see how it all worked seamlessly to date.</p><p>Clean Architecture revolves around the principle of “layers of separation”, where each layer is decoupled from another and the communication flow is clearly defined. My take on the clean architecture is no different, it is strongly based on the same principles but offering a mechanism more suited for Android development.</p><p><strong>The Pillars:</strong></p><p>I don’t want to hypnotize you with the classic clean architecture diagram which is found everywhere (just google it, if you haven’t come across). So let us jump directly to the basic architecture diagram which can be made scalable and reusable for most of the Android application projects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*KMhaja_CFgHPE3bhV7qbew.png" /></figure><p>The architecture above can be separated into 4 main layers: Presentation layer, Domain layer, Repo layer, and Data Store layer.</p><p>The Presentation layer deals with application UI and primarily where the user interacts with the system. This layer consists of Activity, Fragments, ViewModels, and any associated UI components.</p><p>The Domain layer deals with application logic. Any business logic resides in this layer in the form of use-cases. This layer is the brain of the architecture and it knows which repo to contact, which logic to apply, how to transform, and almost everything that deals with any business logic. This is the crux of the entire application. This layer does not persist any data, it just delegates.</p><p>The Repo layer is responsible to fetch corresponding data. The data for the repo in this layer can be from anywhere, be it a local database or a remote server. The repo acts as a facilitator to obtain the data from the corresponding source and send it to the domain layer as requested. For example, assume a LocationRepo, The implementation of this repo can be the one that registers with the Android Location manager and receives location data from it or it could be the one that talks to a GNSS receiver via Bluetooth and obtains a high accuracy location. So the implementation can vary based on data sources.</p><p>The Data layer is the one that provides data to the application. It could be a remote server or a local database. These data sources can even represent similar data as discussed above. The data source can even be an AIDL or service connection from other applications. Any data that comes through needs to be tied to a repository to map them on to application needs.</p><p>So the idea sounds simple right? Mapping different components to these 4 modules and everything can function in unison with harmony. Let’s take a look at some of the curious cases when it comes to Android and how to map them.</p><p><strong>The curious case of {} -&gt;</strong></p><p>ViewModel:</p><p>So, I have come across people from MVVM background who talk about how all business logic should reside in ViewModel and there are others who want nothing to do with the ViewModel. My take would be to have any UI logic to be associated with ViewModel, whereas any application logic to be associated with a use-case (domain layer). For example, assuming I want to sort a list of media based on a button press — I would go for writing the piece of logic in ViewModel. On the other hand, assume my UI would only like to have a list of media of a particular user in which case this can be taken care of at the use-case layer. This would enable the same use-case to be reused by various other components in our Android application.</p><p>Broadcast Receivers:</p><p>Broadcast receivers can be easily mapped on to a data source. A lot of developers tend to directly map this data source to UI and register it via the Presentation layer. This can be avoided and the data can flow through the pillars of clean architecture by using an appropriate repo layer to consume this Data source and subsequently flow through the Domain layer onto the Presentation layer.</p><p>Context:</p><p>Another debated topic is about how to pass the context across these layers? I completely understand the hesitancy to pass context across layers. But then it again depends on what context you are trying to pass across. So having a fragment/activity context in ViewModel is not detrimental as its life cycles are associated with each other, whereas passing the same context to use-case/repo might cause leaks. It can be avoided by passing the application context for repo initialization if needed and not storing any context in the Domain layer whatsoever.</p><p><strong>Denouement:</strong></p><p>This flavor of clean architecture did prove to be quite useful when it came to scaling applications in my experience. Though it is very easy to create one such application from the start, refactoring an existing application might not be that easy, nevertheless still do-able. It does a wonderful job in the separation of layers and unit-testing can be easily achieved in each layer to a great extent. I have used MockK alongside Koin to achieve the same. Finally, I would like to end by saying that, it is not a deviation from our earlier principles rather a thorough refinement of it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4959ae4e18a6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/my-take-on-clean-architecture-implementation-in-android-4959ae4e18a6">My take on Clean Architecture implementation in Android</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>
    </channel>
</rss>