<?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 Armen Galstyan on Medium]]></title>
        <description><![CDATA[Stories by Armen Galstyan on Medium]]></description>
        <link>https://medium.com/@armgal?source=rss-5bb938038453------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6JRcdCk9A_jsCgMIzKO55g.jpeg</url>
            <title>Stories by Armen Galstyan on Medium</title>
            <link>https://medium.com/@armgal?source=rss-5bb938038453------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 14:24:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@armgal/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[Kotlin scope functions]]></title>
            <link>https://armgal.medium.com/kotlin-scope-functions-210e04cc01be?source=rss-5bb938038453------2</link>
            <guid isPermaLink="false">https://medium.com/p/210e04cc01be</guid>
            <category><![CDATA[etl]]></category>
            <category><![CDATA[also]]></category>
            <category><![CDATA[with]]></category>
            <category><![CDATA[run]]></category>
            <category><![CDATA[apply]]></category>
            <dc:creator><![CDATA[Armen Galstyan]]></dc:creator>
            <pubDate>Mon, 10 May 2021 14:15:42 GMT</pubDate>
            <atom:updated>2021-05-10T14:17:56.157Z</atom:updated>
            <content:encoded><![CDATA[<p>The Kotlin standard library contains several functions whose purpose is to execute a block of code within the context of an object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7w8TjQ3LdBwI-titScqHiQ.png" /></figure><p>When you call such a function on an object with a lambda expression provided, it forms a temporary scope. In this scope, you can access the object without its name. Such functions are called <em>scope functions</em>. There are five of them: let, run, with, apply, and also.</p><p>Scope functions declarations</p><pre>public inline fun &lt;T, R&gt; with(receiver: T, block: T.() -&gt; R): R {<br>   ...<br>}<br></pre><pre>public inline fun &lt;T&gt; T.apply(block: T.() -&gt; Unit): T {<br>   ...<br>}<br></pre><pre>public inline fun &lt;T&gt; T.also(block: (T) -&gt; Unit): T {<br>   ...<br>}<br></pre><pre>public inline fun &lt;T, R&gt; T.let(block: (T) -&gt; R): R {<br>   ...<br>}<br></pre><pre>public inline fun &lt;T, R&gt; T.run(block: T.() -&gt; R): R {<br>   ...<br>}</pre><p>These functions do very similar things, there are important differences in their signature and implementation. These differences dictate <em>how </em>they must be used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VHHCgMVnKEShAaItKmTE7A.png" /><figcaption>Picture is taken from Kotlin Academy</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wsS3P98O0YLdLNSmeQTGXQ.png" /><figcaption>Picture is taken from Mindorks</figcaption></figure><h3><strong>Some examples.</strong></h3><p><strong>apply</strong></p><pre>// using apply</pre><pre>val user = User().<em>apply </em><strong>{<br>    </strong>name = &quot;John&quot;<br>    sureName = &quot;Smith&quot;<br><strong>}</strong></pre><pre><br>// without apply</pre><pre>val user = User()<br>user.name = &quot;John&quot;<br>user.sureName = &quot;Smith&quot;</pre><h4><strong>also</strong></h4><pre>// using also</pre><pre>Intent(context, TestActivity::class.<em>java</em>).<em>also </em><strong>{ <br>   </strong>intent <strong>-&gt; </strong>startActivity(intent) </pre><pre><strong>}</strong></pre><pre><br>// without also</pre><pre>val intent = Intent(context, TestActivity::class.<em>java</em>)<br>startActivity(intent)</pre><p><strong>let</strong></p><pre>// using <em>let</em></pre><pre><em>arguments</em>?.<em>let </em><strong>{<br>    </strong>val data = <strong>it</strong>.getString(DATA_KEY)<br><strong>}<br></strong></pre><pre>// without let <br>if (<em>arguments </em>!= null) {<br>    val data = it.getString(DATA_KEY)<br>}</pre><p><strong>run</strong></p><pre>// using run </pre><pre>val result = &quot;text&quot;.run {<br>    val tail = substring(1)<br>    tail.toUpperCase()<br>}</pre><pre><br>// without run</pre><pre>var text = &quot;text&quot;<br>val tail = text.substring(1)<br>val result = tail.toUpperCase()</pre><p><strong>with</strong></p><pre>// using with</pre><pre>val user = User()<br><em>with</em>(user) <strong>{<br>    </strong><em>...<br>    print</em>(&quot;User name : $name sureName : $sureName&quot;)<br><strong>}</strong></pre><pre>// without with<br>val user = User()<br><em>print</em>(&quot;User name : ${user.name} sureName : ${user.sureName}&quot;)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=210e04cc01be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin Data classes]]></title>
            <link>https://armgal.medium.com/kotlin-data-classes-addb72e38738?source=rss-5bb938038453------2</link>
            <guid isPermaLink="false">https://medium.com/p/addb72e38738</guid>
            <category><![CDATA[equals]]></category>
            <category><![CDATA[data-class]]></category>
            <category><![CDATA[hashcode]]></category>
            <category><![CDATA[clone-data]]></category>
            <category><![CDATA[tostring]]></category>
            <dc:creator><![CDATA[Armen Galstyan]]></dc:creator>
            <pubDate>Mon, 03 May 2021 22:09:56 GMT</pubDate>
            <atom:updated>2021-05-03T22:09:56.030Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FnQpUaARVBwNHHmwCZJfZw.jpeg" /></figure><p>For Data classes, the compiler automatically generates:</p><ul><li>copy() function, equals() and hashCode() pair, and toString() form of the primary constructor</li><li>componentN() functions</li></ul><h3>Kotlin Data Class Requirements</h3><p>Here are the requirements:</p><ul><li>The primary constructor must have at least one parameter.</li><li>The parameters of the primary constructor must be marked as either val (read-only) or var (read-write).</li><li>The class cannot be open, abstract, inner or sealed.</li><li>The class may extend other classes or implement interfaces. If you are using Kotlin version before 1.1, the class can only implement interfaces.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*169J8rSJlvyHlZt9-ANbsw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/221/1*Tq8hY9ceWBqi28O4OgWzCw.png" /></figure><p>When you declare a data class, the compiler automatically generates several functions such as toString(), equals(), hashcode() etc behind the scenes. This helps to keep you code concise. Had you used Java, you would need to write a lot of boilerplate code.</p><h3>Copying</h3><p>For a data class, you can create a copy of an object with some of its properties different using copy() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*ccW-RC2ZvDlHyXZtc0jC0Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*UVPdF0sW7gEPZahvk9UeiQ.png" /></figure><h3>toString() method</h3><p>The <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/to-string.html">toString()</a> function returns a string representation of the object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*cbz-Q-RWyhNXILCqEMpPRQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/230/1*bkNUAgiJdkboGJZV7HmA7Q.png" /></figure><h3>hashCode() and equals()</h3><p>The hasCode() method returns hash code for the object. If two objects are equal, hashCode() produces the same integer result. <strong>Recommended Reading:</strong> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/hash-code.html">hashCode()</a></p><p>The equals() returns true if two objects are equal (has same hashCode()). If objects are not equal, equals() returns false. <strong>Recommended Reading:</strong> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/equals.html">equals()</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*aCt49dxPmWdWJdcxFYk3jQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*rAf1S6sZBrqixvyyuGyDIQ.png" /></figure><h3>Destructuring Declarations</h3><p>You can destructure an object into a number of variables using <a href="https://kotlinlang.org/docs/reference/multi-declarations.html">destructing declaration</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*cYqvpAhfZXrvF3rcrDVlZw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/188/1*rtNP0dOxjU4Lwck2oQTqHw.png" /></figure><p>This was possible because the compiler generates componentN() functions all properties for a data class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*t21eyw0VYUPI8LdRmzhoww.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/77/1*HvWC3obRuQEYBrwszm-Iog.png" /></figure><h3>Conclusion</h3><p>Data classes are one of the most used Kotlin features and for a good reason they decrease the boilerplate code you need to write, enable features like destructuring and copying an object.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=addb72e38738" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Build Types, Product Flavors, Build Variants.]]></title>
            <link>https://armgal.medium.com/android-build-types-product-flavors-build-variants-454492de8d0?source=rss-5bb938038453------2</link>
            <guid isPermaLink="false">https://medium.com/p/454492de8d0</guid>
            <category><![CDATA[build-type]]></category>
            <category><![CDATA[productflavor]]></category>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[build-variants]]></category>
            <dc:creator><![CDATA[Armen Galstyan]]></dc:creator>
            <pubDate>Thu, 24 Dec 2020 15:29:34 GMT</pubDate>
            <atom:updated>2020-12-24T15:48:18.099Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*Guxeb0PFhjnKRqJc5SuBZQ.png" /></figure><h3>Build Variants</h3><p>Each <em>build variant</em> represents a different version of your app that you can build. For example, you might want to build one version of your app that’s free, with a limited set of content, and another paid version that includes more.</p><p>A build variant is a cross product of a build type and product flavor, and is the configuration Gradle uses to build your app. Using build variants, you can build the debug version of your product flavors during development, or signed release versions of your product flavors for distribution. Although you do not configure build variants directly, you do configure the build types and product flavors that form them. Creating additional build types or product flavors also creates additional build variants.</p><h3><strong>Build Types</strong></h3><p>You can create and configure build types in the module-level build.gradle file inside the android block. When you create a new module, Android Studio automatically creates the debug and release build types for you. Although the debug build type doesn&#39;t appear in the build configuration file, Android Studio configures it with debuggable true. This allows you to debug the app on secure Android devices and configures APK signing with a generic debug keystore.</p><p>You can add the debug build type to your configuration if you want to add or change certain settings. The following sample specifies an <strong>applicationIdSuffix</strong> for the debug build type, and configures a &quot;staging&quot; build type that is initialized using settings from the debug build type.</p><pre>android {<br>    defaultConfig {<br>        ...<br>    }<br>    buildTypes {<br>        release {<br>            minifyEnabled true<br>            proguardFiles getDefaultProguardFile(&#39;proguard-android.txt&#39;), &#39;proguard-rules.pro&#39;<br>        }<br><br>        debug {<br>            applicationIdSuffix &quot;.debug&quot;<br>            debuggable true<br>        }</pre><pre>        live {<br>            applicationIdSuffix &quot;.live&quot;<br>            ...<br>        }<br>        /**<br>         * The `initWith` property allows you to copy configurations from other build types,<br>         * then configure only the settings you want to change. This one copies the debug build<br>         */<br>        staging {<br>            initWith debug<br>            applicationIdSuffix &quot;.debugStaging&quot;<br>        }<br>    }<br>}</pre><p>In Build variants you can see your build types after sync project.</p><h3>Product Flavors</h3><p>Creating product flavors is similar to creating build types: add them to the productFlavors block in your build configuration and include the settings you want. The product flavors support the same properties as <strong>defaultConfig </strong>this is because <strong>defaultConfig</strong> actually belongs to the <strong>ProductFlavor</strong> class. This means you can provide the base configuration for all flavors in the <strong>defaultConfig</strong> block, and each flavor can change any of these default values, such as the <strong>applicationId</strong>.</p><p>All flavors must belong to a named flavor dimension, which is a group of product flavors. You must assign all flavors to a flavor dimension; otherwise, you will get the build error shown below. If a given module specifies only one flavor dimension, the Android Gradle plugin automatically assigns all of the module’s flavors to that dimension.</p><pre>Error:All flavors must now belong to a named flavor dimension.<br>The flavor &#39;flavor_name&#39; is not assigned to a flavor dimension.</pre><pre>android {<br>    ...<br>    defaultConfig {...}<br>    buildTypes {<br>        debug{...}<br>        release{...}<br>    }<br>    <br>    flavorDimensions &quot;version&quot;<br>    productFlavors {<br>        development {<br>            dimension &quot;version&quot;<br>            versionNameSuffix &quot;.dev&quot;<br>        }<br>        production {<br>            dimension &quot;version&quot;<br>            versionNameSuffix &quot;.prod&quot;<br>        }<br>    }<br>}</pre><p>All product flavors are associated with build types. <br>Our build variants will be:</p><ul><li>developmentDebug</li><li>developmentRelease</li><li>productionDebug</li><li>productionRelease</li></ul><p>Now, you can select any of the build variants you want for your build.</p><p>Thanks ))</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=454492de8d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handle State with Sealed classes in Android.]]></title>
            <link>https://armgal.medium.com/handle-state-with-sealed-class-in-android-3f7ded883e45?source=rss-5bb938038453------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f7ded883e45</guid>
            <category><![CDATA[sealed-classes]]></category>
            <category><![CDATA[enum]]></category>
            <category><![CDATA[when-expression]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Armen Galstyan]]></dc:creator>
            <pubDate>Thu, 24 Dec 2020 13:07:05 GMT</pubDate>
            <atom:updated>2020-12-24T14:26:52.984Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BJNttw1jT2_tO2fhOetXUQ.jpeg" /></figure><p>When we call API from the server, we should handle state. In the past we often used enum in that cases.</p><p>An enum is a special &quot;class&quot; that represents a group of <strong>constants</strong> (unchangeable variables, like final variables).</p><pre>enum class State {<br>    <em>SUCCESS</em>,<br>    <em>ERROR, <br>    LOADING<br></em>}</pre><p>Each enum constant is an object. Enum constants are separated with commas.</p><p>The disadvantage here is, that we can’t insert additional data into each State type.</p><pre>SUCCESS(val data: Data)</pre><h3>Sealed class is the best solution, when we use Kotlin.</h3><p>Sealed classes are used for representing restricted class hierarchies, when a value can have one of the types from a limited set, but cannot have any other type.<br>They are, in a sense, an extension of enum classes: the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances which can contain state.</p><pre>State&lt;T&gt;(<br>     val data: T? = null,<br>     val msg: String? = null<br>) {<br>     class Success&lt;T&gt;(data: T) : State&lt;T&gt;(data)<br>     class Loading&lt;T&gt;(data: T? = null) : State&lt;T&gt;(data)<br>     class Error&lt;T&gt;(msg: String, data: T? = null) : State&lt;T&gt;(data, msg)<br>}</pre><p>A sealed class is abstract by itself, it cannot be instantiated directly and can have <em>abstract</em> members.</p><p>Sealed classes are not allowed to have non-<em>private</em> constructors (their constructors are <em>private</em> by default).</p><p>The key benefit of using sealed classes comes into play when you use them in a <strong>when expression</strong>. If it&#39;s possible to verify that the statement covers all cases, you don&#39;t need to add an else clause to the statement. However, this works only if you use when as an expression (using the result) and not as a statement.</p><pre>viewModel.getData().observe(this, Observer { response -&gt; <strong><br>    </strong>when (response) {<br>        is Loading -&gt; { <br>              ... <br>        }<br>        is Success -&gt; { <br>              val data = response.data  <br>              ... <br>        }<br>        is Error -&gt; {<br>              val message = response.msg<br>              ...<br>        }<br>    }<br>})</pre><p>Thanks )</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*lW4mq63MvfggSagFI89shw.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f7ded883e45" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>