Creating an MVVM Pattern using Artisan is easy. Binding is supported by Pharos and View building is supported by Draftsman, Artisan is the one that make both can work with each other perfectly.
Like if you want to create simple Search Screen:
import UIKit
import Artisan
import Draftsman
import Builder
import Pharos
class SearchScreen: UIPlannedController, ViewBindable {
typealias Model = SearchScreenViewModel
@Subject var allResults: [Result] = []
// MARK: View
lazy var searchBar: UISearchBar = builder(UISearchBar.self)
.placeholder("Search here!")
.sizeToFit()
.tintColor(.text)
.barTintColor(.background)
.delegate(self)
.build()
lazy var tableView: UITableView = builder(UITableView.self)
.backgroundColor(.clear)
.separatorStyle(.none)
.allowsSelection(true)
.delegate(self)
.build()
@LayoutPlan
var viewPlan: ViewPlan {
tableView.drf
.edges.equal(with: .parent)
.cells(from: $allResults) { _, result in
Cell(from: ResultCell.self) { cell, _ in
cell.apply(result)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .background
tableView.keyboardDismissMode = .onDrag
applyPlan()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.tintColor = .main
navigationItem.titleView = searchBar
}
// MARK: This is where View Model bind with View
@BindBuilder
func autoBinding(with model: Model) -> BindRetainables {
model.searchPhraseBindable
.bind(with: searchBar.bindables.text)
}
@BindBuilder
func autoFireBinding(with model: Model) -> BindRetainables {
model.resultsObservable
.relayChanges(to: $allResults)
}
// more code for UITableViewDelegate and UISearchbarDelegate below
...
...
...
}
with ViewModel protocol like this:
protocol SearchScreenDataBinding {
var searchPhraseBindable: BindableObservable<String?> { get }
var resultsObservable: Observable<[Result]> { get }
}
protocol SearchScreenSubscriber {
func didTap(_ event: Result, at indexPath: IndexPath)
}
typealias SearchScreenViewModel = ViewModel & SearchScreenSubscriber & SearchScreenDataBinding
It will create a View using Draftsman and bind Model to View using Pharos. As you can see from the code above, it will bind searchBar.bindables.text with searchPhraseBindable from Model relay changes from resultsObservable to allResults.
This then will make sure that every changes coming from searchBar will be relayed to the Model and then every results changes from Model will be relayed back to the View. The results then will be observed by UITableView built-in datasource (provided by Artisan and powered by DiffableDataSource) for then used to update a cells in the UITableView.
Artisan
Artisan is an MVVM framework for Swift using the bonding features from Pharos, constraints builder from Draftsman and builder pattern from Builder.
Example
To run the example project, clone the repo, and run
pod install
from the Example directory first.Requirements
Installation
Cocoapods
Artisan is available through CocoaPods. To install it, simply add the following line to your Podfile:
Swift Package Manager from XCode
Swift Package Manager from Package.swift
Add as your target dependency in Package.swift
Use it in your target as
Artisan
Author
Nayanda Haberty, hainayanda@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 an MVVM Pattern using Artisan is easy. Binding is supported by Pharos and View building is supported by Draftsman, Artisan is the one that make both can work with each other perfectly. Like if you want to create simple Search Screen:
with ViewModel protocol like this:
It will create a View using
Draftsman
and bindModel
toView
usingPharos
. As you can see from the code above, it will bindsearchBar.bindables.text
withsearchPhraseBindable
fromModel
relay changes fromresultsObservable
toallResults
. This then will make sure that every changes coming fromsearchBar
will be relayed to theModel
and then every results changes fromModel
will be relayed back to theView
. The results then will be observed byUITableView
built-in datasource (provided by Artisan and powered by DiffableDataSource) for then used to update a cells in theUITableView
.You can create your ViewModel like this:
Then binding the
View
andModel
will be as easy like this:Don’t forget that everytime
BindableView
is bind with newViewModel
, all of its old retained Pharos relay will be released.You can clone and check the Example folder or for more wiki, go to here
Contribute
You know how, just clone and do pull request