<?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 Kush Saini on Medium]]></title>
        <description><![CDATA[Stories by Kush Saini on Medium]]></description>
        <link>https://medium.com/@kushsaini?source=rss-22f3bf4c15e9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XderHi2WxQphwZ1h2kl15w@2x.jpeg</url>
            <title>Stories by Kush Saini on Medium</title>
            <link>https://medium.com/@kushsaini?source=rss-22f3bf4c15e9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 21:44:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kushsaini/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[Make a Ticket View with Jetpack Compose]]></title>
            <link>https://medium.com/@kushsaini/make-a-ticketview-with-jetpack-compose-ea0c8f7a00a8?source=rss-22f3bf4c15e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea0c8f7a00a8</guid>
            <category><![CDATA[path]]></category>
            <category><![CDATA[shapes]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[custom-view-android]]></category>
            <dc:creator><![CDATA[Kush Saini]]></dc:creator>
            <pubDate>Thu, 09 Nov 2023 11:26:48 GMT</pubDate>
            <atom:updated>2023-11-09T11:28:08.798Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P6RyufTfRSkeD9Wl8SL7Yg.png" /></figure><p>I wanted to create this ticket or receipt view using Compose, to compare the complexity with creating it with Custom Views using canvas, turns out it is quite similar as we have to deal with <strong>Path</strong> to create this, but there is one thing that is very easier in compose is to create a shadow, as I can remember creating shadows for custom view like these was complex or it was not easier to understand it if you don’t know what the methods are for. With Jetpack Compose creating shadows for was quite easy. So, let’s have a look at the code:</p><pre>class TicketShape(<br>    private val teethWidthDp: Float,<br>    private val teethHeightDp: Float<br>) : Shape {<br><br>    override fun createOutline(<br>        size: Size,<br>        layoutDirection: LayoutDirection,<br>        density: Density<br>    ) = Outline.Generic(Path().apply {<br><br>  ... // path drawing logic here<br><br>}</pre><p>For creating a TicketView with Jetpack Compose, we implement <strong>Shape</strong> interface and override it’s method <strong>createOutline(…) </strong>which retrun a <strong>Outline </strong>object which we will define using <strong>AndroidPath </strong>class.</p><p>We can call a function <strong>Path() </strong>to get a instance of <strong>AndroidPath </strong>and create our shape with it.</p><p>Here is the logic to create the ticket shape:</p><p>I am using percentages to generalize the dimensions without hardcoding them, so the output shape can wrap any size.</p><p>Let’s define some variables we will be using to create the shape, they are pretty self-explanatory :</p><pre>val teethHeightPx = teethHeightDp * density.density<br>var fullTeethWidthPx = teethWidthDp * density.density<br>var halfTeethWidthPx = fullTeethWidthPx / 2<br>val shapeWidthPx = size.width * 0.99f - size.width * 0.01f<br><br>var currentDrawPositionX = size.width * 0.99f // current X position of our pointer<br>var teethBasePositionY = size.height * 0.01f + teethHeightPx // position Y marking base of our teeth</pre><p>First we move our drawing pointer to top-right corner of our shape</p><pre>moveTo(<br>    size.width * 0.99f,<br>    size.height * 0.01f<br>)</pre><p>Next, we have logic to find optimized count of teethes to fit the available width without overflowing or underflowing teethes, by modifying the teeth width.</p><pre><br>val teethCount = shapeWidthPx / fullTeethWidthPx<br>val minTeethCount = floor(teethCount)<br><br>val newTeethWidthPx = shapeWidthPx / minTeethCount<br>fullTeethWidthPx = newTeethWidthPx<br>halfTeethWidthPx = fullTeethWidthPx / 2</pre><p>We will draw half teeth at starting and half teeth at end of the top edge, to keep the drawn teeth count checked, we are starting with:</p><pre>var drawnTeethCount = 1</pre><p>Let’s draw the out top edge, we are drawing from top-right to top-left:</p><pre>// draw half of first teeth<br>lineTo(<br>    currentDrawPositionX - halfTeethWidthPx,<br>    teethBasePositionY + teethHeightPx<br>)<br><br>// draw remaining teethes<br>while (drawnTeethCount &lt; minTeethCount) {<br><br>    currentDrawPositionX -= halfTeethWidthPx<br><br>    // draw right half of teeth<br>    lineTo(<br>        currentDrawPositionX - halfTeethWidthPx,<br>        teethBasePositionY - teethHeightPx<br>    )<br><br>    currentDrawPositionX -= halfTeethWidthPx<br><br>    // draw left half of teeth<br>    lineTo(<br>        currentDrawPositionX - halfTeethWidthPx,<br>        teethBasePositionY + teethHeightPx<br>    )<br><br>    drawnTeethCount++<br>}<br><br>currentDrawPositionX -= halfTeethWidthPx<br><br>// draw half of last teeth<br>lineTo(<br>    currentDrawPositionX - halfTeethWidthPx,<br>    teethBasePositionY - teethHeightPx<br>)</pre><p>We are creating teethes by drawing a line moving right to left posting in x-axis, so, we are decreasing the <strong>currentDrawPositionX </strong>by half of teeth width, we are creating two halves of teeth, manipulating the Y position of our pointer by incrementing or decrementing teeth height from <strong>teethBasePositionY</strong>.</p><p>Next we will draw the left edge:</p><pre>lineTo(<br>    size.width * 0.01f,<br>    size.height * 0.99f<br>)</pre><p>Reset variable to draw bottom edge of teethes from left to right:</p><pre>drawnTeethCount = 1<br>teethBasePositionY = size.height * 0.99f - teethHeightPx<br>currentDrawPositionX = size.width * 0.01f</pre><p>Again, we will write similar logic to make the teethes in downward direction, from bottom-left to bottom-right:</p><pre>// draw half of first teeth<br>lineTo(<br>    currentDrawPositionX,<br>    teethBasePositionY + teethHeightPx<br>)<br><br>lineTo(<br>    currentDrawPositionX + halfTeethWidthPx,<br>    teethBasePositionY - teethHeightPx<br>)<br><br>// draw remaining teethes<br>while (drawnTeethCount &lt; minTeethCount) {<br><br>    currentDrawPositionX += halfTeethWidthPx<br><br>    // draw left half of teeth<br>    lineTo(<br>        currentDrawPositionX + halfTeethWidthPx,<br>        teethBasePositionY + teethHeightPx<br>    )<br><br>    currentDrawPositionX += halfTeethWidthPx<br><br>    // draw right half of teeth<br>    lineTo(<br>        currentDrawPositionX + halfTeethWidthPx,<br>        teethBasePositionY - teethHeightPx<br>    )<br><br>    drawnTeethCount++<br>}<br><br>currentDrawPositionX += halfTeethWidthPx<br><br>// draw half of last teeth<br>lineTo(<br>    currentDrawPositionX + halfTeethWidthPx,<br>    teethBasePositionY + teethHeightPx<br>)<br><br>close() // close the path</pre><p>Last thing, to add shadow we can use the generated <strong>Shape</strong> object from the <strong>TicketShape</strong> class to define a shape with shadow extension function of <strong>Modifier </strong>class.</p><pre>Box(<br>    modifier = Modifier<br>        .padding(8.dp)<br>        .shadow(<br>            2.dp, // change the shadow as needed<br>            shape = TicketShape(8f, 4f), // here<br>            clip = true<br>        )<br>        .background(Color.White)<br>) {<br><br>    Box(modifier = Modifier.padding(16.dp)) {<br>        content()<br>    }<br>}</pre><p>Finally this is the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*gSwVBnixUTo2_1hOMboc2Q.png" /><figcaption>with 24dp teeth width and 8dp teeth height</figcaption></figure><p>You can find the whole code here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae9b6e6f83d94688112843285dda524e/href">https://medium.com/media/ae9b6e6f83d94688112843285dda524e/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea0c8f7a00a8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Wanted espresso to watch for internet connectivity so I wrote my own library for it]]></title>
            <link>https://medium.com/@kushsaini/i-wanted-espresso-tests-to-watch-for-internet-connectivity-so-i-wrote-my-own-idling-resource-and-b8e53ee67eba?source=rss-22f3bf4c15e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8e53ee67eba</guid>
            <category><![CDATA[testing-framework]]></category>
            <category><![CDATA[android-espresso]]></category>
            <category><![CDATA[android-libraries]]></category>
            <category><![CDATA[testing-support-library]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Kush Saini]]></dc:creator>
            <pubDate>Sun, 07 Jan 2018 16:58:41 GMT</pubDate>
            <atom:updated>2018-01-08T06:26:31.739Z</atom:updated>
            <content:encoded><![CDATA[<p>If you have ever written Android UI tests you might already know about Espresso, for those who don’t <a href="https://developer.android.com/training/testing/espresso/index.html">Espresso</a> is an open source testing framework under <a href="https://developer.android.com/topic/libraries/testing-support-library/index.html">Android Testing Support Library</a> by Google which provides an <a href="https://es.wikipedia.org/wiki/API">API</a> that allows user interface tests to be created (from now on UI) to simulate user interactions in an Android application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6SWkmQ3wf3ZCOqEkhCtTnw.png" /><figcaption>Espresso</figcaption></figure><p>Using espresso I have to write UI tests with cases for online and offline scenarios. It was all good until I had to write a test case in which initially device is offline to see the application being tested works on offline mode properly but should also work when user performs action to reload data after connecting to the internet, so, in between the test I had to activate device’s internet through code and explicitly add a sleep to halt the test until internet is connected. I had to take an estimated time I which device will be online and provide it in the sleep method, but tests failed if device couldn’t connect to the internet even after that estimated time. For some time I increased the time of sleep for the test, but, this was just a workaround and the test started taking longer to execute because it had to wait until the sleep time even if device came online fast.</p><p>I thought it would be better if the test knew about the internet connectivity state and wait for device to come online, so, I created an <a href="https://developer.android.com/training/testing/espresso/idling-resource.html">Idling Resource</a> to monitor the connectivity state and made a library <a href="https://github.com/kushsaini10/connectivity-idling-resource"><strong>connectivity-idling-resource</strong></a><strong> </strong>for it.</p><p>You can add it to your project by adding below line in build.gradle file under app module:</p><pre><strong>dependencies {<br>    ...<br>    androidTestImplementation &#39;com.github.kushsaini10:connectivity-idling-resource:1.0.0&#39;<br>}</strong></pre><p>After doing this and syncing you will have a class ConnectivityIdlingResource in your project.</p><p>There are two modes for this Idling Resource:</p><ol><li>WAIT_FOR_CONNECTION - waits for device to be connected to internet before test moves on to next operation</li><li>WAIT_FOR_DISCONNECTION - waits for device to be disconnected from internet before test moves on to the next operation</li></ol><p>You will have to initialize an Idling Resource like this with a resource name, context and your preferred mode:</p><pre>IdlingResource idlingResource = new                               <br>                ConnectivityIdlingResource(&quot;resourceName&quot;,<br>                context,<br>                ConnectivityIdlingResource.<em>WAIT_FOR_CONNECTION</em>);</pre><p>Register the idling resource with Espresso before or while running your tests depends on your case.</p><pre>Espresso.registerIdlingResources(idlingResource);</pre><p>If your test covers only online cases then with WAIT_FOR_CONNECTION mode you can register the Idling Resource before your test so the test will halt and wait for internet availability.</p><p>To solve the case mentioned above, we have to register the idling resource (with WAIT_FOR_CONNECTION mode) after the code which enables internet, and the test runs perfectly fine without wasting time on halting the test.</p><p><a href="https://github.com/kushsaini10/connectivity-idling-resource">kushsaini10/connectivity-idling-resource</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8e53ee67eba" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>