LNEditProfilePhotoWallView.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // LNEditProfilePhotoWallView.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/19.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. protocol LNEditProfilePhotoWallViewDelegate: AnyObject {
  11. func onEditProfilePhotoWallViewDidChanged(view: LNEditProfilePhotoWallView)
  12. }
  13. class LNEditProfilePhotoWallView: UIView {
  14. private let maxCount = 9
  15. private let stackView = UIStackView()
  16. private let addImageButton = UIButton()
  17. private let photoWallCountLabel = UILabel()
  18. weak var delegate: LNEditProfilePhotoWallViewDelegate?
  19. var curPhotos: [String] {
  20. let imageViews = stackView.arrangedSubviews.filter { $0 is LNImageUploadView } as! [LNImageUploadView]
  21. return imageViews.compactMap { $0.imageUrl }
  22. }
  23. override init(frame: CGRect) {
  24. super.init(frame: frame)
  25. setupViews()
  26. }
  27. func loadImages(_ urls: [String]) {
  28. for url in urls {
  29. let imageView = buildImageView()
  30. imageView.loadImage(url: url)
  31. stackView.addArrangedSubview(imageView)
  32. imageView.snp.makeConstraints { make in
  33. make.height.equalToSuperview()
  34. make.width.equalTo(imageView.snp.height)
  35. }
  36. }
  37. updateImageCount()
  38. }
  39. required init?(coder: NSCoder) {
  40. fatalError("init(coder:) has not been implemented")
  41. }
  42. }
  43. extension LNEditProfilePhotoWallView: LNImageUploadViewDelegate {
  44. func onImageUploadView(view: LNImageUploadView, didUploadImage url: String) {
  45. delegate?.onEditProfilePhotoWallViewDidChanged(view: self)
  46. }
  47. func onImageUploadViewDidClickDelete(view: LNImageUploadView) {
  48. stackView.removeArrangedSubview(view)
  49. view.removeFromSuperview()
  50. delegate?.onEditProfilePhotoWallViewDidChanged(view: self)
  51. updateImageCount()
  52. }
  53. }
  54. extension LNEditProfilePhotoWallView {
  55. private func updateImageCount() {
  56. let imageViews = stackView.arrangedSubviews.filter { $0 is LNImageUploadView }
  57. photoWallCountLabel.text = .init(key: "A00190", imageViews.count, 9)
  58. addImageButton.isHidden = imageViews.count >= maxCount
  59. }
  60. private func setupViews() {
  61. clipsToBounds = false
  62. photoWallCountLabel.font = .heading_h4
  63. photoWallCountLabel.textColor = .text_5
  64. photoWallCountLabel.text = .init(key: "A00190", 0, 9)
  65. addSubview(photoWallCountLabel)
  66. photoWallCountLabel.snp.makeConstraints { make in
  67. make.horizontalEdges.equalToSuperview()
  68. make.top.equalToSuperview().offset(10)
  69. }
  70. let descLabel = UILabel()
  71. descLabel.text = .init(key: "A00191")
  72. descLabel.font = .body_s
  73. descLabel.textColor = .text_4
  74. descLabel.numberOfLines = 0
  75. addSubview(descLabel)
  76. descLabel.snp.makeConstraints { make in
  77. make.horizontalEdges.equalToSuperview()
  78. make.top.equalTo(photoWallCountLabel.snp.bottom).offset(4)
  79. }
  80. let scrollView = UIScrollView()
  81. scrollView.showsVerticalScrollIndicator = false
  82. scrollView.showsHorizontalScrollIndicator = false
  83. scrollView.clipsToBounds = false
  84. addSubview(scrollView)
  85. scrollView.snp.makeConstraints { make in
  86. make.horizontalEdges.equalToSuperview()
  87. make.top.equalTo(descLabel.snp.bottom).offset(10)
  88. make.bottom.equalToSuperview()
  89. make.height.equalTo(100)
  90. }
  91. stackView.axis = .horizontal
  92. stackView.spacing = 6
  93. scrollView.addSubview(stackView)
  94. stackView.snp.makeConstraints { make in
  95. make.horizontalEdges.equalToSuperview()
  96. make.verticalEdges.equalToSuperview()
  97. make.height.equalToSuperview()
  98. }
  99. let config = UIImage.SymbolConfiguration(pointSize: 17)
  100. addImageButton.backgroundColor = .fill_1
  101. addImageButton.layer.cornerRadius = 8.33
  102. addImageButton.setImage(.init(systemName: "plus", withConfiguration: config), for: .normal)
  103. addImageButton.tintColor = .text_3
  104. addImageButton.addAction(UIAction(handler: { [weak self] _ in
  105. guard let self else { return }
  106. LNBottomSheetMenu.showImageSelectMenu(view: self) { [weak self] image, _ in
  107. guard let self else { return }
  108. guard let image = image?.compress(type: .photoWall) else { return }
  109. let imageView = buildImageView()
  110. imageView.uploadImage(image: image)
  111. stackView.insertArrangedSubview(imageView, at: 1)
  112. imageView.snp.makeConstraints { make in
  113. make.height.equalToSuperview()
  114. make.width.equalTo(imageView.snp.height)
  115. }
  116. updateImageCount()
  117. }
  118. }), for: .touchUpInside)
  119. stackView.addArrangedSubview(addImageButton)
  120. addImageButton.snp.makeConstraints { make in
  121. make.height.equalToSuperview()
  122. make.width.equalTo(addImageButton.snp.height)
  123. }
  124. }
  125. private func buildImageView() -> LNImageUploadView {
  126. let imageView = LNImageUploadView()
  127. imageView.uploadType = .cover
  128. imageView.layer.cornerRadius = 8.33
  129. imageView.clipsToBounds = true
  130. imageView.showClearButton = true
  131. imageView.delegate = self
  132. return imageView
  133. }
  134. }