返回顶部
f

fosmvvm-swiftui-view-generatorFOSMVVM视图生成器

Generate SwiftUI views that render FOSMVVM ViewModels. Scaffolds ViewModelView pattern with binding, loading states, and previews.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 2.0.6
安全检测
已通过
683
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

fosmvvm-swiftui-view-generator

FOSMVVM SwiftUI 视图生成器

生成渲染 FOSMVVM ViewModel 的 SwiftUI 视图。

概念基础

完整架构上下文请参阅 FOSMVVMArchitecture.md | OpenClaw 参考

在 FOSMVVM 中,视图是渲染 ViewModel 的薄渲染层

┌─────────────────────────────────────────────────────────────┐
│ ViewModelView 模式 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ViewModel (数据) ViewModelView (SwiftUI) │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ title: String │────►│ Text(vm.title) │ │
│ │ items: [Item] │────►│ ForEach(vm.items)│ │
│ │ isEnabled: Bool │────►│ .disabled(!...) │ │
│ └──────────────────┘ └──────────────────┘ │
│ │
│ 操作 (Actions) │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ submit() │◄────│ Button(action:) │ │
│ │ cancel() │◄────│ .onAppear { } │ │
│ └──────────────────┘ └──────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘

关键原则: 视图不转换或计算数据。它们只渲染 ViewModel 提供的内容。



视图-ViewModel 对齐

视图文件名应与其渲染的 ViewModel 匹配。

Sources/
{ViewModelsTarget}/
{Feature}/
{Feature}ViewModel.swift ←──┐
{Entity}CardViewModel.swift ←──┼── 相同名称

{ViewsTarget}/ │
{Feature}/ │
{Feature}View.swift ────┤ (渲染 {Feature}ViewModel)
{Entity}CardView.swift ────┘ (渲染 {Entity}CardViewModel)

这种对齐提供了:

  • - 可发现性 - 即时找到任何 ViewModel 对应的视图
  • 一致性 - 整个代码库使用相同的命名规范
  • 可维护性 - ViewModel 的更改会反映在视图位置中



核心组件

1. ViewModelView 协议

每个视图都遵循 ViewModelView 协议:

swift
public struct MyView: ViewModelView {
private let viewModel: MyViewModel

public var body: some View {
Text(viewModel.title)
}

public init(viewModel: MyViewModel) {
self.viewModel = viewModel
}
}

必需:

  • - private let viewModel: {ViewModel}
  • public init(viewModel:)
  • 遵循 ViewModelView 协议

2. 操作(可选)

交互式视图具有操作:

swift
public struct MyView: ViewModelView {
private let viewModel: MyViewModel
private let operations: MyViewModelOperations

#if DEBUG
@State private var repaintToggle = false
#endif

public var body: some View {
Button(action: performAction) {
Text(viewModel.buttonLabel)
}
#if DEBUG
.testDataTransporter(viewModelOps: operations, repaintToggle: $repaintToggle)
#endif
}

public init(viewModel: MyViewModel) {
self.viewModel = viewModel
self.operations = viewModel.operations
}

private func performAction() {
operations.performAction()
toggleRepaint()
}

private func toggleRepaint() {
#if DEBUG
repaintToggle.toggle()
#endif
}
}

当视图具有操作时:

  • - 在 init 中从 viewModel.operations 存储 operations
  • 添加 @State private var repaintToggle = false(仅 DEBUG)
  • 添加 .testDataTransporter(viewModelOps:repaintToggle:) 修饰符(仅 DEBUG)
  • 每次操作调用后调用 toggleRepaint()

3. 子视图绑定

父视图使用 .bind(appState:) 绑定子视图:

swift
public struct ParentView: ViewModelView {
@Environment(AppState.self) private var appState
private let viewModel: ParentViewModel

public var body: some View {
VStack {
Text(viewModel.title)

// 使用父视图数据的子集绑定子视图
ChildView.bind(
appState: .init(
itemId: viewModel.selectedId,
isConnected: viewModel.isConnected
)
)
}
}
}

.bind() 模式:

  • - 子视图使用 .bind(appState:) 从父视图接收数据
  • 父视图从其自身的 ViewModel 数据创建子视图的 AppState
  • 实现组合而不紧密耦合

4. 带验证的表单视图

表单使用 FormFieldView 和 Validations 环境:

swift
public struct MyFormView: ViewModelView {
@Environment(Validations.self) private var validations
@Environment(\.focusState) private var focusField
@State private var error: Error?

private let viewModel: MyFormViewModel
private let operations: MyFormViewModelOperations

public var body: some View {
Form {
FormFieldView(
fieldModel: viewModel.$email,
focusField: focusField,
fieldValidator: viewModel.validateEmail,
validations: validations
)

Button(errorBinding: $error, asyncAction: submit) {
Text(viewModel.submitButtonLabel)
}
.disabled(validations.hasError)
}
.onAsyncSubmit {
await submit()
}
.alert(
errorBinding: $error,
title: viewModel.errorTitle,
message: viewModel.errorMessage,
dismissButtonLabel: viewModel.dismissButtonLabel
)
}
}

表单模式:

  • - @Environment(Validations.self) 用于验证状态
  • 每个输入字段使用 FormFieldView
  • 异步操作使用 Button(errorBinding:asyncAction:)
  • 提交按钮使用 .disabled(validations.hasError)
  • 验证错误与一般错误分开处理

5. 预览

使用 .previewHost() 进行 SwiftUI 预览:

swift
#if DEBUG
#Preview {
MyView.previewHost(
bundle: MyAppResourceAccess.localizationBundle
)
.environment(AppState())
}

#Preview(带数据) {
MyView.previewHost(
bundle: MyAppResourceAccess.localizationBundle,
viewModel: .stub(title: 预览标题)
)
.environment(AppState())
}
#endif

视图类别

仅显示视图

仅渲染数据的视图(无用户交互):

swift
public struct InfoView: ViewModelView {
private let viewModel: InfoViewModel

public var body: some View {
VStack {
Text(viewModel.title)
Text(viewModel.description)

if viewModel.isActive {
Text(viewModel.activeStatusLabel)
}
}
}

public init(viewModel: InfoViewModel) {
self.viewModel = viewModel
}
}

特征:

  • - 无 operations 属性
  • 无 repaintToggle 或 testDataTransporter
  • 仅渲染 ViewModel 属性
  • 可能基于 ViewModel 状态进行条件渲染

交互式视图

具有用户操作的视图:

swift
public struct ActionView: ViewModelView {
@State private var error: Error?

private let viewModel: ActionViewModel
private let operations: ActionViewModelOperations

#if DEBUG
@State private var repaintToggle = false
#endif

public var body: some View {
VStack {
Button(action: performAction) {
Text(viewModel.actionLabel)
}

Button(role: .cancel, action: cancel) {
Text(viewModel.cancelLabel)
}
}
.alert(
errorBinding: $error,
title: viewModel.errorTitle,
message: viewModel.errorMessage,
dismissButtonLabel: viewModel.dismissButtonLabel
)
#if DEBUG
.testDataTransporter(viewModelOps: operations, repaintToggle: $repaintToggle)
#endif
}

public init(viewModel: ActionViewModel) {
self.viewModel = viewModel
self.operations = viewModel.operations
}

private func performAction() {
operations.performAction()
toggleRepaint()
}

private func cancel() {
operations.cancel()
toggleRepaint()
}

private func toggleRepaint() {
#if

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 fosmvvm-swiftui-view-generator-1776420040 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 fosmvvm-swiftui-view-generator-1776420040 技能

通过命令行安装

skillhub install fosmvvm-swiftui-view-generator-1776420040

下载

⬇ 下载 fosmvvm-swiftui-view-generator v2.0.6(免费)

文件大小: 11.91 KB | 发布时间: 2026-4-17 19:55

v2.0.6 最新 2026-4-17 19:55
Initial ClawHub release

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部