Swiftpack.co - crystalwinghero/ViperUIKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by crystalwinghero.
crystalwinghero/ViperUIKit 1.1.2
Viper structure for UIKit
⭐️ 0
🕓 2 years ago
.package(url: "https://github.com/crystalwinghero/ViperUIKit.git", from: "1.1.2")

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 Input = Any
    typealias Response = Any
    
    func fetch(_ input : Input?, _ completion: @escaping (Response) -> 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 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

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