LNCommonAlertView.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // LNCommonAlertView.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/12.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. extension LNCommonAlertView {
  11. static func showCommonDeleteAlert(handler: (() -> Void)?) {
  12. let alert = LNCommonAlertView()
  13. alert.titleLabel.text = .init(key: "A00308")
  14. alert.showConfirm {
  15. handler?()
  16. }
  17. alert.showCancel()
  18. alert.popup()
  19. }
  20. }
  21. class LNCommonAlertView: UIView {
  22. private let background = UIView()
  23. private let closeButton = UIButton()
  24. var showCloseButton = true {
  25. didSet {
  26. closeButton.isHidden = !showCloseButton
  27. }
  28. }
  29. private let miniScale = 0.01
  30. private let animateDuration = 0.15
  31. private let textViews = UIStackView()
  32. private let messageView = UIStackView()
  33. private let buttonViews = UIStackView()
  34. let container = UIView()
  35. let titleLabel = UILabel()
  36. let messageLabel = UILabel()
  37. var touchOutsideCancel = true
  38. override init(frame: CGRect) {
  39. super.init(frame: frame)
  40. setupViews()
  41. }
  42. func showConfirm(_ title: String = .init(key: "A00002"),
  43. autoDismiss: Bool = true,
  44. handler: @escaping () -> Void) {
  45. let button = buildConfirmButton()
  46. button.setTitle(title, for: .normal)
  47. button.addAction(UIAction(handler: { [weak self] _ in
  48. guard let self else { return }
  49. if autoDismiss {
  50. dismiss()
  51. }
  52. handler()
  53. }), for: .touchUpInside)
  54. buttonViews.insertArrangedSubview(button, at: 0)
  55. }
  56. func showCancel(_ title: String = .init(key: "A00003"),
  57. autoDismiss: Bool = true,
  58. handler: (() -> Void)? = nil) {
  59. let button = buildCancelButton()
  60. button.setTitle(title, for: .normal)
  61. button.addAction(UIAction(handler: { [weak self] _ in
  62. guard let self else { return }
  63. if autoDismiss {
  64. dismiss()
  65. }
  66. handler?()
  67. }), for: .touchUpInside)
  68. buttonViews.addArrangedSubview(button)
  69. }
  70. func showCustomAction(_ view: UIView) {
  71. buttonViews.addArrangedSubview(view)
  72. }
  73. func showCustomMessage(_ view: UIView) {
  74. messageView.addArrangedSubview(view)
  75. }
  76. required init?(coder: NSCoder) {
  77. fatalError("init(coder:) has not been implemented")
  78. }
  79. }
  80. extension LNCommonAlertView {
  81. func popup(_ holder: UIView? = nil) {
  82. let parentView: UIView? = if let window = holder as? UIWindow {
  83. window
  84. } else if let view = holder?.viewController?.view {
  85. view
  86. } else if let window = UIView.appKeyWindow {
  87. window
  88. } else {
  89. nil
  90. }
  91. guard let parentView else { return }
  92. parentView.addSubview(self)
  93. frame = parentView.bounds
  94. titleLabel.isHidden = titleLabel.text?.isEmpty != false
  95. messageLabel.isHidden = messageLabel.text?.isEmpty != false
  96. messageView.isHidden = messageLabel.isHidden
  97. layoutIfNeeded()
  98. container.transform = .init(scaleX: miniScale, y: miniScale)
  99. UIView.animate(withDuration: animateDuration, delay: 0,
  100. options: .curveEaseInOut)
  101. { [weak self] in
  102. guard let self else { return }
  103. container.transform = .identity
  104. }
  105. }
  106. func dismiss() {
  107. UIView.animate(withDuration: animateDuration, delay: 0,
  108. options: .curveEaseInOut)
  109. { [weak self] in
  110. guard let self else { return }
  111. container.transform = .init(scaleX: miniScale, y: miniScale)
  112. } completion: { [weak self] _ in
  113. guard let self else { return }
  114. removeFromSuperview()
  115. }
  116. }
  117. }
  118. extension LNCommonAlertView {
  119. private func setupViews() {
  120. background.backgroundColor = .black.withAlphaComponent(0.4)
  121. background.onTap { [weak self] in
  122. guard let self else { return }
  123. guard touchOutsideCancel else { return }
  124. dismiss()
  125. }
  126. addSubview(background)
  127. background.snp.makeConstraints { make in
  128. make.edges.equalToSuperview()
  129. }
  130. container.backgroundColor = .fill
  131. container.layer.cornerRadius = 20
  132. container.clipsToBounds = true
  133. addSubview(container)
  134. container.snp.makeConstraints { make in
  135. make.center.equalToSuperview()
  136. make.width.equalToSuperview().multipliedBy(0.8)
  137. }
  138. closeButton.setImage(.init(systemName: "xmark"), for: .normal)
  139. closeButton.tintColor = .text_2
  140. closeButton.addAction(UIAction(handler: { [weak self] _ in
  141. guard let self else { return }
  142. dismiss()
  143. }), for: .touchUpInside)
  144. container.addSubview(closeButton)
  145. closeButton.snp.makeConstraints { make in
  146. make.top.equalToSuperview().offset(10)
  147. make.trailing.equalToSuperview().offset(-10)
  148. make.width.height.equalTo(24)
  149. }
  150. textViews.axis = .vertical
  151. textViews.spacing = 10
  152. container.addSubview(textViews)
  153. textViews.snp.makeConstraints { make in
  154. make.horizontalEdges.equalToSuperview().inset(24)
  155. make.top.equalToSuperview().offset(30)
  156. }
  157. titleLabel.font = .heading_h3
  158. titleLabel.textColor = .text_4
  159. titleLabel.textAlignment = .center
  160. titleLabel.numberOfLines = 0
  161. textViews.addArrangedSubview(titleLabel)
  162. messageView.axis = .vertical
  163. messageView.spacing = 6
  164. textViews.addArrangedSubview(messageView)
  165. messageLabel.font = .body_m
  166. messageLabel.textColor = .text_4
  167. messageLabel.textAlignment = .center
  168. messageLabel.numberOfLines = 0
  169. messageView.addArrangedSubview(messageLabel)
  170. buttonViews.axis = .vertical
  171. buttonViews.spacing = 16
  172. container.addSubview(buttonViews)
  173. buttonViews.snp.makeConstraints { make in
  174. make.horizontalEdges.equalToSuperview().inset(50)
  175. make.top.equalTo(textViews.snp.bottom).offset(16)
  176. make.bottom.equalToSuperview().offset(-30)
  177. }
  178. }
  179. private func buildConfirmButton() -> UIButton {
  180. let button = UIButton()
  181. button.setBackgroundImage(.primary_8, for: .normal)
  182. button.setTitleColor(.text_1, for: .normal)
  183. button.titleLabel?.font = .heading_h4
  184. button.layer.cornerRadius = 18
  185. button.clipsToBounds = true
  186. button.snp.makeConstraints { make in
  187. make.height.equalTo(36)
  188. }
  189. return button
  190. }
  191. private func buildCancelButton() -> UIButton {
  192. let button = UIButton()
  193. button.setTitleColor(.text_4, for: .normal)
  194. button.titleLabel?.font = .body_m
  195. button.layer.cornerRadius = 18
  196. button.layer.borderWidth = 1
  197. button.layer.borderColor = UIColor.text_3.cgColor
  198. button.clipsToBounds = true
  199. button.snp.makeConstraints { make in
  200. make.height.equalTo(36)
  201. }
  202. return button
  203. }
  204. }
  205. #if DEBUG
  206. import SwiftUI
  207. struct LNCommonAlertViewPreview: UIViewRepresentable {
  208. func makeUIView(context: Context) -> some UIView {
  209. let container = UIView()
  210. container.backgroundColor = .lightGray
  211. let button = UIButton()
  212. button.backgroundColor = .red
  213. button.addAction(UIAction(handler: { [weak container] _ in
  214. guard let container else { return }
  215. let view = LNCommonAlertView()
  216. view.popup(container)
  217. }), for: .touchUpInside)
  218. container.addSubview(button)
  219. button.snp.makeConstraints { make in
  220. make.center.equalToSuperview()
  221. make.width.height.equalTo(50)
  222. }
  223. return container
  224. }
  225. func updateUIView(_ uiView: UIViewType, context: Context) { }
  226. }
  227. #Preview(body: {
  228. LNCommonAlertViewPreview()
  229. })
  230. #endif