Feature
Link: https://github.com/GeekTree0101/Vetty/projects/2
Usage
Make a model
class User: VettyProtocol { // <--- STEP1: Inherit VettyProtocol
var uniqueKey: VettyIdentifier {
return userId // <--- STEP2: return model uniqueKey
}
var userId: Int = -1
var username: String = ""
var profileURL: URL?
enum CodingKeys: String, CodingKey {
case userId = "id"
case username = "login"
case profileURL = "avatar_url"
}
func commitSubModelIfNeeds() { // <---- STEP3: (Optional)
// Will pass, user model doen't has VettyProtocol Sub-Model
}
}
Commit
- Directly commit model
let userIdForVetty: VettyIdentifier = Vetty.shared.commit(user)
- Using Reactive Extension
let userIdForVettyObservable: Observable<VettyIdentifier> =
Observable.just(user)
.asObservable()
.map({ $0.map({ $0 as VettyProtocol }) })
.commits(ignoreSubModel: false)
READ
Directly read model object from Vetty
let user: User? = Vetty.shared.read(type: User.self, uniqueKey: 12345)
Model Observable
let userObservable: Observable<User?> = Vetty.rx.observer(type: User.self, uniqueKey: 12345)
Mutating
- Directly Mutating
guard let user = Vetty.shared.read(type: User.self, uniqueKey: 12345) else { return }
user.profileURL = URL(string: "https://avatars1.githubusercontent.com/u/19504988?s=460&v=4")
Vetty.shared.commit(user, ignoreSubModel: true)
- Using Observable Extension
let observable: Observable<User?> = Vetty.rx.observer(type: User.self, uniqueKey: 12345)
Observable.just(URL(string: "https://avatars1.githubusercontent.com/u/19504988?s=460&v=4"))
.mutate(with: observable,
{ user, newURL -> User? in
user?.profileURL = newURL
return user
})
.disposed(by: disposeBag)
Advanced
Sub-Model Observable from Root-Model Observable
let repoObservable = Vetty.rx.observer(type: Repository.self, uniqueKey: repoId)
let userObservable = repoObservable
.filterNil()
.map { $0.user?.uniqueKey }
.asObserver(type: User.self)
Ignore Sub-Model Mutating
let observable: Observable<User?> = Vetty.rx.observer(type: User.self, uniqueKey: 12345)
Observable.just(URL(string: "https://avatars1.githubusercontent.com/u/19504988?s=460&v=4"))
.mutate(with: observable,
ignoreSubModel: true) <--- Default is true!
{ user, newURL -> User? in
user?.profileURL = newURL
return user
})
.disposed(by: disposeBag)
Non-Ignore Sub-Model with Latest Sub-Model
Model Example, Repository has User(Sub-Model) property!
class Repository: VettyProtocol {
var uniqueKey: VettyIdentifier {
return id
}
var id: Int = -1
var user: User?
var repositoryName: String?
var desc: String?
var isPrivate: Bool = false
var isForked: Bool = false
enum CodingKeys: String, CodingKey {
case id = "id"
case user = "owner"
case repositoryName = "full_name"
case desc = "description"
case isPrivate = "private"
case isForked = "fork"
}
func commitSubModelIfNeeds() {
Vetty.shared.commitIfNeeds(user)
}
}
class User: VettyProtocol {
var uniqueKey: VettyIdentifier {
return userId
}
var userId: Int = -1
var username: String = ""
var profileURL: URL?
enum CodingKeys: String, CodingKey {
case userId = "id"
case username = "login"
case profileURL = "avatar_url"
}
func commitSubModelIfNeeds() {
}
}
If you don't wanna update user model(sub-model) than you just should set ignoreSubModel as True. But, If uou should update repository model(root-model) with latest user model from vetty than you just follow under the example.
let observable: Observable<Repository?> = Vetty.rx.observer(type: Repository.self, uniqueKey: "repo-23")
Observable.just("New Repository Description")
.mutate(with: observable,
ignoreSubModel: false)
{ repo, newDesc -> Repository? in
if let userId = repo.user?.userId,
let latestUser = Vetty.shared.read(type: User.self, uniqueKey: userId) {
repo.user = latestUser
}
repo?.desc = newDesc
return repo
})
.disposed(by: disposeBag)
Installation
Vetty is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Vetty'
Author
Geektree0101, [email protected]
License
Vetty is available under the MIT license. See the LICENSE file for more info.