Files
e-party-iOS/issues/组件抽离到CommonComponents重构.md
edwinQQQ 428aa95c5e feat: 更新Swift助手样式规则和应用结构
- 在swift-assistant-style.mdc中添加项目背景、代码结构、命名规范、Swift最佳实践、UI开发、性能、安全性、测试与质量、核心功能、开发流程、App Store指南等详细规则。
- 在yanaApp.swift中将SplashView替换为Splash,简化应用结构。
2025-08-06 14:12:20 +08:00

4.7 KiB
Raw Blame History

组件抽离到CommonComponents重构

重构概述

将MVVM目录中重复定义的UI组件抽离到CommonComponents.swift中,实现组件的统一管理和复用,避免代码重复。

重名组件分析

发现的重名组件

  1. IDLoginBackgroundView - 在IDLoginPage.swiftIDLoginView.swift中重复定义
  2. IDLoginHeaderView - 在IDLoginPage.swiftIDLoginView.swift中重复定义
  3. CustomInputField - 在IDLoginPage.swiftIDLoginView.swiftCommonComponents.swift中重复定义
  4. IDLoginButton/IDLoginButtonView - 在IDLoginPage.swiftIDLoginView.swift中重复定义

组件功能对比

所有重复组件功能完全相同,只是命名略有不同,适合统一管理。

重构方案

1. 组件命名统一

  • IDLoginBackgroundViewLoginBackgroundView
  • IDLoginHeaderViewLoginHeaderView
  • IDLoginButtonViewLoginButtonView
  • CustomInputField → 保持原名已在CommonComponents中

2. 文件修改列表

修改的文件

  • yana/MVVM/IDLoginPage.swift - 移除重复组件使用CommonComponents
  • yana/Views/IDLoginView.swift - 移除重复组件使用CommonComponents
  • yana/MVVM/EMailLoginPage.swift - 使用CommonComponents组件
  • yana/MVVM/RecoverPasswordPage.swift - 使用CommonComponents组件
  • yana/MVVM/LoginPage.swift - 使用CommonComponents组件
  • yana/MVVM/Splash.swift - 使用CommonComponents组件
  • yana/MVVM/MainPage.swift - 使用CommonComponents组件

保持的文件

  • yana/MVVM/CommonComponents.swift - 统一管理所有组件

重构内容

1. IDLoginPage.swift

  • 移除IDLoginBackgroundViewIDLoginHeaderViewCustomInputFieldIDLoginButton组件定义
  • 使用LoginBackgroundViewLoginHeaderViewCustomInputFieldLoginButtonView
  • 保持ViewModel和主视图逻辑不变

2. IDLoginView.swift (Views目录)

  • 移除IDLoginBackgroundViewIDLoginHeaderViewCustomInputFieldIDLoginButtonView组件定义
  • 使用LoginBackgroundViewLoginHeaderViewCustomInputFieldLoginButtonView
  • 保持TCA架构和主视图逻辑不变

3. EMailLoginPage.swift

  • 使用LoginBackgroundView替换直接使用Image("bg")
  • 使用LoginHeaderView替换内联的导航栏代码
  • 使用LoginButtonView替换内联的按钮代码
  • 使用CustomInputField替换内联的输入框代码
  • 简化了UI组件的定义提高代码复用性

4. RecoverPasswordPage.swift

  • 使用LoginBackgroundView替换直接使用Image("bg")
  • 使用LoginHeaderView替换内联的导航栏代码
  • 保持其他UI组件不变因为它们是特定的

5. LoginPage.swift

  • 使用LoginBackgroundView替换backgroundView中的Image("bg")
  • 保持其他特定组件不变

6. Splash.swift

  • 使用LoginBackgroundView替换Image("bg")
  • 保持启动画面的其他元素不变

7. MainPage.swift

  • 使用LoginBackgroundView替换Image("bg")
  • 保持底部导航栏等特定组件不变

技术要点

1. 组件接口保持兼容

  • 所有组件的参数和返回值保持不变
  • 确保现有调用代码无需修改

2. 命名规范统一

  • 使用通用的Login前缀,而不是特定的IDLogin前缀
  • 保持组件名称的语义清晰

3. 代码复用最大化

  • 背景图片、导航栏、按钮等通用组件统一管理
  • 输入框组件支持多种类型text、number、password、verificationCode

验证结果

组件定义验证

  • LoginBackgroundView - 仅在CommonComponents中定义
  • LoginHeaderView - 仅在CommonComponents中定义
  • LoginButtonView - 仅在CommonComponents中定义
  • CustomInputField - 仅在CommonComponents中定义

组件使用验证

  • 所有MVVM文件都正确使用了CommonComponents中的组件
  • 没有发现重复的组件定义
  • 组件调用接口保持一致

功能验证

  • 所有页面的UI显示正常
  • 组件交互功能正常
  • 没有引入新的编译错误

后续优化建议

  1. 组件扩展可以考虑将更多通用组件添加到CommonComponents中
  2. 主题支持:为组件添加主题支持,支持不同的颜色方案
  3. 动画支持:为组件添加统一的动画效果
  4. 无障碍支持:为组件添加无障碍标签和描述
  5. 测试覆盖为CommonComponents中的组件添加单元测试
  6. 文档完善:为每个组件添加详细的使用文档和示例

完成状态

  • 重名组件识别和分析
  • 组件抽离到CommonComponents
  • 所有MVVM文件更新完成
  • Views目录文件更新完成
  • 组件使用验证通过
  • 功能验证通过
  • 文档记录完成