作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
已关注 this blog post我在我的应用程序中创建了一个 MVVM 模式。
我有以下内容 -
protocol ViewModelType {
associatedtype Dependency
associatedtype Bindings
init(dependency: Dependency, bindings: Bindings)
}
enum Attachable<VM: ViewModelType> {
case detached(VM.Dependency)
case attached(VM.Dependency, VM)
mutating func bind(_ bindings: VM.Bindings) -> VM {
switch self {
case let .detached(dependency):
let vm = VM(dependency: dependency, bindings: bindings)
self = .attached(dependency, vm)
return vm
case let .attached(dependency, _):
let vm = VM(dependency: dependency, bindings: bindings)
self = .attached(dependency, vm)
return vm
}
}
}
View Controller
final class StartViewController: BaseViewController<StartView> {
var viewModel: Attachable<StartViewModel>!
var bindings: StartViewModel.Bindings {
let viewWillAppear = rx.sentMessage(#selector(UIViewController.viewWillAppear))
.mapToVoid()
.asDriverOnErrorJustComplete()
return StartViewModel.Bindings(
checkAuthState: viewWillAppear
)
}
}
extension StartViewController: ViewModelAttaching {
func bind(viewModel: StartViewModel) -> StartViewModel {
return viewModel
}
}
View 模型
final class StartViewModel: ViewModelType {
typealias Dependency = HasAuthService
let authCheck: Driver<Void>
struct Bindings {
let checkAuthState: Driver<Void>
}
private let disposeBag = DisposeBag()
init(dependency: Dependency, bindings: Bindings) {
authCheck = bindings.checkAuthState
.flatMap {
return dependency.authSvc.checkSession()
.mapToVoid()
.asDriver(onErrorJustReturn: ())
}
}
}
这允许我按如下方式设置我的 View -
let viewController = StartViewController()
let avm: Attachable<StartViewModel> = .detached(dependencies)
let viewModel = viewController.attach(wrapper: avm)
navigationController.setViewControllers([viewController], animated: false)
这很好用,非常容易测试,我对它的工作方式很满意。然而,我有一个场景,我需要将一些额外的 Prop 传递到我的 ViewModel
中。 。
例如,userId
在个人资料场景的情况下。
此模式保留 init
方法,所以我需要一种通过 .detached(dependencies)
注入(inject)额外 Prop 的方法
类似于 .detached(dependencies, userId)
但是我不确定如何以通用方式执行此操作。
我已经尝试过 - 注意添加 Props
和props
protocol ViewModelType {
associatedtype Dependency
associatedtype Bindings
associatedtype Props
init(dependency: Dependency, bindings: Bindings, props: Props?)
}
enum Attachable<VM: ViewModelType> {
case detached(VM.Dependency, VM.Props)
case attached(VM.Dependency, VM, VM.Props)
mutating func bind(_ bindings: VM.Bindings) -> VM {
switch self {
case let .detached(dependency, props):
let vm = VM(dependency: dependency, bindings: bindings, props: props)
self = .attached(dependency, vm, props)
return vm
case let .attached(dependency, _, props):
let vm = VM(dependency: dependency, bindings: bindings, props: props)
self = .attached(dependency, vm, props)
return vm
}
}
}
但这需要我更新我的 ViewModel
与以下 -
**ViewModel**
```swift
typealias Props = <Some Prop Type>
init(dependency: Dependency, bindings: Bindings, props: Props?)
这非常有效,除非我不需要注入(inject)任何 Prop 。然后我陷入了需要为未使用的东西指定类型的困境。
我已经尝试过了 -
typealias Props = Void?
init(dependency: Dependency, bindings: Bindings, props: Props?)
然后用 -
设置我的 View let avm: Attachable<StartViewModel> = .detached(dependencies, nil)
但必须添加typealias Props = Void?
在我所有不接受任何东西的模型中,感觉不太好
如何以最具扩展性、通用的方式实现这一目标?
最佳答案
与其引入 Props
,为什么不将附加信息隐藏在 Dependency
后面呢?例如,在您的情况下,您可以:
final class ProfileViewModel: ViewModelType {
let authCheck: Driver<Void>
struct Dependency {
let authSvc: HasAuthService
let userId: String
}
struct Bindings {
let checkAuthState: Driver<Void>
}
private let disposeBag = DisposeBag()
init(dependency: Dependency, bindings: Bindings) {
authCheck = bindings.checkAuthState
.flatMap {
return dependency.authSvc.checkSession()
.mapToVoid()
.asDriver(onErrorJustReturn: ())
}
// use dependency.userId
}
}
关于swift - 使用泛型将额外的 props 注入(inject)到 View 模型中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58892197/
我是一名优秀的程序员,十分优秀!