A MVVM and Data Binding framework for Swift

Artisan

Artisan is a MVVM framework for Swift using the bonding features from Pharos and constraints builder from Draftsman.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • Swift 5.1 or higher
  • iOS 10.0 or higher

Installation

Cocoapods

Artisan is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'Artisan ~> 3.0.9'
pod 'Draftsman ~> 1.0.9'
pod 'Pharos ~> 1.1.5'

Swift Package Manager from XCode

  • Add it using xcode menu File > Swift Package > Add Package Dependency
  • Add https://github.com/nayanda1/Artisan.git as Swift Package url
  • Set rules at version, with Up to Next Major option and put 3.0.9 as its version
  • Click next and wait

Swift Package Manager from Package.swift

Add as your target dependency in Package.swift

dependencies: [
    .package(url: "https://github.com/nayanda1/Artisan.git", .upToNextMajor(from: "3.0.9"))
]

Use it in your target as Artisan

 .target(
    name: "MyModule",
    dependencies: ["Artisan"]
)

Usage

Read wiki for more detailed information.

Basic Usage

Creating MVVM Pattern using Artisan is easy. All you need to do is extend ViewMediator, TableCellMediator or CollectionCellMediator and implement bonding method.
For the example, If you want to create custom UITableViewCell:

import Artisan
import UIKit
import Draftsman
import Pharos

class MyCell: TableFragmentCell {
    lazy var title = builder(UILabel.self)
        .font(.boldSystemFont(ofSize: 16))
        .numberOfLines(1)
        .textAlignment(.left)
        .textColor(.secondary)
        .build()
    lazy var subTitle = builder(UILabel.self)
        .font(.systemFont(ofSize: 12))
        .numberOfLines(1)
        .textAlignment(.left)
        .textColor(.main)
        .build()
    
    // MARK: Dimensions
    var margin: UIEdgeInsets = .init(insets: 16)
    var spacing: CGFloat = 6
    
    override func planContent(_ plan: InsertablePlan) {
        plan.fit(title)
            .at(.fullTop, .equalTo(margin), to: .parent)
        plan.fit(subTitle)
            .at(.bottomOf(title), .equalTo(spacing))
            .at(.fullBottom, .equalTo(margin), to: .parent)
    }
}

class MyCellVM<Cell: MyCell>: TableCellMediator<Cell> {
    @Observable var model: MyModel
    
    init(model: MyModel) {
        self.model = model
    }

    override func bonding(with view: Cell) {
        $event.map { $0.title }.relayValue(to: .relay(of: view.title, \.text))
        $event.map { $0.description }.relayValue(to: .relay(of: view.subTitle, \.text))
    }
}

then add it to UITableView

import Artisan
import UIKit
import Draftsman
import Pharos

class MyViewController: UIViewController {
    var tableView: UITableView!
    var searchBar: UISearchBar!
  
    @Observable var searchPhrase: String?
    @Observable var models: [MyModel] = []

    override viewDidLoad() {
        super.viewDidLoad()
        $searchPhrase.bonding(with: .relay(of: searchBar, \.text)
            .multipleSetDelayed(by: 1)
            .whenDidSet(invoke: self, method: MyViewController.getData(from:))
        $models.compactMap { MyCellVM(model: $0) }
            .observe(on: .main)
            .relayValue(to: tableView.mediator.$cells)
    }

    func getData(from: changes: Changes<String?>) {
        doGetDataFromAPI(for: changes.new) { [weak self] data in
            self?.models = data
        }
    }
}

It will automatically run getData when user type in searchBar, with minimum interval between method call is 1 second and will update table cells with new data on Main Thread everytime you get data from API

Author

Nayanda Haberty, nayanda1@outlook.com

GitHub

https://github.com/nayanda1/Artisan