Build SwiftUI App with API request

Fetching data from GitHub API and publish with List in SwiftUI

Nikita Koniukh
MSApps Development
4 min readMay 14, 2020

--

Intro

Here I will not explain what is it SwiftUI, because if you already started reading this article, you definitely know what it is šŸ¤Ŗ. I not, visit developer.apple.com to read more about SwiftUI. Also, I don't want to explain how to create a new project (please don't forget to check SwiftUI instead of StoryboardšŸ˜‚)

In this article, I will show how I've built a simple app that does call request and fetching result on the List(TableView). Weā€™ll use GitHub open-source API.

So Letā€™s dive straight into what we love to do ā€” coding šŸ§‘šŸ»ā€šŸ’».

OTR, I love to use emoji šŸ¤«

1. Building API Service šŸ§ 

For this task, Iā€™m using the Alamofire networking library to make my network requests. Also, for downloading and cashing images, was used AlamofireImage. Itā€™s not the main topic of our article so you can check the code by the link in the open project at GitHub.šŸ‘

2. ViewModel

Ok, we have got results. But how we can update our view?

Letā€™s create our ViewModel and it will conform to ObservableObject protocol. Then mark @Published any properties you want to be observed by SwiftUI within this model. In this class, we create a property ā€œreposā€ that marked with ā€œ@Publishedā€ property wrapper.

Also, we need to create an actual PassthroughSubject. PassthroughSubject is used to send values to subscribers when they become available.

And finally create and call ain Init() a function ā€œfetchRepoā€, where we call ā€downloadTrendingReposā€.

Here I also created a property called ā€œisLoadingā€. Its a boolean state of completion downloading repos. By default the state is true but when weā€™ve finished downloading, the state changes to false. Iā€™ll use it in the future for showing my custom indicator.

3. Main ContentView

Here, in fact, we begin our work with SwiftUI. For now, weā€™ve been added Text to our View ā€œšŸ”„HOT REPOSšŸ”„ ā€œ. We have to add at least one object šŸ¤·ā€ā™‚ļø because the compiler will show us an error. Next, create an instance of our view model: @ObservedObject var repoViewModel = RepoViewModel()

@ObservedObject will wrap your object into a dynamic view property. It allows SwiftUI to subscribe to your object. And if our model will changes, we can update our View.

The next step is to make a design for each cell.

4. List ā€œCellā€

Create a new file ā€œRepoCellā€. This file is similar to UITableViewCell that created in UIKit. Also here we add to the variable ā€œrepoModel: Repoā€ which will hold our object.

At this stage, we will build the design of our cell. This process reminds me of the function ā€œsetupCellā€ that Iā€™ve created in the UITableViewCell. What I like about the whistle is that I donā€™t need to create a separate xib and a Swift file. I can write everything in one View. Of course, we can split the code of our View into separate views. But for this project, we will leave everything in one file.

Vertical Devider šŸ¤Æ. It took me a while to figure out how to draw the vertical lines that in UIKit we made using with Views. In SwiftUI Iā€™ve used ā€œDevider()ā€. I've put him to HStack and gave a ā€œ.frame(width:3) .background(Color.gitHubText)ā€.

When I needed to pick a color, I could not find the variation of colors that were previously in UIKit. So I decided to create a custom color. Here Iā€™ll show a few steps on how to create it.

Cool thing, right? You can use the preview in Xcode to see the result in ā€œlive modeā€.

5. Put everything together

We are done with our cell and now we are returning to our main view.

Follow Where we started We created a project in which there was a View and the text inside this view was a text. So first let's wrap all our View with NavigationControlleršŸ˜±šŸ˜± In SwiftUi its called NavigationView. And this brings us for free all the animation and logic.

In the following order, add our TableView. Exactly, youā€™re right!šŸ˜ No more Table or CollectionView. Instead, we have a List. No more delegates and data source!šŸ˜®

Next, we just call our cell and pass our object. Add the rest of the design like shadow and padding. And voila, weā€™ve done! šŸ„± Easy peasy šŸ„“

Also, I really didnā€™t like the separators between the cells; I found a way to remove them - UITableView.appearance().separatorStyle = .none

I hope you enjoyed reading my article and found some useful info šŸ¤—šŸ‘

The source code you can find by the link.

--

--