feat(travel-album): 按当前景区创建相册及筛选订单
This commit is contained in:
@@ -8,16 +8,15 @@ import UIKit
|
||||
|
||||
/// 新建相册任务底部 Sheet,对齐 Android `CreateTravelAlbumModal`。
|
||||
final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
var onDismissed: (() -> Void)?
|
||||
|
||||
private let viewModel: TravelAlbumEntryViewModel
|
||||
private let api: any TravelAlbumServing
|
||||
private let mode: TravelAlbumEntryViewModel.CreateMode = .preShoot
|
||||
|
||||
private let scrollView = UIScrollView()
|
||||
private let contentView = UIView()
|
||||
private let titleLabel = UILabel()
|
||||
private let preShootCard = TravelAlbumModeOptionView()
|
||||
private let preOrderCard = TravelAlbumModeOptionView()
|
||||
private let orderPicker = UIButton(type: .system)
|
||||
private let fieldsStack = UIStackView()
|
||||
private let freeCountField = UITextField()
|
||||
private let singlePriceField = UITextField()
|
||||
@@ -29,7 +28,7 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
private let aiRetouchOption = TravelAlbumModeOptionView()
|
||||
private let cancelButton = UIButton(type: .system)
|
||||
private let confirmButton = UIButton(type: .system)
|
||||
private var autoRetouchConfiguration: TravelAlbumAutoRetouchConfiguration = .disabled
|
||||
private var renderedMode: TravelAlbumEntryViewModel.CreateMode?
|
||||
|
||||
init(viewModel: TravelAlbumEntryViewModel, api: any TravelAlbumServing) {
|
||||
self.viewModel = viewModel
|
||||
@@ -62,15 +61,10 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
titleLabel.textColor = AppColor.textPrimary
|
||||
titleLabel.textAlignment = .center
|
||||
|
||||
preShootCard.apply(
|
||||
title: "先拍再买",
|
||||
desc: "拍完后分享给用户,用户在小程序上选择性购买",
|
||||
selected: true
|
||||
)
|
||||
|
||||
fieldsStack.axis = .vertical
|
||||
fieldsStack.spacing = 14
|
||||
|
||||
configurePicker(orderPicker, accessibilityIdentifier: "travelAlbum.create.order")
|
||||
configureTextField(freeCountField, placeholder: "请输入免费张数", keyboardType: .numberPad)
|
||||
configureTextField(singlePriceField, placeholder: "请输入单张照片价格", keyboardType: .decimalPad)
|
||||
configureTextField(packagePriceField, placeholder: "请输入打包价格", keyboardType: .decimalPad)
|
||||
@@ -81,8 +75,8 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
|
||||
view.addSubview(scrollView)
|
||||
scrollView.addSubview(contentView)
|
||||
[titleLabel, preShootCard, fieldsStack, cancelButton, confirmButton].forEach(contentView.addSubview)
|
||||
rebuildFields()
|
||||
[titleLabel, preShootCard, preOrderCard, fieldsStack, cancelButton, confirmButton].forEach(contentView.addSubview)
|
||||
applyCreateState()
|
||||
}
|
||||
|
||||
override func setupConstraints() {
|
||||
@@ -101,8 +95,12 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
make.top.equalTo(titleLabel.snp.bottom).offset(18)
|
||||
make.leading.trailing.equalToSuperview().inset(16)
|
||||
}
|
||||
preOrderCard.snp.makeConstraints { make in
|
||||
make.top.equalTo(preShootCard.snp.bottom).offset(10)
|
||||
make.leading.trailing.equalTo(preShootCard)
|
||||
}
|
||||
fieldsStack.snp.makeConstraints { make in
|
||||
make.top.equalTo(preShootCard.snp.bottom).offset(18)
|
||||
make.top.equalTo(preOrderCard.snp.bottom).offset(18)
|
||||
make.leading.trailing.equalTo(preShootCard)
|
||||
}
|
||||
cancelButton.snp.makeConstraints { make in
|
||||
@@ -120,6 +118,9 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
}
|
||||
|
||||
override func bindActions() {
|
||||
preShootCard.addTarget(self, action: #selector(preShootTapped), for: .touchUpInside)
|
||||
preOrderCard.addTarget(self, action: #selector(preOrderTapped), for: .touchUpInside)
|
||||
orderPicker.addTarget(self, action: #selector(retryOrdersTapped), for: .touchUpInside)
|
||||
cancelButton.addTarget(self, action: #selector(cancelTapped), for: .touchUpInside)
|
||||
confirmButton.addTarget(self, action: #selector(confirmTapped), for: .touchUpInside)
|
||||
[freeCountField, singlePriceField, packagePriceField].forEach {
|
||||
@@ -127,13 +128,15 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
}
|
||||
noRetouchOption.addTarget(self, action: #selector(noRetouchTapped), for: .touchUpInside)
|
||||
aiRetouchOption.addTarget(self, action: #selector(aiRetouchTapped), for: .touchUpInside)
|
||||
sheetPresentationController?.delegate = self
|
||||
viewModel.onCreateStateChange = { [weak self] in
|
||||
Task { @MainActor in self?.applyCreateState() }
|
||||
}
|
||||
}
|
||||
|
||||
override func viewDidDisappear(_ animated: Bool) {
|
||||
super.viewDidDisappear(animated)
|
||||
if isBeingDismissed {
|
||||
onDismissed?()
|
||||
}
|
||||
if isBeingDismissed { viewModel.dismissCreateSheet() }
|
||||
}
|
||||
|
||||
private func configureTextField(_ field: UITextField, placeholder: String, keyboardType: UIKeyboardType) {
|
||||
@@ -158,14 +161,121 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
button.layer.cornerRadius = 10
|
||||
}
|
||||
|
||||
private func configurePicker(_ button: UIButton, accessibilityIdentifier: String) {
|
||||
var configuration = UIButton.Configuration.plain()
|
||||
configuration.contentInsets = .init(top: 8, leading: 12, bottom: 8, trailing: 12)
|
||||
configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer {
|
||||
var attributes = $0
|
||||
attributes.font = .systemFont(ofSize: 14)
|
||||
return attributes
|
||||
}
|
||||
button.configuration = configuration
|
||||
button.contentHorizontalAlignment = .leading
|
||||
button.layer.cornerRadius = 8
|
||||
button.layer.borderWidth = 1
|
||||
button.layer.borderColor = AppColor.border.cgColor
|
||||
button.accessibilityIdentifier = accessibilityIdentifier
|
||||
button.snp.makeConstraints { $0.height.equalTo(44) }
|
||||
}
|
||||
|
||||
private func rebuildFields() {
|
||||
renderedMode = viewModel.createMode
|
||||
fieldsStack.arrangedSubviews.forEach { $0.removeFromSuperview() }
|
||||
fieldsStack.addArrangedSubview(makeFieldGroup(title: "免费张数", required: false, field: freeCountField))
|
||||
fieldsStack.addArrangedSubview(makeFieldGroup(title: "单张照片价格(元)", required: true, field: singlePriceField))
|
||||
fieldsStack.addArrangedSubview(makeFieldGroup(title: "打包价格(元)", required: false, field: packagePriceField))
|
||||
switch viewModel.createMode {
|
||||
case .preShoot:
|
||||
fieldsStack.addArrangedSubview(makeFieldGroup(title: "免费张数", required: false, field: freeCountField))
|
||||
fieldsStack.addArrangedSubview(makeFieldGroup(title: "单张照片价格(元)", required: true, field: singlePriceField))
|
||||
fieldsStack.addArrangedSubview(makeFieldGroup(title: "打包价格(元)", required: false, field: packagePriceField))
|
||||
case .preOrder:
|
||||
fieldsStack.addArrangedSubview(makeFieldGroup(title: "绑定订单", required: true, field: orderPicker))
|
||||
}
|
||||
fieldsStack.addArrangedSubview(autoRetouchSectionView)
|
||||
}
|
||||
|
||||
private func applyCreateState() {
|
||||
guard viewModel.isCreateSheetVisible else {
|
||||
if !isBeingDismissed { presentingViewController?.dismiss(animated: true) }
|
||||
return
|
||||
}
|
||||
if renderedMode != viewModel.createMode { rebuildFields() }
|
||||
preShootCard.apply(
|
||||
title: "先拍后买",
|
||||
desc: "拍完后分享给用户,用户在小程序上选择性购买",
|
||||
selected: viewModel.createMode == .preShoot
|
||||
)
|
||||
preOrderCard.apply(
|
||||
title: "先买后拍",
|
||||
desc: "关联当前景区的已购订单,再开始拍摄",
|
||||
selected: viewModel.createMode == .preOrder
|
||||
)
|
||||
applyOrderPicker()
|
||||
updateAutoRetouchSection()
|
||||
|
||||
let isCreating = viewModel.isCreating
|
||||
isModalInPresentation = isCreating
|
||||
preShootCard.isEnabled = !isCreating
|
||||
preOrderCard.isEnabled = !isCreating
|
||||
noRetouchOption.isEnabled = !isCreating
|
||||
aiRetouchOption.isEnabled = !isCreating
|
||||
aiRetouchOption.alpha = aiRetouchOption.isEnabled ? 1 : 0.5
|
||||
cancelButton.isEnabled = !isCreating
|
||||
[freeCountField, singlePriceField, packagePriceField].forEach { $0.isEnabled = !isCreating }
|
||||
confirmButton.setTitle(isCreating ? "创建中…" : "确定", for: .normal)
|
||||
updateConfirmAvailability()
|
||||
}
|
||||
|
||||
private func applyOrderPicker() {
|
||||
guard viewModel.createMode == .preOrder, let scenicId = viewModel.createScenicId else { return }
|
||||
switch viewModel.orderLoadState {
|
||||
case .idle, .loading:
|
||||
applyPicker(orderPicker, title: "正在加载订单…")
|
||||
case .failed(let message):
|
||||
applyPicker(orderPicker, title: "订单加载失败,点击重试", retryMessage: message)
|
||||
case .loaded:
|
||||
let actions = viewModel.availableOrders.map { order in
|
||||
UIAction(
|
||||
title: orderTitle(order),
|
||||
state: order == viewModel.selectedOrder ? .on : .off
|
||||
) { [weak self] _ in
|
||||
self?.viewModel.selectCreateOrder(number: order.orderNumber, scenicId: scenicId)
|
||||
}
|
||||
}
|
||||
let title = viewModel.selectedOrder.map(orderTitle)
|
||||
?? (actions.isEmpty ? "当前景区暂无可关联订单" : "请选择绑定订单")
|
||||
applyPicker(orderPicker, title: title, actions: actions)
|
||||
}
|
||||
}
|
||||
|
||||
private func orderTitle(_ order: TravelAlbumAvailableOrder) -> String {
|
||||
order.projectName.isEmpty ? order.orderNumber : "\(order.projectName) · \(order.orderNumber)"
|
||||
}
|
||||
|
||||
private func applyPicker(_ button: UIButton, title: String, actions: [UIAction] = [], retryMessage: String? = nil) {
|
||||
button.configuration?.title = title
|
||||
button.configuration?.image = actions.isEmpty
|
||||
? nil
|
||||
: UIImage(systemName: "chevron.down", withConfiguration: UIImage.SymbolConfiguration(pointSize: 10))
|
||||
button.configuration?.imagePlacement = .trailing
|
||||
button.configuration?.imagePadding = 8
|
||||
button.menu = actions.isEmpty ? nil : UIMenu(children: actions)
|
||||
button.showsMenuAsPrimaryAction = !actions.isEmpty
|
||||
button.isEnabled = !viewModel.isCreating && (!actions.isEmpty || retryMessage != nil)
|
||||
button.configuration?.baseForegroundColor = button.isEnabled ? AppColor.textPrimary : AppColor.textSecondary
|
||||
button.accessibilityHint = retryMessage
|
||||
}
|
||||
|
||||
private func updateConfirmAvailability() {
|
||||
let hasRequiredFields: Bool
|
||||
switch viewModel.createMode {
|
||||
case .preShoot:
|
||||
hasRequiredFields = Double(singlePriceField.text ?? "") != nil
|
||||
case .preOrder:
|
||||
hasRequiredFields = viewModel.orderLoadState == .loaded && viewModel.selectedOrder != nil
|
||||
}
|
||||
confirmButton.isEnabled = hasRequiredFields && !viewModel.isCreating
|
||||
confirmButton.alpha = confirmButton.isEnabled ? 1 : 0.45
|
||||
}
|
||||
|
||||
private func configureAutoRetouchSection() {
|
||||
autoRetouchSectionView.backgroundColor = .white
|
||||
autoRetouchSectionView.layer.cornerRadius = 10
|
||||
@@ -201,7 +311,7 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
updateAutoRetouchSection()
|
||||
}
|
||||
|
||||
private func makeFieldGroup(title: String, required: Bool, field: UITextField) -> UIView {
|
||||
private func makeFieldGroup(title: String, required: Bool, field: UIView) -> UIView {
|
||||
let container = UIView()
|
||||
let label = UILabel()
|
||||
label.attributedText = fieldTitle(title, required: required)
|
||||
@@ -228,39 +338,49 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
return result
|
||||
}
|
||||
|
||||
@objc private func preShootTapped() {
|
||||
view.endEditing(true)
|
||||
Task { @MainActor in await viewModel.selectCreateMode(.preShoot, api: api) }
|
||||
}
|
||||
|
||||
@objc private func preOrderTapped() {
|
||||
view.endEditing(true)
|
||||
Task { @MainActor in await viewModel.selectCreateMode(.preOrder, api: api) }
|
||||
}
|
||||
|
||||
@objc private func retryOrdersTapped() {
|
||||
guard case .failed = viewModel.orderLoadState else { return }
|
||||
Task { @MainActor in await viewModel.loadAvailableOrders(api: api) }
|
||||
}
|
||||
|
||||
@objc private func cancelTapped() {
|
||||
guard !viewModel.isCreating else { return }
|
||||
viewModel.dismissCreateSheet()
|
||||
dismiss(animated: true)
|
||||
}
|
||||
|
||||
@objc private func confirmTapped() {
|
||||
Task {
|
||||
view.endEditing(true)
|
||||
Task { @MainActor in
|
||||
await viewModel.confirmCreate(
|
||||
mode: mode,
|
||||
freeCount: freeCountField.text ?? "",
|
||||
singlePrice: singlePriceField.text ?? "",
|
||||
packagePrice: packagePriceField.text ?? "",
|
||||
autoRetouchConfiguration: autoRetouchConfiguration,
|
||||
order: nil,
|
||||
api: api
|
||||
)
|
||||
await MainActor.run {
|
||||
if !viewModel.isCreateSheetVisible {
|
||||
dismiss(animated: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func noRetouchTapped() {
|
||||
autoRetouchConfiguration = .disabled
|
||||
updateAutoRetouchSection()
|
||||
guard let scenicId = viewModel.activeCreateScenicId() else { return }
|
||||
viewModel.setCreateRetouchConfiguration(.disabled, scenicId: scenicId)
|
||||
}
|
||||
|
||||
@objc private func aiRetouchTapped() {
|
||||
guard presentedViewController == nil else { return }
|
||||
guard presentedViewController == nil, !viewModel.isCreating, let scenicId = viewModel.activeCreateScenicId() else { return }
|
||||
let settingViewModel = TravelAlbumAutoRetouchSettingViewModel(
|
||||
scenicId: AppStore.shared.session.currentScenicId,
|
||||
configuration: autoRetouchConfiguration,
|
||||
scenicId: scenicId,
|
||||
configuration: viewModel.createRetouchConfiguration,
|
||||
allowsModeSelection: false
|
||||
)
|
||||
let controller = TravelAlbumAutoRetouchSettingSheetViewController(
|
||||
@@ -268,23 +388,19 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
api: api
|
||||
)
|
||||
controller.onConfirm = { [weak self] configuration in
|
||||
self?.autoRetouchConfiguration = configuration
|
||||
self?.updateAutoRetouchSection()
|
||||
self?.viewModel.setCreateRetouchConfiguration(configuration, scenicId: scenicId)
|
||||
}
|
||||
present(controller, animated: true)
|
||||
}
|
||||
|
||||
private func updateAutoRetouchSection() {
|
||||
noRetouchOption.apply(title: "不修图", desc: "保留原图", selected: !autoRetouchConfiguration.enabled)
|
||||
let isEnabled = viewModel.createRetouchConfiguration.enabled
|
||||
noRetouchOption.apply(title: "不修图", desc: "保留原图", selected: !isEnabled)
|
||||
aiRetouchOption.apply(
|
||||
title: "AI 修图",
|
||||
desc: autoRetouchConfiguration.enabled ? "已选择真实 AI 模板" : "点击选模板",
|
||||
selected: autoRetouchConfiguration.enabled
|
||||
desc: isEnabled ? "已选择模板" : "点击选模板",
|
||||
selected: isEnabled
|
||||
)
|
||||
noRetouchOption.accessibilityLabel = "不修图,保留原图"
|
||||
aiRetouchOption.accessibilityLabel = "AI 修图,点击选择模板"
|
||||
noRetouchOption.accessibilityValue = autoRetouchConfiguration.enabled ? "未选择" : "已选择"
|
||||
aiRetouchOption.accessibilityValue = autoRetouchConfiguration.enabled ? "已选择" : "未选择"
|
||||
}
|
||||
|
||||
@objc private func textFieldEditingChanged(_ field: UITextField) {
|
||||
@@ -294,6 +410,7 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
} else {
|
||||
field.text = sanitizeMoneyInput(text)
|
||||
}
|
||||
updateConfirmAvailability()
|
||||
}
|
||||
|
||||
private func sanitizeMoneyInput(_ value: String) -> String {
|
||||
@@ -322,6 +439,13 @@ final class CreateTravelAlbumSheetViewController: BaseViewController {
|
||||
}
|
||||
}
|
||||
|
||||
extension CreateTravelAlbumSheetViewController: UISheetPresentationControllerDelegate {
|
||||
/// 滑动关闭时结束创建会话。
|
||||
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
|
||||
viewModel.dismissCreateSheet()
|
||||
}
|
||||
}
|
||||
|
||||
/// 新建相册模式选择卡片。
|
||||
private final class TravelAlbumModeOptionView: UIControl {
|
||||
private let dotView = UIView()
|
||||
@@ -373,6 +497,9 @@ private final class TravelAlbumModeOptionView: UIControl {
|
||||
func apply(title: String, desc: String, selected: Bool) {
|
||||
titleLabel.text = title
|
||||
descLabel.text = desc
|
||||
isAccessibilityElement = true
|
||||
accessibilityLabel = "\(title),\(desc)"
|
||||
accessibilityTraits = selected ? [.button, .selected] : [.button]
|
||||
backgroundColor = selected ? AppColor.primary.withAlphaComponent(0.06) : .white
|
||||
layer.borderColor = (selected ? AppColor.primary : AppColor.border).cgColor
|
||||
titleLabel.textColor = selected ? AppColor.primary : AppColor.textPrimary
|
||||
|
||||
@@ -28,6 +28,7 @@ final class TravelAlbumAutoRetouchSettingSheetViewController: BaseViewController
|
||||
private let cancelButton = UIButton(type: .system)
|
||||
private let confirmButton = UIButton(type: .system)
|
||||
private var lastTemplatesVisible: Bool?
|
||||
private var templateLoadTask: Task<Void, Never>?
|
||||
|
||||
/// 创建设置 Sheet;方式选择与模板区域共用同一份配置草稿。
|
||||
init(viewModel: TravelAlbumAutoRetouchSettingViewModel, api: any TravelAlbumServing) {
|
||||
@@ -217,7 +218,18 @@ final class TravelAlbumAutoRetouchSettingSheetViewController: BaseViewController
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
applyViewModel()
|
||||
Task { await viewModel.loadTemplates(api: api) }
|
||||
loadTemplates()
|
||||
}
|
||||
|
||||
override func viewDidDisappear(_ animated: Bool) {
|
||||
super.viewDidDisappear(animated)
|
||||
if isBeingDismissed { templateLoadTask?.cancel() }
|
||||
}
|
||||
|
||||
private func loadTemplates() {
|
||||
guard !viewModel.isLoading else { return }
|
||||
templateLoadTask?.cancel()
|
||||
templateLoadTask = Task { [viewModel, api] in await viewModel.loadTemplates(api: api) }
|
||||
}
|
||||
|
||||
private func configureDataSource() {
|
||||
@@ -307,7 +319,7 @@ final class TravelAlbumAutoRetouchSettingSheetViewController: BaseViewController
|
||||
@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 retryTapped() { Task { await viewModel.loadTemplates(api: api) } }
|
||||
@objc private func retryTapped() { loadTemplates() }
|
||||
@objc private func confirmTapped() {
|
||||
guard viewModel.canConfirm, let configuration = viewModel.pendingConfiguration else { return }
|
||||
let completion = onConfirm
|
||||
|
||||
@@ -117,7 +117,15 @@ final class TravelAlbumEntryViewController: BaseViewController {
|
||||
Task { @MainActor in self?.applyViewModel() }
|
||||
}
|
||||
viewModel.onShowMessage = { [weak self] message in
|
||||
Task { @MainActor in self?.showToast(message) }
|
||||
Task { @MainActor in
|
||||
guard let self else { return }
|
||||
if self.viewModel.isCreateSheetVisible,
|
||||
let sheet = self.presentedViewController as? CreateTravelAlbumSheetViewController {
|
||||
sheet.showToast(message)
|
||||
} else {
|
||||
self.showToast(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,15 +178,8 @@ final class TravelAlbumEntryViewController: BaseViewController {
|
||||
}
|
||||
|
||||
@objc private func createTapped() {
|
||||
Task {
|
||||
await viewModel.openCreateSheet(api: api)
|
||||
await MainActor.run {
|
||||
guard viewModel.isCreateSheetVisible, presentedViewController == nil else { return }
|
||||
let sheet = CreateTravelAlbumSheetViewController(viewModel: viewModel, api: api)
|
||||
sheet.onDismissed = { [weak self] in self?.viewModel.dismissCreateSheet() }
|
||||
present(sheet, animated: true)
|
||||
}
|
||||
}
|
||||
guard presentedViewController == nil, viewModel.openCreateSheet() else { return }
|
||||
present(CreateTravelAlbumSheetViewController(viewModel: viewModel, api: api), animated: true)
|
||||
}
|
||||
|
||||
private func loadAlbumCode(_ album: TravelAlbum) {
|
||||
|
||||
Reference in New Issue
Block a user