16.3 C
London
Monday, September 9, 2024

VIPER greatest practices for iOS builders


Getting began with VIPER

To start with, you must learn my earlier (extra theoretical) article in regards to the VIPER structure itself. It is a fairly respectable writing explaining all of the VIPER parts and reminiscence administration. I’ve additionally polished it just a little bit, final week. ⭐️

The issue with that article nevertheless was that I have never present you the true deal, aka. the Swift code for implementing VIPER. Now after a full yr of tasks utilizing this structure I can lastly share all my greatest practices with you.

So, let’s begin by making a model new Xcode challenge, use the one view app template, title the challenge (VIPER greatest practices), use Swift and now you are able to take the subsequent step of creating an superior “enterprise grade” iOS app.

Producing VIPER modules

Lesson 1: by no means create a module by hand, at all times use a code generator, as a result of it is a repetative job, it is fuckin’ boring plus you must concentrate on extra necessary issues than making boilerplate code. You should utilize my light-weight module generator referred to as:

This part is outdated, you must use the swift template repository.

Simply obtain or clone the repository from GitHub. You may set up the binary device by working swift run set up –with-templates. It will set up the vipera app underneath /usr/native/bin/ and the essential templates underneath the ~/.vipera listing. You should utilize your individual templates too, however for now I will work with the default one. 🔨

I normally begin with a module referred to as Primary that is the basis view of the appliance. You may generate it by calling vipera Primary within the challenge listing, so the generator can use the right challenge title for the header feedback contained in the template recordsdata.

Clear up the challenge construction just a little bit, by making use of my conventions for Xcode, which means that assets goes to an Belongings folder, and all of the Swift recordsdata into the Sources listing. These days I additionally change the AppDelegate.swift file, and I make a separate extension for the UIApplicationDelegate protocol.

Create a Modules group (with a bodily folder too) underneath the Sources listing and transfer the newly generated Primary module underneath that group. Now repair the challenge points, by choosing the Data.plist file from the Belongings folder for the present goal. Additionally do take away the Primary Interface, and after which you could safely delete the Primary.storyboard and the ViewController.swift recordsdata, as a result of we’re not going to want them in any respect.

Contained in the AppDelegate.swift file, it’s a must to set the Primary module’s view controller as the basis view controller, so it ought to look considerably like this:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder {

    var window: UIWindow?
}

extension AppDelegate: UIApplicationDelegate {

    func utility(_ utility: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        self.window = UIWindow(body: UIScreen.important.bounds)
        self.window?.rootViewController = MainModule().buildDefault()
        self.window?.makeKeyAndVisible()

        return true
    }
}

Congratulations, you have created your very first VIPER module! 🎉

UITabBarController & VIPER

I’ve a brilliant easy answer for utilizing a tab bar controller in a VIPER module. First let’s generate a couple of new modules, these are going to be the tabs. I will use the JSONPlaceholder service, so lets say a separate tab for every of those assets: posts, albums, photographs, todos (with the identical module title). Generate all of them, and transfer them into the modules folder.

Now, let’s generate yet another module referred to as Residence. It will implement our tab bar controller view. If you would like you need to use the Primary module for this function, however I prefer to maintain that for animation functions, to have a neat transition between the loading display screen and my Residence module (all of it is dependent upon your wants).

So the principle logic that we’ll implement is that this: the principle view will notify the presenter in regards to the viewDidAppear occasion, and the presenter will ask the router to show the Residence module. The Residence module’s view will probably be a subclass of a UITabBarController, it will additionally notify it is presenter about viewDidLoad, and the presenter will ask for the right tabs, through the use of its router.

Right here is the code, with out the interfaces:

class MainDefaultView: UIViewController {

    var presenter: MainPresenter?

    override func viewDidAppear(_ animated: Bool) {
        tremendous.viewDidAppear(animated)

        self.presenter?.viewDidAppear()
    }
}

extension MainDefaultPresenter: MainPresenter {

    func viewDidAppear() {
        self.router?.showHome()
    }
}

extension MainDefaultRouter: MainRouter {

    func showHome() {
        let viewController = HomeModule().buildDefault()
        self.viewController?.current(viewController, animated: true, completion: nil)
    }
}

extension HomeDefaultView: HomeView {

    func show(_ viewControllers: [UIViewController]) {
        self.viewControllers = viewControllers
    }
}



extension HomeDefaultPresenter: HomePresenter {

    func setupViewControllers() {
        guard let controllers = self.router?.getViewControllers() else {
            return
        }
        self.view?.show(controllers)
    }

}

extension HomeDefaultRouter: HomeRouter {

    func getViewControllers() -> [UIViewController] {
        return [
            PostsModule().buildDefault(),
            AlbumsModule().buildDefault(),
            PhotosModule().buildDefault(),
            TodosModule().buildDefault(),
        ].map { UINavigationController(rootViewController: $0) }
    }
}

class HomeModule {

    func buildDefault() -> UIViewController {
        

        presenter.setupViewControllers()

        return view
    }
}

There may be one extra line contained in the Residence module builder perform that triggers the presenter to setup correct view controllers. That is simply because the UITabBarController viewDidLoad technique will get referred to as earlier than the init course of finishes. This behaviour is kind of undocumented however I assume it is an UIKit hack so as to keep the view references (or only a easy bug… is anybody from Apple right here?). 😊

Anyway, now you’ve got a correct tab bar contained in the challenge built-in as a VIPER module. It is time to get some knowledge from the server and right here comes one other necessary lesson: not every thing is a VIPER module.

Providers and entities As you may seen there isn’t any such factor as an Entity inside my modules. I normally wrap APIs, CoreData and lots of extra knowledge suppliers as a service. This fashion, all of the associated entities might be abstracted away, so the service might be simply changed (with a mock for instance) and all my interactors can use the service by means of the protocol definition with out understanding the underlying implementation.

One other factor is that I at all times use my promise library if I’ve to cope with async code. The explanation behind it’s fairly easy: it is far more elegant than utilizing callbacks and non-compulsory end result parts. It’s best to study guarantees too. So right here is a few a part of my service implementation across the JSONPlaceholder API:

protocol Api {

    func posts() -> Promise<[Post]>
    func feedback(for put up: Put up) -> Promise<[Comment]>
    func albums() -> Promise<[Album]>
    func photographs(for album: Album) -> Promise<[Photo]>
    func todos() -> Promise<[Todo]>
}



struct Put up: Codable {

    let id: Int
    let title: String
    let physique: String
}



class JSONPlaceholderService {

    var baseUrl = URL(string: "https://jsonplaceholder.typicode.com/")!

    enum Error: LocalizedError {
        case invalidStatusCode
        case emptyData
    }

    non-public func request<T>(path: String) -> Promise<T> the place T: Decodable {
        let promise = Promise<T>()
        let url = baseUrl.appendingPathComponent(path)
        print(url)
        URLSession.shared.dataTask(with: url) { knowledge, response, error in
            if let error = error {
                promise.reject(error)
                return
            }
            guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
                promise.reject(Error.invalidStatusCode)
                return
            }
            guard let knowledge = knowledge else {
                promise.reject(Error.emptyData)
                return
            }
            do {
                let mannequin = strive JSONDecoder().decode(T.self, from: knowledge)
                promise.fulfill(mannequin)
            }
            catch {
                promise.reject(error)
            }
        }.resume()
        return promise
    }
}

extension JSONPlaceholderService: Api {

    func posts() -> Promise<[Post]> {
        return self.request(path: "posts")
    }

    
}

Often I’ve a mock service implementation subsequent to this one, so I can simply check out every thing I need. How do I swap between these companies? Nicely, there’s a shared (singleton – do not hate me it is utterly advantageous 🤪) App class that I take advantage of largely for styling functions, however I additionally put the dependency injection (DI) associated code there too. This fashion I can go round correct service objects for the VIPER modules.

class App {

    static let shared = App()

    non-public init() {

    }

    var apiService: Api {
        return JSONPlaceholderService()
    }
}



class PostsModule {

    func buildDefault() -> UIViewController {
        let view = PostsDefaultView()
        let interactor = PostsDefaultInteractor(apiService: App.shared.apiService)

        

        return view
    }
}



class PostsDefaultInteractor {

    weak var presenter: PostsPresenter?

    var apiService: Api

    init(apiService: Api) {
        self.apiService = apiService
    }
}

extension PostsDefaultInteractor: PostsInteractor {

    func posts() -> Promise<[Post]> {
        return self.apiService.posts()
    }

}

You are able to do this in a 100 different methods, however I at present desire this method. This fashion interactors can instantly name the service with some additional particulars, like filters, order, kind, and many others. Principally the service is only a excessive idea wrapper across the endpoint, and the interactor is creating the fine-tuned (higher) API for the presenter.

Making guarantees

Implementing the enterprise logic is the duty of the presenter. I at all times use guarantees so a fundamental presenter implementation that solely hundreds some content material asynchronously and shows the outcomes or the error (plus a loading indicator) is just some strains lengthy. I am at all times making an attempt to implement the three fundamental UI stack parts (loading, knowledge, error) through the use of the identical protocol naming conventions on the view. 😉

On the view facet I am utilizing my good outdated assortment view logic, which considerably reduces the quantity of code I’ve to put in writing. You may go together with the standard approach, implementing a couple of knowledge supply & delegate technique for a desk or assortment view will not be a lot code in any case. Right here is my view instance:

extension PostsDefaultPresenter: PostsPresenter {

    func viewDidLoad() {
        self.view?.displayLoading()
        self.interactor?.posts()
        .onSuccess(queue: .important) { posts  in
            self.view?.show(posts)
        }
        .onFailure(queue: .important) { error in
            self.view?.show(error)
        }
    }
}



class PostsDefaultView: CollectionViewController {

    var presenter: PostsPresenter?

    init() {
        tremendous.init(nibName: nil, bundle: nil)

        self.title = "Posts"
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        self.presenter?.viewDidLoad()
    }
}

extension PostsDefaultView: PostsView {

    func displayLoading() {
        print("loading...")
    }

    func show(_ posts: [Post]) {
        let grid = Grid(columns: 1, margin: UIEdgeInsets(all: 8))

        self.supply = CollectionViewSource(grid: grid, sections: [
            CollectionViewSection(items: posts.map { PostViewModel($0) })
        ])
        self.collectionView.reloadData()
    }

    func show(_ error: Error) {
        print(error.localizedDescription)
    }
}

The cell and the ViewModel is exterior the VIPER module, I are inclined to dedicate an App folder for the customized utility particular views, extensions, view fashions, and many others.

class PostCell: CollectionViewCell {

    @IBOutlet weak var textLabel: UILabel!
}

class PostViewModel: CollectionViewViewModel<PostCell, Put up> {

    override func config(cell: PostCell, knowledge: Put up, indexPath: IndexPath, grid: Grid) {
        cell.textLabel.textual content = knowledge.title
    }

    override func dimension(knowledge: Put up, indexPath: IndexPath, grid: Grid, view: UIView) -> CGSize {
        let width = grid.width(for: view, gadgets: grid.columns)
        return CGSize(width: width, top: 64)
    }
}

Nothing particular, if you would like to know extra about this assortment view structure, you must learn my different tutorial about mastering assortment views.

Module communication

One other necessary lesson is to learn to talk between two VIPER modules. Usually I’m going with easy variables – and delegates if I’ve to ship again some form of information to the unique module – that I go round contained in the construct strategies. I will present you a very easy instance for this too.

class PostsDefaultRouter {

    weak var presenter: PostsPresenter?
    weak var viewController: UIViewController?
}

extension PostsDefaultRouter: PostsRouter {

    func showComments(for put up: Put up) {
        let viewController = PostDetailsModule().buildDefault(with: put up, delegate: self)
        self.viewController?.present(viewController, sender: nil)
    }
}

extension PostsDefaultRouter: PostDetailsModuleDelegate {

    func toggleBookmark(for put up: Put up) {
        self.presenter?.toggleBookmark(for: put up)
    }
}




protocol PostDetailsModuleDelegate: class {
    func toggleBookmark(for put up: Put up)
}

class PostDetailsModule {

    func buildDefault(with put up: Put up, delegate: PostDetailsModuleDelegate? = nil) -> UIViewController {
        let view = PostDetailsDefaultView()
        let interactor = PostDetailsDefaultInteractor(apiService: App.shared.apiService,
                                                      bookmarkService: App.shared.bookmarkService)
        let presenter = PostDetailsDefaultPresenter(put up: put up)

        

        return view
    }
}

class PostDetailsDefaultRouter {

    weak var presenter: PostDetailsPresenter?
    weak var viewController: UIViewController?
    weak var delegate: PostDetailsModuleDelegate?
}

extension PostDetailsDefaultRouter: PostDetailsRouter {

    func toggleBookmark(for put up: Put up) {
        self.delegate?.toggleBookmark(for: put up)
    }
}


class PostDetailsDefaultPresenter {

    var router: PostDetailsRouter?
    var interactor: PostDetailsInteractor?
    weak var view: PostDetailsView?

    let put up: Put up

    init(put up: Put up) {
        self.put up = put up
    }
}

extension PostDetailsDefaultPresenter: PostDetailsPresenter {

    func reload() {
        self.view?.setup(with: self.interactor!.bookmark(for: self.put up))

        
        self.interactor?.feedback(for: self.put up)
        .onSuccess(queue: .important) { feedback in
            self.view?.show(feedback)
        }
        .onFailure(queue: .important) { error in
            
        }
    }

    func toggleBookmark() {
        self.router?.toggleBookmark(for: self.put up)
        self.view?.setup(with: self.interactor!.bookmark(for: self.put up))
    }
}

Within the builder technique I can entry each part of the VIPER module so I can merely go across the variable to the designated place (identical applies for the delegate parameter). I normally set enter variables on the presenter and delegates on the router.

It is normally a presenter who wants knowledge from the unique module, and I prefer to retailer the delegate on the router, as a result of if the navigation sample adjustments I haven’t got to vary the presenter in any respect. That is only a private desire, however I like the best way it appears like in code. It is actually arduous to put in writing down these items in a single article, so I would advocate to obtain my completed pattern code from GitHub.

Abstract

As you’ll be able to see I am utilizing varied design patterns on this VIPER structure tutorial. Some say that there isn’t any silver bullet, however I consider that I’ve discovered a very superb methodology that I can activate my benefit to construct high quality apps in a short while.

Combining Guarantees, MVVM with assortment views on prime of a VIPER construction merely places each single piece into the precise place. Over-engineered? Perhaps. For me it is well worth the overhead. What do you concentrate on it? Be at liberty to message me by means of twitter. You may also subscribe to my month-to-month e-newsletter under.

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here