<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luis Mierez</title>
    <description>The latest articles on DEV Community by Luis Mierez (@luismierez).</description>
    <link>https://dev.to/luismierez</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F48219%2Ff83db5b9-d303-4601-b038-bba71c0ab04f.png</url>
      <title>DEV Community: Luis Mierez</title>
      <link>https://dev.to/luismierez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luismierez"/>
    <language>en</language>
    <item>
      <title>Infinite LazyColumn in Jetpack Compose </title>
      <dc:creator>Luis Mierez</dc:creator>
      <pubDate>Wed, 09 Jun 2021 05:45:04 +0000</pubDate>
      <link>https://dev.to/luismierez/infinite-lazycolumn-in-jetpack-compose-44a4</link>
      <guid>https://dev.to/luismierez/infinite-lazycolumn-in-jetpack-compose-44a4</guid>
      <description>&lt;p&gt;Displaying a list of content is one of the most common things that we are required to add to our apps and in my experience the lists are more often than not required to have some sort of pagination. This is a fairly common pattern where we only fetch the items needed at the time. The way we handle that is to show a list with a number of items (say 20) and as the user reaches the end of those first 20 items, we fetch the next 20 items. &lt;/p&gt;

&lt;p&gt;In our app we have an implementation of &lt;a href="https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.OnScrollListener"&gt;RecyclerView.OnScrollListener&lt;/a&gt; where we check if we are close to the end of the list and trigger a &lt;code&gt;loadMore&lt;/code&gt; callback. Now that we are moving to compose I wanted to find a way to do the same pattern and came up with this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Works similar to our old RecyclerView implementation but now in compose! Let me explain what is going on.&lt;/p&gt;

&lt;p&gt;First the parameters:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;listState: LazyListState&lt;/code&gt; as the name implies is the state of the list, which you can get by calling &lt;code&gt;val listState = rememberLazyListState()&lt;/code&gt;. This state has all the info that we need to observe to create our infinite loading list. It can also be used to control the list, but we aren't using that in here. &lt;strong&gt;REMEMBER:&lt;/strong&gt; This same &lt;code&gt;listState&lt;/code&gt; needs to be passed to the &lt;code&gt;LazyColumn&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;buffer: Int = 2&lt;/code&gt; This tells our &lt;code&gt;InfiniteListHandler&lt;/code&gt; how many items &lt;strong&gt;BEFORE&lt;/strong&gt; we get to the end of the list to call the &lt;code&gt;onLoadMore&lt;/code&gt; function. This is technically not needed, but it makes the infinite loading more seamless, since we can  control when to fetch new data and we don't need to show a loading indicator at the end of the list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;onLoadMore: () -&amp;gt; Unit&lt;/code&gt; This function will get called once the user reaches the load more threshold.&lt;/p&gt;

&lt;p&gt;Now to our logic. It's fairly straightforward: we want to call the &lt;code&gt;onLoadMore&lt;/code&gt; function when the last visible item on the list is within the threshold of our total items in the list minus our buffer. For example:&lt;br&gt;
If we have a list with 20 items, we have a buffer of 4, and with our test device we can see 6 items at a time. As the user scrolls the last visible item index will keep getting higher and higher until it reaches 16, which is our threshold of the last item index (20) minus our buffer (4). At which point the &lt;code&gt;onLoadMore&lt;/code&gt; will get called and we can fetch and load the next 20 items. Then our list will have 40 items and we will fetch more items once the last visible item index reaches 36, totalItems(40) -  buffer(4). And so on until we have no more items to show.&lt;/p&gt;

&lt;p&gt;We can write this in code like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;val loadMore = remember {
        derivedStateOf {
            val layoutInfo = listState.layoutInfo
            val totalItemsNumber = layoutInfo.totalItemsCount
            val lastVisibleItemIndex = (layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: 0) + 1

            lastVisibleItemIndex &amp;gt; (totalItemsNumber - buffer)
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We use a remembered derived state to minimize unnecessary compositions. See &lt;a href="https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#derivedStateOf(kotlin.Function0)"&gt;derivedStateOf&lt;/a&gt; for more info.&lt;/p&gt;

&lt;p&gt;But inside we use the &lt;code&gt;listState&lt;/code&gt; to get the &lt;code&gt;totalItemsNumber&lt;/code&gt; and the &lt;code&gt;lastVisibleItemIndex&lt;/code&gt; which we add 1 to, to make it match the total count. We then check that &lt;code&gt;lastVisibleItemIndex&lt;/code&gt; is greater than our &lt;code&gt;totalItemsNumber&lt;/code&gt; minus our &lt;code&gt;buffer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is one last thing that we need to get this working, and that is that we need to observe changes to the &lt;code&gt;loadMore&lt;/code&gt; &lt;code&gt;State&lt;/code&gt;. We do that like so:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LaunchedEffect(loadMore) {
        snapshotFlow { loadMore.value }
            .distinctUntilChanged()
            .collect {
                onLoadMore()
            }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We now have a fully working &lt;code&gt;InfiniteListHandler&lt;/code&gt; that will notify you whenever you are at the end of the list, or close to it depending on your buffer. And to use it you can do something like:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Happy Composing!&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>compose</category>
    </item>
    <item>
      <title>Simple Android Studio Live Template for Serialized Name</title>
      <dc:creator>Luis Mierez</dc:creator>
      <pubDate>Tue, 18 May 2021 23:11:21 +0000</pubDate>
      <link>https://dev.to/luismierez/simple-android-studio-live-template-for-serialized-name-54o4</link>
      <guid>https://dev.to/luismierez/simple-android-studio-live-template-for-serialized-name-54o4</guid>
      <description>&lt;p&gt;Most times when building an application we have to talk to some back end service we do not control. That back end service might use a different naming scheme than what we use in Kotlin/Java. In comes the &lt;code&gt;@SerializedName&lt;/code&gt; from the &lt;a href="https://github.com/google/gson"&gt;Gson&lt;/a&gt; library (or &lt;code&gt;@Json&lt;/code&gt; from &lt;a href="https://github.com/square/moshi"&gt;Moshi&lt;/a&gt;) to the rescue. It allows us to tell our deserializer where to look for the fields. However that means that we have to write some extra code in our objects since we have to add that &lt;code&gt;@SerializedName&lt;/code&gt; annotation to each field that has a different naming scheme.&lt;/p&gt;

&lt;p&gt;Let's look at the &lt;a href="https://pokeapi.co/"&gt;poke api&lt;/a&gt; as an example. The Poke Api has a endpoint to get info on berries. That object looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "id": 1,
  "name": "cheri",
  "growth_time": 3,
  "max_harvest": 5,
  "natural_gift_power": 60,
  "size": 20,
  "smoothness": 25,
  "soil_dryness": 15,
  "firmness": {
    "name": "soft",
    "url": "https://pokeapi.co/api/v2/berry-firmness/2/"
  },
  "flavors": [
    {
      "potency": 10,
      "flavor": {
        "name": "spicy",
        "url": "https://pokeapi.co/api/v2/berry-flavor/1/"
      }
    }
  ],
  "item": {
    "name": "cheri-berry",
    "url": "https://pokeapi.co/api/v2/item/126/"
  },
  "natural_gift_type": {
    "name": "fire",
    "url": "https://pokeapi.co/api/v2/type/10/"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which as you can see has a couple of fields that use underscores. When making that object in our code we'll have to add a lot of the  &lt;code&gt;SerializedName&lt;/code&gt; annotation. Since I'm lazy and didn't want to have to copy that a bunch of time I created a &lt;a href="https://www.jetbrains.com/help/idea/using-live-templates.html"&gt;Live Template&lt;/a&gt; that does most of the heavy lifting.&lt;/p&gt;

&lt;p&gt;To create a Live Template you need to go to Preferences -&amp;gt; Editor -&amp;gt; Live Templates. In there you'll see all the Live Templates you currently have in the app. For our case, we want to add one for kotlin so go to the kotlin group, hit the &lt;code&gt;+&lt;/code&gt; button and then hit Live Template.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S2_DHcE6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chu1k8pllr4dfco0dyhl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S2_DHcE6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chu1k8pllr4dfco0dyhl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you create the Live Template now it's time to fill it with what we want it to do. We should give it a good abbreviation since that is how we'll call our Live Template from our code. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D7sXHV_X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcfg5e0mex9tq3w1shzu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D7sXHV_X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcfg5e0mex9tq3w1shzu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I like the abbreviation &lt;code&gt;sername&lt;/code&gt; for &lt;code&gt;SerializedName&lt;/code&gt; so we'll go with that. You can see in the image above that it's giving you a warning that there is no applicable context so hit &lt;code&gt;Define&lt;/code&gt; and let's select the drop down for kotlin, and select class and object declaration. This context is where we can call this live template from, in our case we only want to call it from a kotlin class when declaring an object.&lt;/p&gt;

&lt;p&gt;Now to the cool part of Live Templates. In the &lt;code&gt;Template Text&lt;/code&gt; input box, we'll write this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@com.google.gson.annotations.SerializedName("$PARAM$") val $PARAM1$: $END$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's run through this line to explain it.&lt;br&gt;
Live Templates are smart enough to auto import your dependencies if they aren't there already, but for that to work we need to give it the full path with the &lt;code&gt;@&lt;/code&gt; at the front.&lt;br&gt;
The &lt;code&gt;$PARAM$&lt;/code&gt; means that it is inputted by the user. Same with &lt;code&gt;$PARAM1$&lt;/code&gt;. &lt;br&gt;
&lt;code&gt;$END$&lt;/code&gt; is a special case which tells Live Template where to put the cursor once you are done.&lt;/p&gt;

&lt;p&gt;Once we write that, hit the &lt;code&gt;Edit Variables&lt;/code&gt; button to add some more settings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d4-SqQ1R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/al76qg327lu7dp7p00sx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d4-SqQ1R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/al76qg327lu7dp7p00sx.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;$PARAM1$&lt;/code&gt; field will read from the &lt;code&gt;$PARAM$&lt;/code&gt; since we want to copy the name from the back end service. However, we want to change the text to be camel case.&lt;/p&gt;

&lt;p&gt;Once we add that we are done! To see it in action, create a new class and when adding an object that needs the &lt;code&gt;@SerializedName&lt;/code&gt; annotation, type &lt;code&gt;sername&lt;/code&gt; and you'll see how it automatically adds everything. Happy coding!&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Find your Stack</title>
      <dc:creator>Luis Mierez</dc:creator>
      <pubDate>Sun, 24 Jan 2021 05:13:52 +0000</pubDate>
      <link>https://dev.to/luismierez/find-your-stack-1818</link>
      <guid>https://dev.to/luismierez/find-your-stack-1818</guid>
      <description>&lt;p&gt;We recently had to debug some old (&lt;strong&gt;very very old&lt;/strong&gt;) code because of some new issues arising from it. One of the things we were investigating was whether the code was starting activities in the wrong order due to a race condition. So we had to debug our activity stack. We quickly realized that there was no easy way to debug that from android studio. Then we turned to the trusty &lt;code&gt;adb&lt;/code&gt; which this command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;adb shell dumpsys activity activities&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;dumpsys activity activities&lt;/code&gt; command gives you a ton of info about the current activities. But we only care about the activities related to our app, so we pipe the output to &lt;code&gt;grep package.name&lt;/code&gt;. &lt;br&gt;
This still gives us a little too much into if you just want to look at the activity stack, so we pipe output again to &lt;code&gt;grep Hist&lt;/code&gt; to only get the activity stack. The full command ends up looking something like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;adb shell dumpsys activity activities | grep package.name | grep Hist&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This works well if you want a snapshot of the current activity stack, but doesn't really help if you want to continuously log the activity stack. This is where we turned to the &lt;code&gt;watch&lt;/code&gt; command. This command is not on mac by default so you have to install it through brew like &lt;code&gt;brew install watch&lt;/code&gt;. The &lt;code&gt;watch&lt;/code&gt; command runs a command at whatever interval you want. &lt;br&gt;
With &lt;code&gt;watch&lt;/code&gt; installed your full command turns into something like:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;watch -n 0.1 "adb shell dumpsys activity activities | grep package.name | grep Hist"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-n&lt;/code&gt; flag sets the time interval at which we run our adb command. So this means run the command every .1 secs. &lt;br&gt;
This ended up working perfectly and we were able to continuously debug our activity stack and figure out if there was some problem there. Turns out the activity stack was fine but this helped us rule out what and pointed us in the right direction to eventually fix the issue.&lt;/p&gt;

</description>
      <category>android</category>
      <category>cli</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
