Swiftpack.co -  nayanda1/Artisan as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
nayanda1/Artisan
Artisan is a DSL, MVVM and Data Binding framework for Swift
.package(url: "https://github.com/nayanda1/Artisan.git", from: "3.0.5")

Artisan

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

codebeat badge build test Version License Platform

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.5'
pod 'Draftsman ~> 1.0.5'
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.5 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.5"))
]

Use it in your target as Artisan

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

Author

Nayanda Haberty, nayanda1@outlook.com

License

Artisan is available under the MIT license. See the LICENSE file for more info.

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

For more wiki, go to here

Contribute

You know how, just clone and do pull request

GitHub

link
Stars: 1
Last commit: 1 week 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.

Related Packages

Release Notes

Update Draftsman
2 weeks ago

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