LNCommonAlertView.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 container = UIView()
  24. private let closeButton = UIButton()
  25. var showCloseButton = true {
  26. didSet {
  27. closeButton.isHidden = !showCloseButton
  28. }
  29. }
  30. private let miniScale = 0.01
  31. private let animateDuration = 0.15
  32. private let textViews = UIStackView()
  33. private let messageView = UIStackView()
  34. private let buttonViews = UIStackView()
  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. addSubview(container)
  133. container.snp.makeConstraints { make in
  134. make.center.equalToSuperview()
  135. make.width.equalToSuperview().multipliedBy(0.8)
  136. }
  137. closeButton.setImage(.init(systemName: "xmark"), for: .normal)
  138. closeButton.tintColor = .text_2
  139. closeButton.addAction(UIAction(handler: { [weak self] _ in
  140. guard let self else { return }
  141. dismiss()
  142. }), for: .touchUpInside)
  143. container.addSubview(closeButton)
  144. closeButton.snp.makeConstraints { make in
  145. make.top.equalToSuperview().offset(10)
  146. make.trailing.equalToSuperview().offset(-10)
  147. make.width.height.equalTo(24)
  148. }
  149. textViews.axis = .vertical
  150. textViews.spacing = 10
  151. container.addSubview(textViews)
  152. textViews.snp.makeConstraints { make in
  153. make.horizontalEdges.equalToSuperview().inset(24)
  154. make.top.equalToSuperview().offset(30)
  155. }
  156. titleLabel.font = .heading_h3
  157. titleLabel.textColor = .text_4
  158. titleLabel.textAlignment = .center
  159. titleLabel.numberOfLines = 0
  160. textViews.addArrangedSubview(titleLabel)
  161. messageView.axis = .vertical
  162. messageView.spacing = 6
  163. textViews.addArrangedSubview(messageView)
  164. messageLabel.font = .body_m
  165. messageLabel.textColor = .text_4
  166. messageLabel.textAlignment = .center
  167. messageLabel.numberOfLines = 0
  168. messageView.addArrangedSubview(messageLabel)
  169. buttonViews.axis = .vertical
  170. buttonViews.spacing = 16
  171. container.addSubview(buttonViews)
  172. buttonViews.snp.makeConstraints { make in
  173. make.horizontalEdges.equalToSuperview().inset(50)
  174. make.top.equalTo(textViews.snp.bottom).offset(16)
  175. make.bottom.equalToSuperview().offset(-30)
  176. }
  177. }
  178. private func buildConfirmButton() -> UIButton {
  179. let button = UIButton()
  180. button.setBackgroundImage(.primary_8, for: .normal)
  181. button.setTitleColor(.text_1, for: .normal)
  182. button.titleLabel?.font = .heading_h4
  183. button.layer.cornerRadius = 18
  184. button.clipsToBounds = true
  185. button.snp.makeConstraints { make in
  186. make.height.equalTo(36)
  187. }
  188. return button
  189. }
  190. private func buildCancelButton() -> UIButton {
  191. let button = UIButton()
  192. button.setTitleColor(.text_4, for: .normal)
  193. button.titleLabel?.font = .body_m
  194. button.layer.cornerRadius = 18
  195. button.layer.borderWidth = 1
  196. button.layer.borderColor = UIColor.text_3.cgColor
  197. button.clipsToBounds = true
  198. button.snp.makeConstraints { make in
  199. make.height.equalTo(36)
  200. }
  201. return button
  202. }
  203. }
  204. #if DEBUG
  205. import SwiftUI
  206. struct LNCommonAlertViewPreview: UIViewRepresentable {
  207. func makeUIView(context: Context) -> some UIView {
  208. let container = UIView()
  209. container.backgroundColor = .lightGray
  210. let button = UIButton()
  211. button.backgroundColor = .red
  212. button.addAction(UIAction(handler: { [weak container] _ in
  213. guard let container else { return }
  214. let view = LNCommonAlertView()
  215. view.popup(container)
  216. }), for: .touchUpInside)
  217. container.addSubview(button)
  218. button.snp.makeConstraints { make in
  219. make.center.equalToSuperview()
  220. make.width.height.equalTo(50)
  221. }
  222. return container
  223. }
  224. func updateUIView(_ uiView: UIViewType, context: Context) { }
  225. }
  226. #Preview(body: {
  227. LNCommonAlertViewPreview()
  228. })
  229. #endif