Improving Code for an Experimental Project SwiftUI

I found the Food app on GitHub and decided to fix it.

I recently worked on an experimental project where I had the opportunity to improve my coding skills. During the process, I made several changes to the code to make it more organized and efficient. Here are some of the changes I made:

Separated Views in ContentView

One of the first things I did was to separate the views that were created in one file, ContentView. By creating separate files for each view, I was able to easily locate and modify them. This helped make the code easier to read and understand.

import SwiftUI

struct ContentView: View {
@StateObject var viewModel = FoodViewModel()

var body: some View {
ZStack {
NavigationView {
TabView {
.tabItem {
OrderScreen().tabItem {
AccountScreen().tabItem {
UIToolbar.appearance().barTintColor = .lightGray

.onAppear {


Created an API Service

I also created an API service to handle the data requests from the server. By doing so, I was able to keep the data requests separate from the views, which made the code more modular and easier to maintain.

struct APIService {
public static let share = APIService()
private let baseURL = URL(string: SC.baseURLMock.value)
private init() {}

func resultRequest<T: Codable>(endpoint: Endpoint, completion: @escaping (Result<T, Error>) -> Void) {
guard let url = baseURL else {return}
var request = URLRequest(url: url)
request.httpMethod = endpoint.method.rawValue

URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {

guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
let statusCode = (response as? HTTPURLResponse)?.statusCode ?? -1
completion(.failure(APIError.invalidResponse(statusCode: statusCode)))

guard let data = data else {

do {
let decodedResponse = try JSONDecoder().decode(T.self, from: data)
} catch {

Created Image and String Extensions

To make the code more concise, I created extensions for images and strings. These extensions allowed me to add functionality to the built-in Swift classes and made the code more reusable.

extension String {

public static var empty: String {
return SC.empty.value

public static var dash: String {
return SC.dash.value

public static var threeSpace: String {

//: Error response convert

public func errorWith() -> NSError {
let error = NSError(
domain: .empty,
code: 429,
userInfo: [NSLocalizedDescriptionKey: self])
return error

import SwiftUI

extension Image {
// MARK: Asset
public static var homeAsset: Image {
return Image(uiImage: UIImage(named: SC.homeAsset.value)!)

public static var orderAsset: Image {
return Image(uiImage: UIImage(named: SC.orderAsset.value)!)

public static var userAsset: Image {
return Image(uiImage: UIImage(named: SC.userAsset.value)!)

public static var drinkAsset: Image {
return Image(uiImage: UIImage(named: SC.drink.value)!)

public static var fastfoodAsset: Image {
return Image(uiImage: UIImage(named: SC.fastfood.value)!)

public static var healthyAsset: Image {
return Image(uiImage: UIImage(named: SC.healthy.value)!)

public static var riceAsset: Image {
return Image(uiImage: UIImage(named: SC.rice.value)!)

public static var snacksAsset: Image {
return Image(uiImage: UIImage(named: SC.snacks.value)!)

// MARK: System
public static var homeSystem: Image {
return Image(systemName: SC.homeSystem.value)

public static var caseBagSystem: Image {
return Image(systemName: SC.caseBagSystem.value)

public static var personSystem: Image {
return Image(systemName: SC.personSystem.value)

public static var emptyImage: Image {
return Image(systemName: SC.emptyImage.value)

public static var searchSystem: Image {
return Image(systemName: SC.searchSystem.value)


Improved Usage of View Models

I also realized that the usage of the view model in the previous code was incorrect. To fix this, I restructured the code to better use the view model, which made the code more efficient.

import SwiftUI

class FoodViewModel: ObservableObject {
@Published var banner: [BannerModel] = []
@Published var collection : [OfferCollections] = []
@Published var voucher: Int?
@Published var restaurant : [RestaurantCollectionModel] = []
@Published var recommend : [RestaurantsModel] = []
@Published var offer : [OffersModel?] = []

func getBannerData() {
APIService.share.resultRequest(endpoint: FoodEndpoint.getMockData) { (result: Result<FoodDataModel, Error>) in
DispatchQueue.main.async {

switch result {
case .success(let value):
//banner{ item in

self.voucher =

//Collection{ item in

//Restaurants{ item in

//Recomended[0].restaurants.forEach({ item in

//offer[0].restaurants[0].offers.forEach({ item in

case .failure(let error):




Because there is only one API, In ViewModel I made just one request

My Swift UI Wallpaper app Filter and you can edit your image too from your photo in this template has been sold on the CodeCanyon website. Thank you for reading my article. 😄



IOS Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store