feat: 优化自动修图设置与模板网格交互

同页切换修图方式并展开三列模板,复用模板卡片与对比预览,保留草稿和滚动位置,统一卡片与固定底栏样式并补充回归测试。
This commit is contained in:
2026-08-27 17:15:50 +08:00
parent d04641b623
commit 396597a160
6 changed files with 538 additions and 259 deletions
@@ -2,15 +2,9 @@ import Foundation
/// 自动修图设置状态,负责加载真实精修模板、单选和配置校验,不直接创建修图任务。 /// 自动修图设置状态,负责加载真实精修模板、单选和配置校验,不直接创建修图任务。
final class TravelAlbumAutoRetouchSettingViewModel { final class TravelAlbumAutoRetouchSettingViewModel {
/// 设置页当前层级。
enum Stage: Sendable, Equatable {
case mode
case templates
}
let scenicId: Int let scenicId: Int
let startsWithModeSelection: Bool /// 是否在模板上方展示修图方式;创建页已选择 AI 时只展示模板。
private(set) var stage: Stage let allowsModeSelection: Bool
private(set) var templates: [TravelAlbumAIRetouchTemplate] = [] private(set) var templates: [TravelAlbumAIRetouchTemplate] = []
private(set) var selectedTemplateId: Int? private(set) var selectedTemplateId: Int?
private(set) var isEnabled: Bool private(set) var isEnabled: Bool
@@ -23,15 +17,24 @@ final class TravelAlbumAutoRetouchSettingViewModel {
init( init(
scenicId: Int, scenicId: Int,
configuration: TravelAlbumAutoRetouchConfiguration, configuration: TravelAlbumAutoRetouchConfiguration,
startsWithModeSelection: Bool allowsModeSelection: Bool
) { ) {
self.scenicId = scenicId self.scenicId = scenicId
self.startsWithModeSelection = startsWithModeSelection self.allowsModeSelection = allowsModeSelection
self.stage = startsWithModeSelection ? .mode : .templates self.isEnabled = configuration.enabled || !allowsModeSelection
self.isEnabled = configuration.enabled
self.selectedTemplateId = configuration.refinedTemplateId self.selectedTemplateId = configuration.refinedTemplateId
} }
/// 关闭自动修图不依赖模板加载;开启时必须选择有效模板。
var canConfirm: Bool {
!isEnabled || (!isLoading && pendingConfiguration != nil)
}
/// 当前草稿模板名称,用于固定底部的选择摘要。
var selectedTemplateName: String? {
templates.first { $0.id == selectedTemplateId }?.name
}
/// 当前可提交配置;启用状态必须已经选择服务端模板。 /// 当前可提交配置;启用状态必须已经选择服务端模板。
var pendingConfiguration: TravelAlbumAutoRetouchConfiguration? { var pendingConfiguration: TravelAlbumAutoRetouchConfiguration? {
if !isEnabled { return .disabled } if !isEnabled { return .disabled }
@@ -75,14 +78,10 @@ final class TravelAlbumAutoRetouchSettingViewModel {
} }
} }
/// 选择一级修图方式。 /// 切换修图方式,保留本次草稿模板以便再次开启;关闭配置提交时仍不携带模板。
func selectMode(enabled: Bool) { func selectMode(enabled: Bool) {
guard allowsModeSelection else { return }
isEnabled = enabled isEnabled = enabled
if enabled {
stage = .templates
} else {
selectedTemplateId = nil
}
notify() notify()
} }
@@ -94,12 +93,5 @@ final class TravelAlbumAutoRetouchSettingViewModel {
notify() notify()
} }
/// 从模板列表返回修图方式层级。
func returnToModeSelection() {
guard startsWithModeSelection else { return }
stage = .mode
notify()
}
private func notify() { onStateChange?() } private func notify() { onStateChange?() }
} }
@@ -261,7 +261,7 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
let settingViewModel = TravelAlbumAutoRetouchSettingViewModel( let settingViewModel = TravelAlbumAutoRetouchSettingViewModel(
scenicId: AppStore.shared.session.currentScenicId, scenicId: AppStore.shared.session.currentScenicId,
configuration: autoRetouchConfiguration, configuration: autoRetouchConfiguration,
startsWithModeSelection: false allowsModeSelection: false
) )
let controller = TravelAlbumAutoRetouchSettingSheetViewController( let controller = TravelAlbumAutoRetouchSettingSheetViewController(
viewModel: settingViewModel, viewModel: settingViewModel,
@@ -1,44 +1,45 @@
import Kingfisher
import SnapKit import SnapKit
import UIKit import UIKit
/// 相册自动 AI 修图设置 Sheet,使用服务端精修模板并保持单选。 /// 相册自动修图设置:在同一页切换修图方式、展开模板并确认草稿。
final class TravelAlbumAutoRetouchSettingSheetViewController: BaseViewController { final class TravelAlbumAutoRetouchSettingSheetViewController: BaseViewController {
private enum Item: Hashable { /// 确认草稿后通知调用方保存相册配置。
case mode(Bool)
case template(TravelAlbumAIRetouchTemplate)
}
var onConfirm: ((TravelAlbumAutoRetouchConfiguration) -> Void)? var onConfirm: ((TravelAlbumAutoRetouchConfiguration) -> Void)?
private let viewModel: TravelAlbumAutoRetouchSettingViewModel private let viewModel: TravelAlbumAutoRetouchSettingViewModel
private let api: any TravelAlbumServing private let api: any TravelAlbumServing
private let titleLabel = UILabel() private let titleLabel = UILabel()
private let subtitleLabel = UILabel() private let subtitleLabel = UILabel()
private let backButton = UIButton(type: .system) private let headerStack = UIStackView()
private let tableView = UITableView(frame: .zero, style: .plain) private let modeStack = UIStackView()
private var dataSource: UITableViewDiffableDataSource<Int, Item>! private let originalOption = AutoRetouchModeControl(title: "不修图", detail: "保留相机原始照片", symbol: "photo")
private let statusContainer = UIView() private let aiOption = AutoRetouchModeControl(title: "AI 修图", detail: "上传后自动套用模板", symbol: "wand.and.stars")
private let contentRegion = UIView()
private let templateHeader = UIStackView()
private lazy var templateCollectionView = UICollectionView(frame: .zero, collectionViewLayout: makeTemplateLayout())
private var templateDataSource: UICollectionViewDiffableDataSource<Int, TravelAlbumAIRetouchTemplate>!
private let originalHint = UIStackView()
private let statusStack = UIStackView()
private let activityIndicator = UIActivityIndicatorView(style: .medium) private let activityIndicator = UIActivityIndicatorView(style: .medium)
private let statusLabel = UILabel() private let statusLabel = UILabel()
private let retryButton = UIButton(type: .system) private let retryButton = UIButton(type: .system)
private let footer = UIView()
private let selectionLabel = UILabel()
private let cancelButton = UIButton(type: .system) private let cancelButton = UIButton(type: .system)
private let confirmButton = UIButton(type: .system) private let confirmButton = UIButton(type: .system)
private var lastTemplatesVisible: Bool?
/// 创建设置 Sheet。 /// 创建设置 Sheet;方式选择与模板区域共用同一份配置草稿。
init( init(viewModel: TravelAlbumAutoRetouchSettingViewModel, api: any TravelAlbumServing) {
viewModel: TravelAlbumAutoRetouchSettingViewModel,
api: any TravelAlbumServing
) {
self.viewModel = viewModel self.viewModel = viewModel
self.api = api self.api = api
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
modalPresentationStyle = .pageSheet modalPresentationStyle = .pageSheet
if let sheetPresentationController { if let sheet = sheetPresentationController {
sheetPresentationController.detents = [.large()] sheet.detents = [.large()]
sheetPresentationController.selectedDetentIdentifier = .large sheet.selectedDetentIdentifier = .large
sheetPresentationController.prefersGrabberVisible = true sheet.prefersGrabberVisible = true
sheetPresentationController.preferredCornerRadius = 22 sheet.preferredCornerRadius = 24
} }
} }
@@ -46,100 +47,167 @@ final class TravelAlbumAutoRetouchSettingSheetViewController: BaseViewController
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func setupUI() { override func setupUI() {
view.backgroundColor = .white view.backgroundColor = UIColor(hex: 0xF6F8FC)
titleLabel.font = .systemFont(ofSize: 20, weight: .semibold) titleLabel.text = viewModel.allowsModeSelection ? "选择修图方式" : "选择修图模板"
titleLabel.font = .systemFont(ofSize: 22, weight: .bold)
titleLabel.textColor = AppColor.textPrimary titleLabel.textColor = AppColor.textPrimary
titleLabel.textAlignment = .center titleLabel.accessibilityTraits = .header
subtitleLabel.text = "设置仅对后续上传的照片生效"
subtitleLabel.font = .systemFont(ofSize: 13) subtitleLabel.font = .systemFont(ofSize: 13)
subtitleLabel.textColor = AppColor.textSecondary subtitleLabel.textColor = AppColor.textSecondary
subtitleLabel.textAlignment = .center headerStack.axis = .vertical
subtitleLabel.numberOfLines = 0 headerStack.spacing = 8
headerStack.addArrangedSubview(titleLabel)
headerStack.addArrangedSubview(subtitleLabel)
headerStack.setCustomSpacing(20, after: subtitleLabel)
modeStack.axis = .horizontal
modeStack.distribution = .fillEqually
modeStack.spacing = 12
modeStack.accessibilityIdentifier = "travelAlbum.autoRetouchModeOptions"
originalOption.accessibilityIdentifier = "travelAlbum.autoRetouchOriginalOption"
aiOption.accessibilityIdentifier = "travelAlbum.autoRetouchAIOption"
modeStack.addArrangedSubview(originalOption)
modeStack.addArrangedSubview(aiOption)
headerStack.addArrangedSubview(modeStack)
modeStack.isHidden = !viewModel.allowsModeSelection
backButton.setImage(UIImage(systemName: "chevron.left"), for: .normal) let templateTitle = UILabel()
backButton.tintColor = AppColor.textPrimary templateTitle.text = viewModel.allowsModeSelection ? "选择修图模板" : "精修模板"
backButton.accessibilityLabel = "返回修图方式" templateTitle.font = .systemFont(ofSize: 17, weight: .semibold)
templateTitle.textColor = AppColor.textPrimary
templateTitle.accessibilityTraits = .header
let templateTips = UILabel()
templateTips.text = "单选模板 · 点击预览查看前后效果"
templateTips.font = .systemFont(ofSize: 12)
templateTips.textColor = AppColor.textSecondary
templateHeader.axis = .vertical
templateHeader.spacing = 5
templateHeader.addArrangedSubview(templateTitle)
templateHeader.addArrangedSubview(templateTips)
tableView.backgroundColor = .white templateCollectionView.backgroundColor = .clear
tableView.separatorStyle = .none templateCollectionView.alwaysBounceVertical = true
tableView.rowHeight = 116 templateCollectionView.accessibilityIdentifier = "travelAlbum.autoRetouchTemplateCollection"
tableView.delegate = self templateCollectionView.delegate = self
tableView.register(AutoRetouchOptionCell.self, forCellReuseIdentifier: AutoRetouchOptionCell.reuseIdentifier) templateCollectionView.register(TravelAlbumAIRetouchTemplateCell.self,
forCellWithReuseIdentifier: TravelAlbumAIRetouchTemplateCell.reuseIdentifier)
configureDataSource() configureDataSource()
let hintIcon = UIImageView(image: UIImage(systemName: "photo.on.rectangle.angled"))
hintIcon.tintColor = AppColor.primary.withAlphaComponent(0.6)
hintIcon.contentMode = .scaleAspectFit
let hintTitle = UILabel()
hintTitle.text = "保留原图,直接上传"
hintTitle.font = .systemFont(ofSize: 17, weight: .semibold)
hintTitle.textColor = AppColor.textPrimary
let hintDetail = UILabel()
hintDetail.text = "选择上方 AI 修图,即可在这里挑选模板"
hintDetail.font = .systemFont(ofSize: 13)
hintDetail.textColor = AppColor.textSecondary
hintDetail.numberOfLines = 2
hintDetail.textAlignment = .center
originalHint.axis = .vertical
originalHint.alignment = .center
originalHint.spacing = 12
originalHint.accessibilityIdentifier = "travelAlbum.autoRetouchOriginalHint"
originalHint.addArrangedSubview(hintIcon)
originalHint.addArrangedSubview(hintTitle)
originalHint.addArrangedSubview(hintDetail)
hintIcon.snp.makeConstraints { $0.size.equalTo(48) }
statusStack.axis = .vertical
statusStack.alignment = .center
statusStack.spacing = 12
statusStack.addArrangedSubview(activityIndicator)
statusStack.addArrangedSubview(statusLabel)
statusStack.addArrangedSubview(retryButton)
activityIndicator.color = AppColor.primary
statusLabel.font = .systemFont(ofSize: 14) statusLabel.font = .systemFont(ofSize: 14)
statusLabel.textColor = AppColor.textSecondary statusLabel.textColor = AppColor.textSecondary
statusLabel.textAlignment = .center statusLabel.textAlignment = .center
statusLabel.numberOfLines = 0 statusLabel.numberOfLines = 0
retryButton.setTitle("重试", for: .normal) retryButton.setTitle("重新加载", for: .normal)
retryButton.titleLabel?.font = .systemFont(ofSize: 15, weight: .semibold) retryButton.tintColor = AppColor.primary
activityIndicator.color = AppColor.primary retryButton.titleLabel?.font = .systemFont(ofSize: 14, weight: .semibold)
retryButton.snp.makeConstraints { $0.height.equalTo(44) }
footer.backgroundColor = .white
footer.layer.shadowColor = UIColor(hex: 0x182B49).cgColor
footer.layer.shadowOpacity = 0.04
footer.layer.shadowRadius = 12
footer.layer.shadowOffset = CGSize(width: 0, height: -3)
selectionLabel.font = .systemFont(ofSize: 13, weight: .medium)
selectionLabel.textColor = AppColor.textSecondary
selectionLabel.lineBreakMode = .byTruncatingTail
selectionLabel.accessibilityIdentifier = "travelAlbum.autoRetouchSelectionSummary"
configureAction(cancelButton, title: "取消", filled: false) configureAction(cancelButton, title: "取消", filled: false)
configureAction(confirmButton, title: "确定", filled: true) configureAction(confirmButton, title: "确定", filled: true)
confirmButton.accessibilityIdentifier = "travelAlbum.autoRetouchConfirmButton" confirmButton.accessibilityIdentifier = "travelAlbum.autoRetouchConfirmButton"
view.addSubview(titleLabel) view.addSubview(headerStack)
view.addSubview(subtitleLabel) view.addSubview(contentRegion)
view.addSubview(backButton) contentRegion.addSubview(templateHeader)
view.addSubview(tableView) contentRegion.addSubview(templateCollectionView)
view.addSubview(statusContainer) contentRegion.addSubview(originalHint)
statusContainer.addSubview(activityIndicator) contentRegion.addSubview(statusStack)
statusContainer.addSubview(statusLabel) view.addSubview(footer)
statusContainer.addSubview(retryButton) footer.addSubview(selectionLabel)
view.addSubview(cancelButton) footer.addSubview(cancelButton)
view.addSubview(confirmButton) footer.addSubview(confirmButton)
} }
override func setupConstraints() { override func setupConstraints() {
titleLabel.snp.makeConstraints { make in headerStack.snp.makeConstraints { make in
make.top.equalTo(view.safeAreaLayoutGuide).offset(12) make.top.equalTo(view.safeAreaLayoutGuide).offset(16)
make.leading.trailing.equalToSuperview().inset(56) make.leading.trailing.equalToSuperview().inset(16)
} }
subtitleLabel.snp.makeConstraints { make in contentRegion.snp.makeConstraints { make in
make.top.equalTo(titleLabel.snp.bottom).offset(8) make.top.equalTo(headerStack.snp.bottom).offset(22)
make.leading.trailing.equalToSuperview().inset(28) make.leading.trailing.equalToSuperview()
make.bottom.equalTo(footer.snp.top).offset(-8)
} }
backButton.snp.makeConstraints { make in templateHeader.snp.makeConstraints { make in
make.leading.equalToSuperview().offset(18) make.top.equalToSuperview()
make.centerY.equalTo(titleLabel) make.leading.trailing.equalToSuperview().inset(16)
make.size.equalTo(36) }
templateCollectionView.snp.makeConstraints { make in
make.top.equalTo(templateHeader.snp.bottom).offset(12)
make.leading.trailing.bottom.equalToSuperview()
}
originalHint.snp.makeConstraints { make in
make.centerY.equalToSuperview().offset(-12)
make.leading.trailing.equalToSuperview().inset(24)
}
statusStack.snp.makeConstraints { make in
make.center.equalTo(templateCollectionView)
make.leading.trailing.equalToSuperview().inset(32)
}
footer.snp.makeConstraints { make in
make.leading.trailing.bottom.equalToSuperview()
}
selectionLabel.snp.makeConstraints { make in
make.top.equalToSuperview().offset(14)
make.leading.trailing.equalToSuperview().inset(16)
} }
cancelButton.snp.makeConstraints { make in cancelButton.snp.makeConstraints { make in
make.top.equalTo(selectionLabel.snp.bottom).offset(12)
make.leading.equalToSuperview().offset(16) make.leading.equalToSuperview().offset(16)
make.height.equalTo(50)
make.width.equalTo(96)
make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-12) make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-12)
make.height.equalTo(52)
make.width.equalTo(confirmButton)
} }
confirmButton.snp.makeConstraints { make in confirmButton.snp.makeConstraints { make in
make.leading.equalTo(cancelButton.snp.trailing).offset(12) make.leading.equalTo(cancelButton.snp.trailing).offset(12)
make.trailing.equalToSuperview().offset(-16) make.trailing.equalToSuperview().offset(-16)
make.top.bottom.width.equalTo(cancelButton) make.top.bottom.equalTo(cancelButton)
}
tableView.snp.makeConstraints { make in
make.top.equalTo(subtitleLabel.snp.bottom).offset(14)
make.leading.trailing.equalToSuperview()
make.bottom.equalTo(cancelButton.snp.top).offset(-12)
}
statusContainer.snp.makeConstraints { $0.edges.equalTo(tableView) }
activityIndicator.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.centerY.equalToSuperview().offset(-30)
}
statusLabel.snp.makeConstraints { make in
make.top.equalTo(activityIndicator.snp.bottom).offset(12)
make.leading.trailing.equalToSuperview().inset(36)
}
retryButton.snp.makeConstraints { make in
make.top.equalTo(statusLabel.snp.bottom).offset(10)
make.centerX.equalToSuperview()
make.height.equalTo(36)
} }
} }
override func bindActions() { override func bindActions() {
originalOption.addTarget(self, action: #selector(originalTapped), for: .touchUpInside)
aiOption.addTarget(self, action: #selector(aiTapped), for: .touchUpInside)
cancelButton.addTarget(self, action: #selector(cancelTapped), for: .touchUpInside) cancelButton.addTarget(self, action: #selector(cancelTapped), for: .touchUpInside)
confirmButton.addTarget(self, action: #selector(confirmTapped), for: .touchUpInside) confirmButton.addTarget(self, action: #selector(confirmTapped), for: .touchUpInside)
backButton.addTarget(self, action: #selector(backTapped), for: .touchUpInside)
retryButton.addTarget(self, action: #selector(retryTapped), for: .touchUpInside) retryButton.addTarget(self, action: #selector(retryTapped), for: .touchUpInside)
viewModel.onStateChange = { [weak self] in viewModel.onStateChange = { [weak self] in
Task { @MainActor in self?.applyViewModel() } Task { @MainActor in self?.applyViewModel() }
@@ -153,205 +221,172 @@ final class TravelAlbumAutoRetouchSettingSheetViewController: BaseViewController
} }
private func configureDataSource() { private func configureDataSource() {
dataSource = UITableViewDiffableDataSource<Int, Item>(tableView: tableView) { [weak self] tableView, indexPath, item in templateDataSource = UICollectionViewDiffableDataSource<Int, TravelAlbumAIRetouchTemplate>(
collectionView: templateCollectionView
) { [weak self] collectionView, indexPath, template in
guard let self else { return nil } guard let self else { return nil }
let cell = tableView.dequeueReusableCell( let cell = collectionView.dequeueReusableCell(
withIdentifier: AutoRetouchOptionCell.reuseIdentifier, withReuseIdentifier: TravelAlbumAIRetouchTemplateCell.reuseIdentifier, for: indexPath
for: indexPath ) as! TravelAlbumAIRetouchTemplateCell
) as! AutoRetouchOptionCell cell.apply(template: template, selected: self.viewModel.selectedTemplateId == template.id)
switch item { cell.onPreviewTapped = { [weak self] in self?.showPreview(for: template) }
case .mode(let enabled):
cell.apply(
title: enabled ? "AI 修图" : "不修图",
detail: enabled ? "选择后需再选一个修图模板" : "保留原图",
imageURL: nil,
systemImage: enabled ? "wand.and.stars" : "photo",
selected: self.viewModel.isEnabled == enabled
)
case .template(let template):
cell.apply(
title: template.name,
detail: "",
imageURL: template.previewURL,
systemImage: nil,
selected: self.viewModel.selectedTemplateId == template.id
)
}
return cell return cell
} }
} }
private func makeTemplateLayout() -> UICollectionViewCompositionalLayout {
let item = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)
))
let group = NSCollectionLayoutGroup.horizontal(
layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(156)),
subitem: item, count: 3
)
group.interItemSpacing = .fixed(12)
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = 12
section.contentInsets = NSDirectionalEdgeInsets(top: 4, leading: 16, bottom: 12, trailing: 16)
return UICollectionViewCompositionalLayout(section: section)
}
private func showPreview(for template: TravelAlbumAIRetouchTemplate) {
guard presentedViewController == nil else { return }
guard let content = template.comparisonContent else { showToast("暂无对比预览"); return }
present(BeforeAfterComparisonViewController(viewModel: BeforeAfterComparisonViewModel(content: content)), animated: true)
}
@MainActor @MainActor
private func applyViewModel() { private func applyViewModel() {
let isMode = viewModel.stage == .mode let showsTemplates = viewModel.isEnabled
titleLabel.text = isMode ? "选择修图方式" : "选择修图模板" originalOption.apply(selected: !showsTemplates)
subtitleLabel.text = isMode aiOption.apply(selected: showsTemplates)
? "照片上传前可选择保留原图,或使用 AI 自动修图" confirmButton.isEnabled = viewModel.canConfirm
: "缩略图为模板实际效果,后续上传的照片将自动套用" confirmButton.alpha = viewModel.canConfirm ? 1 : 0.4
backButton.isHidden = isMode || !viewModel.startsWithModeSelection selectionLabel.text = !showsTemplates ? "将保留原图,不进行 AI 修图"
confirmButton.isEnabled = viewModel.pendingConfiguration != nil && !viewModel.isLoading : viewModel.selectedTemplateName.map { "已选择:\($0)" } ?? "请选择一个修图模板"
confirmButton.alpha = confirmButton.isEnabled ? 1 : 0.45 selectionLabel.textColor = showsTemplates && viewModel.selectedTemplateName != nil ? AppColor.primary : AppColor.textSecondary
let updateVisibility = {
statusContainer.isHidden = !viewModel.isLoading && viewModel.errorMessage == nil self.originalHint.isHidden = showsTemplates
self.templateHeader.isHidden = !showsTemplates
self.templateCollectionView.isHidden = !showsTemplates || self.viewModel.isLoading || self.viewModel.errorMessage != nil
self.statusStack.isHidden = !showsTemplates || (!self.viewModel.isLoading && self.viewModel.errorMessage == nil)
}
if let previous = lastTemplatesVisible, previous != showsTemplates,
view.window != nil, !UIAccessibility.isReduceMotionEnabled {
UIView.transition(with: contentRegion, duration: 0.2, options: [.transitionCrossDissolve, .beginFromCurrentState], animations: updateVisibility)
} else {
updateVisibility()
}
lastTemplatesVisible = showsTemplates
if viewModel.isLoading { if viewModel.isLoading {
activityIndicator.startAnimating() activityIndicator.startAnimating()
statusLabel.text = "正在加载修图模板…"
retryButton.isHidden = true
} else { } else {
activityIndicator.stopAnimating() activityIndicator.stopAnimating()
statusLabel.text = viewModel.errorMessage
retryButton.isHidden = viewModel.errorMessage == nil
} }
activityIndicator.isHidden = !viewModel.isLoading
let previousItems = Set(dataSource.snapshot().itemIdentifiers) statusLabel.text = viewModel.isLoading ? "正在加载修图模板…" : viewModel.errorMessage
var snapshot = NSDiffableDataSourceSnapshot<Int, Item>() retryButton.isHidden = viewModel.isLoading
let previousTemplates = Set(templateDataSource.snapshot().itemIdentifiers)
var snapshot = NSDiffableDataSourceSnapshot<Int, TravelAlbumAIRetouchTemplate>()
snapshot.appendSections([0]) snapshot.appendSections([0])
let items: [Item] snapshot.appendItems(viewModel.templates)
if isMode { snapshot.reconfigureItems(viewModel.templates.filter(previousTemplates.contains))
items = [.mode(false), .mode(true)] templateDataSource.apply(snapshot, animatingDifferences: true)
} else {
items = viewModel.templates.map(Item.template)
}
snapshot.appendItems(items)
snapshot.reconfigureItems(items.filter(previousItems.contains))
dataSource.apply(snapshot, animatingDifferences: true)
} }
private func configureAction(_ button: UIButton, title: String, filled: Bool) { private func configureAction(_ button: UIButton, title: String, filled: Bool) {
button.setTitle(title, for: .normal) button.setTitle(title, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 16, weight: .semibold) button.titleLabel?.font = .systemFont(ofSize: 16, weight: .semibold)
button.layer.cornerRadius = 12 button.layer.cornerRadius = 14
if filled { button.backgroundColor = filled ? AppColor.primary : .white
button.backgroundColor = AppColor.primary button.setTitleColor(filled ? .white : AppColor.textSecondary, for: .normal)
button.setTitleColor(.white, for: .normal) button.layer.borderWidth = filled ? 0 : 1
} else { button.layer.borderColor = UIColor(hex: 0xDFE5EF).cgColor
button.backgroundColor = UIColor(hex: 0xF4F5F7)
button.setTitleColor(AppColor.textSecondary, for: .normal)
}
} }
@objc private func originalTapped() { viewModel.selectMode(enabled: false) }
@objc private func aiTapped() { viewModel.selectMode(enabled: true) }
@objc private func cancelTapped() { dismiss(animated: true) } @objc private func cancelTapped() { dismiss(animated: true) }
@objc private func backTapped() { viewModel.returnToModeSelection() }
@objc private func retryTapped() { Task { await viewModel.loadTemplates(api: api) } } @objc private func retryTapped() { Task { await viewModel.loadTemplates(api: api) } }
@objc private func confirmTapped() { @objc private func confirmTapped() {
guard let configuration = viewModel.pendingConfiguration else { return } guard viewModel.canConfirm, let configuration = viewModel.pendingConfiguration else { return }
let completion = onConfirm let completion = onConfirm
dismiss(animated: true) { completion?(configuration) } dismiss(animated: true) { completion?(configuration) }
} }
} }
extension TravelAlbumAutoRetouchSettingSheetViewController: UITableViewDelegate { extension TravelAlbumAutoRetouchSettingSheetViewController: UICollectionViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { /// 点击模板更新草稿;预览按钮独立处理,不改变选择。
tableView.deselectRow(at: indexPath, animated: true) func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let item = dataSource.itemIdentifier(for: indexPath) else { return } guard let template = templateDataSource.itemIdentifier(for: indexPath) else { return }
switch item { viewModel.selectTemplate(id: template.id)
case .mode(let enabled): viewModel.selectMode(enabled: enabled)
case .template(let template): viewModel.selectTemplate(id: template.id)
}
} }
} }
/// 自动修图方式或模板列表单元,提供效果图、说明和明确单选状态。 /// 修图方式单选卡片,使用图标、边框和勾选共同传达当前选择。
private final class AutoRetouchOptionCell: UITableViewCell { private final class AutoRetouchModeControl: UIControl {
static let reuseIdentifier = "AutoRetouchOptionCell" private let iconContainer = UIView()
private let card = UIView() private let iconView = UIImageView()
private let previewImageView = UIImageView() private let checkView = UIImageView()
private let titleLabel = UILabel() private let titleLabel = UILabel()
private let detailLabel = UILabel()
private let selectionImageView = UIImageView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { /// 创建可点击的方式卡片;所有内容由整个控件统一响应触摸。
super.init(style: style, reuseIdentifier: reuseIdentifier) init(title: String, detail: String, symbol: String) {
selectionStyle = .none super.init(frame: .zero)
backgroundColor = .clear layer.cornerRadius = 16
card.layer.cornerRadius = 12 layer.borderWidth = 1
card.layer.borderWidth = 1 iconContainer.layer.cornerRadius = 10
previewImageView.contentMode = .scaleAspectFill iconView.image = UIImage(systemName: symbol)
previewImageView.clipsToBounds = true iconView.contentMode = .scaleAspectFit
previewImageView.layer.cornerRadius = 8 titleLabel.text = title
titleLabel.font = .systemFont(ofSize: 16, weight: .semibold) titleLabel.font = .systemFont(ofSize: 16, weight: .semibold)
titleLabel.textColor = AppColor.textPrimary let detailLabel = UILabel()
detailLabel.font = .systemFont(ofSize: 13) detailLabel.text = detail
detailLabel.font = .systemFont(ofSize: 12)
detailLabel.textColor = AppColor.textSecondary detailLabel.textColor = AppColor.textSecondary
detailLabel.numberOfLines = 2 detailLabel.numberOfLines = 2
selectionImageView.contentMode = .scaleAspectFit checkView.contentMode = .scaleAspectFit
[iconContainer, iconView, checkView, titleLabel, detailLabel].forEach {
contentView.addSubview(card) $0.isUserInteractionEnabled = false
card.addSubview(previewImageView) addSubview($0)
card.addSubview(titleLabel)
card.addSubview(detailLabel)
card.addSubview(selectionImageView)
card.snp.makeConstraints { make in
make.top.bottom.equalToSuperview().inset(6)
make.leading.trailing.equalToSuperview().inset(16)
} }
previewImageView.snp.makeConstraints { make in iconContainer.snp.makeConstraints { make in
make.leading.equalToSuperview().offset(12) make.top.leading.equalToSuperview().offset(12)
make.top.bottom.equalToSuperview().inset(10) make.size.equalTo(32)
make.width.equalTo(previewImageView.snp.height)
} }
selectionImageView.snp.makeConstraints { make in iconView.snp.makeConstraints { $0.edges.equalTo(iconContainer).inset(7) }
make.trailing.equalToSuperview().offset(-16) checkView.snp.makeConstraints { make in
make.centerY.equalToSuperview() make.top.trailing.equalToSuperview().inset(12)
make.size.equalTo(24) make.size.equalTo(22)
} }
titleLabel.snp.makeConstraints { make in titleLabel.snp.makeConstraints { make in
make.leading.equalTo(previewImageView.snp.trailing).offset(14) make.top.equalTo(iconContainer.snp.bottom).offset(10)
make.trailing.lessThanOrEqualTo(selectionImageView.snp.leading).offset(-12) make.leading.trailing.equalToSuperview().inset(12)
make.bottom.equalTo(card.snp.centerY).offset(-2)
} }
detailLabel.snp.makeConstraints { make in detailLabel.snp.makeConstraints { make in
make.top.equalTo(titleLabel.snp.bottom).offset(4)
make.leading.trailing.equalTo(titleLabel) make.leading.trailing.equalTo(titleLabel)
make.top.equalTo(card.snp.centerY).offset(4) make.bottom.equalToSuperview().inset(12)
} }
isAccessibilityElement = true
accessibilityLabel = "\(title),\(detail)"
} }
@available(*, unavailable) @available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func prepareForReuse() { /// 更新卡片背景与单选语义。
super.prepareForReuse() func apply(selected: Bool) {
previewImageView.kf.cancelDownloadTask() isSelected = selected
previewImageView.image = nil backgroundColor = selected ? AppColor.primaryLight : .white
} layer.borderColor = (selected ? AppColor.primary : UIColor(hex: 0xDFE5EF)).cgColor
layer.borderWidth = selected ? 1.5 : 1
func apply( iconContainer.backgroundColor = selected ? AppColor.primary : UIColor(hex: 0xEFF2F7)
title: String, iconView.tintColor = selected ? .white : AppColor.textSecondary
detail: String, titleLabel.textColor = selected ? AppColor.primary : AppColor.textPrimary
imageURL: String?, checkView.image = UIImage(systemName: selected ? "checkmark.circle.fill" : "circle")
systemImage: String?, checkView.tintColor = selected ? AppColor.primary : UIColor(hex: 0xC7CFDC)
selected: Bool
) {
titleLabel.text = title
let normalizedDetail = detail.trimmingCharacters(in: .whitespacesAndNewlines)
detailLabel.text = normalizedDetail
detailLabel.isHidden = normalizedDetail.isEmpty
titleLabel.snp.remakeConstraints { make in
make.leading.equalTo(previewImageView.snp.trailing).offset(14)
make.trailing.lessThanOrEqualTo(selectionImageView.snp.leading).offset(-12)
if normalizedDetail.isEmpty {
make.centerY.equalToSuperview()
} else {
make.bottom.equalTo(card.snp.centerY).offset(-2)
}
}
if let imageURL, let url = URL(string: imageURL), !imageURL.isEmpty {
previewImageView.contentMode = .scaleAspectFill
previewImageView.backgroundColor = .clear
previewImageView.kf.setImage(with: url, placeholder: UIImage(systemName: "photo"))
} else {
previewImageView.image = systemImage.flatMap(UIImage.init(systemName:))
previewImageView.contentMode = .center
previewImageView.tintColor = AppColor.primary
previewImageView.backgroundColor = AppColor.primary.withAlphaComponent(0.08)
}
card.backgroundColor = selected ? AppColor.primary.withAlphaComponent(0.07) : .white
card.layer.borderColor = (selected ? AppColor.primary : AppColor.border).cgColor
selectionImageView.image = UIImage(systemName: selected ? "checkmark.circle.fill" : "circle")
selectionImageView.tintColor = selected ? AppColor.primary : AppColor.textTertiary
accessibilityLabel = normalizedDetail.isEmpty ? title : "\(title),\(normalizedDetail)"
accessibilityValue = selected ? "已选择" : "未选择" accessibilityValue = selected ? "已选择" : "未选择"
isAccessibilityElement = true accessibilityTraits = selected ? [.button, .selected] : [.button]
} }
} }
@@ -787,7 +787,7 @@ final class WiredCameraTransferViewController: BaseViewController {
let settingViewModel = TravelAlbumAutoRetouchSettingViewModel( let settingViewModel = TravelAlbumAutoRetouchSettingViewModel(
scenicId: AppStore.shared.session.currentScenicId, scenicId: AppStore.shared.session.currentScenicId,
configuration: viewModel.autoRetouchConfiguration, configuration: viewModel.autoRetouchConfiguration,
startsWithModeSelection: true allowsModeSelection: true
) )
let controller = TravelAlbumAutoRetouchSettingSheetViewController( let controller = TravelAlbumAutoRetouchSettingSheetViewController(
viewModel: settingViewModel, viewModel: settingViewModel,
@@ -955,6 +955,213 @@ final class TravelAlbumDetailViewControllerTests: XCTestCase {
XCTAssertEqual(viewModel.selectedAtmosphereTemplateId, 2) XCTAssertEqual(viewModel.selectedAtmosphereTemplateId, 2)
} }
func testAutoRetouchGridUsesThreeColumnsAndKeepsFooterAndSelectionWhileScrolling() async throws {
let fixture = try await makeAutoRetouchSheet(templateCount: 19)
let controller = fixture.controller
let collection = fixture.collection
let confirm = try XCTUnwrap(controller.view.findSubview {
$0.accessibilityIdentifier == "travelAlbum.autoRetouchConfirmButton"
} as? UIButton)
XCTAssertTrue(collection.collectionViewLayout is UICollectionViewCompositionalLayout)
XCTAssertFalse(confirm.isDescendant(of: collection))
for width: CGFloat in [375, 390, 430] {
controller.view.frame = CGRect(x: 0, y: 0, width: width, height: 667)
controller.view.layoutIfNeeded()
collection.layoutIfNeeded()
let frames = try (0 ..< 4).map { index in
try XCTUnwrap(collection.layoutAttributesForItem(at: IndexPath(item: index, section: 0))).frame
}
XCTAssertEqual(frames[0].minX, 16, accuracy: 0.5)
XCTAssertEqual(frames[0].width, (width - 56) / 3, accuracy: 0.5)
XCTAssertEqual(frames[0].height, 156)
XCTAssertEqual(frames[1].minY, frames[0].minY)
XCTAssertEqual(frames[2].minY, frames[0].minY)
XCTAssertEqual(frames[1].minX - frames[0].maxX, 12, accuracy: 0.5)
XCTAssertEqual(frames[2].minX - frames[1].maxX, 12, accuracy: 0.5)
XCTAssertEqual(frames[3].minX, frames[0].minX)
XCTAssertEqual(frames[3].minY - frames[0].maxY, 12, accuracy: 0.5)
XCTAssertLessThanOrEqual(collection.contentSize.width, collection.bounds.width)
}
let footerFrame = confirm.convert(confirm.bounds, to: controller.view)
let last = IndexPath(item: 18, section: 0)
collection.scrollToItem(at: last, at: .bottom, animated: false)
collection.layoutIfNeeded()
let lastCell = try XCTUnwrap(collection.cellForItem(at: last) as? TravelAlbumAIRetouchTemplateCell)
XCTAssertEqual(lastCell.frame.minX, 16, accuracy: 0.5)
XCTAssertGreaterThan(collection.contentOffset.y, 0)
let scrollOffset = collection.contentOffset
collection.delegate?.collectionView?(collection, didSelectItemAt: last)
await waitUntil { lastCell.accessibilityValue == "已选择" }
XCTAssertEqual(fixture.viewModel.pendingConfiguration?.refinedTemplateId, 19)
XCTAssertEqual(collection.contentOffset, scrollOffset)
XCTAssertEqual(confirm.convert(confirm.bounds, to: controller.view), footerFrame)
XCTAssertLessThanOrEqual(collection.convert(collection.bounds, to: controller.view).maxY, footerFrame.minY)
XCTAssertTrue(confirm.isEnabled)
collection.delegate?.collectionView?(collection, didSelectItemAt: last)
XCTAssertEqual(fixture.viewModel.selectedTemplateId, 19)
collection.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false)
collection.layoutIfNeeded()
let first = try XCTUnwrap(collection.cellForItem(at: IndexPath(item: 0, section: 0)))
XCTAssertEqual(first.accessibilityValue, "未选择")
collection.scrollToItem(at: last, at: .bottom, animated: false)
collection.layoutIfNeeded()
XCTAssertEqual(collection.cellForItem(at: last)?.accessibilityValue, "已选择")
}
func testAutoRetouchModeSelectionExpandsTemplatesInlineAndKeepsDraftWhenToggling() async throws {
let fixture = try await makeAutoRetouchSheet(templateCount: 19, allowsModeSelection: true, configuration: .disabled)
let controller = fixture.controller
let original = try XCTUnwrap(controller.view.findSubview {
$0.accessibilityIdentifier == "travelAlbum.autoRetouchOriginalOption"
} as? UIControl)
let ai = try XCTUnwrap(controller.view.findSubview {
$0.accessibilityIdentifier == "travelAlbum.autoRetouchAIOption"
} as? UIControl)
let confirm = try XCTUnwrap(controller.view.findSubview {
$0.accessibilityIdentifier == "travelAlbum.autoRetouchConfirmButton"
} as? UIButton)
let summary = try XCTUnwrap(controller.view.findSubview {
$0.accessibilityIdentifier == "travelAlbum.autoRetouchSelectionSummary"
} as? UILabel)
XCTAssertTrue(original.isSelected)
XCTAssertTrue(fixture.collection.isHidden)
XCTAssertTrue(confirm.isEnabled)
let modeFrame = ai.convert(ai.bounds, to: controller.view)
ai.sendActions(for: .touchUpInside)
await waitUntil { !fixture.collection.isHidden }
controller.view.layoutIfNeeded()
fixture.collection.layoutIfNeeded()
XCTAssertTrue(ai.isSelected)
XCTAssertFalse(original.isSelected)
XCTAssertFalse(confirm.isEnabled)
XCTAssertNil(controller.presentedViewController)
XCTAssertEqual(ai.convert(ai.bounds, to: controller.view), modeFrame)
XCTAssertGreaterThan(fixture.collection.convert(fixture.collection.bounds, to: controller.view).minY, modeFrame.maxY)
fixture.viewModel.selectTemplate(id: 4)
await waitUntil { summary.text == "已选择:模板4" }
let last = IndexPath(item: 18, section: 0)
fixture.collection.scrollToItem(at: last, at: .bottom, animated: false)
fixture.collection.layoutIfNeeded()
let offset = fixture.collection.contentOffset
original.sendActions(for: .touchUpInside)
await waitUntil { fixture.collection.isHidden }
XCTAssertTrue(confirm.isEnabled)
XCTAssertEqual(fixture.viewModel.pendingConfiguration, .disabled)
ai.sendActions(for: .touchUpInside)
await waitUntil { !fixture.collection.isHidden }
XCTAssertEqual(fixture.viewModel.selectedTemplateId, 4)
XCTAssertEqual(fixture.collection.contentOffset, offset)
XCTAssertEqual(summary.text, "已选择:模板4")
XCTAssertTrue(confirm.isEnabled)
fixture.collection.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false)
controller.view.layoutIfNeeded()
fixture.collection.layoutIfNeeded()
let image = UIGraphicsImageRenderer(bounds: controller.view.bounds).image { context in
controller.view.layer.render(in: context.cgContext)
}
let attachment = XCTAttachment(image: image)
attachment.name = "自动修图-同页模板选择"
attachment.lifetime = .keepAlways
add(attachment)
}
func testAutoRetouchPreviewDoesNotSelectTemplateAndReturnsToSameScrollPosition() async throws {
let fixture = try await makeAutoRetouchSheet(templateCount: 19)
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844))
let host = UIViewController()
window.rootViewController = host
window.makeKeyAndVisible()
defer { window.isHidden = true }
await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in
host.present(fixture.controller, animated: false) { continuation.resume() }
}
fixture.controller.view.layoutIfNeeded()
let collection = fixture.collection
let last = IndexPath(item: 18, section: 0)
collection.scrollToItem(at: last, at: .bottom, animated: false)
collection.layoutIfNeeded()
let offset = collection.contentOffset
let cell = try XCTUnwrap(collection.cellForItem(at: last))
let preview = try XCTUnwrap(cell.findSubview {
$0.accessibilityIdentifier == "travelAlbum.aiRetouchTemplatePreviewButton"
} as? UIButton)
preview.sendActions(for: .touchUpInside)
await waitUntil { fixture.controller.presentedViewController is BeforeAfterComparisonViewController }
let comparison = try XCTUnwrap(fixture.controller.presentedViewController as? BeforeAfterComparisonViewController)
XCTAssertEqual(fixture.viewModel.selectedTemplateId, 1)
XCTAssertEqual(fixture.viewModel.pendingConfiguration?.refinedTemplateId, 1)
let returned = expectation(description: "Return from comparison")
// 等待展示动画完成,再模拟预览返回。
if let transition = comparison.transitionCoordinator {
transition.animate(alongsideTransition: nil) { _ in
comparison.dismiss(animated: false) { returned.fulfill() }
}
} else {
comparison.dismiss(animated: false) { returned.fulfill() }
}
await fulfillment(of: [returned], timeout: 3)
XCTAssertNil(fixture.controller.presentedViewController)
XCTAssertEqual(collection.contentOffset, offset)
XCTAssertEqual(fixture.viewModel.selectedTemplateId, 1)
}
func testAutoRetouchMissingComparisonKeepsSelectionAndShowsMessage() async throws {
let fixture = try await makeAutoRetouchSheet(templateCount: 2, hasComparison: false)
let cell = try XCTUnwrap(fixture.collection.cellForItem(at: IndexPath(item: 1, section: 0)))
let preview = try XCTUnwrap(cell.findSubview {
$0.accessibilityIdentifier == "travelAlbum.aiRetouchTemplatePreviewButton"
} as? UIButton)
preview.sendActions(for: .touchUpInside)
XCTAssertNil(fixture.controller.presentedViewController)
XCTAssertEqual(fixture.viewModel.selectedTemplateId, 1)
XCTAssertTrue(fixture.controller.view.allLabels().contains { $0.text == "暂无对比预览" })
}
private func makeAutoRetouchSheet(
templateCount: Int,
allowsModeSelection: Bool = false,
hasComparison: Bool = true,
configuration: TravelAlbumAutoRetouchConfiguration = TravelAlbumAutoRetouchConfiguration(enabled: true, refinedTemplateId: 1)
) async throws -> (
controller: TravelAlbumAutoRetouchSettingSheetViewController,
viewModel: TravelAlbumAutoRetouchSettingViewModel,
collection: UICollectionView
) {
let api = TravelAlbumMockAPI()
let imageURL = FileManager.default.temporaryDirectory.appendingPathComponent("auto-retouch-\(UUID().uuidString).png")
let imageData = try XCTUnwrap(UIImage(named: "ai_retouch_template_placeholder")?.pngData())
try imageData.write(to: imageURL)
addTeardownBlock { try? FileManager.default.removeItem(at: imageURL) }
api.aiRetouchTemplatesResponse = TravelAlbumAIRetouchTemplatesResponse(
refinedTemplates: (1 ... templateCount).map {
TravelAlbumAIRetouchTemplate(
id: $0, name: "模板\($0)", previewURL: "",
beforeURL: hasComparison ? imageURL.absoluteString : "",
afterURL: hasComparison ? imageURL.absoluteString : ""
)
}
)
let viewModel = TravelAlbumAutoRetouchSettingViewModel(
scenicId: 18,
configuration: configuration,
allowsModeSelection: allowsModeSelection
)
let controller = TravelAlbumAutoRetouchSettingSheetViewController(viewModel: viewModel, api: api)
controller.loadViewIfNeeded()
controller.view.frame = CGRect(x: 0, y: 0, width: 390, height: 667)
let collection = try XCTUnwrap(controller.view.findSubview {
$0.accessibilityIdentifier == "travelAlbum.autoRetouchTemplateCollection"
} as? UICollectionView)
await waitUntil { collection.numberOfItems(inSection: 0) == templateCount && !viewModel.isLoading }
controller.view.layoutIfNeeded()
collection.layoutIfNeeded()
XCTAssertEqual(collection.numberOfItems(inSection: 0), templateCount)
return (controller, viewModel, collection)
}
func testAIRetouchTemplateCellExposesSelectedState() { func testAIRetouchTemplateCellExposesSelectedState() {
let cell = TravelAlbumAIRetouchTemplateCell(frame: .zero) let cell = TravelAlbumAIRetouchTemplateCell(frame: .zero)
let template = TravelAlbumAIRetouchTemplate(id: 1, name: "清透", previewURL: "") let template = TravelAlbumAIRetouchTemplate(id: 1, name: "清透", previewURL: "")
+47 -2
View File
@@ -455,6 +455,51 @@ final class TravelAlbumDetailViewModelTests: XCTestCase {
/// 自动修图配置 ViewModel 测试。 /// 自动修图配置 ViewModel 测试。
@MainActor @MainActor
final class TravelAlbumAutoRetouchSettingViewModelTests: XCTestCase { final class TravelAlbumAutoRetouchSettingViewModelTests: XCTestCase {
func testModeToggleRetainsDraftButDisabledSubmissionOmitsTemplate() async {
let api = TravelAlbumMockAPI()
api.aiRetouchTemplatesResponse = TravelAlbumAIRetouchTemplatesResponse(refinedTemplates: [
TravelAlbumAIRetouchTemplate(id: 12, name: "清透", previewURL: ""),
])
let viewModel = TravelAlbumAutoRetouchSettingViewModel(scenicId: 18, configuration: .disabled, allowsModeSelection: true)
await viewModel.loadTemplates(api: api)
viewModel.selectMode(enabled: true)
XCTAssertFalse(viewModel.canConfirm)
viewModel.selectTemplate(id: 12)
viewModel.selectMode(enabled: false)
XCTAssertEqual(viewModel.pendingConfiguration, .disabled)
XCTAssertEqual(viewModel.selectedTemplateId, 12)
viewModel.selectMode(enabled: true)
XCTAssertEqual(viewModel.pendingConfiguration?.refinedTemplateId, 12)
XCTAssertEqual(viewModel.selectedTemplateName, "清透")
XCTAssertTrue(viewModel.canConfirm)
}
func testOriginalModeRemainsConfirmableDuringTemplateLoadingAndFailure() async {
let api = TravelAlbumMockAPI()
api.aiRetouchTemplatesError = APIError.httpStatus(500, "模板服务不可用")
let viewModel = TravelAlbumAutoRetouchSettingViewModel(scenicId: 18, configuration: .disabled, allowsModeSelection: true)
var couldConfirmWhileLoading = false
viewModel.onStateChange = {
if viewModel.isLoading { couldConfirmWhileLoading = viewModel.canConfirm }
}
await viewModel.loadTemplates(api: api)
viewModel.onStateChange = nil
XCTAssertTrue(couldConfirmWhileLoading)
XCTAssertTrue(viewModel.canConfirm)
XCTAssertEqual(viewModel.pendingConfiguration, .disabled)
viewModel.selectMode(enabled: true)
XCTAssertFalse(viewModel.canConfirm)
}
func testTemplateOnlyEntryRequiresAISelectionAndCannotDisable() {
let viewModel = TravelAlbumAutoRetouchSettingViewModel(scenicId: 18, configuration: .disabled, allowsModeSelection: false)
XCTAssertTrue(viewModel.isEnabled)
XCTAssertFalse(viewModel.canConfirm)
XCTAssertNil(viewModel.pendingConfiguration)
viewModel.selectMode(enabled: false)
XCTAssertTrue(viewModel.isEnabled)
}
func testLoadsOnlyRefinedTemplatesAndKeepsSingleSelection() async { func testLoadsOnlyRefinedTemplatesAndKeepsSingleSelection() async {
let api = TravelAlbumMockAPI() let api = TravelAlbumMockAPI()
api.aiRetouchTemplatesResponse = TravelAlbumAIRetouchTemplatesResponse( api.aiRetouchTemplatesResponse = TravelAlbumAIRetouchTemplatesResponse(
@@ -470,7 +515,7 @@ final class TravelAlbumAutoRetouchSettingViewModelTests: XCTestCase {
let viewModel = TravelAlbumAutoRetouchSettingViewModel( let viewModel = TravelAlbumAutoRetouchSettingViewModel(
scenicId: 18, scenicId: 18,
configuration: .disabled, configuration: .disabled,
startsWithModeSelection: true allowsModeSelection: true
) )
viewModel.selectMode(enabled: true) viewModel.selectMode(enabled: true)
@@ -490,7 +535,7 @@ final class TravelAlbumAutoRetouchSettingViewModelTests: XCTestCase {
let viewModel = TravelAlbumAutoRetouchSettingViewModel( let viewModel = TravelAlbumAutoRetouchSettingViewModel(
scenicId: 18, scenicId: 18,
configuration: TravelAlbumAutoRetouchConfiguration(enabled: true, refinedTemplateId: 12), configuration: TravelAlbumAutoRetouchConfiguration(enabled: true, refinedTemplateId: 12),
startsWithModeSelection: false allowsModeSelection: false
) )
await viewModel.loadTemplates(api: api) await viewModel.loadTemplates(api: api)