<?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 A. A. on Medium]]></title>
        <description><![CDATA[Stories by A. A. on Medium]]></description>
        <link>https://medium.com/@aaalllzzz?source=rss-107f86e82c4c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by A. A. on Medium</title>
            <link>https://medium.com/@aaalllzzz?source=rss-107f86e82c4c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 18:36:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aaalllzzz/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[Integrate Jenkins with Telegram]]></title>
            <link>https://medium.com/android-news/integrate-jenkins-with-telegram-1e5af7765d8?source=rss-107f86e82c4c------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e5af7765d8</guid>
            <category><![CDATA[telegram]]></category>
            <category><![CDATA[jenkins]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ci]]></category>
            <dc:creator><![CDATA[A. A.]]></dc:creator>
            <pubDate>Sun, 13 Oct 2019 15:04:06 GMT</pubDate>
            <atom:updated>2019-10-13T22:02:06.843Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wFihz619ZQpIBjGXKasHkQ.png" /></figure><p>Telegram is a popular messaging application used by so many people and Jenkins has been known to be one of the most popular CI/CD tools. Wouldn’t it be cool if we could send messages from Jenkins to Telegram?</p><p>I’m going to show you how to do it in five easy steps.</p><h3>#1 — Create a Telegram bot</h3><p>Open telegram and in search box type @botfather and click on it. Then it gives you instruction to how to create a bot.</p><p>typing /newbot and entering will ask you for a bot name and username, choose something meaningful and press enter. It’s look like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Ym4Z8b7SLNBNDwyHZCjBA.png" /></figure><p>After the bot is created it gives you a token that you can use to communicate to your bot with HTTP protocol.</p><p>Remember to send a private message to your bot with \start to active it.</p><h3>#2- Add the Telegram bot to a group</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*6cMCNLDIGnYX-o6MBwYo-g.png" /></figure><p>you can easily add your newly created bot to a group and typing @yourBotUserName it will be added like any other user and it can send messages to the group as well.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><p>.</p><h3>#3 —Get your group id</h3><p>Any group in telegram has a unique negative id witch is required as well as the token you previously gained to send messages.</p><p>To get that simply call this api with your token</p><pre><a href="https://api.telegram.org/bot%3CYourBOTToken%3E/getUpdates">https://api.telegram.org/bot&lt;YourBOTToken&gt;/getUpdates</a></pre><p>It will give you bunch of information including the group id. Below is a sample output you might get. You have to find your chat id (negative value representing a group with the group name below it). Store this number.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fBWZMXj4md7q64EY557Q6w.png" /></figure><h3>#4- Prepare Jenkins</h3><p>Now that you have your bot ready you can use http requests to send messages. You can send this calls from any where, in postman for example, in your back-end server on with help of <a href="https://wiki.jenkins.io/display/JENKINS/HTTP+Request+Plugin">HTTP Request</a> plugin, directly from Jenkins. So go ahead and install the plugin.</p><p>Also, the token is a secret value and we don’t want to use it directly. Instead we are going to put it inside Jenkins credentials and use it as a variable.</p><p>To do that go to Jenkins -&gt; Credentials -&gt; System -&gt; Global Credentials and add two new Secret String Values with ids telegramToken and telegramChatId</p><h3>#5- Send a http request</h3><p>Now, we define sendTelegram function in our pipeline to send the messages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95383fcbcb373af60ff40b4032c2a287/href">https://medium.com/media/95383fcbcb373af60ff40b4032c2a287/href</a></iframe><p>As you can see it does nothing but using the HTTP Request plugin to post a http request.</p><p><strong>Congratulations you are done!</strong></p><h3>Still a better way?</h3><p>Since this code is probably be used in more that one job you can define it as a library and use it anywhere you want. I described how to do it here. <a href="https://android.jlelse.eu/how-to-build-libraries-for-jenkins-pipelines-1772aa0da53d">https://android.jlelse.eu/how-to-build-libraries-for-jenkins-pipelines-1772aa0da53d</a></p><p>I already build a library for this and you can find it here. <a href="https://github.com/MobodidTech/jenkins-shared">https://github.com/MobodidTech/jenkins-shared</a></p><p>To use it add this lines to top of your pipeline code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a10fe7b1f723c5a68633bf6b2f1874e/href">https://medium.com/media/9a10fe7b1f723c5a68633bf6b2f1874e/href</a></iframe><p>Then use it like telegram.sendTelegram(yourMessageHere)</p><p>Don’t forget to share your ideas in the comment section below, If you like the articles <strong>Clap</strong>! Also, <strong>Follow Me</strong> to read my other articles as well.</p><ul><li><a href="https://android.jlelse.eu/how-to-build-libraries-for-jenkins-pipelines-1772aa0da53d">How to build libraries for Jenkins pipelines</a></li><li><a href="https://android.jlelse.eu/sending-events-from-viewmodel-to-activities-fragments-the-right-way-26bb68502b24">Sending events from ViewModel to Activities/Fragments — The right way</a></li><li><a href="https://android.jlelse.eu/basics-of-dependency-injection-for-dummies-b844d642e174">Basics of Dependency Injection for dummies!</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e5af7765d8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/integrate-jenkins-with-telegram-1e5af7765d8">Integrate Jenkins with Telegram</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sending events from ViewModel to Activities/Fragments — The right way]]></title>
            <link>https://medium.com/android-news/sending-events-from-viewmodel-to-activities-fragments-the-right-way-26bb68502b24?source=rss-107f86e82c4c------2</link>
            <guid isPermaLink="false">https://medium.com/p/26bb68502b24</guid>
            <category><![CDATA[android-architecture]]></category>
            <category><![CDATA[mvvm]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[A. A.]]></dc:creator>
            <pubDate>Mon, 09 Sep 2019 17:29:40 GMT</pubDate>
            <atom:updated>2020-01-19T18:35:59.290Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sending events from ViewModel to Activities/Fragments — The right way</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*omgCf0D6z5IfGWYIKwh27Q.jpeg" /></figure><p>In MVVM, Whenever you want to show some toasts or snack bar or sending an intent, you have to send a message from your ViewModel to the activity or fragment and ask it to do the job but the normal methods to do so all have big fallbacks.</p><p>In the ShowToast example, ViewModel is deciding when to show the message and what message to show but it is the view that is responsible for showing the actual message. I described View-ViewModel responsibilities in detail in another article. <a href="https://android.jlelse.eu/mvvm-how-view-and-viewmodel-should-communicate-8a386ce1bb42?source=your_stories_page---------------------------">MVVM — How View and ViewModel should communicate?</a></p><h3>The wrong ways to handle events</h3><h4>Using an interface, <a href="https://proandroiddev.com/kotlin-functions-an-alternative-to-interfaces-7cfb3c435900">higher-order function</a> or any other sort of callbacks</h4><p>We can define an interface in the ViewModel and let the Activity/Fragment implement it, then whenever some events happen we simply call the method on the interface. It is simple and straightforward and It works but it is the <strong>most stupid choice</strong> why? because unlike MVP, in MVVM, we don’t use interfaces for communicating to View and ViewModel should not have any reference to the View. <strong>Violating MVVM is not an option!</strong></p><h4>Sending Events with LiveData</h4><p>We can put our event data in a LiveData and let the Activity/Fragment observe it. It works but it has some downsides as well. Something like below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f38eef18a88564476e76a1cc21e58160/href">https://medium.com/media/f38eef18a88564476e76a1cc21e58160/href</a></iframe><p>But this is not going to work because:</p><ul><li>Because in case activity/fragment gets destroyed but the ViewModel does not. n configuration change for example view will be notified again about the event and we might end up handling event more than once. If we observe the data after onViewCreated it’s even worth since now every time the view gets destroyed on onDestroryView and gets recreated we will be notified again.</li><li>In case multiple fragments use the same ViewModel, all of them get notified about the event and handle it simultaneously, and that may not be what we want. We usually want one specific observer to handle the event.</li></ul><h4>Sending Events with OneTime Only LiveData</h4><p>There is some implementation of LiveData that will notify the observers only once, this way we don’t get the first issue with the previous solution.</p><p>It is a much better solution and I know people using it in production for a while but I still don’t like it because it doesn’t address the second problem with the previous solution and it worth now!, If we have multiple fragments observing the same data, now only one of them gets the data and we don’t know which one!</p><h3>The right way to handle events!</h3><p>We can use LiveData and get all the benefits of it, mostly the fact that it is lifecycle aware and It will only notify the view when the view is in an active state but don’t get any previous drawbacks by using a custom wrapper around our data. Our solution would be like this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a372a589d5ec586c1e18b06c35237f6/href">https://medium.com/media/5a372a589d5ec586c1e18b06c35237f6/href</a></iframe><p>What’s different now is that We make handling the events explicitly, All the observers get the data and they may get in more than once (since activity/fragment may get destroyed and upon reaction get the data again) but the event will be handled only once but the first observer who decides to handle it.</p><h3>The last world</h3><p>In my article, <a href="https://android.jlelse.eu/mvvm-how-view-and-viewmodel-should-communicate-8a386ce1bb42?source=your_stories_page---------------------------">MVVM — How View and ViewModel should communicate?</a> you see that handling some events like showing a toast should be done inside a view but preparing the data for that like witch message to show is ViewModel’s responsibility so we need a way to send this data to View and what I described the right way it’s the best thing I found so far! (It’s not my original solutions though but I don’t remember where I read it first to credit the author).</p><p>Also, Let me know what you think in the comment section below, <strong>Follow ME</strong> if you’re interested to read my other stories as well.</p><ul><li><a href="https://android.jlelse.eu/mvvm-how-view-and-viewmodel-should-communicate-8a386ce1bb42">MVVM — How View and ViewModel should communicate?</a></li><li><a href="https://android.jlelse.eu/basics-of-dependency-injection-for-dummies-b844d642e174">Basics of Dependency Injection for dummies!</a></li><li><a href="https://proandroiddev.com/kotlin-functions-an-alternative-to-interfaces-7cfb3c435900">Kotlin functions, An alternative to interfaces?</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26bb68502b24" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/sending-events-from-viewmodel-to-activities-fragments-the-right-way-26bb68502b24">Sending events from ViewModel to Activities/Fragments — The right way</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build libraries for Jenkins pipelines]]></title>
            <link>https://medium.com/android-news/how-to-build-libraries-for-jenkins-pipelines-1772aa0da53d?source=rss-107f86e82c4c------2</link>
            <guid isPermaLink="false">https://medium.com/p/1772aa0da53d</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[cicd]]></category>
            <category><![CDATA[jenkins]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[A. A.]]></dc:creator>
            <pubDate>Fri, 24 May 2019 13:06:46 GMT</pubDate>
            <atom:updated>2019-05-24T13:24:50.978Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oepEy8UlfmQM6a2iCHMVQA.png" /></figure><p>If you use Jenkins pipelines for automating your builds, Soon you realize you have to copy and past similar code between different pipelines. Wouldn&#39;t it be great to write it only once and use it as many times you want?!</p><p>In fact, it is possible and it’s more simple than what you might think. So let’s get started.</p><h3><strong>Building a simple library for shared functions</strong></h3><p>First, create an empty project and add it to a git repository. Then create a folder vars inside the root directory of your project. Go to vars the folder and create any file you want with .groovy extension. For example, I created a file named hello.groovy Inside this file write any functions you want. Below is an example that prints hello world.</p><pre>def printHello() {</pre><pre>    sh &#39;echo &quot;hello world from library&quot;&#39;</pre><pre>}</pre><p>That’s it!! You wrote a library and you are done. Just make sure to put push your changes to Jenkins server.</p><h3>Using your library in the pipeline</h3><p>Put this code on top of your pipeline</p><pre>library identifier: &#39;repo-name@master&#39;, retriever: modernSCM(<br>        [$class: &#39;GitSCMSource&#39;,<br>         remote: &#39;yourReposiotryAddress&#39;,<br>         credentialsId: &#39;my-private-key&#39;])</pre><p>Then in your code use it like this filename.functionName() For the hello world example it would be like this</p><pre>hello.printHello()</pre><p>credentialsId is not required if your repo is public.</p><h3>See an example</h3><p>I recently built a Jenkins library and made it public. You can check it out from here <a href="https://github.com/MobodidTech/jenkins-shared.git">https://github.com/MobodidTech/jenkins-shared.git</a></p><p>It is a very simple library and I’m planning to add more features to it later but even this simple example can be helpful to you.</p><h3>Conclusion</h3><p>Of course, there are more into writing a good Jenkins library, And you may want to use more advanced features. I’m a coder playing with Jenkins and this file -&gt; function structure is all we needed for our team. If you need to do more check out the official documentation. <a href="https://jenkins.io/doc/book/pipeline/shared-libraries/">https://jenkins.io/doc/book/pipeline/shared-libraries/</a></p><p>Don’t forget to share your ideas in the comment section below, If you like the articles <strong>Clap</strong>! Also, <strong>Follow Me</strong> to read my other articles as well.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1772aa0da53d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/how-to-build-libraries-for-jenkins-pipelines-1772aa0da53d">How to build libraries for Jenkins pipelines</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Examples of Kotlin extensions in Android]]></title>
            <link>https://medium.com/@aaalllzzz/examples-of-kotlin-extensions-in-android-ab5cb5d16871?source=rss-107f86e82c4c------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab5cb5d16871</guid>
            <category><![CDATA[extension-functions]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[technolog]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[A. A.]]></dc:creator>
            <pubDate>Thu, 23 May 2019 17:52:08 GMT</pubDate>
            <atom:updated>2019-05-23T17:52:08.175Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2SC2K6T-GhLi2AL3_89ZUA.png" /></figure><p><a href="https://kotlinlang.org/docs/reference/extensions.html">Extension functions</a> is one of my favourite features in Kotlin, To explain it in a most basic way, extensions are a nicer, more beautiful version of static methods! Look at code section below, both lines do the same thing, the only difference is that extension one looks nicer and is cleaner.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/493a7c5e88d40d59e256bcc10ace0678/href">https://medium.com/media/493a7c5e88d40d59e256bcc10ace0678/href</a></iframe><p>At first, this may seem like a very small improvement but applying extension methods throughout a large code base can improve code readability and it feels and acts more natural than static methods.</p><p>Defining an extension is super easy as well, All you have to do is to use the class you want to write an extension for before the method’s name like code section below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12e7cd42971bda98fd6ad0705555abb1/href">https://medium.com/media/12e7cd42971bda98fd6ad0705555abb1/href</a></iframe><p>If it’s still confusing, let’s look at some examples that I personally use in my projects.</p><h4>Example 01: Loading images with Picasso (or Glide or anything else)</h4><p>As you can see with this extension function we hide the complexity of loading images and we can, later on, change how images are loading or even changing Picasso with something else just by editing Extension function in one place.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cb4dd768bfce37eb2f30c0457c5c511/href">https://medium.com/media/7cb4dd768bfce37eb2f30c0457c5c511/href</a></iframe><h4>Example 02: EditText TextChangeListener</h4><p>This is my favourite one! When we use an interface or abstract class but we only care about one of the callbacks, in the example below we only care about afterTextChanged but we have to implement all of them otherwise compiler throws an exception! But with an extension and another feature of Kotlin called <a href="http://Higher-Order Functions">Higher-Order Functions</a> (passing a function as an argument to another function) we can greatly simplify this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd5c02494c92d11fa73f3a1b9a93862f/href">https://medium.com/media/bd5c02494c92d11fa73f3a1b9a93862f/href</a></iframe><h4>Example 04: Show Toast Message</h4><p>This is a nice one as well, This one I copied from <a href="https://medium.com/u/a1003b1eaf86">Suneet Agrawal</a> post from here <a href="https://goo.gl/LBUird">https://goo.gl/LBUird</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ea2de8cff1a768cebdf0c947afb2111/href">https://medium.com/media/1ea2de8cff1a768cebdf0c947afb2111/href</a></iframe><h4>Example 04: Setting click listener on ConstraintLayout group</h4><p>This is a classic use of a function when we have repeated code that we want to put in one place.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e81561e758c20ed01cb3d8941d2449a/href">https://medium.com/media/4e81561e758c20ed01cb3d8941d2449a/href</a></iframe><h4>Conclusion</h4><ul><li>Whenever you have a class that you want to add functionality to it but you don’t want to extend it</li><li>Whenever you want to add another utility class with static methods in it</li><li>Whenever you want to hide some complicity in the code</li></ul><p><strong>Consider extension methods as well</strong>, Using them effectively will make your code cleaner and easier to maintain.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab5cb5d16871" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Modular way of building Android apps]]></title>
            <link>https://proandroiddev.com/modular-way-of-building-android-apps-500ac816825?source=rss-107f86e82c4c------2</link>
            <guid isPermaLink="false">https://medium.com/p/500ac816825</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[A. A.]]></dc:creator>
            <pubDate>Tue, 02 Apr 2019 19:48:36 GMT</pubDate>
            <atom:updated>2020-01-19T18:34:54.193Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OPSXOJ7gJFxOzojfXfmi6A.png" /></figure><p>Having only one module in an Android app is very common and many projects are structured this way. Maybe because Android studio creates one module by default (app module) and many people don’t even think about it.</p><p>In contrast, we can construct our app to use multiple modules to work together to make the app. When we add libraries to build.gradle file, we are using other peoples modules in our code. We can do the same for our own code as well.</p><h4>What are the benefits of modular development?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*6QExcZhgEjQicSp4rpVOfA.jpeg" /></figure><ul><li><strong>Maintainance of the project gets easier<br></strong>When we add more and more code to a project, the project gets more and more complicated and as a result, maintaining the project gets harder, adding new features require more time and energy and fixing bugs get cumbersome. In fact, this is not even a linear relationship but rather an exponential one! So more code equals more complexity equals more headaches! <br>Using a modular approach greatly reduces this problem because instead of having one big project we have multiple smaller one that easier to manage.</li><li><strong>Modules can be reused</strong><br>If you have more than one project, it is very likely that some code is repeated between them, by using a modular approach you can write your code just once and use them as many times as you want. This will hugely decrease the development time, especially when you want to make some changes to the code.</li><li><strong>You write better code and better APIs<br></strong>Working with modular approach forces you to think more about the code and relationship between the different parts, hence letting you write better code.</li><li><strong>You can take advantage of open source community<br></strong>You probably don’t want to share your whole project on the internet and even if you do that may not be very helpful to other developers but you can share the parts of your project that are more general, like the part that crops the photo or uploads files or whatever. By doing this you get all the benefits of open sourcing without exposing your project or your idea to the world.</li></ul><p>There are three different ways we can add modules to our projects.</p><h4>Adding modules directly in our project</h4><p>We can simply add as many modules as we want to a single project. In Android Studio it is as simple as going through File -&gt; New -&gt; New Module</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NMMEwTa9nVgWyM8iSDajuQ.png" /></figure><p>This way all the modules are inside the same project and usually one of them (app modules) uses the others. The advantage of this approach is, it’s super easy to edit the modules and see the result instantly. However, the main reason we want to go with modularization is to have a smaller more manageable projects that are easier to maintain and this approach doesn’t provide that since all the files are still in the same project. Not only that, we can not reuse these modules in other projects. So I don’t recommend doing it this way and this is not what I mean by modular development!</p><h4>Developing modules as separate libraries</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*r8qs0RgdInqSf1gPhMJf8Q.jpeg" /></figure><p>We can create our modules separately in their own project, upload them on a server and then use them like any other library by adding a dependency to build.gradle file. There are many online services to host your libraries publicly or privately. The most famous one is <a href="http://bintray.com/">Bintray.com</a>. You can also host your own server if you want.</p><p>I like this approach a lot better since our project is split in multiple smaller more manageable projects <strong>and</strong> we can developed a module (library) once, and use it in as many projects as we want and hence radically reduce the development time.</p><p>This approach has a big disadvantage though! Each time we want to make a change in the module we have to do the change the code, test it, build the new version of the library and publish it on the server and update the build.gradle on the main module to see the effect. This is really time consuming and frustrating. So unless the library is your main project I don’t recommend this either.</p><h4>Using gradle composite build</h4><p>Gradle composite build is my favourite feature of gradle witch bring the best of both worlds together! And this is what I recommend for doing modular development. The way it works is very simple. While developing, composite build uses your local modules letting you make changes as you would in a single module project and see the result instantly (like solution one) while the modules are developed separately in their own projects and can be developed, published and used separately. (like solution two)</p><p>Configuring composite build is simple as well. You just have to edit settings.gradle file and let the gradle know which libraries you want to replace with local versions while developing. Here is an example from one of my projects.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a2cf4a39343a59d5d8ee9019655f232/href">https://medium.com/media/1a2cf4a39343a59d5d8ee9019655f232/href</a></iframe><p>It code simply says if the project directory on ../..Android-Lib/ProgressButton exists use it instead of the dependency com.alirezaahmadi:progress-button defined in build.gradle file which is the library published on jCenter.</p><p>Now any change to the module directory is instantly available to our project. Also since Android Studio has full support for composite build, it will show all of them in one place and makes editing even easier.</p><h4>Should we always use modular development?</h4><p>Modular development requires a bit of extra work, especially at the beginning of a project. So if your project is very simple or you don’t want to share code between the projects you might want to stick with a single module project.</p><p>However, I believe many projects can benefit from a more modular approach, something that is missing in the Android community.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=500ac816825" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/modular-way-of-building-android-apps-500ac816825">Modular way of building Android apps</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin functions, An alternative to interfaces?]]></title>
            <link>https://proandroiddev.com/kotlin-functions-an-alternative-to-interfaces-7cfb3c435900?source=rss-107f86e82c4c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7cfb3c435900</guid>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[A. A.]]></dc:creator>
            <pubDate>Fri, 22 Mar 2019 07:04:00 GMT</pubDate>
            <atom:updated>2020-02-18T18:07:30.911Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i4oiwlEovrcnO7RESSjJ0Q.png" /></figure><p>So we all know interfaces and we make use them a lot when we want to implement a callback. So the code below is familiar to us.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6e76fd303d067fd691cf2b0309c134f/href">https://medium.com/media/f6e76fd303d067fd691cf2b0309c134f/href</a></iframe><p>In the above code, we simply define an interface and let UserClass send an implementation of that interface to us. This is the way we used to do it in Java for years but now Kotlin is here, should we do the same in Kotlin?</p><h4>What’s different in Kotlin?</h4><p>In Kotlin functions are first-class which means that they can be stored in variables and data structures, passed as arguments to and returned from other <a href="https://kotlinlang.org/docs/reference/lambdas.html#higher-order-functions">higher-order functions</a>. A higher-order function is a function that takes functions as parameters, or returns a function. Read more in official doc here <a href="https://goo.gl/GqArF9">goo.gl/GqArF9</a></p><p>Look at the code below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/866a5aad83797b4cd10973b74f4ee371/href">https://medium.com/media/866a5aad83797b4cd10973b74f4ee371/href</a></iframe><p>This code does exactly the same thing as previous code but with only using functions. So instead of passing an implementation of interfaces, we pass functions themselves to the class. Also, note that by providing an empty method as default value with = {} we easily made the parameter optional.</p><h4>Directly using functions has the following advantages</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*6QExcZhgEjQicSp4rpVOfA.jpeg" /></figure><ul><li>Using functions is simpler and uses less boilerplate code which is always good.</li><li>A common problem using interfaces is that the client may not use all the callbacks but is still forced to implement them all and this goes against one of the <a href="https://en.wikipedia.org/wiki/SOLID">SOLID</a> principles.</li></ul><blockquote><a href="https://en.wikipedia.org/wiki/Interface_segregation_principle"><strong>Interface-segregation principle</strong><br></a>the <strong>interface-segregation principle</strong> (<strong>ISP</strong>) states that no client should be forced to depend on methods it does not use.</blockquote><p>Of course, there are workarounds to solve this like giving interface default implementation or split an interface to two or more interface but with functions, it is much easier, we can make all callbacks optional and let the client code (which may be developed in future by some guy we don’ t know) decide for itself witch one of our callbacks it wants to use and which one to ignore!</p><ul><li>Forwarding the callback is much easier. In an application sometimes the UserClass doesn’t need to do anything except forwarding the event to another class or another layer. Let’s say in our example we have another class called LogicHandlingClass that is responsible for doing the work. by using functions we can do something like this</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25f7bc2fac5c925cdd3f65d246f51fbb/href">https://medium.com/media/25f7bc2fac5c925cdd3f65d246f51fbb/href</a></iframe><p>If we wanted to use an interface we had two choices, implement the interface in UserClass and then call logicHandlingClass.doThis() I don’t like this because this is clearly boilerplate code! or we could let LogicHandlingClass to implement the interface, I don’t like this either because now our LogicHandlingClass has to know about the interface! ‌But in the code above, we don’t have any of these problems.</p><h4>What about the disadvantages?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*SkgJGFBUeA4sNdJzIsyeRQ.jpeg" /></figure><ul><li>Directly using functions instead of interfaces may result in functions and constructors with so many parameters and that may be confusing in some cases.</li><li>Using interface is a more explicit way of coding since an interface has a clear name and an identity.</li><li>Sometimes it&#39;s better to group functions that relate to each other and an interface is a perfect place for that.</li></ul><h4>Conclusion</h4><p>To conclude I want to emphasize that, this post is not meant to discourage you to use interfaces but rather provide an alternative and in my opinion perfectly valid solution you can consider while coding.</p><p>Also, don’t forget to <strong>Clap</strong> and share your ideas in the comment section below. <strong>Follow Me</strong> if you’re interested to read my other posts as well.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7cfb3c435900" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/kotlin-functions-an-alternative-to-interfaces-7cfb3c435900">Kotlin functions, An alternative to interfaces?</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MVVM — How View and ViewModel should communicate?]]></title>
            <link>https://medium.com/android-news/mvvm-how-view-and-viewmodel-should-communicate-8a386ce1bb42?source=rss-107f86e82c4c------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a386ce1bb42</guid>
            <category><![CDATA[jetpack]]></category>
            <category><![CDATA[mvvm]]></category>
            <category><![CDATA[software-design]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[A. A.]]></dc:creator>
            <pubDate>Tue, 19 Mar 2019 10:05:52 GMT</pubDate>
            <atom:updated>2020-01-19T18:34:14.778Z</atom:updated>
            <content:encoded><![CDATA[<h3>MVVM — How View and ViewModel should communicate?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2hBSQxEdBZvlIoGOC92JUw.jpeg" /></figure><p>So you choose MVVM (Model-View-ViewModel) for your app’s architecture, Brawo, a good choice! In MVVM we separate view from view model from the model. Generally speaking, separation is a good thing that’s why we have the principle of “Separation of concerns” that is a fundamental principle to write good software.</p><blockquote><a href="https://simplicable.com/new/separation-of-concerns"><strong>Separation of concerns</strong></a>, or <strong>SoC</strong>, is a principle of software design that code be separated into layers and components that each have distinct functionality with as little overlap as possible.</blockquote><p>Let’s define View and View-Model responsibilities in MVVM architecture, as this definition will guild us to design our code.</p><p><strong>View</strong>: should only be responsible for how the app looks like! What is the color and size of the texts? and things like that.</p><p><strong>View-Model:</strong> should only be responsible for preparing data for the View and defining how the view react and function when different events happen, data comes from the server, the user clicks on something, etc.</p><p>So the most important thing is to design our code in a way that these definitions are satisfied and we don’t mistakenly put codes that relate to View in View-Model and vice versa. To do that I personally follow these rules.</p><h3>Rules for clear View-ViewModel communication</h3><p><strong>Rule Number One:</strong> View should not have any logic in it, not at all! Not even a simple if condition. All logic for the view happens in ViewModel.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cda17b5380f3d9a3d19b983b343d2f4/href">https://medium.com/media/0cda17b5380f3d9a3d19b983b343d2f4/href</a></iframe><p><strong>Rule Number Two: </strong>In response to events view does nothing except notifying view-model by calling a method. View does not pass any view related classes to view model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a83a3f1a87a7acbdf56fccf29c415794/href">https://medium.com/media/a83a3f1a87a7acbdf56fccf29c415794/href</a></iframe><p><strong>Rule Number Three: </strong>ViewModel uses live data as the main way to communicate to view!</p><p>One of the benefits of MVVM is that ViewModel doesn’t have to know anything about the View and has no reference to View classes! Instead, it uses the reactive programming paradigm, so View can still observer the data and be notified about the changes. I prefer LiveData over RxJava because it is lifecycle aware and therefore helps you to avoid many issues and bugs related to updating the view in the wrong time (ex. when the view is not created yet!) More about LiveData here <a href="https://goo.gl/DCCkob">https://goo.gl/DCCkob</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f126ef246fec9fe772a445747cdcf4f4/href">https://medium.com/media/f126ef246fec9fe772a445747cdcf4f4/href</a></iframe><p><strong>Rule Number Four:</strong> View can call view-model whenever it needs something. ViewModel can provide helper methods for the view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fd001df2369157a98a9e59883d87dc9/href">https://medium.com/media/1fd001df2369157a98a9e59883d87dc9/href</a></iframe><p>Note that whether ViewModel updates the calculated values in LiveData or do it in helper method It’s the ViewModel that handles the logic and that’s what’s matter!! Depending on our your case you can choose one of these two solutions to push logic into ViewModel.</p><h3>‌‌‌ Benefits</h3><p>Now that I explained the rules let’s see what&#39;s the benefit of applying them in a real application.</p><ul><li>We are using MVVM the right way and that matters the most!! In my opinion, If we are adapting to new architecture we should respect the rules so our app is similar to other MVVM apps and anybody who knows MVVM can easily maintain our code.</li><li>We pushed logics out of our views (activity/fragment) and therefore made View code simpler and easier to understand. (Fixed an old problem in android that activities/fragments contain most of the code in the app!). Our View code is also more consistent as well since all View does is setting precalculated values and forwarding events to ViewModel. Not only simpler and more consistent code means fewer bugs it also makes it easy to test the View with Espresso.</li><li>ViewModel is also easier to understand and easier to test. You can test your ViewModels with JUnit tests only.</li></ul><p>Also, don’t forget to <strong>Clap</strong> and share your ideas in the comment section below. <strong>Follow Me</strong> if you’re interested to read my other posts as well.</p><ul><li><a href="https://android.jlelse.eu/basics-of-dependency-injection-for-dummies-b844d642e174">Basics of Dependency Injection for dummies!</a></li><li><a href="https://android.jlelse.eu/make-activity-oncreate-method-cleaner-with-help-of-template-method-design-pattern-8789b46711cf">Make Activity onCreate method cleaner with help of Template Method Design Pattern</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a386ce1bb42" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/mvvm-how-view-and-viewmodel-should-communicate-8a386ce1bb42">MVVM — How View and ViewModel should communicate?</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Basics of Dependency Injection for dummies!]]></title>
            <link>https://medium.com/android-news/basics-of-dependency-injection-for-dummies-b844d642e174?source=rss-107f86e82c4c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b844d642e174</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <dc:creator><![CDATA[A. A.]]></dc:creator>
            <pubDate>Fri, 30 Nov 2018 10:04:22 GMT</pubDate>
            <atom:updated>2020-01-19T18:33:52.534Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m7f6IF7iuEzP3JZ4kSZ4_A.png" /></figure><p>In this post, I’m going to talk about DI (Dependency Injection) in a very basic way and explain exactly what is it and why we should use it. This post is aimed for those who have no idea what dependency injection is or they’re not sure why we should use it. So let’s get started.</p><h3>What is a dependency?</h3><p>Let’s start with an example, we have ClassA ClassB and ClassC like below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8f8692058dfbf4d86408db3746967fc/href">https://medium.com/media/b8f8692058dfbf4d86408db3746967fc/href</a></iframe><p>As you can see, ClassA has an instance of ClassB so we say ClassA is dependent on ClassB why? Because ClassA needs ClassB to do its job. We also can say ClassB is a dependency of ClassA</p><p>Before going further I want to clarify this is a very good thing! because in an application we don’t want a class to do all the jobs. we want to separate our logic into different classes each responsible only for one thing, then these classes can work together to deliver the job.</p><h3>How can we handle dependencies?</h3><p>Let&#39;s consider the ways that we can handle dependencies, Generally, there are three paths we can take:</p><h4>First: Handle dependencies inside the dependent class:</h4><p>In a more simpler way, we can say, we can create objects whenever we need them. An example of that would be like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1774cccf6e0dcc46c13fa8e59069668/href">https://medium.com/media/a1774cccf6e0dcc46c13fa8e59069668/href</a></iframe><p>It is as simple as that! We create class whenever we need them.</p><p>Pros</p><ul><li>It’s very simple</li><li>The dependent class or ClassA has full control over how and when to create dependency.</li></ul><p>Cons</p><ul><li>ClassA and ClassB are tightly coupled. Meaning whenever I want to use ClassA I have to use ClassB as well, So <strong>Replacing ClassB with something else is impossible</strong></li><li>Any change to the way ClassB is initialized requires the change of code inside ClassA as well. (And all the other users of ClassB), So changing dependencies is hard!</li><li><strong>ClassA is untestable!</strong> If you care about testing and you should since it’s one of the most important aspects of software development, you need to unit test each class in isolation. Meaning when you want to see if ClassA does its job right by writing some unit tests you don’t care about ClassB or any other classes in your code, you only care about ClassA but as you can see in the example, whenever you create an instance of ClassA, ClassB is created as well. Now when you test a method it might fail but you don’t know if it was because ClassA has an error or ClassA is fine but some code inside ClassB made the test to fail! Basically unit testing in impossible because units (classes) cannot be isolated.</li><li>ClassA needs to know how to instantiate dependencies. In our example, it has to know how to create ClassC and use it to create ClassB. It’s better if it doesn’t know anything about this stuff! Why? because of the <strong>Single Responsibility Principle</strong></li></ul><blockquote>Each class should be responsible for one and only one thing!</blockquote><p>So we don’t want our classes to worry about anything other than their defined jobs, how to handle dependencies is an extra task we put on classes!</p><h4>Second: Let the user class handle the dependencies</h4><p>So by now, we found out handling dependencies inside a dependent class is not such a good idea, how about the dependent class defines all the dependencies it needs for example inside the constructor and let the user class provide them for it. Would it solve our problems? Let’s see.</p><p>Look at example code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da48a36801ccd6038faa7c537b55888a/href">https://medium.com/media/da48a36801ccd6038faa7c537b55888a/href</a></iframe><p>Now ClassA receives all the dependencies inside the constructor and it can simply call methods on ClassB without initializing anything!</p><p>Pros</p><ul><li>ClassA and ClassB are now loosely coupled and we can replace ClassB without breaking any code inside ClassA For example instead of passing ClassB we can now pass AssumeClassB that is a subclass of ClassB and our code will work without any issues!</li><li>ClassA now is testable! When writing a unit test we can create our own version of ClassB (mock object) and pass it to the ClassA now if some tests fail its definitely the fault in ClassA.</li><li>ClassA doesn’t have to worry or think about dependencies and can focus on its own job!</li></ul><p>Cons</p><ul><li>It’s like a chain and It should end somewhere! For example here the user of ClassA has to know all about the initialization of ClassB and to do that it has to know about initialization of ClassC as well and so on. So you see that any change to the constructor of any of this classes can result in a change in the caller class, not to mention ClassA may have more than one user, so the logic for creating objects is repeated.</li><li>Even though our dependencies are clear and easy to understand the user code is complicated and hard to manage! So it’s not simple any more! Also, it violates the Single Responsibility Principle as the user code is now both responsible for its job and handling dependencies for dependent classes as well!</li></ul><p>The second example is clearly better than the first one but It still has some downsides, can we do better? Before considering the third way let’s define Dependency Injection first.</p><h4>What is Dependency Injection?</h4><blockquote>Dependency injection is a way that we handle dependencies outside dependent class so depended class doesn’t have to worry about it anymore!</blockquote><p>As you can see, based on this definition, our first solution is clearly not using dependency injection, but our second solution is since the dependent class doesn’t do anything about providing dependencies, but we still consider the second solution a bad one, <strong>WHY?!</strong></p><p>Because dependency injection definition as <a href="https://en.wikipedia.org/wiki/Dependency_injection">described here</a> doesn’t say anything about where the dependencies should be handled (other than not inside the dependent class), It’s up to the developer to choose the right place for handling dependencies and as you can see in example 2, the user class is not the right place!</p><p>Can we do better? Let’s consider the third way we can handle dependencies:</p><h4>Third: Let someone else handle dependencies for us!</h4><p>In the first approach dependent classes are responsible for getting their own dependencies and in the second approach we moved handling dependency from the dependent class to the user class but what if there was someone else that could handle dependencies so neither dependent class nor the user class has to worry about it? This is the idea that allows the clean handling of dependencies in our app.</p><blockquote><strong>Clean implementation of Dependency Injection (my opinion)</strong></blockquote><blockquote>The responsibility of handling the dependencies are given to a third party so no other part of the app should be woried about it anymore.</blockquote><p>Dependency injection is not a technology, framework, library or anything like that, It’s just an idea! The idea of handling dependencies somewhere other than the dependent class (preferably in some dedicated part). It can be done without using any library or framework but usually, we use a framework for dependency injection because it makes it super easy to handle dependencies and avoids writing boilerplate code.</p><p>Every dependency injection framework has two essential part, It may provide so many other features as well but this two are the basics that they should have:</p><p>First, they give us a way to define fields (object) that are going to be injected, some frameworks do this by annotating the field or the constructor with @Inject annotation and some may do it differently. By Inject we simply mean that the dependency should be handled by the DI framework. The code will look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a00a08ba962356eeaefd074b7a490602/href">https://medium.com/media/a00a08ba962356eeaefd074b7a490602/href</a></iframe><p>Second, they allow us to define how each dependency should be provided and It will be done in a completely separate file (files). It may look something like this. (Again this is just an example, It may change based on your framework)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a25a2acb436962d0450fb33b61dc8c0d/href">https://medium.com/media/a25a2acb436962d0450fb33b61dc8c0d/href</a></iframe><p>So as you see each function is responsible for handling one dependency, so if we need ClassA some place in our app our dependency injection framework is going to create one instance of ClassC by calling provideClassC, passing it to provideClassB and get an instance of ClassB then pass that to provideClassA and creates the ClassA for us! It almost seems like a magic. So let’s see what are the pros and cons.</p><p>pros:</p><ul><li>Dead simple! both the dependent class and the part that provide the dependencies are clear and easy to understand.</li><li>Classes are loosely coupled and easily can be replaced with some another class. Let say we want to replace ClassC with AssumeClassC which is a subclass of ClassC. we simply change the provider code like this and all the places we use ClassC are now using the new version automatically!</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bed7f67275904de67813cc3ef7d647c/href">https://medium.com/media/8bed7f67275904de67813cc3ef7d647c/href</a></iframe><p>Note that we did not change any code inside our app only the provider method! It can not be simpler or more flexible than this!</p><ul><li>Highly testable! we can replace the dependencies provided by dependency injection with mock version while we’re testing and that’s very easy. In fact, dependency injection is your number one friend when it comes to testing.</li><li>It’s a better design because now we have a dedicated part in our app for handling the dependencies and as a result, other parts can focus on doing their own job and not worrying about the dependencies.</li></ul><p>Cons:</p><ul><li>DI frameworks have a learning curve, So you and all the other developers working on the project should spend time and understand it well before using it effectively.</li></ul><p><strong>Conclusion:</strong></p><ul><li>Handling dependencies without DI is possible but it brings downside to our app.</li><li>DI is just an idea! A powerful one, It simply says that we should handle dependencies outside the dependent class.</li><li>It’s always better to dedicate some part of your app to DI. Many frameworks encourage this as well.</li><li>Frameworks and libraries are not necessary for DI but they can help a lot, many of DI frameworks offer advanced features so DI can be down with the minimal amount of code!</li></ul><p>In this post, I tried to touch the basics so you know what Dependency injection is and why we should use it. There are plenty of resources you can read to learn more about how you can apply DI in your own app.</p><p><strong>Thanks</strong> to <a href="https://medium.com/u/95221cfe9ffc">Michael Spitsin</a>’s feedback, I was able to edit the original story and make it more accurate.</p><p>Also, Let me know what you think in the comment section below, <strong>Follow ME</strong> if you’re interested to read my other stories as well.</p><ul><li><a href="https://android.jlelse.eu/make-activity-oncreate-method-cleaner-with-help-of-template-method-design-pattern-8789b46711cf">Make Activity onCreate method cleaner with help of Template Method Design Pattern</a></li><li><a href="https://android.jlelse.eu/call-view-methods-when-testing-by-espresso-and-kotlin-in-android-781262f7348e">Call view methods when testing by Espresso and Kotlin in Android</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b844d642e174" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/basics-of-dependency-injection-for-dummies-b844d642e174">Basics of Dependency Injection for dummies!</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Call view methods when testing by Espresso and Kotlin in Android]]></title>
            <link>https://medium.com/android-news/call-view-methods-when-testing-by-espresso-and-kotlin-in-android-781262f7348e?source=rss-107f86e82c4c------2</link>
            <guid isPermaLink="false">https://medium.com/p/781262f7348e</guid>
            <category><![CDATA[android-testing]]></category>
            <category><![CDATA[espresso]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[A. A.]]></dc:creator>
            <pubDate>Tue, 09 Oct 2018 11:39:50 GMT</pubDate>
            <atom:updated>2020-01-19T18:33:06.822Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qEHlwf2tY5btjTTXoR1brg.png" /></figure><p>When you write UI test with espresso it&#39;s common when you need to call a method on a testing view, and it’s very easy, all you have to do is define a new View Action like below:</p><pre>onView(withId(R.id.myView))<br>        .perform(object: ViewAction {<br>            override fun getDescription(): String {<br>                return &quot;call doSomething() method&quot;<br>            }<br><br>            override fun getConstraints(): Matcher&lt;View&gt; {<br>                return isEnabled()<br>            }<br><br>            override fun perform(uiController: UiController?, view: View?) {<br>                (view as MyCustomView).doSomething()<br>            }<br>        })</pre><p>This method has two problems, first, it makes the code ugly! and second, you have to define this for every single method!</p><p>I’m here to tell you that there is a much better way that solves both these issues, the calling code would be like this and it works on any view even custom views.</p><pre>onView(withId(R.id.<em>myView</em>))<br>        .perform(callMethod&lt;MyCustomView&gt; <strong>{<br>            it</strong>.doSomething()<br>        <strong>}</strong>)</pre><p>To make this code work all you have to do is to define a generic function that returns a ViewAction like below:</p><pre>private fun &lt;T&gt;callMethod(message: String = &quot;&quot;, someMethod: (view: T) -&gt; Unit): ViewAction {<br>    return object: ViewAction {<br>        override fun getDescription(): String {<br>            return if(message.<em>isBlank</em>()) someMethod.toString() else message<br>        }<br><br>        override fun getConstraints(): Matcher&lt;View&gt; {<br>            return isEnabled()<br>        }<br><br>        override fun perform(uiController: UiController?, view: View?) {<br>            someMethod(view!! as T)<br>        }<br>    }<br>}</pre><p>This function uses generic types so it can use the type to cast the view so when you’re testing you don’t have to cast the view yourself, you simply call the methods on the view.</p><p>Second, it uses a cool feature of Kotlin that allows you to pass a function as a parameter to another function, and in perform section it simply calls that function, so you can pass any function to this method and it simply runs it for you!</p><p>Note: When this method fails for any reason the error message is not very intuitive by default, its something like this:</p><pre>Error performing ‘Function1&lt;mypackagename.component.MyCustomView, kotlin.Unit&gt;’ on view ‘with id: mypackagename:id/myView’.</pre><p>I don’t usually care about this because even without the error message it gives me enough data to debug the issue but if you want a better message you can use this ViewAction like this:</p><pre>onView(withId(R.id.<em>myView</em>))<br>        .perform(callMethod&lt;MyCustomView&gt;(&quot;call doSomething() method&quot;) <strong>{<br>            it</strong>.doSomething()<br>        <strong>}</strong>)</pre><p>That turns the error to be like this:</p><pre>Error performing call doSomething() method on view ‘with id: mypackagename:id/myView’.</pre><p>I hope this helps you write cleaner code for testing, Let me know what you think in the comments section below.</p><ul><li><a href="https://android.jlelse.eu/basics-of-dependency-injection-for-dummies-b844d642e174">Basics of Dependency Injection for dummies!</a></li><li><a href="https://android.jlelse.eu/make-activity-oncreate-method-cleaner-with-help-of-template-method-design-pattern-8789b46711cf">Make Activity onCreate method cleaner with help of Template Method Design Pattern</a></li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e491f%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F3e491f%2F&amp;image=http%3A%2F%2Fapi.screenshotlayer.com%2Fapi%2Fcapture%3Faccess_key%3Dfe59908dad3baab69ffab249a2224b03%26viewport%3D1024x612%26width%3D1000%26url%3Dhttps%253A%252F%252Fupscri.be%252F3e491f%253Fscreenshot&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href">https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=781262f7348e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/call-view-methods-when-testing-by-espresso-and-kotlin-in-android-781262f7348e">Call view methods when testing by Espresso and Kotlin in Android</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>