Vetty 1.1.0

Vetty 1.1.0

Geektree0101维护。



 
依赖
RxSwift~> 4.0
RxCocoa~> 4.0
 

Vetty 1.1.0

alt text

CI Status Version License Platform

功能

链接:https://github.com/GeekTree0101/Vetty/projects/2




使用方法


创建一个模型


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
    }
}



提交


  1. 直接提交模型
let userIdForVetty: VettyIdentifier = Vetty.shared.commit(user)
  1. 使用响应式扩展
let userIdForVettyObservable: Observable<VettyIdentifier> =
  Observable.just(user)
            .asObservable()
            .map({ $0.map({ $0 as VettyProtocol }) })
            .commits(ignoreSubModel: false)



读取


直接从Vetty读取模型对象

let user: User? = Vetty.shared.read(type: User.self, uniqueKey: 12345)



模型可观察


let userObservable: Observable<User?> = Vetty.rx.observer(type: User.self, uniqueKey: 12345)



修改


  1. 直接修改
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)



  1. 使用可观察扩展
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)



高级



从根模型可观察获取子模型可观察


let repoObservable = Vetty.rx.observer(type: Repository.self, uniqueKey: repoId)
        
let userObservable = repoObservable
            .filterNil()
            .map { $0.user?.uniqueKey }
            .asObserver(type: User.self)



忽略子模型修改


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)



非忽略子模型与最新子模型


模型示例,仓库有用户(子模型)属性!

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() {
        
    }
}

如果您不想更新用户模型(子模型),只需将 ignoreSubModel 设置为 True。但是,如果您需要从 vetty 更新仓库模型(根模型)为最新的用户模型,请按照以下示例操作。


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)



安装

VeTTY 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中

pod 'Vetty'

作者

Geektree0101, [email protected]

许可证

VeTTY 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。