LNInterestSetupViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // LNInterestSetupViewController.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/3.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. extension UIView {
  11. func pushToInterestSetup(_ config: LNProfileUpdateConfig) {
  12. let vc = LNInterestSetupViewController(config: config)
  13. navigationController?.pushViewController(vc, animated: true)
  14. }
  15. }
  16. class LNInterestSetupViewController: LNViewController {
  17. private let updateConfig: LNProfileUpdateConfig
  18. private let horizentalSpace = 22.0
  19. private let lineSpace = 8.0
  20. private let itemSpacing = 13.0
  21. private let minWidth = 115.0
  22. private let itemHeight = 85.0
  23. static let headerHeight = 44.0
  24. private let collectionViewLayout = UICollectionViewFlowLayout()
  25. private let collectionView: UICollectionView
  26. private let nextButton = UIButton()
  27. private let bottomGradient = CAGradientLayer()
  28. private let maxCount = LNGameMateManager.gameSelectionMaxCount
  29. private var selectedIndexs: [IndexPath] = []
  30. private var games: [LNGameTypeItemVO] = LNGameMateManager.shared.curGameTypes
  31. init(config: LNProfileUpdateConfig) {
  32. updateConfig = config
  33. collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
  34. super.init(nibName: nil, bundle: nil)
  35. }
  36. required init?(coder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. setupViews()
  42. }
  43. override func viewDidLayoutSubviews() {
  44. super.viewDidLayoutSubviews()
  45. if let superlayer = bottomGradient.superlayer {
  46. bottomGradient.frame = superlayer.bounds
  47. }
  48. }
  49. }
  50. extension LNInterestSetupViewController: UICollectionViewDataSource, UICollectionViewDelegate {
  51. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  52. games[section].children.count
  53. }
  54. func numberOfSections(in collectionView: UICollectionView) -> Int {
  55. games.count
  56. }
  57. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  58. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LNInterestItemCell.className, for: indexPath) as! LNInterestItemCell
  59. let item = games[indexPath.section].children[indexPath.row]
  60. cell.update(item)
  61. cell.inSelected = selectedIndexs.contains(indexPath)
  62. cell.isEnable = selectedIndexs.count < maxCount || selectedIndexs.contains(indexPath)
  63. return cell
  64. }
  65. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  66. guard kind == UICollectionView.elementKindSectionHeader else {
  67. return UICollectionReusableView()
  68. }
  69. let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: LNInterestGroupHeader.className, for: indexPath) as! LNInterestGroupHeader
  70. let section = games[indexPath.section]
  71. view.update(section.name)
  72. return view
  73. }
  74. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  75. let needReload: Bool
  76. if selectedIndexs.contains(indexPath) {
  77. needReload = selectedIndexs.count == maxCount
  78. selectedIndexs.removeAll { $0 == indexPath }
  79. } else if selectedIndexs.count < maxCount {
  80. selectedIndexs.append(indexPath)
  81. needReload = selectedIndexs.count == maxCount
  82. } else {
  83. needReload = false
  84. }
  85. if needReload {
  86. collectionView.reloadData()
  87. } else {
  88. collectionView.reloadItems(at: [indexPath])
  89. }
  90. checkNextButtonEnable()
  91. }
  92. }
  93. extension LNInterestSetupViewController {
  94. private func checkNextButtonEnable() {
  95. nextButton.isEnabled = selectedIndexs.count > 0
  96. }
  97. private func setupViews() {
  98. view.backgroundColor = .primary_1
  99. navigationBarColor = .clear
  100. let bg = UIImageView()
  101. bg.image = .icLoginInterestBg
  102. view.addSubview(bg)
  103. bg.snp.makeConstraints { make in
  104. make.horizontalEdges.equalToSuperview()
  105. make.top.equalTo(fakeNaviBgView)
  106. }
  107. let titleView = buildTitles()
  108. view.addSubview(titleView)
  109. titleView.snp.makeConstraints { make in
  110. make.horizontalEdges.equalToSuperview().inset(22)
  111. make.top.equalToSuperview().offset(22)
  112. }
  113. let listView = buildListView()
  114. view.addSubview(listView)
  115. listView.snp.makeConstraints { make in
  116. make.horizontalEdges.equalToSuperview().inset(horizentalSpace)
  117. make.top.equalTo(titleView.snp.bottom).offset(6)
  118. }
  119. let bottom = buildBottomView()
  120. view.addSubview(bottom)
  121. bottom.snp.makeConstraints { make in
  122. make.horizontalEdges.equalToSuperview()
  123. make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
  124. make.top.equalTo(listView.snp.bottom).offset(-16)
  125. }
  126. }
  127. private func buildTitles() -> UIView {
  128. let stackView = UIStackView()
  129. stackView.axis = .vertical
  130. stackView.spacing = 6
  131. let mainLabel = UILabel()
  132. mainLabel.text = .init(key: "A00111")
  133. mainLabel.font = .heading_h1
  134. mainLabel.textColor = .text_5
  135. mainLabel.numberOfLines = 0
  136. stackView.addArrangedSubview(mainLabel)
  137. let descLabel = UILabel()
  138. descLabel.font = .body_m
  139. descLabel.textColor = .text_3
  140. descLabel.text = .init(key: "A00112")
  141. descLabel.numberOfLines = 0
  142. stackView.addArrangedSubview(descLabel)
  143. return stackView
  144. }
  145. private func buildListView() -> UIView {
  146. let count = floor(CGFloat((view.bounds.width - horizentalSpace * 2) + itemSpacing) / minWidth)
  147. let itemWidth = (view.bounds.width - horizentalSpace * 2 + itemSpacing) / count - itemSpacing
  148. collectionViewLayout.itemSize = .init(width: itemWidth, height: itemHeight)
  149. collectionViewLayout.minimumLineSpacing = lineSpace
  150. collectionViewLayout.minimumInteritemSpacing = itemSpacing
  151. collectionViewLayout.headerReferenceSize = .init(width: view.bounds.width - horizentalSpace * 2, height: Self.headerHeight)
  152. // collectionViewLayout.sectionHeadersPinToVisibleBounds = true
  153. collectionView.backgroundColor = .clear
  154. collectionView.dataSource = self
  155. collectionView.delegate = self
  156. collectionView.showsHorizontalScrollIndicator = false
  157. collectionView.showsVerticalScrollIndicator = false
  158. collectionView.register(LNInterestItemCell.self, forCellWithReuseIdentifier: LNInterestItemCell.className)
  159. collectionView.register(LNInterestGroupHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: LNInterestGroupHeader.className)
  160. collectionView.contentInset = .init(top: 0, left: 0, bottom: 20, right: 0)
  161. return collectionView
  162. }
  163. private func buildBottomView() -> UIView {
  164. let container = UIView()
  165. bottomGradient.colors = [
  166. UIColor.white.withAlphaComponent(0).cgColor,
  167. UIColor.primary_1.cgColor,
  168. UIColor.primary_1.cgColor
  169. ]
  170. bottomGradient.locations = [0, 0.3, 1]
  171. bottomGradient.startPoint = .init(x: 0, y: 0)
  172. bottomGradient.endPoint = .init(x: 0, y: 1)
  173. container.layer.addSublayer(bottomGradient)
  174. nextButton.setBackgroundImage(.primary_8, for: .normal)
  175. nextButton.setTitle(.init(key: "A00113"), for: .normal)
  176. nextButton.setTitleColor(.text_1, for: .normal)
  177. nextButton.titleLabel?.font = .heading_h3
  178. nextButton.layer.cornerRadius = 23.5
  179. nextButton.clipsToBounds = true
  180. nextButton.isEnabled = false
  181. nextButton.addAction(UIAction(handler: { [weak self] _ in
  182. guard let self else { return }
  183. let interests = selectedIndexs.compactMap {
  184. self.games[$0.section].children[$0.row].code
  185. }
  186. updateConfig.interest = interests
  187. LNProfileManager.shared.modifyMyProfile(config: updateConfig) { [weak self] success in
  188. guard let self else { return }
  189. guard success else { return }
  190. navigationController?.popToRootViewController(animated: true)
  191. LNGameMateManager.shared.getGameTypeList { _ in }
  192. }
  193. }), for: .touchUpInside)
  194. container.addSubview(nextButton)
  195. nextButton.snp.makeConstraints { make in
  196. make.horizontalEdges.equalToSuperview().inset(16)
  197. make.top.equalToSuperview().offset(16)
  198. make.bottom.equalToSuperview().offset(-4)
  199. make.height.equalTo(47)
  200. }
  201. return container
  202. }
  203. }
  204. private class LNInterestItemCell: UICollectionViewCell {
  205. private let ic = UIImageView()
  206. private let nameLabel = UILabel()
  207. private let bg = UIImageView()
  208. var inSelected: Bool = false {
  209. didSet {
  210. bg.image = inSelected ? .icLoginInterestItemSelected : .icLoginInterestItem
  211. }
  212. }
  213. var isEnable: Bool = true {
  214. didSet {
  215. contentView.alpha = isEnable ? 1.0 : 0.5
  216. isUserInteractionEnabled = isEnable
  217. }
  218. }
  219. override init(frame: CGRect) {
  220. super.init(frame: frame)
  221. contentView.addSubview(bg)
  222. bg.snp.makeConstraints { make in
  223. make.center.equalToSuperview()
  224. make.width.equalTo(102)
  225. make.height.equalTo(85)
  226. }
  227. ic.layer.cornerRadius = 25
  228. ic.clipsToBounds = true
  229. ic.backgroundColor = .white
  230. bg.addSubview(ic)
  231. ic.snp.makeConstraints { make in
  232. make.centerX.equalToSuperview()
  233. make.top.equalToSuperview().offset(7)
  234. make.width.height.equalTo(50)
  235. }
  236. nameLabel.font = .body_s
  237. nameLabel.textColor = .text_5
  238. nameLabel.textAlignment = .center
  239. bg.addSubview(nameLabel)
  240. nameLabel.snp.makeConstraints { make in
  241. make.horizontalEdges.equalToSuperview().inset(6)
  242. make.bottom.equalToSuperview().offset(-6)
  243. }
  244. }
  245. func update(_ item: LNGameCategoryItemVO) {
  246. ic.sd_setImage(with: URL(string: item.icon))
  247. nameLabel.text = item.name
  248. let curState = isSelected
  249. isSelected = curState
  250. }
  251. required init?(coder: NSCoder) {
  252. fatalError("init(coder:) has not been implemented")
  253. }
  254. }
  255. private class LNInterestGroupHeader: UICollectionReusableView {
  256. private let titleLabel = UILabel()
  257. override init(frame: CGRect) {
  258. super.init(frame: frame)
  259. snp.makeConstraints { make in
  260. make.height.equalTo(LNInterestSetupViewController.headerHeight)
  261. }
  262. titleLabel.font = .heading_h2
  263. titleLabel.textColor = .text_5
  264. addSubview(titleLabel)
  265. titleLabel.snp.makeConstraints { make in
  266. make.bottom.equalToSuperview().offset(-8)
  267. make.leading.equalToSuperview()
  268. }
  269. }
  270. func update(_ title: String) {
  271. titleLabel.text = title
  272. }
  273. required init?(coder: NSCoder) {
  274. fatalError("init(coder:) has not been implemented")
  275. }
  276. }