<?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 Jarosław Michalik on Medium]]></title>
        <description><![CDATA[Stories by Jarosław Michalik on Medium]]></description>
        <link>https://medium.com/@jarekmichalik?source=rss-b1a4c4027575------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CMqTBnDmptE2IyhGXXPO_g.jpeg</url>
            <title>Stories by Jarosław Michalik on Medium</title>
            <link>https://medium.com/@jarekmichalik?source=rss-b1a4c4027575------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 20:24:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jarekmichalik/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[Code reviews that don’t make people cry]]></title>
            <link>https://jarekmichalik.medium.com/code-reviews-that-dont-make-people-cry-b74170adce39?source=rss-b1a4c4027575------2</link>
            <guid isPermaLink="false">https://medium.com/p/b74170adce39</guid>
            <dc:creator><![CDATA[Jarosław Michalik]]></dc:creator>
            <pubDate>Sat, 30 Nov 2024 11:01:44 GMT</pubDate>
            <atom:updated>2024-11-30T11:01:44.961Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mk64ucwdpfCNgU8HcC9A9Q.jpeg" /></figure><p>Story time. Eight years ago, I was a junior dev, and my PR just got demolished. 50+ comments. Every single line questioned. I remember staring at my screen, blood boiling, thinking “This guy is just showing off. What a jerk.”</p><p><strong>I wanted to punch him in the face. (Sorry, Tomek, if you’re reading this!)</strong></p><p>Plot twist: Today, I’m that “jerk” leaving detailed comments on PRs. But here’s what I learned about making code reviews actually helpful instead of soul-crushing.</p><h3>comments that nade me want to QUIT</h3><p>Here’s what triggered me back then:<br>- “Why are you using a switch here? Make it a strategy pattern”<br>- “This should be private”<br>- “Read about SOLID principles”<br>- “???”</p><p>These comments weren’t technically wrong. <strong>But they made me feel stupid</strong>, incompetent, and defensive. Not exactly a learning mindset, right?</p><h3>reality check</h3><p>Years later, I realized something: My reviewer wasn’t trying to be mean. He was actually investing time to help me grow. But his communication style… well, it needed work.</p><p>And you know what’s funny? When I became a senior dev, <strong>I caught myself writing the exact same type of comments</strong>!️</p><h3>don’t take it personally</h3><p>Here’s the thing about code reviews: Your code is not you. We often get emotionally attached to our solutions because we spent hours crafting them. But that elegant architecture you designed? That clever optimization? <strong>It’s just code. It’s a tool to solve a problem.</strong></p><p>When someone suggests changes, they’re not attacking you. They’re helping you build a better tool. The moment I started seeing code reviews as collaborative tool-building instead of personal criticism, everything changed.</p><h3>what actually works</h3><p>After reviewing thousands of PRs (and making plenty of mistakes), here’s what I learned:</p><p><strong>1. Start with the good stuff:<br></strong> Instead of: “This is wrong”<br> Try: “The error handling here is solid. For the network call, what do you think about…”</p><p><strong>2. Ask questions instead of making statements:<br></strong> Instead of: “Use WorkManager here”<br> Try: “What made you choose AlarmManager over WorkManager?”</p><p><strong>3. Explain the ‘why’:<br></strong> Instead of: “Make this private”<br> Try: “Making this private would prevent accidental access from other modules, which could help us when we modularize later”</p><p><strong>4. Share your own failures:<br></strong> “I made this same mistake last year. Here’s what I learned…”</p><h3>quick tip 🚀</h3><p>Here’s the mental checklist I use before submitting review comments:</p><ol><li>Find at least two positive things first — always start with what works well</li><li>Focus on critical issues (crashes, memory leaks, security) — these are non-negotiable</li><li>Mark everything else explicitly as suggestions — use “Consider…” or “What do you think about…”</li><li>Add helpful resources or documentation links — make it easy to learn more</li><li>Re-read your comments before submitting — would they make you defensive?</li></ol><p>That reviewer who made me angry? We became good friends. Last year at Droidcon, I thanked him for those harsh reviews. They did make me better — even though his delivery could’ve been smoother.</p><p>Remember: Code reviews are about the code, not the person. The goal isn’t to show how smart you are — it’s to help the team ship better code.</p><p>What’s your code review horror story? <a href="https://androidpro.io/free-community">Join our AndroidPro community and share it — I bet you’re not alone!</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b74170adce39" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to refactor your code (without turning the project inside-out)]]></title>
            <link>https://jarekmichalik.medium.com/how-to-refactor-your-code-without-turning-the-project-inside-out-a652150007ba?source=rss-b1a4c4027575------2</link>
            <guid isPermaLink="false">https://medium.com/p/a652150007ba</guid>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Jarosław Michalik]]></dc:creator>
            <pubDate>Fri, 27 Oct 2023 10:40:27 GMT</pubDate>
            <atom:updated>2023-10-27T10:40:27.436Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>This blogposts was originally published on AndroidPro. <a href="https://androidpro.io/how-to-refactor-your-code-without-turning-the-project-inside-out/">https://androidpro.io/how-to-refactor-your-code-without-turning-the-project-inside-out/</a></blockquote><p>Are you stuck in a loop of having an old codebase that’s not testable, <strong>but not being able to refactor it because there’s no test coverage?</strong></p><p>This is a common challenge that many software development teams face, but there’s a way out.</p><p>In this article, we’ll explore a 5-step process to break the cycle and improve the quality of your codebase.</p><blockquote><em>Since I’m discussing a process to improve code quality and testability, I intentionally did not include actual code examples. My focus is on the process and methodology rather than specific programming languages or technologies.</em></blockquote><h3>Step 1: Identify the most critical code paths</h3><p><em>The first step is to identify the most critical code paths in your application. These are the parts of the code that are most important for the business logic and functionality of your application. Start by analyzing the use cases and user stories of your application to determine which code paths are most critical.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5yGu__9EM0YoFrTr" /></figure><p>When faced with an <strong>old codebase</strong> that’s not testable, the first step to improving its quality is to <strong>pinpoint the danger zones.</strong> This means exploring the codebase to identify the high-risk elements that are most critical to the business logic and functionality of the application.</p><p>Start by <strong>analyzing</strong> the codebase to understand its architecture and design. Look for <strong>patterns and anti-patterns </strong>that can make the code difficult to maintain or modify. Consider the dependencies between different modules and components, and identify any areas that are <strong>particularly complex or tightly coupled.</strong></p><p>Once you have a good understanding of the codebase, identify the most critical code paths that are most susceptible to bugs and errors. These are the parts of the codebase that have the <strong>greatest potential for introducing bugs during refactoring.</strong></p><p><strong>By targeting these high-risk elements first, you can ensure that the refactoring process is focused on improving the most critical parts of the codebase.</strong> This can help to reduce the risk of introducing new bugs and errors, and ensure that the application remains stable and functional after refactoring.</p><h3>Step 2: Characterization test</h3><p><em>After identifying the high-risk elements in the codebase, the next step is to describe the existing flow. This means writing tests to capture the current behavior of the code, without making any changes to the production code.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*02P2lZS_xQwgeXsQ" /></figure><p>Start by writing tests for the critical code paths identified in step 1. These tests should be designed to <strong>capture the existing behavior of the code</strong>, so that you can verify that the refactored code behaves in the same way. You may need to mock static or final dependencies to isolate the code under test and make it more testable.</p><p>Write <strong>as many tests as possible </strong>to ensure that you have good coverage of the critical code paths. This will help to ensure that you don’t introduce any <strong>regressions</strong> or new bugs during the refactoring process. It’s important to be thorough and capture all of the edge cases and potential failure scenarios.</p><p>This approach is called <strong>characterization testing</strong>. The goal of characterization testing is to capture the existing behavior of the code so that you can verify that the refactored code behaves in the same way. By using this approach, you can gain confidence that your refactored code is <strong>functionally equivalent to the original cod</strong>e and that you have not introduced any regressions or new bugs.</p><h3>Step 3: Baby steps</h3><p><em>After describing the existing flow of the codebase through characterization testing, it’s time to make small changes to the production code. This is where you can start refactoring the code to make it more maintainable, testable, and performant.</em></p><p>Start by making <strong>small changes</strong> to the codebase, such as <strong>renaming variables,</strong> <strong>extracting</strong> methods, simplifying code, and reducing complexity. Use the refactoring capabilities of your IDE to automate these changes and ensure that they are made consistently throughout the codebase.</p><p>As you make these changes, <strong>run the characterization tests</strong> to verify that the behavior of the code has not changed. This will help you catch any regressions or unintended consequences that may arise from the changes you’re making.</p><p>It’s important to make these changes in <strong>small increments</strong> and test each change thoroughly before moving on to the next one. This will help you catch any issues early and ensure that the refactoring process proceeds smoothly.</p><p>By making small changes and running tests at each step, you can <strong>gradually improve the quality</strong> and maintainability of your codebase without introducing new bugs or breaking existing functionality. This approach allows you to refactor with confidence and ensures that your application remains stable and functional throughout the process.</p><h3>Step 4: Take the power back</h3><p><em>After making small changes to the production code and running characterization tests, it’s time to take the power back by hiding implementation details behind abstractions. This means abstracting away any implementation details that are not necessary for the client to know, and providing interfaces or abstract classes that define the public API of your code.</em></p><p>Once you’ve identified the abstractions that you need, inject them into the target class using constructor injection or setter injection. This approach allows you to <strong>decouple your code and make it more testable</strong>, since you can now replace the implementation of the abstraction with a test double, such as a mock or stub.</p><p>Think about <strong>inversion of control</strong> when injecting dependencies into your code. This means inverting the control of the dependencies, so that the <strong>client class no longer creates or manages the dependencies directly</strong>, but instead relies on an external framework or container to manage the dependencies for it. This approach can make your code more modular, maintainable, and testable.</p><p>With implementation details hidden behind abstractions, you can now <strong>write proper unit tests for your code</strong>. These tests can be focused on testing the behavior of individual units of code in isolation from the rest of the system. This approach allows you to catch bugs and regressions early in the development process, and ensure that your code remains stable and functional throughout its lifecycle.</p><h3>Step 5: Repeat</h3><p><em>The final step in the process of breaking the loop of having an old, untestable codebase is to repeat the process. This means continuing to identify high-risk areas, writing characterization tests, refactoring the code, running the tests, and writing unit tests.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bPHgYKD-PmkX5q2K" /></figure><p>The <strong>first characterization test is often the hardest part</strong>, since it requires a good understanding of the existing behavior of the code. However, once you have a good characterization test in place, <strong>the process becomes much easier</strong>.</p><p>By repeating this process regularly, you can gradually improve the quality, maintainability, and testability of your codebase over time. This can help to reduce the risk of introducing new bugs or breaking existing functionality, and ensure that your code remains stable and functional throughout its lifecycle.</p><p>Remember to focus on <strong>making small, incremental changes</strong> to the codebase, and to test each change thoroughly before moving on to the next one. This approach allows you to catch any issues early and ensure that the refactoring process proceeds smoothly.</p><p>Breaking the loop of having an old, untestable codebase can be <strong>challenging, but it’s not impossible</strong>. By following the 5-step process outlined in this article, you can gradually improve the quality and testability of your codebase, and ensure that your application remains stable and functional throughout its lifecycle.</p><blockquote>This blogposts was originally published on AndroidPro. <a href="https://androidpro.io/how-to-refactor-your-code-without-turning-the-project-inside-out/">https://androidpro.io/how-to-refactor-your-code-without-turning-the-project-inside-out/</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a652150007ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Top articles on Kotlin Testing (2022)]]></title>
            <link>https://jarekmichalik.medium.com/top-articles-on-kotlin-testing-2022-d6a6e408a755?source=rss-b1a4c4027575------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6a6e408a755</guid>
            <dc:creator><![CDATA[Jarosław Michalik]]></dc:creator>
            <pubDate>Wed, 25 May 2022 08:40:03 GMT</pubDate>
            <atom:updated>2022-05-25T08:40:03.070Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oxY4R-l3U2uVKjpWogt6cQ.png" /></figure><p>Here is the top 5 articles on KotlinTesting in 2022.</p><h3>#1 Using Mockito in Kotlin projects</h3><p>Refactoring Java Mockito based test suite to Mockito-Kotlin DSL<br><a href="https://kotlintesting.com/using-mockito-in-kotlin-projects/">https://kotlintesting.com/using-mockito-in-kotlin-projects/</a></p><h3>#2 Don’t mock static: test SLF4J Loggers with appenders</h3><p>Do you want to assert that SLF4J actually logged warn with proper message? Check this guide on how to mock logger in tests, in Kotlin and Java.<br><a href="https://kotlintesting.com/mock-slf4j/">https://kotlintesting.com/mock-slf4j/</a></p><h3>#3 Mocking coroutines with MockK</h3><p>How to easily stub or mock suspend function in Kotlin? MockK is the solution. See MockK examples for mocking coroutines.<br><a href="https://kotlintesting.com/mocking-suspend-with-mockk/">https://kotlintesting.com/mocking-suspend-with-mockk/</a></p><h3>#4 How to mock final classes in Kotlin with Mockito?</h3><p>Mockito cannot mock/spy because your class is final class? We can fix that.<br><a href="https://kotlintesting.com/mock-final-in-kotlin/">https://kotlintesting.com/mock-final-in-kotlin/</a></p><h3>#5 Parameterized tests with Kotest</h3><p>Creating parameterized tests in Kotlin: data driven testing, collection inspections and generating test cases with FreeSpec.<br><a href="https://kotlintesting.com/kotest-parameterized/">https://kotlintesting.com/kotest-parameterized/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6a6e408a755" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JVM Testing Newsletter | June 2021]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/kotlintesting/jvm-testing-newsletter-june-2021-10778b130c30?source=rss-b1a4c4027575------2"><img src="https://cdn-images-1.medium.com/max/1080/1*1vs2W1f2laEZSxlFkptQTw.png" width="1080"></a></p><p class="medium-feed-snippet">What&#x2019;s new in JVM testing? Check June 2021 issue.</p><p class="medium-feed-link"><a href="https://medium.com/kotlintesting/jvm-testing-newsletter-june-2021-10778b130c30?source=rss-b1a4c4027575------2">Continue reading on KotlinTesting »</a></p></div>]]></description>
            <link>https://medium.com/kotlintesting/jvm-testing-newsletter-june-2021-10778b130c30?source=rss-b1a4c4027575------2</link>
            <guid isPermaLink="false">https://medium.com/p/10778b130c30</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Jarosław Michalik]]></dc:creator>
            <pubDate>Fri, 02 Jul 2021 08:00:00 GMT</pubDate>
            <atom:updated>2021-07-02T09:07:17.159Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[JVM testing newsletter | May 2021]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/kotlintesting/jvm-testing-newsletter-may-2021-596a1a4ecb9f?source=rss-b1a4c4027575------2"><img src="https://cdn-images-1.medium.com/max/1000/1*MbkUhp4QY_gMad638C-TDw.jpeg" width="1000"></a></p><p class="medium-feed-snippet">What&#x2019;s new in JVM testing? Check May 2021 issue.</p><p class="medium-feed-link"><a href="https://medium.com/kotlintesting/jvm-testing-newsletter-may-2021-596a1a4ecb9f?source=rss-b1a4c4027575------2">Continue reading on KotlinTesting »</a></p></div>]]></description>
            <link>https://medium.com/kotlintesting/jvm-testing-newsletter-may-2021-596a1a4ecb9f?source=rss-b1a4c4027575------2</link>
            <guid isPermaLink="false">https://medium.com/p/596a1a4ecb9f</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[newsletter]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Jarosław Michalik]]></dc:creator>
            <pubDate>Sun, 06 Jun 2021 14:14:49 GMT</pubDate>
            <atom:updated>2021-06-06T14:14:49.352Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[JVM Testing Newsletter | July 2021]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/kotlintesting/jvm-testing-newsletter-july-2021-29911dca4540?source=rss-b1a4c4027575------2"><img src="https://cdn-images-1.medium.com/max/1080/1*Z8sNhBqP6oq5JtVJXwNR8w.png" width="1080"></a></p><p class="medium-feed-snippet">What&#x2019;s new in JVM testing? Check July 2021 issue.</p><p class="medium-feed-link"><a href="https://medium.com/kotlintesting/jvm-testing-newsletter-july-2021-29911dca4540?source=rss-b1a4c4027575------2">Continue reading on KotlinTesting »</a></p></div>]]></description>
            <link>https://medium.com/kotlintesting/jvm-testing-newsletter-july-2021-29911dca4540?source=rss-b1a4c4027575------2</link>
            <guid isPermaLink="false">https://medium.com/p/29911dca4540</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Jarosław Michalik]]></dc:creator>
            <pubDate>Sun, 06 Jun 2021 09:07:03 GMT</pubDate>
            <atom:updated>2021-08-04T08:02:36.768Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Introducing Kotlin Testing — new publication about testing with Kotlin]]></title>
            <link>https://jarekmichalik.medium.com/introducing-kotlin-testing-new-publication-about-testing-with-kotlin-384054b12b0a?source=rss-b1a4c4027575------2</link>
            <guid isPermaLink="false">https://medium.com/p/384054b12b0a</guid>
            <dc:creator><![CDATA[Jarosław Michalik]]></dc:creator>
            <pubDate>Sun, 31 May 2020 11:37:44 GMT</pubDate>
            <atom:updated>2020-05-31T11:37:44.533Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v9tz3TrazLnvlDxrVLGkLQ.png" /></figure><p>I finally decided to implement one of my ideas — portal dedicated to testing with Kotlin.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*V4WZYaID9QxlpkbAeWLJBw.png" /><figcaption>.kotlinTesting logo</figcaption></figure><h4>I already migrated my Medium articles about unit testing to this site, and added brand new about using assertSoftly :</h4><p><a href="https://kotlintesting.com/assert-softly-when-one-assertion-is-not-enough/">Assert softly - when one assertion is not enough</a></p><p>Do you like idea behind KotlinTesting.com? Follow us on Twitter:</p><p><a href="https://twitter.com/kotlintesting">kotlintesting</a></p><p>And give feedback — what do you expect from KotlinTesting?</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjaroslawmichalikrozkmin.typeform.com%2Fto%2FHXLpwU%3Ftypeform-embed%3Doembed%26format%3Djson&amp;display_name=Typeform&amp;url=https%3A%2F%2Fjaroslawmichalikrozkmin.typeform.com%2Fto%2FHXLpwU&amp;image=https%3A%2F%2Fimages.typeform.com%2Fimages%2FFYUps4mFKPYK%2Fimage%2Fdefault&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=typeform" width="900" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/57688e6b0b911cab9a5fd6dd414e2e10/href">https://medium.com/media/57688e6b0b911cab9a5fd6dd414e2e10/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=384054b12b0a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ktor for fast server prototyping]]></title>
            <link>https://medium.com/kotlin-academy/ktor-for-fast-server-prototyping-6e7c6d2ec296?source=rss-b1a4c4027575------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e7c6d2ec296</guid>
            <category><![CDATA[servers]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[prototyping]]></category>
            <category><![CDATA[ktor]]></category>
            <dc:creator><![CDATA[Jarosław Michalik]]></dc:creator>
            <pubDate>Mon, 28 Oct 2019 06:42:23 GMT</pubDate>
            <atom:updated>2020-05-20T09:59:40.585Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OTQVzaeXTrtH2k3r" /><figcaption>Photo by <a href="https://unsplash.com/@seletcky?utm_source=medium&amp;utm_medium=referral">Mark Seletcky</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Ktor is a framework for creating server and client applications in Kotlin. As the creator says — its ultimate goal is to provide end to end solution for connected multiplatform apps.</p><p>In this article you will find basics of Ktor setup and implementation for simple server application.</p><h3>Installation</h3><p>For fast prototyping you often don’t have to create a new project — you may even add Ktor module as a Gradle subproject in an Android project. The most basic build.gradle with Ktor dependency will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2ff7435feba6fa52602c74edca98597/href">https://medium.com/media/b2ff7435feba6fa52602c74edca98597/href</a></iframe><p>You should also define application.conf file in src/resources, so the application plugin would know how to start your app:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41e0b96dfcdb1e1e443cf2e4228c13f9/href">https://medium.com/media/41e0b96dfcdb1e1e443cf2e4228c13f9/href</a></iframe><h3>Implementation</h3><h4>Up and running</h4><p>Let’s start with a simple endpoint that will return predefined value and Http status code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd7d2127a6d388c5a1c557b7a56615eb/href">https://medium.com/media/bd7d2127a6d388c5a1c557b7a56615eb/href</a></iframe><p>Let’s test this endpoint locally. Execute ./gradlew run and wait for an application to start. Then use curl or your favorite rest client and see what happens:</p><pre>$ curl -i localhost:8080/<br>HTTP/1.1 200 OK<br>Content-Length: 13<br>Content-Type: text/plain; charset=UTF-8</pre><pre>Test message</pre><p>We received a raw text response. In an application client side we don’t like text responses, we use JSON everywhere. How to return JSON from your API? Nothing easier. We will make the use of one of Ktor JSON features.</p><blockquote>Feature in KTOR is pluggable dependency — in most cases, it intercepts request and response and is able to do something with them. In application DSL we add the feature using install function.</blockquote><p>Add dependency:</p><pre>implementation &quot;io.ktor:ktor-gson:$ktor_version&quot;</pre><p>And install the feature in Application:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a537ec27f431a319ecd533fe678ce79b/href">https://medium.com/media/a537ec27f431a319ecd533fe678ce79b/href</a></iframe><p>As you can see, there is mapOf(&quot;status&quot; to &quot;Test message&quot;) added — Gson will parse that structure to readable Json.</p><p>Rebuild the project and test your endpoint again:</p><pre>$ curl -i localhost:8080/<br>HTTP/1.1 200 OK<br>Content-Length: 25<br>Content-Type: application/json; charset=UTF-8</pre><pre>{&quot;status&quot;:&quot;Test message&quot;}</pre><h4>Create, read, update, delete</h4><p>To work with CRUD functionalities we will use the following repository with in-memory data source implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c421ce134b3220e2e40ec575f949504/href">https://medium.com/media/8c421ce134b3220e2e40ec575f949504/href</a></iframe><h4>Create &amp; update — receive function</h4><p>We will create post route where we will <strong>receive</strong> a proper Item, add the Item to repository and <strong>respond</strong> with some <strong>result</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b4367c08b4f2dde560766789fb1645b/href">https://medium.com/media/0b4367c08b4f2dde560766789fb1645b/href</a></iframe><p>We are using io.ktor.reqeust.receive&lt;*&gt; function on our call — since we have added gson() in the previous steps, JSON item will be converted to Item data class.</p><pre>$ curl -i -XPOST -H &quot;Content-type: application/json&quot; <br>-d &#39;{<br>      &quot;id&quot;:&quot;3&quot;,<br>      &quot;message&quot;:&quot;First message&quot;<br>    }&#39;<br>&#39;localhost:8080/items&#39;</pre><p>What should return:</p><pre>HTTP/1.1 201 Created<br>Content-Length: 36<br>Content-Type: application/json; charset=UTF-8</pre><pre>{&quot;id&quot;:&quot;3&quot;,&quot;message&quot;:&quot;First message&quot;}</pre><h4>Delete — call parameters</h4><p>To create delete endpoint we will make use of <strong>call parameters </strong>property.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a59d9e0f88efc3b7df8f265705b85ba/href">https://medium.com/media/6a59d9e0f88efc3b7df8f265705b85ba/href</a></iframe><p>As you can see, we defined {id} in the path definition. Ktor is able to <strong>recognize and retrieve path parameter</strong> so we could use it later.</p><h4>Read — get</h4><p>Finally, let’s implement get endpoint which will retrieve a full list of items:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88447bb6ea6b52e9fcbe210a677c65b9/href">https://medium.com/media/88447bb6ea6b52e9fcbe210a677c65b9/href</a></iframe><p>And after executing GET on /items you should receive all items you’ve put into the repository:</p><pre>$ curl -i localhost:8080/items<br>HTTP/1.1 200 OK<br>Content-Length: 77<br>Content-Type: application/json; charset=UTF-8</pre><pre>[{&quot;id&quot;:&quot;3&quot;,&quot;message&quot;:&quot;First message&quot;},{&quot;id&quot;:&quot;23&quot;,&quot;message&quot;:&quot;Second message&quot;}]</pre><h3>Summary</h3><ul><li>✔ Kotlin first — there is no need to handle complex Java based APIs</li><li>️✔ Easy to start with — ready to go with one dependency</li><li>✔ Readability — what you see is what you get. Routing DSL is quite an elegant way to define endpoints</li><li>❌ Not as many integrations as Spring</li><li>❌ No golden standards yet</li></ul><p>Previously my first choice tool for API prototyping was express.js — now Ktor took its place. I find it super easy to use — almost every functionality works as I would expect it to behave. For bigger projects I would still use Spring — it’s much more mature and has a lot of integrations.</p><p>Those pros and cons are only my humble opinion. You should see yourself how Ktor can serve you on the backend and frontend side.</p><h3>Links and resources</h3><ul><li><a href="https://ktor.io/">Ktor - asynchronous Web framework for Kotlin</a></li><li><a href="https://github.com/rozkminiacz/ktor-playground">rozkminiacz/ktor-playground</a></li><li><a href="https://blog.kotlin-academy.com/how-to-create-a-rest-api-client-and-its-integration-tests-in-kotlin-multiplatform-d76c9a1be348">How to create a REST API client and its integration tests in Kotlin Multiplatform</a></li></ul><h3>Click the 👏 to say “thanks!” and help others find this article.</h3><p>To be up-to-date with great news on Kt. Academy, <a href="https://kotlin-academy.us17.list-manage.com/subscribe?u=5d3a48e1893758cb5be5c2919&amp;id=d2ba84960a">subscribe to the newsletter</a>, <a href="https://twitter.com/ktdotacademy">observe Twitter</a> and follow us on medium.</p><p>If you need a Kotlin workshop, check how we can help you: <a href="https://www.kt.academy/">kt.academy</a>.</p><figure><a href="https://kotlin-academy.us17.list-manage.com/subscribe?u=5d3a48e1893758cb5be5c2919&amp;id=d2ba84960a"><img alt="" src="https://cdn-images-1.medium.com/max/700/1*54OqlYA4etu7wfpmMP5TKQ.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e7c6d2ec296" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kotlin-academy/ktor-for-fast-server-prototyping-6e7c6d2ec296">Ktor for fast server prototyping</a> was originally published in <a href="https://medium.com/kotlin-academy">kt.academy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Unit Tests for ViewModel in TDD style]]></title>
            <link>https://proandroiddev.com/building-unit-tests-for-viewmodel-in-tdd-style-f8c502681ad4?source=rss-b1a4c4027575------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8c502681ad4</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[viewmodel]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Jarosław Michalik]]></dc:creator>
            <pubDate>Mon, 01 Jul 2019 07:31:01 GMT</pubDate>
            <atom:updated>2019-07-01T07:31:01.342Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zlrZFsjBxtzeEz-y" /><figcaption>Photo by <a href="https://unsplash.com/@the_milon_333?utm_source=medium&amp;utm_medium=referral">The Milon</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Your view model class usually has several functionalities. It somehow fetches data, it optionally maps your data entities to displayable entities, handles clicks and performs action for given events. You want to be sure that after refactor your code will still work perfectly. To achieve that you need to write and maintain unit tests. I will show you simple way of creating unit tests for Android ViewModel implementation with step-by-step explanation and Test-Driven-Development spirit.</p><h3>Prerequisites</h3><p>In his example we will try to implement VenueDetailsViewModel in TDD style. TDD (Test Driven Development) is development technique, where developer relies on <strong>unit tests created before actual implementation</strong>.</p><h4>Tools</h4><p>I’ll be using Mockito and Mockito-Kotlin for mocking:</p><p><a href="https://github.com/nhaarman/mockito-kotlin">GitHub - mockito/mockito-kotlin: Using Mockito with Kotlin</a></p><p>And Strikt for assertions:</p><p><a href="https://github.com/robfletcher/strikt">robfletcher/strikt</a></p><h4>Classes in package</h4><p>Let’s consider screen which displays some venue details and allows user to mark that place as favorite. We have following constructions for our view model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aef67570c23489865ee7a3497e1d6e4f/href">https://medium.com/media/aef67570c23489865ee7a3497e1d6e4f/href</a></iframe><p>Repository fetches data from remote API and emits value or error and FavoriteVenueController can check if venue is already marked by user as favorite and change its favorite state in local storage.</p><p>Note that <strong>every component is described by an interface</strong> — at a moment we don’t care about real implementations.</p><h3>Initial code</h3><p>Our initial code is class extending androidx.lifecycle.ViewModel with some fields that are ready to be bound into layout. We also have start() method that will be invoked just after ViewModel is available to be used and favoriteClick() to handle click on favorite icon on screen. We also have errorCallback — function which will be implemented in the higher class (e.x Activity) to display error message in Snackbar or Toast.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8fda5b76b43314669918e9c729a76dd/href">https://medium.com/media/d8fda5b76b43314669918e9c729a76dd/href</a></iframe><p>As you can see, I wrote createViewModel() factory method with default values:mock&lt;&gt; from Kotlin-Mockito extensions. It will be useful while writing single test cases, to <strong>not invoke full constructor each time.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cfba03aa396463134e127dddf30ddb37/href">https://medium.com/media/cfba03aa396463134e127dddf30ddb37/href</a></iframe><h4>First test case</h4><p>Let’s write our first test case. First of all, our ViewModel should display VenueDisplayable from repository. We will cover two basic cases — case when Venue is found and second when error is emitted.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/042748f79077fde019a6052ab42e405f/href">https://medium.com/media/042748f79077fde019a6052ab42e405f/href</a></iframe><p>Now it’s time to create assertion:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37ce50c6b740ca905c9cb78de18dc12c/href">https://medium.com/media/37ce50c6b740ca905c9cb78de18dc12c/href</a></iframe><p>When you hit run test then you should have two tests failing, because no implementation is present yet. So lets write simple start method in our view model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd7fa434659f0cc5f1f2dc217ab234dd/href">https://medium.com/media/dd7fa434659f0cc5f1f2dc217ab234dd/href</a></iframe><p>There are more ways to achieve that behavior — your unit tests should follow arrange-act-assert flow or given-when-then.</p><blockquote>Arrange — create objects, implement every stuff that is given.</blockquote><blockquote>Act — run your test method (in our case start()) — when.</blockquote><blockquote>Assert — check if desired change of state happened or verify if some mocked object was invoked with proper parameters — then.</blockquote><h4>More test cases!</h4><p>Now our tests will pass. Let’s create more test cases, for setting venue favorite status:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c66712a1ba8e448b668a4a2806263cce/href">https://medium.com/media/c66712a1ba8e448b668a4a2806263cce/href</a></iframe><p>If you run this test you will probably get NullPointerException on venueRepository.getOne(venueId). That’s because we didn’t provided any stub for that method call. We already created in our tests default implementations for all class dependencies, but Mockito won’t handle default return values for us. We will add specific default implementation for all cases in our factory method. For stubbing rx calls I usually make use of .never() method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b13e0b5bb600e07eadcd89a424a8fff/href">https://medium.com/media/1b13e0b5bb600e07eadcd89a424a8fff/href</a></iframe><p>Now our tests won’t throw NullPointerException. Instead there will be simple assertion fail. After that we can implement our functionality, and of course other test cases for checking if venue is favorite.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f267d46804652eababa15f6979eb4d4/href">https://medium.com/media/9f267d46804652eababa15f6979eb4d4/href</a></iframe><p>Of course we need to update our createViewModel factory method to return default value for venue controller too, so our previous test cases won’t throw NullPointerException.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0dd508b3e873374bf7700a2639bcbb5/href">https://medium.com/media/f0dd508b3e873374bf7700a2639bcbb5/href</a></iframe><p>Now we have one thing left — handling favoriteClick.</p><p>First, lets write tests in given-when-then manner:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3db0bc7bf008fe1f8efccee877a7746c/href">https://medium.com/media/3db0bc7bf008fe1f8efccee877a7746c/href</a></iframe><p>Please note — in first test case I could’ve mock on <strong>{ </strong>checkIsFavorite(FAKE_ID) <strong>} </strong><em>doReturn </em>Single.just(false) , but it wouldn’t be unit test anymore since it would be bound with other components. That’s not desired behavior all the time. In that way, when something in the class breaks, you will have clear information which singular piece of code failed.</p><p>Now when we wrote our test cases, we can proceed with implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/519d002244bd6e67f5b291732ee41978/href">https://medium.com/media/519d002244bd6e67f5b291732ee41978/href</a></iframe><p>Nothing too fancy — it passes test cases we wrote. Code prettifying can be done later.</p><h3>Final code</h3><p>We finally covered all major test cases, our tests are <strong>unit</strong> tests, they <strong>do not depend directly on implementation</strong> and are relatively fast. Let’s see how final code looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5efe5b065385586471553353107de102/href">https://medium.com/media/5efe5b065385586471553353107de102/href</a></iframe><p>And our implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e8802c675921d6c701b2d5372561f5e/href">https://medium.com/media/2e8802c675921d6c701b2d5372561f5e/href</a></iframe><p>We can do much more in here. We should inject somehow proper Schedulers, we can indicate progress loading in some way, we can optimize repository calls and so on. We should also remember about disposing every call in onCleared method from ViewModel.</p><p>With this TDD-like process we were able to write tests that do not depend strongly on direct implementation. In that way, when you would someday change RxJava calls to Kotlin Coroutines, your <strong>base test cases won’t change</strong>.</p><h3>Tips &amp; tricks:</h3><ul><li>try to think in given-when-then pattern — it will help you create cleaner code</li><li>test behavior, not internal implementation — in that particular view model we were asserting view model state (isFavorite, VenueDisplayable) and interaction (checking if errorCallback was invoked). <br>We shouldn’t make verification if venueRepository was called with proper parameters or check if favoriteVenueController was invoked. That would cause more harm than good.</li><li>make use of Kotlin default values — we created createViewModel() method with default implementations — thanks to that our test cases are smaller and more readable</li><li>keep one check per test method — in that way it you will immediately know which component of your code is failing.</li></ul><p>I believe you found something interesting in this article and you are ready to try something new while writing or refactoring features of your app. Good luck!</p><h3>Check my other publications about unit testing:</h3><h4>Kotlin testing libraries, frameworks and utilities with examples</h4><p><a href="https://github.com/rozkminiacz/KotlinUnitTesting">rozkminiacz/KotlinUnitTesting</a></p><h4>Testing with Kotlin (Mobilization Conference 2018 talk)</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F0PsVmLE3zss%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D0PsVmLE3zss&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F0PsVmLE3zss%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec3c55d6bf5409679f8f4193f546de75/href">https://medium.com/media/ec3c55d6bf5409679f8f4193f546de75/href</a></iframe><h4>Using Spek to write parameterized tests:</h4><p><a href="https://blog.kotlin-academy.com/parametrized-tests-with-spek-e0e02d5766a">Parametrized tests with Spek</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8c502681ad4" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/building-unit-tests-for-viewmodel-in-tdd-style-f8c502681ad4">Building Unit Tests for ViewModel in TDD style</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[Model-View-Intent & Data Binding]]></title>
            <link>https://proandroiddev.com/model-view-intent-data-binding-39c7a6a6512f?source=rss-b1a4c4027575------2</link>
            <guid isPermaLink="false">https://medium.com/p/39c7a6a6512f</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[data-binding]]></category>
            <dc:creator><![CDATA[Jarosław Michalik]]></dc:creator>
            <pubDate>Sat, 14 Jul 2018 08:21:01 GMT</pubDate>
            <atom:updated>2018-07-14T08:21:01.195Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pXUm0AHM0vK-Yk-f" /><figcaption>“Birds flying around a half-built building and construction site” by <a href="https://unsplash.com/@danist07?utm_source=medium&amp;utm_medium=referral">贝莉儿 NG</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Recently, thanks to joining the new project, I started using Model-View-Intent pattern. I always try to simplify development process and reduce boilerplate code, therefore I decided to dive deeper into MVI and discover new horizons. This approach is highly testable and has reactive nature — I consider that as big advantages.</p><p>Since I already heavily use data binding in my projects, I’d like to take advantage of MVI and have <strong>view state rendered in just two lines of code</strong>.</p><p>As usual, I will show several concepts on a simple example. In this case it will be login screen.</p><p>How does standard login screen looks like? It’s simple form where user inputs email, password, there is button which is clickable or not — we will change button status depending on input. We may additionally want to run some frontend-side validation for edit fields. I believe such case was described million times, however we will <strong>take a closer look at Model-View-Intent</strong> approach and try to <strong>reduce boilerplate with data binding</strong>.</p><p><em>In this article I will be using Mosby MVI library:</em></p><p><a href="https://github.com/sockeqwe/mosby">sockeqwe/mosby</a></p><h3>Show me the code!</h3><h4>Describe it with ViewState</h4><p>In MVI we describe ViewState by one data class. In this case it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fe8345d62014c35313825df0b30ee5f/href">https://medium.com/media/6fe8345d62014c35313825df0b30ee5f/href</a></iframe><p>As I used to do with MVP, I wrote contract interface for login screen — I like to keep basic relations in one place:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e9b7fe2c95ff50247cbdc180abd98f2/href">https://medium.com/media/0e9b7fe2c95ff50247cbdc180abd98f2/href</a></iframe><p>As you can see, I also defined Interactor (some developers would name it UseCase) — I decided to put it in contract for clarity, because it emits partial view changes, so it’s bound directly to the login flow.</p><p>Now, the biggest part of login screen implementation process will be creating new ViewStates.</p><h4>Partial view changes</h4><p>When you are prototyping screen with MVI, you should ask yourself — which user or backend changes affects this view state? When you start thinking like that, MVI will be your good friend during whole development process.</p><p>Additionally, when you create screen with data binding, you may want to keep your <strong>view state model as class with primitives</strong>.</p><p>PartialViewChange is a sealed class which is responsible for <strong>changing parts of view state</strong>. It takes processed change, such as email edit field content and creates new view state, based on previous state. Smooth syntax is possible due to Kotlin data classes copy() method with named arguments.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/745300654d58fbb319495c52b394e24b/href">https://medium.com/media/745300654d58fbb319495c52b394e24b/href</a></iframe><p>I also decided to put abstract reduce() in PartialChanges class. From my point of view, it’s this class responsibility to create new state. With this approach you can also <strong>avoid huge </strong>when<strong> statements</strong> in presenter.</p><p>Enabling or disabling submit button can be triggered by additional validation on email and password — however in this example simple checking if they are not empty should be enough.</p><p>Our presenter binds intents from view — it subscribes to edit text changes and button clicks.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/534e226444ab7d947e7878ba1ea32471/href">https://medium.com/media/534e226444ab7d947e7878ba1ea32471/href</a></iframe><p>Submit intent is mapped to business logic method, where API calls are performed. At the end of the method, I’m using scan operator on the merged observable.</p><blockquote>Scan operator takes the actual and previous item and applies function to them. It gives us possibility to create new state based on previous.</blockquote><p><a href="http://reactivex.io/documentation/operators/scan.html">ReactiveX - Scan operator</a></p><p>After that I’m reducing changes (with LoginPartialState method) to new view state.</p><h4>Finally — the view</h4><p>As we have logic for login screen prepared, we can implement our View:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a31467ba1f0ccfb34507715a13476794/href">https://medium.com/media/a31467ba1f0ccfb34507715a13476794/href</a></iframe><blockquote>You can use abstract MviActivity&lt;View, Presenter&gt; from Mosby library — it takes care of presenter flow.</blockquote><p>I’m mapping both email and password edit fields to specific partial states. RxBindings by Jake Wharton are very handy because of reactive nature of MVI. You can additionally use some kind of extension function for EditText changes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0a9227594b59611343fa02b0ef9931a/href">https://medium.com/media/c0a9227594b59611343fa02b0ef9931a/href</a></iframe><p>Let’s take a look at our layout file. It has two input fields, submit button, loading view and some container for error message. Now we put &lt;data&gt; into layout tags and try to set properties in xml:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14dba2f9d4b48cab93a028b6a49dc85e/href">https://medium.com/media/14dba2f9d4b48cab93a028b6a49dc85e/href</a></iframe><p>If button should be enabled, we set <em>android:enabled=true</em> on this button. If we should show progress bar, we toggle visibility on progress bar view. And finally, if we should display error — we set text to specific text container.</p><blockquote>If you want to use View.VISIBLE property with data binding, you must import it’s type as described in gist.</blockquote><h4>Back to ViewState</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fe8345d62014c35313825df0b30ee5f/href">https://medium.com/media/6fe8345d62014c35313825df0b30ee5f/href</a></iframe><p>Each property is immutable and has default value. What could be done better here? Sometimes we want our error message to be in Snackbar or custom dialog. We won’t achieve that in a simple way with data binding, we will have to implement it in View class.</p><p>My favorite part of this approach is toggling loading state. In MVP I was invoking showProgress() and hideProgress() methods very often. Here I just change loading property to true, and binding in xml does the rest.</p><p>Another thing that could be improved is observing edit texts state. I’d like to have some kind of custom binding adapter which will propagate changes to the presenter. But that would be more MVVM way, not MVI. Yet, RxBinding library and set of Kotlin extensions seems to do the job and doesn’t look horrible in the code.</p><h3>Conclusion</h3><p>I showed you my approach to designing screens. I know, that that’s not the smoothest, cleanest solution possible.</p><p>There are cases where MVI is the best tool to get things done and with data binding it will be even better. I don’t use MVI in every screen, and I don’t use data binding if it’s not suitable. <strong>Always choose the right tool for the job.</strong></p><p>I believe you found something interesting in this article, and you are ready to try something new while developing new feature to your app!</p><p>All examples used in this article can be found on Github:</p><p><a href="https://github.com/rozkminiacz/MviBinding">rozkminiacz/MviBinding</a></p><h4>Resources about MVI and Data Binding</h4><ul><li><a href="https://proandroiddev.com/the-contract-of-the-model-view-intent-architecture-777f95706c1e">The Contract of the Model-View-Intent Architecture</a></li><li><a href="https://android.jlelse.eu/from-mvp-to-mvi-model-view-intent-journey-part-i-8542f9fe7ef7">From MVP to MVI — Model-View-Intent Journey Part I</a></li><li><a href="https://blog.untitledkingdom.com/refactoring-recyclerview-adapter-to-data-binding-5631f239095f">Refactoring RecyclerView Adapter to Data Binding</a></li><li><a href="https://proandroiddev.com/mvp-architecture-with-kotlin-dagger-2-retrofit-rxandroid-and-databinding-17bffe27393d">MVP Architecture with Kotlin — Dagger 2, Retrofit, RxAndroid and DataBinding</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39c7a6a6512f" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/model-view-intent-data-binding-39c7a6a6512f">Model-View-Intent &amp; Data Binding</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>
    </channel>
</rss>