Swiftpack.co -  crystalwinghero/ViperUIKit as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
crystalwinghero/ViperUIKit
Viper structure for UIKit
.package(url: "https://github.com/crystalwinghero/ViperUIKit.git", from: "1.1.0")

ViperUIKit

A set of VIPER protocols for UIKit. To help you quicky implement VIPER pattern to your UIViewController.

Installation

Swift Package Manager

  1. In Xcode, select File > Swift Packages > Add Package Dependency...
  2. Enter the URL for this repository https://github.com/crystalwinghero/ViperUIKit.git
  3. Choose a minimum semantic version of v1.1.0

Implementation

Note: you can follow the examples for in Examples folder.

Viper

Mapping object for all V-I-P-E-R classes/structs together as a whole.

import ViperUIKit

struct SampleViper : BaseViper {
    typealias View = SampleViewController
    typealias Interactor = SampleInteractor
    typealias Presenter = SamplePresenter
    typealias Entity = SampleEntity
    typealias Router = SampleRouter
}

View

Add PresentableView protocol on your ViewController.

import ViperUIKit

class SampleViewController : UIViewController, PresentableView {
    //1. declare type
    typealias Viper = SampleViper
    
    //2. declare presenter
    var presenter: Viper.Presenter!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //3. call setup
        presenter.setup()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //4. load content
        presenter.loadContent()
    }
}

Interactor

Add BaseInteractor on your Interactor.

import ViperUIKit

struct SampleInteractor : BaseInteractor {
    typealias Response = Any
    
    func fetch(_ completion: @escaping (Any) -> Void) {
        //TODO: add logic here
        completion("Hello, world!")
    }
    
}

Presenter

Add BasePresenter on your Presenter.

import ViperUIKit

///NOTE: mark as final class + inherits from NSObject
final class SamplePresenter : NSObject, BasePresenter {
    //1. declare type
    typealias Viper = SampleViper
    
    //2. declare variables for your VIPER
    //NOTE: always weak, to prevent retain cycle
    weak var view : Viper.View!
    var interactor: Viper.Interactor!
    var router: Viper.Router!
    var item : Viper.Entity?
    var list : [Viper.Entity] = []
    
    //3. default methods
    func setup() {
        //TODO: add your setup, i.e. navigation title, tableview data source, etc.
    }
    func loadContent() {
        //TODO: fetch your data
        interactor.fetch { (_) in
            //TODO: refresh UI
        }
    }
    func reloadContent() {
        //TODO: add reload method
    }
}

Entity

Add BaseEntity on your Entity.

import ViperUIKit

struct SampleEntity : BaseEntity {
    typealias PK = Int
    var pk : PK { id }
    var id : Int
}

Router

Add BaseRouter on your Router.

Note: default Router.create() will call createWithNib() by default, override it with createWithStoryboard(), or implements it the way your viewController needed to otherwise.

import ViperUIKit

struct SampleRouter : BaseRouter {
    typealias Viper = SampleViper
    
}

Usage

Create your VC through Router.create() method.

    //... 
    let vc = MyRouter.create() // VC will be created and linked with VIPER objects 
    self.navigationController?.pushViewController(vc, animated: true)
    //...

GitHub

link
Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API