<?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 Leonardo Souza on Medium]]></title>
        <description><![CDATA[Stories by Leonardo Souza on Medium]]></description>
        <link>https://medium.com/@leonardo-matos?source=rss-e8cf5fa3c593------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*YMmSnyBz4VNjI_vcpInnRw.jpeg</url>
            <title>Stories by Leonardo Souza on Medium</title>
            <link>https://medium.com/@leonardo-matos?source=rss-e8cf5fa3c593------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 18:24:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leonardo-matos/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[Identifying what OS you’re app is being presented]]></title>
            <link>https://leonardo-matos.medium.com/identifying-what-os-youre-app-is-being-presented-df594436df41?source=rss-e8cf5fa3c593------2</link>
            <guid isPermaLink="false">https://medium.com/p/df594436df41</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[ipad]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[macos]]></category>
            <dc:creator><![CDATA[Leonardo Souza]]></dc:creator>
            <pubDate>Sat, 20 Feb 2021 02:11:42 GMT</pubDate>
            <atom:updated>2021-02-20T02:11:42.616Z</atom:updated>
            <content:encoded><![CDATA[<p>If you ever have adventured yourself using SwifUI you must certainly know how powerful it can be for making it a lot easier to create interactive and reactive UI, but jumping a step forward you can use SwiftUI on multiple platforms and but it’s pretty much obvious you can&#39;t wright the same UI rules for all of them.</p><p>With that being said, I’ll show you a quick solution to localize yourself inside the code and decide where to put certain rules for each platform you want your app to support.</p><h3>Straightforward Code Time!</h3><p>So, if you don’t know, UIKit isn’t apart of macOS development, so if you’re developing universally you need to do some workaround to exclude iOS related libraries at compile-time and for this, we use some <a href="https://www.swiftbysundell.com/articles/using-compiler-directives-in-swift/">Swift Compiler Directives</a> to avoid compiler issues. In the example below, we’re using the #if and #endif to check if the current OS isn&#39;t macOS, so we can import UIKit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*sAIIjvITPLnC41u8wBx0mg.png" /></figure><p>But why we’re doing this? Here is the answer, simply to access UIDevice.current.userInterfaceIdiom this is the class that will help us to identify between iPad and iPhone. So as we learned in the previous step to identify macOS, you&#39;re all set!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_2mcxMbFZEV7hepWPDzg6g.png" /></figure><p>Just to wrap things up, in our ContentView we’re creating a device variable containing the iOS device type, inside the body, we using a swift compiler directive to separate between iOS and macOS. Within the iOS statement, we’re doing a simple if statement for iPhone and iPad, and we’re done! If you create a universal application, copy and paste type this code on your own content view and run it on each platform the result will look something like this…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*anzN7_dfRu9mH7CACOlpUg.png" /></figure><p>Thank you for reading 😊</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df594436df41" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Combine's MergeMany to fulfill your requests]]></title>
            <link>https://leonardo-matos.medium.com/using-combines-mergemany-to-fulfill-your-requests-99e652b89cbf?source=rss-e8cf5fa3c593------2</link>
            <guid isPermaLink="false">https://medium.com/p/99e652b89cbf</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[rick-and-morty]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[combine]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <dc:creator><![CDATA[Leonardo Souza]]></dc:creator>
            <pubDate>Wed, 24 Jul 2019 15:42:05 GMT</pubDate>
            <atom:updated>2019-07-24T20:34:50.211Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OSkv94Kq0ce0ubfybdi_dg.png" /><figcaption>“Meeseeks and Destroy”, from <a href="https://www.adultswim.com/videos/rick-and-morty/">AdultSwim</a></figcaption></figure><p>Many men and women, have struggle to keep their sanity, while working with asyncronous programming, most have wondered beyond reactiveness, and many have failed, many times. The part the succeded, and now lives in the reactive world, perhaps are scared and don&#39;t fully understand how the pillars of reactive programming work. And the fallen, but brave developers who were demolished by complexity and framework adoption, find themselves deep in a callback hell.</p><p>Sorry about the dramatic introduction, but it was necessary, reactive programming isn&#39;t a simple subject and many people, myself included, have struggled with the concept and with frameworks that supports it.</p><p>If you felt a bit scared, or doesn&#39;t know what I&#39;m talking about, I strongly recommend you to read the following article, to get familiar with subject and understand the problem I&#39;m trying to solve. These articles helped me (a lot) to understand and use this concept in projects that I&#39;ve worked on, helped to a point that I felt comfortable to write this post to help other fellow developers.</p><p><a href="https://medium.com/ios-os-x-development/learn-and-master-%EF%B8%8F-the-basics-of-rxswift-in-10-minutes-818ea6e0a05b">Learn &amp; Master ⚔️ the Basics of RxSwift in 10 Minutes</a></p><h3>The Problem</h3><p>You doesn&#39;t need to be an experienced developer, to know that in some time you&#39;ll need to do some work with asynchronous programming, but most cases don&#39;t get too complex, but when they do, oh boy, things can get ugly. Imagine have to work with multiple, asynchronous network requests, that in the end you want to be part of the same result.</p><p>There are many aways to achieve this behavior, you can find a variety of third party frameworks that solve this issue, until now, my go to option was to use <a href="https://gist.github.com/LeoDeMatos/c237512f797d2a858e8951d8f2d85eb6"><strong>DispatchGroup</strong></a><strong>, </strong>entering and leaving the group and when all ended group.notify is called and you can do some work with your data.</p><p>But as June of 2019, apple introduced the Combine framework, as apple describe it&#39;s, combine is framework for handling of asynchronous events by combining event-processing operators.</p><blockquote>The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events. Combine declares <em>publishers</em> to expose values that can change over time, and <em>subscribers</em> to receive those values from the publishers.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*YTqcI0ic9Biq5n8jtoMj9g.jpeg" /></figure><p>Alright, so from here I&#39;m assuming, you know your way around iOS development, have worked with some remote API, and is familiar with some reactive concepts, if you don&#39;t, just tag along, it will be quite a ride 😎.</p><p>First create a new iOS application project, add a simple UITableView to your main UIViewController and register some UITableViewCell that is able to display some information, like an image, title and subtitle.</p><p>We’re going to use the awesome, open and free <a href="https://rickandmortyapi.com/">Rick and Morty API</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*AuX60rJf5OUxksaEv8IvjQ.gif" /></figure><p>For now we’re only going to use the character/ endpoint, to retrieve a list of characters and later more info about that character.</p><p>Let&#39;s begin, start by adding the combine import in your view controller file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72343e33664422686211d6feb6d8ca23/href">https://medium.com/media/72343e33664422686211d6feb6d8ca23/href</a></iframe><p>Now let&#39;s do some work to fetch some characters, first we need to map the API responses, convert JSON to a struct that we can use in code. In some cases the API returns the result of the request wrapped in a result object, the result may vary from a single object or an array of objects. So let&#39;s create an abstraction using generics that can handle both cases.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc44df71845a2f416d214359f00472a1/href">https://medium.com/media/bc44df71845a2f416d214359f00472a1/href</a></iframe><p>The struct above will handle API results as long as the type T conforms with the Codable protocol, and we will create a Rick and Morty character representation just for that. By taking a quick look at the API&#39;s documentation I was able to identify some attributes that compose a character, so you don&#39;t have to do so 😚 .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c0c1a2aeb96bac2e9ed117f6b2d9cdd/href">https://medium.com/media/9c0c1a2aeb96bac2e9ed117f6b2d9cdd/href</a></iframe><p>OK, now that we got the object mapping sorted, let dive into some Combine code. First we will create a URLRequest, this part requires some boilerplate code that most of us are familiar with, but to summarize, we need to create a JSONDecoder, to decode JSON string into object or structs, &quot;duh!&quot;, a URL containing the endpoint that will return the list of characters, the request object itself and finally set the HttpMethod to be used, which in our case is GET, because we want to GET some things.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3236b855d7d72953a50874957f764d22/href">https://medium.com/media/3236b855d7d72953a50874957f764d22/href</a></iframe><p>After all this preparation, here it comes the new stuff, if you are one of the real OG developers, you know that, to make requests using the URLSession, you will need a dataTask with request and completion params, now with the Combine framework, the implementation of URLSession has a dataTaskPublisher, that is used in the same context but it now publishes things, in other words, the data you are requesting will now be published when done.</p><p>let session = URLSession.shared.dataTaskPublisher(for: request)</p><p>That’s cool and all but we don&#39;t work with plain data objects, we need to map this data into something that we can work on, that&#39;s were the combine framework and it&#39;s operators comes into play.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10ec4f92bd12bd257a84d94fdf54b753/href">https://medium.com/media/10ec4f92bd12bd257a84d94fdf54b753/href</a></iframe><p>Let&#39;s break these down.</p><ol><li>tryMap here we can work with the network response, check for errors, headers etc, but as our main goal is simple, we&#39;re just returning data down the chain. Keep in mind that if the request fails none of the operator below will be called, you can handle any level of errors with the mapError operator.</li><li>decode if everything went well with the request, decode will receive a data object, and that is just what our JSONDecoder need to turn JSON data into objects. We just need to pass the type of object that we need to decode from and the decoder that we want to use, remember the boilerplate code? Yep the decoder is up there, with our convertFromSnakeCase configuration. Nice 🐍</li><li>map if you don&#39;t suffer from short memory (as I do), you don&#39;t need to scroll up to see that, our lovely API returns, all characters data inside a results object, and for that we created an RickAndMortyResponseWrapper, but weren&#39;t expecting this object, we only working with a array of Characters, this is what map if here for, we&#39;re mapping the result into results, witch are the array of characters.</li><li>receive(on:) this indicates that we want our data in the main thread as we&#39;re reloading our tableView, by default the dataTaskPublisher work is done in a background thread.</li><li>sink the one that will join us all, this operation as apple describes in their docs “Attaches a subscriber with closure-based behavior.”, will trigger the request itself, this mean that someone is waiting to receive the data from the publisher. Here we can do some safe work with the characters and reload our tableView in the main, as we specified in the step above.</li></ol><p>Now, besides the configuration to make the table view work, everything is ready to run. You can go ahead and see you application working with combine e be happy about it, but that&#39;s only the beginning, there&#39;s plenty more to come — <em>deep sight — .</em></p><h3>But what if we had many requests?</h3><p>The next step is to display all episodes each character has appeared in, but our API only return de URL for these episodes, and is our job to fetch every single one of them to get more information to display. Before Combine, you would have to do some work to get every episode together, maybe use aDispatchGroup to get all in one place, creating a lot of boilerplate and some error prone code, but that&#39;s not cool and now were cool 😎. Apple provided a structure called MergeMany, as may have guessed, it merges many Publishers.</p><p>Now what we have to do is, create a function that returns a publisher given an episode URL.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7d8983e90695def10051262b4764c3e/href">https://medium.com/media/e7d8983e90695def10051262b4764c3e/href</a></iframe><p>That&#39;s pretty much the same as before, except I introduced a BANG! in the URL just to make this example easier, since the function now expects a return type of some Publisher.</p><p>With everything in place, we can create a function to fetch all episodes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b4dff55cfe972d73bd6a0312637a8f7/href">https://medium.com/media/2b4dff55cfe972d73bd6a0312637a8f7/href</a></iframe><p>First we make sure the selected character has the episode URL array, then we use the compactMap operator to create an array of publishers for each URL using the function we created before, and finally the MergeMany structure itself, and all we need to do is serve the publisher array that we created and the structure will handle the rest. As you can see I&#39;m using again compactMapto map every publisher response into aEpisode and in the sink callback, appending the current episode into my episodes array and updating the tableView for every result just because it looks nice, check it out!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*vNHPazFzuNibn-FQkrvoqw.gif" /></figure><p>Now that you know these structures exists go ahead and start playing with them, get comfortable and maybe introduce it into your projects. I tried to cover a lot of subjects and I hope, I could spark a little of curiosity in your day.</p><h3>The End?</h3><p>The source code used in this example can be found in my <a href="https://github.com/LeoDeMatos/Rick-And-Morty">GitHub</a>, this repository will be in constant change, as I plan to use it in future posts, exploring Combine, theming this application for Dark Mode and any iOS related subject.</p><p>This exact example can be found in the <a href="https://github.com/LeoDeMatos/Rick-And-Morty/tree/feature/combine-marge-many">feature/combine-merge-many</a> branch in GitHub. That is it, for now…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99e652b89cbf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use SwiftUI to Speed up your View Coding]]></title>
            <link>https://leonardo-matos.medium.com/how-to-use-swiftui-to-speed-up-your-view-coding-6dbb0fcabc99?source=rss-e8cf5fa3c593------2</link>
            <guid isPermaLink="false">https://medium.com/p/6dbb0fcabc99</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Leonardo Souza]]></dc:creator>
            <pubDate>Thu, 20 Jun 2019 00:42:24 GMT</pubDate>
            <atom:updated>2019-06-20T00:42:24.448Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to use SwiftUI to Speed Up your View Coding</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DJGFWlHAqq1XZMHzTdAQBg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@kellysikkema?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Kelly Sikkema</a> on <a href="https://unsplash.com/search/photos/fast?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>So if you&#39;re here, you definitely want to use SwiftUI in your current project. Unfortunately the complete migration in some cases, are close to impossible, but maybe SwiftUI can help us spark a little of joy in our day to day ⌘+R, to check out our awesome UI, that we created using view code (because we&#39;re cool kids 😎, who likes to build and run our projects 72 times a hour).</p><h4>There&#39;s no, maybe, SwiftUI is here to to save us!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*iTnGOuqpWXQ5fHrY8EvFGQ.png" /></figure><p>If you are able to migrate to the newer Xcode 11 (ATM in Beta) and is using macOS Catalina (ATM in Beta), all you need to do is import <em>SwiftUI</em> to your <em>UIKit</em> <em>ViewController</em> and with some quick setup, you&#39;ll be able to see your ViewController&#39;s view written with view code, in the SwiftUI preview panel.</p><h4>Enough! We don&#39;t wanna waste any more time building our projects and reading this introduction! Let&#39;s do this!</h4><p>First, you&#39;ll need to bump your deployment target to 13.0 while using SwiftUI API&#39;s, don&#39;t worry the time you will waste to change it back afterwards, trust me, it&#39;s worthy.</p><p>First, import SwiftUI in your view controller.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e62e6114ff052b88ca01fbfd0348f02/href">https://medium.com/media/0e62e6114ff052b88ca01fbfd0348f02/href</a></iframe><p>Second, let&#39;s write our ViewController code with plain old UIKit</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06d35a18960122c939c5013ea5d989bd/href">https://medium.com/media/06d35a18960122c939c5013ea5d989bd/href</a></iframe><p>And now comes the magic 🧙‍♂️. We need to create a representation of this UIKit ViewController in SwiftUI, this is where <em>UIViewControllerRepresentable protocol </em>comes in to action, basically it will make our ViewController &quot;Presentable&quot; to SwiftUI preview. To do this, create a struct that conforms with <em>UIViewControllerRepresentable</em> and add it at end of ViewController&#39;s file. After, you must override <em>updateUIViewController </em>and<em> </em>makeUIViewController<em> </em>functions, as their names suggests, the former is to update your UI (ifNeeded), and the latter is to create an instance of your ViewController.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65cacf3a2d100fd7a786cd479feb2ac7/href">https://medium.com/media/65cacf3a2d100fd7a786cd479feb2ac7/href</a></iframe><p>Now that our view controller is is able to represented in SwiftUI&#39;s preview, let&#39;s setup the preview itself. Right after the struct that we just created, add another struct, but this time conforming with the protocol <em>PreviewProvider, </em>that will create a preview environment for us. The <em>PreviewProvider </em>protocol<em> </em>requires <em>previews</em> static variable, which is expecting a View, and the <em>ViewControllerRepresentable</em> that we just created is one 😊, so just return a new instance <em>ViewControllerRepresentable</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc5335d6f1bc4c6dc23c0f594ee16d63/href">https://medium.com/media/bc5335d6f1bc4c6dc23c0f594ee16d63/href</a></iframe><h4>And we&#39;re done!</h4><p>Stop right there cowpoke ✋! I know you hit, or was preparing your thumb and index finger to hit ⌘+B or ⌘+R. But we no longer belong to that life 🤠. Open the Xcode SwiftUI preview window and press ⌥+⌘+P to run preview.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fC_vRFwAiPCOmPBw5rFfdQ.gif" /></figure><p>That’s all folks! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6dbb0fcabc99" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>