<?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 Ali Shobeyri on Medium]]></title>
        <description><![CDATA[Stories by Ali Shobeyri on Medium]]></description>
        <link>https://medium.com/@drflakelorenzgerman?source=rss-e101d5161daf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*C9Uie5GnKiUbhWxeQ045TA.png</url>
            <title>Stories by Ali Shobeyri on Medium</title>
            <link>https://medium.com/@drflakelorenzgerman?source=rss-e101d5161daf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 15:48:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@drflakelorenzgerman/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[KMM Experience]]></title>
            <link>https://medium.com/@drflakelorenzgerman/kmm-experience-7b8bfbb94ba4?source=rss-e101d5161daf------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b8bfbb94ba4</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kmm]]></category>
            <category><![CDATA[kmp]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Ali Shobeyri]]></dc:creator>
            <pubDate>Mon, 26 Aug 2024 08:39:12 GMT</pubDate>
            <atom:updated>2024-08-26T08:39:12.929Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tVpxByGjhJz8OGXoc0i-NQ.jpeg" /></figure><p>some months ago, my company made a contract to develop an application and since it was a small app we decided to use cross-platform and not native for it, because of my Android background we chose KMM for this project which is not that much new but it’s growing day after day; in this article I’m going to talk about some experience that we had in this project with KMM.</p><h3>KMM</h3><p>KMM stand for Kotlin Multiplatform Mobile (before that it was called KMP, Kotlin MultiPlatform), KMM is developed by Jetbrain and allows you to create one base code for different targets such as Android, iOS, Desktop and Web . Jetbrain and Google are really hype about KMM and I believe it will gain more fame during time to be used by companies (it’s already used in some companies, for example cash-app and Netflix use it).</p><p>KMM has some advantages over React-Native and Flutter and some disadvantages, I can list you some of the pros and cons from my point of view.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/0*oHRivK7J8AN-lKd_.gif" /></figure><h4>Pros</h4><ol><li>it’s Kotlin and Jetpack Compose !!!, if you’re an Android developer you don’t have to learn anything new, if you’re iOS developer you can learn Kotlin with a little effort since Swift and Kotlin are sister languages, Jetpack Compose in KMM is same as Jetpack Compose in Android world, even in navigation (which they added after our project) the concepts and codes are the same.</li><li>Performance; it’s not a secret that React-Native has one of the worst performance in the cross-platform world, Flutter and KMM performance are way better than React-Native.</li><li>KMM allows you to access rich UI components with Jetpack Compose and you can customise your UI, add some elements from Native Code to it and etc.</li><li>you can use your Kotlin code in your native iOS code with just adding some third-party libraries, the XCode can use AutoComplete feature and if your code is only shared in logic and not UI, iOS developers working on Native iOS UI can easily use those codes.</li><li>you can deiced if you want to a partial cross-platform app (only sharing logic codes) or full cross-platform applications (shared UI), also I would say even you can decide if you want to have native UI in some place and shared UI in other places.</li></ol><h4>Cons</h4><ol><li>It’s growing, so you might expect some changes in feature releases.</li><li>The community is not large like React-Native or Flutter but you should consider Kotlin and Android community too, if you face a problem in Jetpack compose or Kotlin code you can use Android community instead.</li><li>Lack of third party libraries.</li></ol><p>In our project we decided to not use the shared UI (because it was alpha in that time and it didn’t support navigation), but now KMM added navigation like the way we had in Android so you can easliy use powerful Jetpack Compose navigation (and maybe you can combine it with <a href="https://medium.com/daresay/ksp-and-compose-navigation-0c0fe457fde9">KSP</a>).</p><p>Since we didn’t use shared UI we had to make a way to use our Kotlin code inside Swift and for this matter we used <a href="https://skie.touchlab.co/">SKIE</a>, with SKIE you can have a mapping system of what you have in Kotlin inside your Swift code (for example Sealed classes are doesn’t existed so it will be converted to Enum in Swift).</p><p>here’s an example of how you can use Kotlin in your Swift code :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec019b20f4d9abd70e2a8cc9a765fbd4/href">https://medium.com/media/ec019b20f4d9abd70e2a8cc9a765fbd4/href</a></iframe><p>you can imagine item’s type is a sealed class in Kotlin (Eatable) and it’s converted to an Enum in Swift; also InjectionHelper component is our DI system which we can access by importing Shared :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4532ee0ac568b1b1f4dc823628d34662/href">https://medium.com/media/4532ee0ac568b1b1f4dc823628d34662/href</a></iframe><p>You can’t use Hilt or Dagger2 (because they are either Android Component base like Hilt or Java Annotation processing based like Dagger2), so the alternative is <a href="https://insert-koin.io/">Koin</a> which I like it myself and I used it in my Android samples before and then you can have some helper code for it in your iOS code; and for your network instead of Retrofit you can use Ktor.</p><p>there’s an actual-expect system in KMM, sometimes you want to have a code in your logic but the behaviour or response is different in Android or iOS so KMM have a good support for it :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cyxk56XXG1P8cEKV2TigiA.png" /><figcaption>you can access some platform information and component in each one, my example is simple</figcaption></figure><h3>Is it worth it to learn KMM?</h3><p>Well, I would say yes to this question, KMM is a trend right now and you must consider lots of companies are interested to have a cross-platform application for smaller and simpler apps, and KMM seems to be a good option here, it has Jetbrain behind it and you can be sure they will support it and add new features to it, also React-Native has not a good fame in cross-platform world so it’s a good idea to learn an alternative solution for creating cross-platform applications.</p><p>So, if you’re an Android developer, you must at least take a look to it, if you’re an iOS developer, you can simply learn Kotlin and with a little bit of effort you can manage to learn it.</p><p>if you had any experience in KMM I’m glad to hear it from you to see what’s your point of view if it :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/0*Gqc-Ar9iWDzayND7" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b8bfbb94ba4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[KSP and Compose Navigation]]></title>
            <link>https://medium.com/daresay/ksp-and-compose-navigation-0c0fe457fde9?source=rss-e101d5161daf------2</link>
            <guid isPermaLink="false">https://medium.com/p/0c0fe457fde9</guid>
            <category><![CDATA[annotation-processing]]></category>
            <category><![CDATA[ksp]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Ali Shobeyri]]></dc:creator>
            <pubDate>Mon, 17 Jun 2024 09:51:31 GMT</pubDate>
            <atom:updated>2024-07-01T09:09:20.001Z</atom:updated>
            <content:encoded><![CDATA[<p>One day I was sitting behind my desk and I was developing a system for compose navigation that I wanted to use inside my client project, I never liked the system we were using for compose navigation and always compared it to what we had in fragment navigation, an easy to use system with generated code for arguments and optional arguments and etc. then a lamp lighted up above my head that why can’t I generate code for it by myself ???</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/0*RXdHnunMk5oVq3wS.gif" /></figure><h3>Purpose</h3><p>what we’ll achieve in this paper is how we can have a system in Android that can generate code for later usage</p><blockquote>Notice: the Github Link for this article =&gt; <a href="https://github.com/sasssass/ksp_compose_navigation/tree/master">https://github.com/sasssass/ksp_compose_navigation/tree/master</a></blockquote><h3>KSP</h3><p>For this matter we have different tools, as an Android developer you probably have heard about kapt, kapt stands for Kotlin Annotation Processing Tool, with kapt you’re enabled to use the power of Java Annotation Processor in your Kotlin code, and via that you can generate code in the compile time, many different plugin such as Hilt, Room and etc use kapt to generated these code for us so we can use them inside our code.</p><p>So what is KSP here? KSP stands for Kotlin Symbol Processor, KSP is a newer tool over kapt that is designed to work without Java!</p><p>KSP has advantages over kapt, it has better performance on build time since it doesn’t require to interact with Java, so if you have a pure Kotlin project, you can choose KSP over kapt</p><p>But what is annotation processing? Annotation processing in Java and Kotlin is a feature that allows you to do some compile-time tasks (such as generating code)</p><p><strong>Annotation</strong> is a marker that you can apply in your class, functions and etc :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51c079287dc1591cf067b602ab574f3c/href">https://medium.com/media/51c079287dc1591cf067b602ab574f3c/href</a></iframe><p>When you have your annotation you can process it via the <strong>processor</strong>, then you can generate codes, XML files, and so on, also you can do some other tasks like validation of the code that has annotation and etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*h85jfUrcvRV9dLhxpa-zyg.png" /><figcaption>schema of what will happen in your gradle</figcaption></figure><h3>Combine it with Compose Navigation</h3><p>Now it’s the time for actual code, firstly you need these plugins and dependencies :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1389698c70ba8f7399ec76aafea6f16a/href">https://medium.com/media/1389698c70ba8f7399ec76aafea6f16a/href</a></iframe><p>Also, you need to create two Kotlin based modules (or Android depending on what you’re going to do)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*G2LT1s1jWnnWS2WghSJUPQ.png" /></figure><p>You must add kotlinpoet and symbol-processing and annotation in the <strong>processor</strong> module as dependencies and in your <strong>app</strong> you need to add annotation and processor :</p><p>processor gradle :</p><pre>plugins {<br>    alias(libs.plugins.ksp)<br>}<br><br>dependencies {<br>    implementation(project(&quot;:code_generator:annotation&quot;))<br>    implementation(libs.symbol.processing)<br>    implementation(libs.kotlinpoet)<br>}</pre><p>app gradle :</p><pre>plugins {<br>    alias(libs.plugins.ksp)<br>}<br><br>implementation(project(path = &quot;:code_generator:annotation&quot;))<br>ksp(project(path = &quot;:code_generator:processor&quot;))</pre><p>also don’t forget to apply KSP plugin in your project gradle :</p><pre>plugins {<br>    alias(libs.plugins.ksp) apply false<br>}</pre><p>I want to create an annotation that I can add over my composable function to create a graph, for this matter, I need three parameters :</p><p>I. Route II. Arguments III. Optional arguments</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03c16c7666097806d75650a8c4eb93c7/href">https://medium.com/media/03c16c7666097806d75650a8c4eb93c7/href</a></iframe><p>You’ll have this file in your annotation module, let’s deep into it, here I defined an annotation with three parameters as I said, I also defined a Target for my annotation, as you can see this annotation is used to apply functions (you can also have annotation for classes, properties and …)</p><p>Also, I defined retention, which is used for controlling the lifecycle of annotations and how they can be stored :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d11bbbe82df345f2369ade665566c346/href">https://medium.com/media/d11bbbe82df345f2369ade665566c346/href</a></iframe><p>Now we need to define our processor in our processor module, we’ll have two classes, NodeProcessorProvider and NodeProcessor, when KSP runs, the provider class will look for creating an instance of processor class when it’s needed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94a06ac9d5fa9441f1cf67d56ee1cd30/href">https://medium.com/media/94a06ac9d5fa9441f1cf67d56ee1cd30/href</a></iframe><p>In this example NodeProcessor has two inputs, a logger and a code generator, both of these inputs can be extracted from the environment, environment has the necessary contexts and utils that can fill the requirements of the processer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93aeafc4e44a61d4c1c387c753d77da6/href">https://medium.com/media/93aeafc4e44a61d4c1c387c753d77da6/href</a></iframe><p>If we go step by step on this code you can see first I searched for my symbols with specific annotation (NavigationNode), then I iterated all of the symbols I found and checked if they were declared in function (because NavigationNode is based on function as I defined it) and if they are valid or not, if these conditions are true we can extract annotation from symbols and then we can extract route, args, and optionalArgs from the annotation.</p><p>In the end, we’ll return all the symbols that can’t be processed.</p><p>You can also see I used logger, logger has other mods like warn and … that you can use depending on your needs.</p><p>Now we have all the parameters that we need and it’s time for generating code!</p><p>We need to create a new File in our code, you can add this function to NodeProcessor and call it from process (replace it with the commented part)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d495d61ec82fab8ea25bca6c82d11158/href">https://medium.com/media/d495d61ec82fab8ea25bca6c82d11158/href</a></iframe><p>First, we need a proper name for our generated code which is the concatenation of functionName (the name of the function that you add an annotation above it) and “GraphNode”, we also need packageName to save generated code, now it’s time to user kotlinpoet library which helps us to generate .kt files, FileSpec helps you to create the file you need to generate and TypeSpec helps you to add properties, functions and … to your generated file (also we have dependencies but for this example, this file doesn’t depend on other files), now you can use codeGenetrator (that you got it from environment already) and combine it with FileSpec to generate your code.</p><p>Let’s talk more about TypeSepc because, in this new file, we need some properties and functions for our compose navigation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6091c7c9e5ef18249817746e437c4bcb/href">https://medium.com/media/6091c7c9e5ef18249817746e437c4bcb/href</a></iframe><p>Here’s an example of how you can add a new property to your type, for this matter you need PropertySpec, you can add the name (“rawRoute”), the type of the property (String), and a modifier (in this case it’s Public).</p><p>Now let’s see an example for functions :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/023c6f2fc3941fe69d3a09311b075e0f/href">https://medium.com/media/023c6f2fc3941fe69d3a09311b075e0f/href</a></iframe><p>Here is a dummy function that has an input as String and output as String and some code, the comment below the function is our final output, you can also add comment (addComment) or other things in your FunSepc, I’ll suggest you to play around with it to see how powerful this library (kotlinpoet) is and how much flexibility it has for generting code .</p><p>When your FunSepc is ready you can simply add it to your typeBuilder :</p><pre>typeBuilder.addFunction(myFunction) // myFunction as FunSepc</pre><p>If you check out this <a href="https://github.com/sasssass/ksp_compose_navigation/blob/master/code_generator/processor/src/main/java/com/sass/processor/NodeProcessor.kt">link</a>, you can see the full implementation of processer to see how I created my own navigation class inside it, now it’s time for Rebuild the Project and you’ll have this beautiful generated file :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Om_JiyFhwI1RfxPlDNxv3g.png" /></figure><p>In my example I used the node like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03118989879deb4067c739672d603fba/href">https://medium.com/media/03118989879deb4067c739672d603fba/href</a></iframe><p>You can see generated objects like ScreenWithArgumentGraphNode and etc here, if you add NavigationNode annotation to any function it would generate this helper class for you to help you through navigation :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aada570e216843f6f38f03d507987147/href">https://medium.com/media/aada570e216843f6f38f03d507987147/href</a></iframe><p>ez pz lemon squeezy, now you have a powerful tools for compose navigation like the way you had it with fragment navigation !</p><p>Once again, check the <a href="https://github.com/sasssass/ksp_compose_navigation/tree/master">Github link</a> to see explore the way I generated my own helper classes, and then you can modify.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0c0fe457fde9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/daresay/ksp-and-compose-navigation-0c0fe457fde9">KSP and Compose Navigation</a> was originally published in <a href="https://medium.com/daresay">Daresay</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Programming Paradigms (Styles)]]></title>
            <link>https://medium.com/daresay/programming-paradigms-styles-581df31b31ad?source=rss-e101d5161daf------2</link>
            <guid isPermaLink="false">https://medium.com/p/581df31b31ad</guid>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Ali Shobeyri]]></dc:creator>
            <pubDate>Tue, 24 Oct 2023 08:54:18 GMT</pubDate>
            <atom:updated>2024-10-04T14:17:16.218Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*fkFExCx6A9HegRFkeHP8ZA.png" /></figure><p>Have you ever heard about some terms like “declarative programming”, “imperative programming”, “reactive programming” and etc.?</p><p><strong><em>even if you haven’t heard about them you would probably use some of them before when you were writing codes without knowing that you were using them!</em></strong></p><p>but what are they and why have you heard about them so much?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*QlhwNHwI3g3Jihl80lRQJQ.gif" /></figure><h3>What is Programming Paradigms</h3><p>Programming Paradigms are a series of principles, methodologies, and styles that specify structures and implementations of your software, also programming languages can be classified based on these paradigms.</p><p>we can classify these paradigms into two main groups :</p><ol><li>Imperative</li><li>Declarative</li></ol><h3>Imperative programming vs. Declarative programming</h3><p>According to the Cambridge Dictionary, we have these definitions for these two words :</p><p><a href="https://dictionary.cambridge.org/dictionary/english/imperative?q=imperatives">Imperative</a> (noun): the mood (= form) of a verb used for giving orders</p><p><a href="https://dictionary.cambridge.org/dictionary/english/declaration?q=Declaration">Declaration</a> (noun): an announcement, often one that is written and official</p><p>In Imperative Programming we care about the required steps to do a task and in Declarative Programming we care about the final result and not about the steps that are required to be done!</p><p>let’s take a look at these two codes (in Kotlin), I’m going to write a program that can calculate the sum of even numbers :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af188e6628abb4e8f1333ed93331c2d5/href">https://medium.com/media/af188e6628abb4e8f1333ed93331c2d5/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d74bf2de28a1cded2249713a45cbc1b/href">https://medium.com/media/8d74bf2de28a1cded2249713a45cbc1b/href</a></iframe><p>as you can see in the Imperative method I specify <strong><em>HOW</em> </strong>I want to reach my target but in the declarative way, I just declare to the system <strong><em>WHAT </em></strong>I want and use the built-in functions from Kotlin.</p><p>so by now, you must know that the whole imperative and declarative can be summarized in this shape :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oiVuu36SR4J_MLOXFBZkZA.png" /></figure><p>for instance, think about SQL, is SQL Imperative or Declarative? yes you’re right, it’s Declarative because in SQL you only say “I want these rows from table X which have Y and Z inside them, you don’t say <strong>HOW</strong> you want to reach your output but you only say <strong>WHAT</strong> you want</p><h3>Imperative Paradigms</h3><p>we have different types of Imperative paradigms but the most famous ones are :</p><ol><li>Procedural</li><li>Object-Oriented Programming (OOP)</li><li>Structured</li></ol><h4>Procedural Programming</h4><p>In procedural Programming, we have a collection of procedures (functions (we have another term “Functional Programming”, it’s not the same thing)) and each one has a task, in order to generate our output we might use some of them.</p><p>There are many programming languages that can be counted as Procedural like C, C++, Java and etc, but here I’ll make an example of Pascal for you, the reason is in Pascal we have function and we also have procedure!</p><blockquote>in Pascal, a procedure doesn’t have a return type and a function has, simple!</blockquote><p>let’s do the same program we did for calculating the sum of even numbers :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0262c98a5d1b97a7d39d396df4fe1b31/href">https://medium.com/media/0262c98a5d1b97a7d39d396df4fe1b31/href</a></iframe><p>you can see in procedural programming we defined a “procedure” called “CalculateSumOfEvenNumbers” and in order to get our desired output we must run this procedure (in this case we only have one)</p><p>procedural programming is really straightforward and I believe lots of code you’ve already written was in procedural style :)</p><h4>Structured Programming</h4><p>in Structured Programming we care about structured control flow, we have three concepts in Structured Programming :</p><p>Sequence: the code will be executed from top to bottom in a sequence that we write them (same as procedural actually)</p><p>Selection: it simply means if and else and switch-case, by if/else we can choose different flows to execute our code</p><p>Itraration: you already know them, loops! for, while, do while which prevents us from duplicating codes.</p><p>in a Structured way of programming, we try to divide the program into sub-systems (functions).</p><p>you already know lots of structured programming languages, C, C++, Java, Kotlin, and so on!</p><p>now you may ask what’s the difference between procedural and structured programming? they look exactly the same, well we can consider structured programming a subset of procedural programming with more features, in structured programming style we use modularisation and more advanced control flow.</p><p>let’s see another example, let’s write a program that can calculate factorial in both procedural and structured ways in Kotlin :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/baa4aa8301b070d25d9924fe537402dc/href">https://medium.com/media/baa4aa8301b070d25d9924fe537402dc/href</a></iframe><p>you can see in the first function we have a while loop that can calculate the amount of factorial, this function is counted as a procedure that we need to run to reach our output, but for the second function we have more control flow, if n is 0 or 1 then we don’t have to run a loop, also you can see we directly return the output and put “return” keyword after { so we can have a more structured way of coding.</p><p>I’d like to have this picture for procedural vs. structured, there’s no absolute procedural and no absolute structured, it’s more like a spectrum for me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*1uTh0mV9w97_MuQqhLeyLw.png" /></figure><h4>Object-Oriented Programming (OOP)</h4><p>in some languages (like C) we have structure and we have object, structure (struct in C) is a way to group related variables into one place, in some other languages (like C++) we have another term which is Object (class in C++ and many other languages), an object is a struct that also has functions</p><p>let’s say like this, variables in an object are specifications of a real-world entity and functions are behaviors of that model! you have a Bird, the bird has a type (sparrow, canary and etc) and it can move and fly (functions or behaviors); also this bird is a kind of Animal, so we can have another class called Animal which has a type and move (it doesn’t have fly).</p><p>Object-Oriented Programming is the implementation of real word entities in code and it’s based on four concepts :</p><p><strong>inheritance</strong>: a Bird is a kind of Animal, we can inherit some objects from other objects and borrow the specifications and behaviors of the parent class (with modification) and add extra specifications and behaviors</p><p><strong>polymorphism:</strong> having multiple forms for behaviors that can be divided into dynamic (run-time) and static (compile-time), for example, we can have two functions with the same name but with different inputs for an object (static) or we can override a function of a parent object in child object (dynamic)</p><p>the reason that dynamic is run-time is sometimes we can have a code like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee88cb1dd8fc3c9a4deb3c857347242a/href">https://medium.com/media/ee88cb1dd8fc3c9a4deb3c857347242a/href</a></iframe><p>here the obj is from Parent type but we build it in Child form, so in run-time when we reach the doSth function the system will decide if it must call doSth from Child or Parent (it will call it from Child in this example)</p><p>but in static one :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fbeaab5b08aa28f393196814ce158d3/href">https://medium.com/media/1fbeaab5b08aa28f393196814ce158d3/href</a></iframe><p>the system will understand if it must call doSth() or doSth(input: Int) in compile-time</p><p><strong>encapsulation: </strong>hiding data from other objects and restricting access to them only by setter and getter function :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ada0419a27bb292eb8b726e7772fcd1d/href">https://medium.com/media/ada0419a27bb292eb8b726e7772fcd1d/href</a></iframe><p>the reason we would like to have a setter or getter is shown in the example, for instance here we don’t want the number to be a negative value, also maybe when some objects try to access the number we also want to an additional job to be done(for example here printing a log)</p><p><strong>abstraction</strong>: it’s actually related to inheritance, we want to reduce complexity in objects, for example, think about that Bird class, we have another class called Animal which can be an abstract class and have some Initialization such as variables and functions (like move) and some abstract functions like eat (Bird will eat seed and Lion will eat meat) that must be implemented later in child objects but only defined in parent objects, we can not have an instance of this Animal class that is initialized with Animal constructor (we can have an Animal object which is initialized by Bird or Lion or …) because there’s no such thing as <strong>just</strong> an animal but every Bird and every Lion and … have some shared specifications that must be defined somewhere.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d255b37318ca3e7085a5eacd4d9230f/href">https://medium.com/media/3d255b37318ca3e7085a5eacd4d9230f/href</a></iframe><p>Like procedural programming, I believe you wrote <strong>LOTS</strong> of programs in this paradigm already :)</p><h3>Declarative programming</h3><p>we have different types of Declarative paradigms but the most famous ones are :</p><ol><li>Functional Programming</li><li>Reacting Programming</li><li>Logic Programming</li></ol><h4>Functional Programming</h4><p>In functional programming, we care about mathematical functions (like f(x) = y), we have some key elements in function programming :</p><p><strong>immutability</strong>: if you have a function and it has an input, that input must not be changed (so we must not use global variables)</p><p><strong>recursion</strong>: don’t use loops, in order to create a loop you must use a recursive function, if you think about immutability this makes sense, for example, to create a sum of even numbers you normally create a sum variable out of the loop and you try to <strong>CHANGE</strong> it which is forbidden here (you always can mix one programming paradigm with another, so when I said forbidden I meant if you want to write 100% functional but this may never happen and shouldn’t happen too, you choose your paradigm based on the question and application)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2f33466c36b332b8b640022ec733bbb/href">https://medium.com/media/f2f33466c36b332b8b640022ec733bbb/href</a></iframe><p>here is an example in Kotlin, in order to calculate the sum of even numbers we used a recursive function in a functional programming style.</p><blockquote><strong>NOTICE: YOU MUST BE CAREFUL WITH USING RECURSIVE FUNCTIONS</strong>, normally they take more memory and for some expensive calculations you might get a crash and etc, you always must be sure when you need functional programming and when you need a different paradigm rather than functional</blockquote><p><strong>Higher-order functions: </strong>you can pass a function inside another function as input, like f(y) = z + 2 and y or f(x) = x² in math</p><h4>Reacting Programming</h4><p>reactive programming is mainly about events and asynchronous data streams, every time there’s a new event there will be a new stream of data and as the name implies, we will react to these changes.</p><p>reacting programming is based on the observable design pattern, in this pattern, there’s an observable that provides (emits) that data stream for us and there’s an observer that can subscribe to this observable and handle the result of the stream.</p><p>everything is asynchronous, meaning we can have multiple observables and multiple observers and there’s no blocker between them.</p><p>mostly when we write a program in reacting style we use a powerful tool called “<a href="https://reactivex.io/">reactive extension</a>”, for example for Java developers we have RxJava and for C++ developers we have RxCpp.</p><p>Rx is a tool that can provide lots of extensions and different types of observables based on our needs and it’s a combination of reacting programming and functional programming, you can have multiple extensions for casting this flow of data or filtering them or combining them or etc.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8453718e9e66da309d3fcaba1cccf01c/href">https://medium.com/media/8453718e9e66da309d3fcaba1cccf01c/href</a></iframe><p>here we want to calculate the sum of even numbers (again :)) in reactive style, we have a list of numbers and we define an Observable based on them, we will subscribe to that observable so every time we have a new input then we have a new emission, we use functional programming here as well (filter and reduce) and at the end, we have our final sum.</p><h4>Logic Programming</h4><p>The logic programming paradigm is based on formal logic, what is formal logic you would say? well, we have formal and informal logic, in formal logic we use symbols and in informal logic, we use natural languages (like English, Russian, Spanish, and so on), in formal logic, we have syntax and clear rules to reach a true or false statement and in informal logic, we care about quality.</p><p>for example, if you ask me “Is the weather cold today?” and I want to answer it in informal logic, I can say “It’s not that cold” or “It’s very cold” but in formal logic, I only can say “yes (true)” or “no (false)”.</p><p>In logic programming paradigms we have a set of rules, facts, and patterns and we can reach our final output based on these rules.</p><p>the most famous programming language for logic programming is Prolog, let’s see an example in Prolog, I want to write a program that has a set of rules for people with their relation (father and mother) and a set of rules of gender (male or female) and by passing two names to it I want to see what’s the relation between that two persons :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a2810aa5cc005b86cfbb48d98d2d0b7/href">https://medium.com/media/0a2810aa5cc005b86cfbb48d98d2d0b7/href</a></iframe><p>this program is divided into two Facts and Rules :</p><p>Facts I :</p><ol><li>John is Mary’s Parent</li><li>John is Lisa’s Parent</li><li>and so one</li></ol><p>Facts II :</p><ol><li>John is Male</li><li>Alex is Male</li><li>Lisa is Male</li><li>and so one</li></ol><p>Rules :</p><ol><li>X is the father of Y if X is the parent of Y AND X is male</li><li>X is the mother of Y if X is the parent of Y AND X is female</li></ol><p>now if I run father(john, mary) the output is true and if I run mother(john, mary) the output is false</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*MVQAiqN5Zg5ZGdgLzdxDWQ.gif" /></figure><p>Any comment? =&gt; s.shb.s.ali@gmail.com</p><p>Also, you can find me on LinkedIn: <a href="https://www.linkedin.com/in/iryebohs/">https://www.linkedin.com/in/iryebohs/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=581df31b31ad" width="1" height="1" alt=""><hr><p><a href="https://medium.com/daresay/programming-paradigms-styles-581df31b31ad">Programming Paradigms (Styles)</a> was originally published in <a href="https://medium.com/daresay">Daresay</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep dive into my Clean MVVM Arch Project — Part II]]></title>
            <link>https://medium.com/daresay/deep-dive-into-my-clean-mvvm-arch-project-part-ii-7d73248d1c21?source=rss-e101d5161daf------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d73248d1c21</guid>
            <dc:creator><![CDATA[Ali Shobeyri]]></dc:creator>
            <pubDate>Thu, 01 Dec 2022 14:56:06 GMT</pubDate>
            <atom:updated>2023-01-20T16:12:42.159Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deep dive into Android Clean MVVM Architecture — Part II</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1zdjquVdt1dKpxDGDZQUAg.png" /></figure><blockquote>Notice: first read <a href="https://medium.com/@drflakelorenzgerman/deep-dive-into-my-clean-mvvm-arch-project-part-i-a5dca3ed7c02">Part I</a></blockquote><p>Ok, now it’s time for some coding 💻</p><p>Let’s first clone the project from <a href="https://github.com/sasssass/Clean-MVVM-Kotlin-Flow-TDD-With-Robolectric-">GitHub</a></p><p>I will show you the feature “search” and together we can see how can we achieve to create a powerful fully Clean Architecture app in Android 🤖</p><p>Here we have “core”, “features” and “app” modules. The features module have sub-modules that are the different parts of our app. For example, “search” is a feature that I developed earlier. Every feature has its own sub-module as well, “data”, “domain” and “UI”. Complex? Don’t worry, it’s like a piece of cake when you understand it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*rgFn_ONdoDNbmNdry6afoQ.png" /></figure><p>Let’s start from the bottom, the domain layer :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*eyJupXQETYiuSOMY_xxEiw.png" /></figure><p>On every domain module we have 3 packages :</p><h4><strong>Model</strong></h4><p>It is the same as “Entity” in clean architecture, we put our model data here and sometimes some utils functions and similar.</p><h4><strong>Repository</strong></h4><p>Here we keep all repositories related to the “search” feature. As you can see they are interfaces, not classes, why? Because we may want to create multiple implementations of them (for example if we want to create tests for the app we must create a fake repository based on this interface).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*i81edtnpHXGOahP2KNFZSg.png" /></figure><p>Here we define what we want (SearchModel) and what we should pass to get that model <em>(q : String)</em>.</p><p>You can see I created a wrapper class called “Domain” here :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7-xOVFvaOgdp4slzJqIb_A.png" /></figure><p>Domain is a sealed class representing states of requesting data, once we request data the app first returns a “Progress” state so then we can show a loading indicator. When the data is ready it will give us a “Result”, you can see “Result” has a model of <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/"><em>kotlin.Result</em></a> type (stupid naming, sorry, I should call it another thing) which can show us if the request was a failure or a success.</p><h4>Use-Case</h4><p>The last part is Use-Case.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X0O2nkyAiLWnZVxDVwRGOw.png" /></figure><p>Whenever we invoke our Use-Case, then the repository will return the proper data we want ✅</p><blockquote>Notice: if you don’t know about <a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/">Flow</a> (a combination of Flow and Coroutines behave like RxJava, this term is called <a href="https://en.wikipedia.org/wiki/Reactive_programming">Reactive programming</a>), read this link, you can find some examples on my <a href="https://github.com/sasssass/Kotlin-Flow-Examples">GitHub</a> too.</blockquote><p>Every use case is extended from “MotherUseCase” :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YSUzYHXQ7jyuo7x_HYGWCA.png" /></figure><p>Here we have three different Use-Cases, if we only have input or output we use the two bottom ones. If we have both input and output we use <em>MotherUseCase&lt;in Input, Output&gt;</em>.</p><p>That’s all about the domain layer. As you can see in the domain layer there are just some “definitions” and not real business logic. Here we just define what we want but we won’t tell how we want it!</p><blockquote>Notice: Every domain module should add baseDomain as a dependency in the Gradle.</blockquote><blockquote>Notice II: Domain layer is pure Kotlin or Java</blockquote><p>Let’s move to the data layer.</p><h4>Data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*HRfIjF45SQszfzkaxXurdA.png" /></figure><p>On every data layer, we have 4 packages. DI is for dependency injection and we won’t dive into that in this article. The other ones are:</p><ul><li>Datasource — This is a list of API services or database objects (DAOs) :</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*1ESl0-WNNjMtDusiJWs7tg.png" /></figure><ul><li>Model–Here is a list of our Network models. You might notice that we already have these on the “domain” layer, if we want to achieve clean architecture, we’d better separate network responses from our real models (you can ignore it too). The reason is that sometimes the network responses might be different from what we want to show to the user, so we have to cast them to the domain models.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*luTASsZJhY7RB6AKD9p4oA.png" /></figure><p>Every DTO (data transfer object) must implement the “cast” interface (which is an interface from baseDomain), so we have something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*KQUTUHcgJYaSEdM5NTUpwA.png" /></figure><p>Since here our domain model is not different from the data model so we didn’t change anything. But as I said before sometimes there is a need to change some elements.</p><p>And now the most important part of data layer, the repository:</p><p>In the repository we create our implementations of the repository interfaces in the domain layer :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*miGtq75Lj1JnulITzhLJfQ.png" /></figure><p>As you can see (and if you can recall I mentioned that earlier), we first emit or pass a “Progress” state, then with the help of the <em>safeApi</em> function (which is a helper function for catching errors and deciding if it is failure or success), we can emit a “Result” state to the next level of our app.</p><p>That’s all about the data layer.</p><blockquote>Notice: every data layer must add baseData and baseDomain as its dependency in the Gradle</blockquote><blockquote>Notice II: every data layer must add its own domain as its dependency in the Gradle</blockquote><p>Now it’s time for the UI layer.</p><h4>UI Layer</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*nSudeYujHfWybnELryf-dw.png" /></figure><p>Our application is “single activity”, so we shouldn’t add any Android Activity here (we only have one activity in the “App” module), but we must add our fragments and view-models.</p><p>Here is what is happening in a View-Model:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m_QGHvMYPvNcSgabG6Felw.png" /></figure><p>Here we can see what will be happened in the UI/View (fragment):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*kDHZ1i3IF6T_FxIZ2faI4w.png" /></figure><p>We simply collect data from the View-Model and react to it (in this case, filling an adapter for a recycler view).</p><p>That’s it! Now you must clone the project (or any other clean MVVM project) and explore it yourself 😎</p><p>Any comment? =&gt; s.shb.s.ali@gmail.com</p><p>Also, you can find me on LinkedIn: <a href="https://www.linkedin.com/in/iryebohs/">https://www.linkedin.com/in/iryebohs/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d73248d1c21" width="1" height="1" alt=""><hr><p><a href="https://medium.com/daresay/deep-dive-into-my-clean-mvvm-arch-project-part-ii-7d73248d1c21">Deep dive into my Clean MVVM Arch Project — Part II</a> was originally published in <a href="https://medium.com/daresay">Daresay</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep dive into my Clean MVVM Arch Project — Part I]]></title>
            <link>https://medium.com/daresay/deep-dive-into-my-clean-mvvm-arch-project-part-i-a5dca3ed7c02?source=rss-e101d5161daf------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5dca3ed7c02</guid>
            <dc:creator><![CDATA[Ali Shobeyri]]></dc:creator>
            <pubDate>Thu, 01 Dec 2022 14:55:52 GMT</pubDate>
            <atom:updated>2023-01-20T13:43:30.394Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deep dive into Android Clean MVVM Architecture — Part I</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E8tezNxjgA4wisV-uNgCQQ.png" /></figure><p>About eight months ago, I pushed a project based on <a href="https://github.com/sasssass/Clean-MVVM-Kotlin-Flow-TDD-With-Robolectric-">clean MVVM with Flow and TDD</a>, but I never created an article to explain what I did there (although I had created a short diagram on <a href="https://github.com/sasssass/Clean-MVVM-Kotlin-Flow-TDD-With-Robolectric-">GitHub</a>) 😃</p><p>So here I am with complete documentation about that project; now you can learn how we can create an Android project based on Clean MVVM Arch 🎉</p><p>In the first part, I will explain the different aspects of Clean Architecture and MVVM, and in <a href="https://medium.com/@drflakelorenzgerman/deep-dive-into-my-clean-mvvm-arch-project-part-ii-7d73248d1c21">Part II</a>, we will investigate one of the features of my project so you will read the theoretical issues and actual issues (code)!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*cDEaHt9RVJ2Lf0l-oi6lOA.jpeg" /><figcaption>We use Architecture to trace our codes and maintain them EZ PZ!</figcaption></figure><h3>What is Clean Architecture?</h3><p>When we are talking about Clean Arch, we simply mean separating your codes into individual modules with a specific task (we can say it’s related to the Single Responsibility Principle), but how can we achieve this?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*B7LkQDyDqLN3rRSrNYkETA.jpeg" /></figure><p>Here we have a diagram of <a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html">Uncle Bob’s article</a> about Clean Architecture; as we can see, we have four different parts in a Clean Architecture project, and each level only is aware of the subset of itself; for example, “Use-Case” knows about Entity and doesn’t know anything about “Interface-Adapter” layer, ok let’s see what each of these layers is:</p><h4>Entity</h4><p>An independent framework part of your application that contains some basic logic of your code; for example, if we are in the Android project, the entity would be our pure model data without any dependencies from Android and some interfaces of repositories, etc. (we’ll create the implementation of those repositories in another layer), it must (should) be only based on Kotlin or Java (or whatever language you want) or in a nutshell “the core business logic/model of your App” so it can be re-usable for any other framework.</p><h4>Use-Case</h4><p>In simple words, “Actions/Events of your App” if you have a computer Eng background, you can recall what was UML, right? (if you don’t, <a href="https://en.wikipedia.org/wiki/Unified_Modeling_Language">UML</a> is a bunch of Diagrams, I don’t want to make it hard 😄) on UML, we have something named “Use-Case,” and that’s the possibles actions of the user/system etc.</p><p>For instance, if we want to fetch a list of movies from the server, we have to create a Use-Case named “GetListOfMovieFromServer” use-cases don’t have to contain complex logic, and they are only a bridge from the Interface Adapters layer to the Framework layer.</p><h4>Interface Adapters</h4><p>Here we have all of our API and Repositories implementation and other things; here, we can trigger our Use-Cases and get data, then we can modify those data in a way we want and pass it to the Framework layer.</p><h4>Frameworks</h4><p>Well, it’s just Framework 🦾, like the Android SDK, Activities, Fragments, and other things, although we should keep our ViewModels in this section too.</p><p>And that’s it, now we with these four different layers, we can achieve to have a really good clean architecture, but how should we do this on Android?</p><p>Now look at this diagram :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wSnzktAlcQWSBmz13Bu6ZQ.png" /></figure><p>Here is how data will be shown on the Framework layer, first “Framework” layer triggers a “Use-Case,” then that “Use-Case” will call a proper repository (or whatever) from the “Interface Adapter” layer, then the “Interface Adapter” layer is going to create a network request, and it knows what will be received from the server according to the “Entity” layer. After that “Interface Adapter” will modify that data and pass it to the “Framework” through the “Use-Case.”</p><p>And that is how good clean architecture works 🙌</p><h3>Android Implementation Layers</h3><p>There are lots of different ways to implement Clean Arch in Android, but I believe the best practice is this :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3LZeUSRDC2INDwLj5KmAZQ.png" /></figure><p>In the Android world, every feature should be a separate module, and it must have its own architecture, either clean or whatever. This way, if in further future we want to use another architecture, we won’t face any problems (I worked in a company where we met this issue, and it was a real pain 😭).</p><p>So we will have a “core” modules, which contains some basic modules and some utils classes, then we can have an “app” module which doesn’t have many responsibilities and it’s only a host for your features (it has a small number of duties), and we have “feature” modules which contains different features.</p><p>On every feature module, we will have three different sub-modules :</p><ul><li>Domain: it contains the Entity layer and Use-case layer</li><li>Data: it includes the Interface Adapters layer</li><li>UI: it contains the Framework layer</li></ul><p>One thing is missing here; if we have a clean architecture, where is MVVM here? The point is; Actually, you can use another MV* architecture or any other architecture within your clean architecture, for example in this case, “MVVM” is only for your “UI” layer 📱</p><p>Let’s talk about MVVM, too (if you know that, pass that section).</p><h3>What is MVVM?</h3><p>MVVM stands for Model, View, View-Model; it’s an architecture for lots of frameworks; View is your activity and fragments and other things, Model is your data and some utils for data, and View-Model is a replacement of Presenter on the older architecture (MVP), on MVVM we create a “stream of data.” The View can observe that stream of data, and with any changes, View can react and show different information to the user:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fsq1bGx7n8fvFfRUI0hZjw.png" /></figure><p>As you can see in this picture, View-Model doesn’t have any information about View (that’s why I showed it as a dashed line), and it doesn’t know how View reacts to the user on the older architecture MVP; there was no “stream of data,” we simply handled things with “call-backs,” if we use call-back we will have an Object A, and an Interface B (which is a call-back) then Object A must have an instance of Interface B (a concrete class of that interface) and either Concrete class B must have an instance of object A too. This is crazy! It will create conflicts and tracing code would be really hard (there is a term in programming called de-coupling, we always try to remove dependencies). But in MVVM, there is no instance of View in View-Model; View simply observes (or collects or subscribes or other terms) a stream of data of View-Model, and View-Model won’t do anything about passing data to View; it just triggers the stream of data and notifies that it is changed 👋</p><p>And that’s enough for this part! Let’s go for <a href="https://medium.com/@drflakelorenzgerman/deep-dive-into-my-clean-mvvm-arch-project-part-ii-7d73248d1c21">Part II</a> and explore the feature “search” on the project.</p><p>Any comment? =&gt; s.shb.s.ali@gmail.com</p><p>Also, you can find me on LinkedIn: <a href="https://www.linkedin.com/in/iryebohs/">https://www.linkedin.com/in/iryebohs/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5dca3ed7c02" width="1" height="1" alt=""><hr><p><a href="https://medium.com/daresay/deep-dive-into-my-clean-mvvm-arch-project-part-i-a5dca3ed7c02">Deep dive into my Clean MVVM Arch Project — Part I</a> was originally published in <a href="https://medium.com/daresay">Daresay</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TDD — Part III, Hilt and Robolectric (Android)]]></title>
            <link>https://medium.com/@drflakelorenzgerman/tdd-part-iii-hilt-and-robolectric-android-dc941e3538f4?source=rss-e101d5161daf------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc941e3538f4</guid>
            <category><![CDATA[test]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tdd]]></category>
            <dc:creator><![CDATA[Ali Shobeyri]]></dc:creator>
            <pubDate>Sat, 26 Mar 2022 07:28:48 GMT</pubDate>
            <atom:updated>2022-03-26T07:54:17.135Z</atom:updated>
            <content:encoded><![CDATA[<h3>TDD — Part III, Hilt and Robolectric (Android)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w8h74Md7StKsigO02ufrmg.jpeg" /></figure><p>ok, one challenge left is how we should handle DI when we’re doing TDD?</p><p>Is that really a challenge or what? well when we’re testing our Fragment/Activity we may not have access to our data sources because they’re Retrofit and Room and …, we can have an on-memory room but what about web services?</p><p>as I know we can have different approaches to handle this issue :</p><ol><li>using Mock Web Service to simulate network connection and pass a proper retrofit</li><li>uninstall Reposotry or UseCase DI and Install a fake Repository to your fragment/activity</li></ol><p>I’d like to do the second approach, let’s imagine you have this design :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*A7sXBIQkRI-v9PtttnNI7w.jpeg" /></figure><p>for UI test we should not connect to real API and we have to fake one part of our code but which part is better? I think the best choice is Repository so we can have :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*xoYPcWZ43f1GRTBqGCMYnw.jpeg" /></figure><p>but where is the problem? the problem is you probably have used DI and in this case Hilt, so you set an injection for your UseCase to inject Repository and if you build your app on a proper architecture you must have an interface for your repository and pass that to UseCase instead of implantation of that repository (based on SOLID principle) and when you try to create a UI test the Hilt will inject your implementation to UseCase instead of FakeRepository, you have to uninstall that specific DI and create another DI but how?</p><p>let’s first check what libraries we need :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b43c73cd013d33bfbbf06c513fba999a/href">https://medium.com/media/b43c73cd013d33bfbbf06c513fba999a/href</a></iframe><p>ok let’s look at the code and I’ll explain the rest :</p><p>first, think about that we have a module for providing out use-case like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4141b095ce6126e94c1544e8b09fb461/href">https://medium.com/media/4141b095ce6126e94c1544e8b09fb461/href</a></iframe><p>our problem is when we are running our UI test we don’t have Internet and all objects should be fake or mocked or whatever! so we need a proper repository, for this case if you used clean-arch for your project you should have a SomeRepository class and SomeRepositoryImp class and we don’t want SomeRepositoryImp in our UI test, instead of that we need SomeRepositoryFake ! so somehow we need to override SomeUseCaseModule, in our test folder we can do such things like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89b0406f29ddda27d4b22be6cf3e698a/href">https://medium.com/media/89b0406f29ddda27d4b22be6cf3e698a/href</a></iframe><p>really simple ! on this code which exists on our test folder we replaced our SomeUseCase module with a new version of it (I did this but you can override your RepositoryModule instead of it too)</p><p>there are some other problems too, as you can see we are in the test, not the actual code, so we need to change our dependency graph, we need another activity and another application and we don’t want our actual activity or application</p><p>so at the very first step, we need another Activity, you can put this on your debug folder or test folder (I’ve seen most of the people create a debug folder beside the main and test folder and put this code there) :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba73b7af9612c55fcda72cee92175150/href">https://medium.com/media/ba73b7af9612c55fcda72cee92175150/href</a></iframe><p>and you need a manifest for this activity too, such thing as this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f578a7dcb2de326d29751f206079f11/href">https://medium.com/media/3f578a7dcb2de326d29751f206079f11/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*_CtFEqRH20B1fFi6NN6g3A.png" /></figure><p>now think about we want to test a Fragment, on the android-test library you can have something is named Scenario, you can run an Activity Scenario or Fragment Scenario but this approach won’t work with the hilt, we need to customize that, so we can have an extension like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce7835d1712739079d04c928d30c5193/href">https://medium.com/media/ce7835d1712739079d04c928d30c5193/href</a></iframe><p>this code will launch a Scenario of HiltAndroidActivityTest and attach a fragment to it, this is a common code that you can find on every article!</p><p>on other thing left and that is changing your real Application class with HiltTestApplication which is provided by the hilt library, let’s take a look at a UI test :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/384a77d4337bcc8d858917606bc94d03/href">https://medium.com/media/384a77d4337bcc8d858917606bc94d03/href</a></iframe><p>here I used Robolectric which is a library for UI testing, the advantage of this library is this: you can put your UI test on your test folder instead of androidTest so you won’t need a device or emulator anymore, as you can see I put some config like the SDK version and etc but the important thing is that application, I’ve passed HiltTestApplication type so the test will generate another dependency graph, you also need to add HiltAndroidRule as a Rule so Hilt can provide your dependencies on the test.</p><p>and now you can test your fragment and be happy :)</p><p>any problem =&gt; contact me : s.shb.s.ali@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc941e3538f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TDD — Part II, MVVM (Android)]]></title>
            <link>https://medium.com/@drflakelorenzgerman/tdd-part-ii-mvvm-android-eed78e2523dd?source=rss-e101d5161daf------2</link>
            <guid isPermaLink="false">https://medium.com/p/eed78e2523dd</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[tdd]]></category>
            <dc:creator><![CDATA[Ali Shobeyri]]></dc:creator>
            <pubDate>Tue, 11 Jan 2022 07:50:54 GMT</pubDate>
            <atom:updated>2022-03-07T19:35:16.999Z</atom:updated>
            <content:encoded><![CDATA[<h3>TDD — Part II, MVVM (Android) -Updated</h3><blockquote>ALERT : first read <a href="https://medium.com/@drflakelorenzgerman/tdd-part-i-theory-b7d18aefb50d">part I</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pkQWJ_CT-NpqwuVlNyZtnQ.jpeg" /></figure><blockquote>Update: I’ve updated some code in this article because some of them became deprecated :( the problem is coroutines because the testing area for that was Experimental and now it’s deprecated, another thing is I add a note in the middle of this paper about using Flow instead of LiveData</blockquote><p>All right, in this part we’re going to create an MVVM project with TDD, the main idea of this article is not just for Android developers because TDD is not specific to a framework but some tools that I’ll use just for Android developers (and some of them for Java’s developer).</p><p>There are two schools of thought about writing a test:</p><ol><li>Outside In</li><li>Inside Out</li></ol><p>assume we have class A and B and another class named AB which use both A and B and celebrate them</p><p>Outside In : you know about the combination of modules, so you know the design, if we want to write a test we have to first start with AB class, and then whenever you need A and B you start to writing test for them and create them</p><p>look at this diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*AhEYIzT7c2KbDzqd1gx80Q.png" /></figure><p>first, you will write an integration/feature/acceptance test(in this diagram it is named sociable test), that test will fail, so now you need to repeat a cycle of TDD to make that main test pass, the inner cycle would be some tests for A and B and the sociable test would be AB test</p><p>Inside Out: in this scenario, you start writing tests for A and B and then you go for AB, so you don’t have your design at the very first step, the design will appear at the end</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*gevBC3ZAh2rmOijL9UgzIQ.png" /></figure><p>You can see here we will write out unit tests for A and B and then we go for the writing acceptance test for AB</p><p>I want to use outside in for this article</p><p>so what is the acceptance/integration test in Android? there is another name for them which is instrumentation test, another name is UI test, UI is a place which you can see the result of the collaboration of other classes such as ViewModel, Repository and … so that would be our starting point.</p><p>we want to write a little project, getting a list from an API, the acceptance test for that is verifying a list has more than 0 item</p><p>LET’S CODE !!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/354/1*ugXqIzNuNPi2h1NfEbhiUQ.png" /></figure><p>when you create a project in Android studio you will see there are three folders for coding</p><p>Main: you know that, no need to explain :)</p><p>AndroidTest: here is a place for our UI test</p><p>Test: here is a place for tests that don’t have UI, all of our components like ViewModel, Repository, and … would be tested here</p><p>these are dependencies I’ve used for this project :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/289909c723b9a8a7df9dfcae2daab5bf/href">https://medium.com/media/289909c723b9a8a7df9dfcae2daab5bf/href</a></iframe><blockquote>ALERT : for adding library when you use testImplementation it means it’s for Test and if we use androidTestImplementation it means it’s for AndroidTest</blockquote><blockquote>ALERT : using architecture for your app have many advantages and one of them is you can put your logic code outside the UI so you can write normal tests for them, UI tests need an Android device/emulator but normal tests would not need that so the process of testing would be easier</blockquote><p>look at this test:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*hqTeK5sxwKnCQPv3drX3eA.png" /></figure><p>assume we have a button, whenever we click on that we get a text from our API and then we put that text into a TextView with the id of txt, besides that we have a variable which is named testString in our activity, we want to fill that variable with our text</p><p>ok here we have something to explain, first about RunWith, when we what to write an instrumentation test (UI test) we need a Runner for JUnit (tools we use for writing tests), and because we want to write UI tests we have to use AndroidJUntit4 which is a Runner for Android platform, and a Runner is just a class that has the responsibility to run our tests (you can write your own Runner)</p><p>after that we have Rule, we may add different Rules for our test which add some extra code around your test to get the ability to be executed, for example here we want to test our Activity so we use ActivityScenarioRule which allow us to access the member of the target Activity (in this case, MainActivity), as you can see we can access testString which is a public variable in our Activity.</p><p>then we use scenario and after that onActivity to access our activity,</p><p>this Rule belongs to “androidx.test.ext:junit”, a library that helps us to test our Activity.</p><p>for the name of your method always use a name that clarifies what is the target of that test</p><p>now you can see we have some errors, let’s first fix them by adding a button is named btn and an adapter (I ignore writing this part because it’s obvious)</p><p>about the onView() and …, these functions belong to espresso, a library which can access your XML file and we can perform click and other things by it.</p><p>ok but there’re still some problem and your test will be failed, because you don’t have your logic layer (ViewModel, Repo, and …), so let’s fix them, first you need an instance of ViewModel in your view so let’s create a test for that, our first method would be getListOfPosts() and it will be failed because we want to do the TDD cycle and make it passed :</p><blockquote>ALERT : you must put any UI test on AndroidTest folder and any Logic test on Test folder</blockquote><p>by the way for the API I’ve used: <a href="https://jsonplaceholder.typicode.com/posts">https://jsonplaceholder.typicode.com/posts</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*O_xEO6oopg8k6WcG6-Gq2g.png" /></figure><blockquote>you can use Flow instead of LiveData, but you may have a problem in collecting it, for that you can use this <a href="https://github.com/cashapp/turbine">library</a></blockquote><p>WOW, lots of errors, ok let’s fix them but before that let’s talk about some new stuff on this code :</p><p>InstantTaskExecutorRule : when you want to test a liveData or something like that you will get errors, to handle it we need this rule, as Google describes it =&gt; A JUnit Test Rule that swaps the background executor used by the Architecture Components with a different one which executes each task synchronously.</p><p>mock(): mock in general is an object with nothing inside it, it’s just a shape of the object, it doesn&#39;t have value or anything, it’s fake! on our TDD we can’t use our server response and instead, we should use mock objects, the mock() function will create a mock object for you</p><blockquote>ALERT : when you are testing class A, if A need B and C and … you must create a real object for ATest and the other objects have to be mocked, this is why viewModel is real object here and listOfPosts is mocked</blockquote><p>verify(): it’s a method that can verify a function is called or not, we give a repository to it and it will be checked if the repository is called one time (times(1) or not</p><p>getValueForTest(): we want to test our viewModel and we need the liveData inside the viewModel to be triggered, for that purpose we write this function so when we call getValueForTest we’ll get the response without observing it</p><p>ok that’s it, now we have to fix errors, at first we need a LiveData for listOfPosts :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*cG3bAZzehFJWdnI4po_O5w.png" /></figure><p>then we need a repository with a method which is named getListsOfPosts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/629/1*XeWI1zFSvu1oISq0UP1JEg.png" /></figure><p>now we get rid of all errors but our test will be failed again, we have to fix it, the reason is there’s no Repo instance at our ViewModel, and … you have to repeat this cycle a few times and also add a data source class (in this case, Api) and blaw blaw blaw, I don’t want to give you headache and make this article long so at the final stage we have these classes :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ae75839d8dea22d51d31999a555a838/href">https://medium.com/media/9ae75839d8dea22d51d31999a555a838/href</a></iframe><p>that is our ViewModel which is simple and no need to be explained</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6199e1cd7e50e351ca0089518e00375/href">https://medium.com/media/c6199e1cd7e50e351ca0089518e00375/href</a></iframe><p>that is our Test for ViewModel, I added some extra parts as you can see :</p><p>StandardTestDispatcher and TestScope: for using coroutines in testing we have to add a testing Dispatcher and then we can use runTest method from that</p><p>whenever : a really fantastic functions, when you have a mock object it’s fake, so there’s no real data, with this function you can simulate a response for a function of mock object</p><p>at the end we’ll check if the data is not or null, if it’s not then our test is passed (the same thing to verify, if repository.getListsOfPosts() don’t be called then verify make test failed too)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c57642a87114b07d895d78a86be75415/href">https://medium.com/media/c57642a87114b07d895d78a86be75415/href</a></iframe><p>we already had this function at MainActivityViewModelTest, I’ve decided to move it to Utils (it can happen on Refactor step of TDD life-cycle)</p><p>ok now ViewModel is finished, let’s see how our Repo should be :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/396613961874553c5828faf13b39ba58/href">https://medium.com/media/396613961874553c5828faf13b39ba58/href</a></iframe><p>a really minimal Repository, nothing to be explained</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be395369544bf61204b2f9c95d5f5736/href">https://medium.com/media/be395369544bf61204b2f9c95d5f5736/href</a></iframe><p>and it’s not RepoTest, there’s nothing new in this class, we’ve already covered it on ViewModelTest</p><p>and about Api :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/513f37f5cb6b1acf6c17db6740e1c6f7/href">https://medium.com/media/513f37f5cb6b1acf6c17db6740e1c6f7/href</a></iframe><p>and DONE ! all our logic codes have a test, ok now if you complete your UI and connect them to logic code your MainActivityTest should be fined too .</p><p>there’s still a problem, these kinds of tests I’ve covered are actually really minimal, for example, you can’t see any DI on my code, well, wait for the third part to see how to have both Hilt and TDD beside together.</p><p>Happy coding !!!</p><blockquote>Extra point : search about Before and After about JUnit, I’ll explain it on next article anyway and it’s not compliacted</blockquote><p>Any comment? =&gt; s.shb.s.ali@gmail.com</p><p>also, you can find me on LinkedIn: <a href="https://www.linkedin.com/in/iryebohs/">https://www.linkedin.com/in/iryebohs/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eed78e2523dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TDD — Part I, Theory]]></title>
            <link>https://medium.com/@drflakelorenzgerman/tdd-part-i-theory-b7d18aefb50d?source=rss-e101d5161daf------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7d18aefb50d</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[tdd]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Ali Shobeyri]]></dc:creator>
            <pubDate>Tue, 21 Dec 2021 05:55:28 GMT</pubDate>
            <atom:updated>2021-12-21T05:55:28.199Z</atom:updated>
            <content:encoded><![CDATA[<h3>TDD — Part I, Theory</h3><p>You’ve heard from lots of programmers about TDD and the importance of it, and to be honest, it is one of the important subjects for every programming field.</p><p>In this article, we are going to discuss the theoretical aspect of TDD and in the future, we will cover the practical part for Android developers (cause I am an Android developer).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fy9qc62nPZ2CKA%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fy9qc62nPZ2CKA&amp;image=https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2Fy9qc62nPZ2CKA%2F200.gif%3Fcid%3D790b76117d02a030dac780ff7c555cbcfe90749d2165adad%26rid%3D200.gif%26ct%3Dg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="256" frameborder="0" scrolling="no"><a href="https://medium.com/media/b25e57f4bd841d378fca1932c26933b7/href">https://medium.com/media/b25e57f4bd841d378fca1932c26933b7/href</a></iframe><p>TDD is standing for <strong>Test-driven development, </strong>according to <a href="https://en.wikipedia.org/wiki/Test-driven_development">Wikipedia</a> it means :</p><blockquote>software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against all test cases</blockquote><p>There’s a really important sentence in this paragraph <strong>before software is fully developed</strong>, TDD is a methodology which you will approach your implementation from the test, that means at the very first step you have to write your test, the test represents the validation of then expected functionality and then you make that validation to succeed.</p><p>Ok, we talked about the TDD but what is a Test? well, it’s just a block of code that validates another block of code, nothing really complicated :)</p><p>Now you may think about why we should use TDD? Let’s mention some advantages of TDD to you :</p><ol><li>Making developing faster: how we can achieve more speed while we are actually writing mode code? well, the answer is by writing a test you will reduce your debug time.</li><li>Maintenance: lots of time you will face a situation and that is when you develop a new feature and the old features start to be broken :( really painful right? well by writing tests you can be sure about the old features, if anything goes wrong you’ll be informed of that.</li><li>Documentation: this is really helpful, most of the time you write the name of the test with the understandable sentence which gives you a guide about the duty of that code, besides that the output of the code will show you the proper result, so if anybody outside of the project joins you, they’ll understand the functionality of a code by looking at the test related to that code</li></ol><p>We have 3 types of tests:</p><ol><li>Unit test: it’s just a simple block of code that tests another block of code, think about you have a class like Calculator with two operators plus and minus, now we will have two unit tests for approving their functionality</li><li>Integration test: think about you have class A and class B, these two classes are working together and became a module with each other, an integration test is a test which will test the relation between components</li><li>End to End test: testing a flow from beginning to the end, because it’s going to test a flow so it will be expensive to implement.</li></ol><p>(there are more types than these three but it’s not related to our discussion)</p><p>Let’s see a pyramid provided by Google :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-a2S9vTw8tKSNvTqn3hMYQ.png" /></figure><p>as you can see our most tests should be on Unit category, then integration and at the end, it should be E2E, and it makes sense, because you have plenty of functions to test (Unit) and you have a couple of Module/Features (Integration) and you have some flow (E2E).</p><p>and now what qualification should a test have?</p><ol><li>Fast: the test must run as fast as possible</li><li>Short: it’s just a test, not an implementation so it should be short as possible</li><li>Independent: a test must not depend on another test</li><li>Repeatable</li><li>Timing: It’s important that you write the test before the actual implementation</li></ol><p>Ok now let’s see a really simple unit test example and then we’ll continue this article on Part II</p><p>Before we write a test we must know the proper flow of writing a test which is TDD lifecycle:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/1*IceTUse2J6PUc90G1VpokQ.png" /></figure><p>When you want to write a code first you must write the related test, on the RED phase you write a test that is going to be failed (because there is no code to test), then you make that test passed by writing the actual code of your feature or … and that is GREEN phase, now you have your test and your production code so there is just only one step remains which is BLUE and that is refactoring, on this phase we can refactor both test and actual code (and sometimes there is nothing to refactor)</p><p>I want to do this example in Kotlin, when we are using Kotlin/Java we can write our tests by a framework called JUnit, you write your test code in the test folder and the implementation on main (or whatever it is depending on your platform/framework or etc)</p><p>consider we want to write a calculator with a simple plus and minus, firstly we must write our test, we want to test the plus function so we start:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*S2gxBQWEX4ird-zMWfIChA.png" /></figure><p>as you can see our test will face compile error and it’s good, now we know what is the minimum of code we need to pass this test, now we create a Calculator class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RCblRr4pHBChC0UQ79_-FA.png" /></figure><p>one part is fixed now let’s write our add function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*EwZ-uNg9GQSpWC85P9vesw.png" /></figure><p>now you can run your test by clicking the arrow on the left of your test function and then when we run our test:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*qVxksItAoebH9RpBj70cgg.png" /></figure><p>We’ll continue TDD in part II, wait for it …</p><p>Any comment? =&gt; s.shb.s.ali@gmail.com</p><p>also, you can find me on LinkedIn: <a href="https://www.linkedin.com/in/iryebohs/">https://www.linkedin.com/in/iryebohs/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7d18aefb50d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Documentation with KDoc for Kotlin/Android]]></title>
            <link>https://medium.com/@drflakelorenzgerman/documentation-with-kdoc-for-kotlin-android-a93c99dfe74?source=rss-e101d5161daf------2</link>
            <guid isPermaLink="false">https://medium.com/p/a93c99dfe74</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[documentation]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Ali Shobeyri]]></dc:creator>
            <pubDate>Thu, 02 Sep 2021 17:12:53 GMT</pubDate>
            <atom:updated>2021-09-02T17:13:10.227Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MxY5W89EgcQ38F7oKoC7MQ.png" /></figure><p>Think about you’re creating a library and you want to publish it on Git, or even you’re writing your casual code on some project, you need to write good documentation for other people too. what happens if someone wants to use your library or work beside you on an old project?</p><p>Here is KDoc, a language for writing documents for Kotlin code !!!</p><p>Similar to JavaDoc you the main format for KDoc is :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48d900bcbeb08f07bd8433822d6cfec8/href">https://medium.com/media/48d900bcbeb08f07bd8433822d6cfec8/href</a></iframe><p>starting with /** and end with */.</p><p>In KDoc we can use tags, a tag formula is @tagname+ space +name (or sometimes just @tagname ), here is an example :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a7dab70e3a5ca534985865d3f8a7719/href">https://medium.com/media/4a7dab70e3a5ca534985865d3f8a7719/href</a></iframe><p>On the code above you can see :</p><ol><li>first I write a summary about the foo function</li><li>adding @param input so other programmers can understand what is input</li></ol><p>Let’s see some famous tags :</p><p>@param =&gt; you can document a parameter of a function or generic type of a class or … :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/297f8cb2debea1b9b9dd47a9c348710a/href">https://medium.com/media/297f8cb2debea1b9b9dd47a9c348710a/href</a></iframe><p>as you can see we can use T or [T] (when you use [T] you can don’t use space after @param.</p><p>@propertity =&gt; document properties of constructor</p><p>@return =&gt; document what is the output of a function</p><p>@constructor =&gt; document what is the primary constructor</p><p>@throw + class =&gt; document what kind of exception will throw when something goes wrong</p><p>@see =&gt; add a link or method as a reference</p><p><a href="http://twitter.com/sample">@sample</a> =&gt; show an example of code</p><p>you can find more tags on <a href="https://kotlinlang.org/docs/kotlin-doc.html#property-name">Kotlin </a>website.</p><p>here is a good example :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/58cf8d00612482936e0de88644167978/href">https://medium.com/media/58cf8d00612482936e0de88644167978/href</a></iframe><p>It’s nice, right? remember the documentation should not be so long, just a summary of what is happening in this class would be enough.</p><p>Let’s move forward to the next step, using Dokka!</p><p>Dokka is a documentation engine for Kotlin, It can create better visual documentation for us.</p><p>Look at this picture:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W__2P2p2-WK1aTXEXnBn9w.png" /><figcaption>Amazing!!! right?</figcaption></figure><p>Ok let’s see how we should create this, first we need to add these to Gradle:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/1*CVa6pbEraL1dLjQOm_G0qQ.jpeg" /><figcaption>classpath(<strong>“org.jetbrains.dokka:dokka-gradle-plugin:1.4.30”</strong>)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*0hGXZj-8NhLhcu5CpJFzNA.png" /><figcaption>id <strong>‘org.jetbrains.dokka’</strong></figcaption></figure><p>after this enter this on terminal: gradlew.bat dokkaHtml (I believe ./gradlew for Linux/Mac) and after building you can find that on app\build\dokka.</p><p>Instead of dokkaHtml you can try dokkaGfm and dokkaJavadoc.</p><p>contact me: <a href="/service/mailto:shobeyri.ali@gmail.com">shobeyri.ali@gmail.com</a></p><p>my Linkedin : <a href="https://www.linkedin.com/in/iryebohs/">https://www.linkedin.com/in/iryebohs/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a93c99dfe74" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dagger2 in Android (Part 2)]]></title>
            <link>https://medium.com/@drflakelorenzgerman/dagger2-in-android-part-2-f7b1d43e880d?source=rss-e101d5161daf------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7b1d43e880d</guid>
            <category><![CDATA[android-programming]]></category>
            <category><![CDATA[android-studio]]></category>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[dagger-2]]></category>
            <dc:creator><![CDATA[Ali Shobeyri]]></dc:creator>
            <pubDate>Thu, 23 Apr 2020 21:16:55 GMT</pubDate>
            <atom:updated>2020-04-23T21:17:18.463Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*9mG-SQpV6j4rrOV_OpZaLA.png" /><figcaption>ِDagger2 in Android (Part 2)</figcaption></figure><p>now you should know what is DI, Dagger2, component, module, sub-component, if you don’t you better read <a href="https://medium.com/@drflakelorenzgerman/dagger2-in-android-part-1-df5b136953a6">Part 1</a></p><p>in this article, I’m going to explain about fragment and MVVM</p><h3>Fragment + Dagger2</h3><p>we already learn how to build a sub-component via dagger, our sub-component is our Activity, nowadays apps have single activity and we change pages with fragments, as you can guess our fragments could be a sub-component of our activity, something like this :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*4eWusyfV5EjfYRzPjtyjTg.png" /></figure><p>to creating a fragment sub-component we do the same thing we did in activity sub-component :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*m0A_vK_sSYxox0fd2YmNyg.png" /></figure><p>and for Fragment1 we’re going to extend it from DaggerFragment :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SXEKVtQXdVuaZekj2TvEcw.png" /></figure><p>other things are similar to activity, if you want to have a module for this sub-component (or sub-sub-component) you can do whatever you did in your activity, one thing you have to do is link this sub-component to its father (activity), for doing this you go back to your ActivityBuilderModule and change it to this :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/737/1*L1-sPAo15HgLFTKXsUg5HA.png" /></figure><p>now we have the hierarchy we need.</p><h3>@Named</h3><p>sometimes you need have two or more <a href="http://twitter.com/Provider">@Provider</a> with the same return type, for this, you have to create a <a href="http://twitter.com/Named">@Named</a> like this :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*GOhdQNMXeNJWN4ItTzZglg.png" /></figure><p>and then in your code :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*JAugqiYtZ7xPWvuTEEOGig.png" /></figure><h3>ViewModel</h3><p>if you’re familiar with MVVM you know we have a ViewModel class, some funny thing here is android provide us ViewModel and it provides us Dagger2 (both components are developed by Google) but they can’t work with each other !! that’s mean you can’t inject a ViewModel !!!!</p><blockquote>if you didn’t this part IT DOES NOT MATTER, because it’s a common solution and all the thing you have to do is copy/paste this into your project</blockquote><p>what we are going to do is :</p><ol><li>create a custom provider class (ViewModelProviderFactory) which has a Map of ViewModel and Provider (not Dagger’s provider, ViewModel Provider)</li><li>create a ViewModelModue and it will inject ViewModel for ViewModelProviderFactory</li></ol><p>first of all, you need to have a ViewModel :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*XjvxZPU-ZK0_rPdTJxbsGw.png" /></figure><p>what is @Inject <strong>constructor</strong>() ? it’s called construction injection, when you put this phrase you don’t need have a @Provider to provider it, whenever it needed it provider easily, you can add some item in the constructor too but you should beware these items have @Provider method in proper modules or the have @Inject <strong>constructor</strong>() too, okay now for our ViewModelFactoryModule :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mlmj-4GIREZwUCz941GdLA.png" /></figure><p>what is <a href="http://twitter.com/Bind">@Bind</a>? it’s <a href="http://twitter.com/Provider">@Provider</a> but when you just want to return a class without extra code you can use <a href="http://twitter.com/Bind">@Bind</a> instead of <a href="http://twitter.com/Provider">@Provider</a>.</p><p>this class would inject ViewModelProviderFactory as a ViewModelProvider.Factory. you have to add ViewModelProvider.Factory into your AppComponent so entire app can access it :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*oVEfzrN48-R-JRzQbnjfMg.png" /></figure><p>now we need to create ViewModelModule :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*jsGTTrcxwi2IpX86j39Y2g.png" /></figure><p>this class <a href="http://twitter.com/ViewModelKey">@ViewModelKey</a> and it will inject our ViewModel, how? I explained before, in ViewModelProviderFactory we have a Map&lt;Key, Value&gt; and ViewModelKey is the key and ViewModel is Value, you have to add this class to your subComponent :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*6kRtkN6R2mmDJV2fr5UgFg.png" /></figure><p>now @ViewModelKey :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*4Dl5OQ9ygN4w4n9Fe63ckg.png" /></figure><p>why we use a key? lots of time you have more that one subcomponent in your app and you should group viewModels, here we have just one but if you separate your sub-component to more than one part the ViewModelProviderFactory need to know this group and inject proper provider to ViewModelModule (and if you have more that one part you will have more than one ViewModelModule, for example, if you separate your login part from other parts then you will have LoginViewModelModule and OtherViewModelModule and you will need two key: LoginViewModelKey and OtherViewModelKey, this is not necessarily because the whole purpose is just injecting ViewModel not separating parts)</p><p>and last but not least, what is this ViewModelProviderFactory thing?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ijpPrY6bXNEV0geegTn5dQ.png" /></figure><p>this class will provide a Provider in this line :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/931/1*Y-Hdj6ihhVk5TkY2ZwADjA.png" /></figure><p>and check the injected viewModel is defined in the ViewModelModule or not, if there is no problem it will inject the Provider into your class (Activity, Fragment, or …)</p><p>and at the end :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*Q7l_kvnOZ3oZ_TNtCjFGrA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*2yYU2MMNW2ANLjvVPJdnqA.png" /></figure><p>I create a simple project of these content (branch: step2) :</p><p><a href="https://gitlab.com/drflakelorenzgerman/daggersample/-/tree/step2">Files · step2 · drflakelorenzgerman / DaggerSample</a></p><p>contact me: <a href="/service/mailto:shobeyri.ali@gmail.com">shobeyri.ali@gmail.com</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7b1d43e880d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>