jsdw_ios/QuickLocation/Section/Group/MemberInfo/RelationSelectPopVC.swift

169 lines
5.9 KiB
Swift

//
// RelationSelectPopVC.swift
// QuickLocation
//
import UIKit
final class RelationSelectPopVC: DLCustomPopVC {
var onConfirm: ((String) -> Void)?
private var selectedIdx: String
private let items: [RelationModel]
private var chipButtons: [UIButton] = []
init(currentIdx: String, onConfirm: ((String) -> Void)? = nil) {
self.selectedIdx = currentIdx
self.items = RelationStore.shared.list
self.onConfirm = onConfirm
super.init(nibName: nil, bundle: nil)
popStyle = .bottom
bottomCornerRadius = 40
dimmingClick = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
refreshSelection()
}
private func setupUI() {
let headerBgImage = UIImageView()
headerBgImage.image = UIImage(named: "Common/popup_header_bg")
headerBgImage.contentMode = .scaleToFill
headerBgImage.isUserInteractionEnabled = false
contentView.addSubview(headerBgImage)
headerBgImage.layoutChain
.top()
.edgesHorzontal()
.heightToWidth(120 / 375)
let titleLab = UILabel()
titleLab.text = "关系设置"
titleLab.font = .systemFont(ofSize: 18, weight: .semibold)
titleLab.textColor = UIColor(hexStr: "#293445")
contentView.addSubview(titleLab)
titleLab.layoutChain.top(22).centerX()
let closeBtn = UIButton(type: .custom)
closeBtn.setImage(UIImage(named: "MemberInfo/close_circle"), for: .normal)
closeBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
contentView.addSubview(closeBtn)
closeBtn.layoutChain.right(16).centerY(titleLab).width(28).height(28)
let columns = 5
let rows = max(1, Int(ceil(Double(max(items.count, 1)) / Double(columns))))
let chipHeight: CGFloat = 36
let rowSpacing: CGFloat = 8
let gridHeight = CGFloat(rows) * chipHeight + CGFloat(max(0, rows - 1)) * rowSpacing
let grid = UIStackView()
grid.axis = .vertical
grid.spacing = rowSpacing
grid.distribution = .fillEqually
contentView.addSubview(grid)
grid.layoutChain
.topToBottomOfView(titleLab, offset: 24)
.left(16)
.right(16)
.height(gridHeight)
for row in 0..<rows {
let rowStack = UIStackView()
rowStack.axis = .horizontal
rowStack.spacing = 8
rowStack.distribution = .fillEqually
rowStack.alignment = .fill
for col in 0..<columns {
let index = row * columns + col
if items.indices.contains(index) {
let chip = makeChip(items[index], tag: index)
chipButtons.append(chip)
rowStack.addArrangedSubview(chip)
} else {
rowStack.addArrangedSubview(UIView())
}
}
grid.addArrangedSubview(rowStack)
}
let confirm = UIButton(type: .custom)
confirm.setTitle("确 定", for: .normal)
confirm.setTitleColor(.white, for: .normal)
confirm.titleLabel?.font = FontManager.boboBold(18)
confirm.setBackgroundImage(UIImage(named: "Common/button_bg_2"), for: .normal)
confirm.layer.cornerRadius = 20
confirm.clipsToBounds = true
confirm.addTarget(self, action: #selector(confirmTapped), for: .touchUpInside)
contentView.addSubview(confirm)
confirm.layoutChain
.topToBottomOfView(grid, offset: 22)
.left(30)
.right(30)
.height(50)
.bottom(16 + kSafeBottomMargin)
let height = 22 + 28 + 24 + gridHeight + 22 + 50 + 16 + kSafeBottomMargin
contentBgView.layoutChain.height(height)
contentView.layoutChain.height(height)
view.layoutIfNeeded()
}
private func makeChip(_ item: RelationModel, tag: Int) -> UIButton {
let button = UIButton(type: .custom)
button.tag = tag
button.layer.cornerRadius = 18
button.clipsToBounds = true
button.titleLabel?.font = .systemFont(ofSize: 13, weight: .medium)
button.addTarget(self, action: #selector(tapChip(_:)), for: .touchUpInside)
if item.showsHeart {
let heart = UIImage(named: "Mine/name_heart")?.withRenderingMode(.alwaysOriginal)
button.setImage(heart, for: .normal)
button.setTitle(" \(item.name)", for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -2, bottom: 0, right: 2)
} else {
button.setTitle(item.name, for: .normal)
}
return button
}
private func refreshSelection() {
for (index, button) in chipButtons.enumerated() {
guard items.indices.contains(index) else { continue }
let selected = items[index].idx == selectedIdx
button.backgroundColor = selected ? UIColor(hexStr: "#16B3FF") : .white
button.setTitleColor(selected ? .white : UIColor(hexStr: "#293445"), for: .normal)
button.layer.borderWidth = selected ? 0 : 1
button.layer.borderColor = UIColor(hexStr: "#E8EEF2").cgColor
}
}
@objc private func tapChip(_ sender: UIButton) {
guard items.indices.contains(sender.tag) else { return }
selectedIdx = items[sender.tag].idx
refreshSelection()
}
@objc private func confirmTapped() {
guard !selectedIdx.isEmpty else {
DLToast.show(text: "请选择关系")
return
}
let idx = selectedIdx
dismiss(animated: true) { [weak self] in
self?.onConfirm?(idx)
}
}
@objc private func close() {
dismiss(animated: true)
}
}