FOSMVVM SwiftUI App Setup
Generate the main App struct for a SwiftUI application using FOSMVVM architecture.
Conceptual Foundation
For full architecture context, see FOSMVVMArchitecture.md | OpenClaw reference
The App struct is the entry point of a SwiftUI application. In FOSMVVM, it has three core responsibilities:
CODEBLOCK0
Core Components
1. MVVMEnvironment
The MVVMEnvironment provides FOSMVVM infrastructure to all views:
CODEBLOCK1
Key configuration:
- -
appBundle - Usually Bundle.main (the app bundle) - INLINECODE3 - Array of localization bundles from your modules
- INLINECODE4 - URLs for each deployment environment
Resource Bundle Accessors:
Each module that contains localization resources should provide a bundle accessor:
CODEBLOCK2
This pattern:
- - Uses
Bundle.module which SPM automatically provides for each module - Provides a clean public API for accessing the module's resources
- Keeps bundle access centralized in one place per module
2. Environment Injection
The MVVMEnvironment is injected at the WindowGroup level:
CODEBLOCK3
This makes the environment available to all views in the hierarchy.
3. Test Infrastructure
The test infrastructure enables UI testing with specific configurations:
.testHost { } modifier:
CODEBLOCK4
Key points:
- - Apply to the top-level view in WindowGroup (the outermost view in your hierarchy)
- This ensures the modifier wraps the entire view hierarchy to intercept test configurations
- Always include the
default: case - The default case detects test mode via process arguments
- Sets
@State private var underTest = false flag - Optional: Add specific test configurations for advanced scenarios
registerTestingViews() function:
CODEBLOCK5
Key points:
- - Extension on the App struct (not MVVMEnvironment)
- Called from INLINECODE11
- Registers every ViewModelView for isolated testing
- DEBUG only
When to Use This Skill
- - Starting a new FOSMVVM SwiftUI application
- Migrating an existing SwiftUI app to FOSMVVM
- Setting up the App struct with proper FOSMVVM infrastructure
- Configuring test infrastructure for UI testing
What This Skill Generates
| Component | Location | Purpose |
|---|
| Main App struct | INLINECODE12 | Entry point with MVVMEnvironment setup |
| MVVMEnvironment configuration |
Computed property in App struct | Bundles and deployment URLs |
| Test infrastructure | DEBUG blocks in App struct | UI testing support |
Project Structure Configuration
| Placeholder | Description | Example |
|---|
| INLINECODE13 | Your app name | INLINECODE14 , INLINECODE15 |
| INLINECODE16 |
Main app target |
App |
|
{ResourceBundles} | Module names with localization |
MyAppViewModels,
SharedResources |
How to Use This Skill
Invocation:
/fosmvvm-swiftui-app-setup
Prerequisites:
- - App name understood from conversation context
- Deployment URLs discussed or documented
- Resource bundles identified (modules with localization)
- Test support requirements clarified
Workflow integration:
This skill is used when setting up a new FOSMVVM SwiftUI application or adding FOSMVVM infrastructure to an existing app. The skill references conversation context automatically—no file paths or Q&A needed.
Pattern Implementation
This skill references conversation context to determine App struct configuration:
Configuration Detection
From conversation context, the skill identifies:
- - App name (from project discussion or existing code)
- Deployment environments (production, staging, debug URLs)
- Resource bundles (modules containing localization YAML files)
- Test infrastructure (whether UI testing support needed)
MVVMEnvironment Setup
Based on project structure:
- - App bundle (typically Bundle.main)
- Resource bundle accessors (from identified modules)
- Deployment URLs (for each environment)
- Current version (from shared module)
Test Infrastructure Planning
If test support needed:
- - Test detection (process arguments check)
- Test host modifier (wrapping top-level view)
- View registration (all ViewModelViews for testing)
File Generation
- 1. Main App struct with @main attribute
- MVVMEnvironment computed property
- WindowGroup with environment injection
- Test infrastructure (if requested, DEBUG-only)
- registerTestingViews() extension (if test support)
Context Sources
Skill references information from:
- - Prior conversation: App requirements, deployment environments discussed
- Project structure: From codebase analysis of module organization
- Existing patterns: From other FOSMVVM apps if context available
Key Patterns
MVVMEnvironment as Computed Property
The MVVMEnvironment is a computed property, not a stored property:
CODEBLOCK6
Why computed?
- - Keeps initialization logic separate
- Can be customized in DEBUG vs RELEASE
- Clear dependency on bundles and URLs
Test Detection Pattern
The default test detection uses process arguments:
CODEBLOCK7
Why this approach?
- - Simple and reliable for DEBUG builds
- No additional dependencies
- Process arguments are set by test runner
Register All ViewModelViews
Every ViewModelView should be registered for testing:
CODEBLOCK8
Organization tips:
- - Group by feature/screen with comments
- Alphabetical order within groups
- One view per line for easy scanning
Common Customizations
Multiple Environment Values
You can inject multiple environment values:
CODEBLOCK9
Conditional Test Registration
You can conditionally register views based on build configuration:
CODEBLOCK10
Advanced Test Configurations
You can add specific test configurations in .testHost:
CODEBLOCK11
File Templates
See reference.md for complete file templates.
Naming Conventions
| Concept | Convention | Example |
|---|
| App struct | INLINECODE23 | INLINECODE24 , INLINECODE25 |
| Main file |
{Name}App.swift |
MyApp.swift |
| MVVMEnvironment property |
mvvmEnv | Always
mvvmEnv |
| Test flag |
underTest | Always
underTest |
Deployment Configuration
FOSMVVM supports deployment detection via Info.plist:
CODEBLOCK12
Local development override:
- - Edit Scheme → Run → Arguments → Environment Variables
- Add: INLINECODE32
See Also
Version History
| Version | Date | Changes |
|---|
| 1.0 | 2026-01-23 | Initial skill for SwiftUI app setup |
| 1.1 |
2026-01-24 | Update to context-aware approach (remove file-parsing/Q&A). Skill references conversation context instead of asking questions or accepting file paths. |
FOSMVVM SwiftUI 应用设置
使用 FOSMVVM 架构生成 SwiftUI 应用程序的主 App 结构体。
概念基础
完整架构上下文,请参阅 FOSMVVMArchitecture.md | OpenClaw 参考
App 结构体是 SwiftUI 应用程序的入口点。在 FOSMVVM 中,它有三个核心职责:
┌─────────────────────────────────────────────────────────────┐
│ @main App 结构体 │
├─────────────────────────────────────────────────────────────┤
│ 1. MVVMEnvironment 设置 │
│ - 资源包(应用 + 本地化资源) │
│ - 部署 URL(生产、预发布、调试) │
│ │
│ 2. 环境注入 │
│ - 在 WindowGroup 上使用 .environment(mvvmEnv) │
│ - 自定义环境值 │
│ │
│ 3. 测试基础设施(仅 DEBUG 模式) │
│ - 用于 UI 测试的 .testHost { } 修饰器 │
│ - 用于单个视图测试的 registerTestingViews() │
└─────────────────────────────────────────────────────────────┘
核心组件
1. MVVMEnvironment
MVVMEnvironment 为所有视图提供 FOSMVVM 基础设施:
swift
private var mvvmEnv: MVVMEnvironment {
MVVMEnvironment(
appBundle: Bundle.main,
resourceBundles: [
MyAppViewModelsResourceAccess.localizationBundle,
SharedResourceAccess.localizationBundle
],
deploymentURLs: [
.production: .init(serverBaseURL: URL(string: https://api.example.com)!),
.debug: .init(serverBaseURL: URL(string: http://localhost:8080)!)
]
)
}
关键配置:
- - appBundle - 通常是 Bundle.main(应用包)
- resourceBundles - 来自模块的本地化包数组
- deploymentURLs - 每个部署环境的 URL
资源包访问器:
每个包含本地化资源的模块应提供一个包访问器:
swift
// 在您的 ViewModels 模块中(例如,MyAppViewModels/ResourceAccess.swift)
public enum MyAppViewModelsResourceAccess {
public static var localizationBundle: Bundle { Bundle.module }
}
此模式:
- - 使用 Bundle.module,SPM 自动为每个模块提供
- 提供清晰的公共 API 来访问模块的资源
- 将包访问集中到每个模块的一个位置
2. 环境注入
MVVMEnvironment 在 WindowGroup 级别注入:
swift
var body: some Scene {
WindowGroup {
MyView()
}
.environment(mvvmEnv) // ← 使 FOSMVVM 基础设施可用
}
这使得环境对层级中的所有视图都可用。
3. 测试基础设施
测试基础设施支持使用特定配置进行 UI 测试:
.testHost { } 修饰器:
swift
var body: some Scene {
WindowGroup {
ZStack {
LandingPageView()
}
#if DEBUG
.testHost { testConfiguration, testView in
// 处理特定测试配置...
default:
testView
.onAppear {
underTest = ProcessInfo.processInfo.arguments.count > 1
}
}
#endif
}
}
关键点:
- - 应用于 WindowGroup 中的顶级视图(层级中最外层的视图)
- 这确保修饰器包裹整个视图层级以拦截测试配置
- 始终包含 default: 分支
- 默认分支通过进程参数检测测试模式
- 设置 @State private var underTest = false 标志
- 可选:为高级场景添加特定测试配置
registerTestingViews() 函数:
swift
#if DEBUG
private extension MyApp {
@MainActor func registerTestingViews() {
mvvmEnv.registerTestView(LandingPageView.self)
mvvmEnv.registerTestView(SettingsView.self)
// ... 注册所有 ViewModelView 以进行单独测试
}
}
#endif
关键点:
- - 在 App 结构体(而非 MVVMEnvironment)上的扩展
- 从 init() 调用
- 注册每个 ViewModelView 以进行隔离测试
- 仅 DEBUG 模式
何时使用此技能
- - 启动新的 FOSMVVM SwiftUI 应用程序
- 将现有 SwiftUI 应用迁移到 FOSMVVM
- 使用正确的 FOSMVVM 基础设施设置 App 结构体
- 为 UI 测试配置测试基础设施
此技能生成的内容
| 组件 | 位置 | 目的 |
|---|
| 主 App 结构体 | Sources/App/{AppName}.swift | 带有 MVVMEnvironment 设置的入口点 |
| MVVMEnvironment 配置 |
App 结构体中的计算属性 | 包和部署 URL |
| 测试基础设施 | App 结构体中的 DEBUG 块 | UI 测试支持 |
项目结构配置
| 占位符 | 描述 | 示例 |
|---|
| {AppName} | 您的应用名称 | MyApp, AccelApp |
| {AppTarget} |
主应用目标 | App |
| {ResourceBundles} | 包含本地化的模块名称 | MyAppViewModels, SharedResources |
如何使用此技能
调用:
/fosmvvm-swiftui-app-setup
前提条件:
- - 从对话上下文中理解应用名称
- 已讨论或记录部署 URL
- 已识别资源包(包含本地化的模块)
- 已明确测试支持需求
工作流集成:
此技能在设置新的 FOSMVVM SwiftUI 应用程序或向现有应用添加 FOSMVVM 基础设施时使用。该技能自动引用对话上下文——无需文件路径或问答。
模式实现
此技能引用对话上下文来确定 App 结构体配置:
配置检测
从对话上下文中,技能识别:
- - 应用名称(来自项目讨论或现有代码)
- 部署环境(生产、预发布、调试 URL)
- 资源包(包含本地化 YAML 文件的模块)
- 测试基础设施(是否需要 UI 测试支持)
MVVMEnvironment 设置
基于项目结构:
- - 应用包(通常是 Bundle.main)
- 资源包访问器(来自已识别模块)
- 部署 URL(每个环境)
- 当前版本(来自共享模块)
测试基础设施规划
如果需要测试支持:
- - 测试检测(进程参数检查)
- 测试宿主修饰器(包裹顶级视图)
- 视图注册(所有 ViewModelView 用于测试)
文件生成
- 1. 带有 @main 属性的主 App 结构体
- MVVMEnvironment 计算属性
- 带有环境注入的 WindowGroup
- 测试基础设施(如果请求,仅 DEBUG 模式)
- registerTestingViews() 扩展(如果支持测试)
上下文来源
技能引用的信息来自:
- - 先前对话:讨论的应用需求、部署环境
- 项目结构:来自模块组织的代码库分析
- 现有模式:来自其他 FOSMVVM 应用(如果上下文可用)
关键模式
MVVMEnvironment 作为计算属性
MVVMEnvironment 是计算属性,而非存储属性:
swift
private var mvvmEnv: MVVMEnvironment {
MVVMEnvironment(
appBundle: Bundle.main,
resourceBundles: [...],
deploymentURLs: [...]
)
}
为什么是计算属性?
- - 保持初始化逻辑分离
- 可在 DEBUG 与 RELEASE 模式下自定义
- 对包和 URL 的清晰依赖
测试检测模式
默认测试检测使用进程参数:
swift
@State private var underTest = false
// 在 .testHost 默认分支中:
testView
.onAppear {
// 目前没有其他方法检测应用是否在测试中。
// 这只是调试代码,所以我们可以暂时继续。
underTest = ProcessInfo.processInfo.arguments.count > 1
}
为什么采用这种方法?
- - 对于 DEBUG 构建简单可靠
- 无需额外依赖
- 进程参数由测试运行器设置
注册所有 ViewModelView
每个 ViewModelView 都应注册用于测试:
swift
@MainActor func registerTestingViews() {
// 登录页面
mvvmEnv.registerTestView(LandingPageView.self)
// 设置
mvvmEnv.registerTestView(SettingsView.self)
mvvmEnv.registerTestView(ProfileView.self)
// 仪表盘
mvvm