Build SwiftUI App with API request
Fetching data from GitHub API and publish with List in SwiftUI
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 š¤š