LNSortedEditView.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // LNSortedEditView.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/11/14.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. enum LNSortedType: Int, CaseIterable {
  11. case none = -1
  12. case up = 0
  13. case down = 1
  14. var text: String {
  15. switch self {
  16. case .none: .init(key: "A00008")
  17. case .up: .init(key: "A00009")
  18. case .down: .init(key: "A00010")
  19. }
  20. }
  21. }
  22. protocol LNSortedEditViewDelegate: NSObject {
  23. func sortedEditView(view: LNSortedEditView, sortedTypeChanged: LNSortedType)
  24. }
  25. class LNSortedEditView: UIView {
  26. let titleLabel = UILabel()
  27. private let icon = UIImageView()
  28. weak var delegate: LNSortedEditViewDelegate?
  29. var curType: LNSortedType = .none {
  30. didSet {
  31. guard oldValue != curType else { return }
  32. icon.image = switch curType {
  33. case .none: .icSortedNone
  34. case .up: .icSortedUp
  35. case .down: .icSortedDown
  36. }
  37. delegate?.sortedEditView(view: self, sortedTypeChanged: curType)
  38. }
  39. }
  40. override init(frame: CGRect) {
  41. super.init(frame: frame)
  42. setupViews()
  43. }
  44. required init?(coder: NSCoder) {
  45. fatalError("init(coder:) has not been implemented")
  46. }
  47. }
  48. extension LNSortedEditView {
  49. private func setupViews() {
  50. titleLabel.textColor = .text_4
  51. titleLabel.font = .body_xs
  52. addSubview(titleLabel)
  53. titleLabel.snp.makeConstraints { make in
  54. make.leading.equalToSuperview()
  55. make.centerY.equalToSuperview()
  56. }
  57. icon.image = .icSortedNone
  58. addSubview(icon)
  59. icon.snp.makeConstraints { make in
  60. make.top.bottom.trailing.equalToSuperview()
  61. make.leading.equalTo(titleLabel.snp.trailing)
  62. }
  63. onTap { [weak self] in
  64. guard let self else { return }
  65. curType = switch curType {
  66. case .none : .up
  67. case .up: .down
  68. case .down: .none
  69. }
  70. }
  71. }
  72. }
  73. #if DEBUG
  74. import SwiftUI
  75. struct LNSortedEditViewPreview: UIViewRepresentable {
  76. func makeUIView(context: Context) -> some UIView {
  77. let container = UIView()
  78. container.backgroundColor = .lightGray
  79. let view = LNSortedEditView()
  80. view.titleLabel.text = "some thing"
  81. container.addSubview(view)
  82. view.snp.makeConstraints { make in
  83. make.center.equalToSuperview()
  84. }
  85. return container
  86. }
  87. func updateUIView(_ uiView: UIViewType, context: Context) { }
  88. }
  89. #Preview(body: {
  90. LNSortedEditViewPreview()
  91. })
  92. #endif