LNIMNotificationPermissionView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // LNIMNotificationPermissionView.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/30.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNIMNotificationPermissionView: UIView {
  11. override init(frame: CGRect) {
  12. super.init(frame: frame)
  13. setupViews()
  14. checkPermission()
  15. }
  16. required init?(coder: NSCoder) {
  17. fatalError("init(coder:) has not been implemented")
  18. }
  19. }
  20. extension LNIMNotificationPermissionView {
  21. private func requestPermission() {
  22. LNPermissionHelper.requestNotificationAuthorization(options: [.alert, .sound, .badge])
  23. { [weak self] granted, _ in
  24. guard let self else { return }
  25. guard granted else { return }
  26. UIApplication.shared.registerForRemoteNotifications()
  27. checkPermission()
  28. }
  29. }
  30. private func checkPermission() {
  31. LNPermissionHelper.fetchNotificationSettings { [weak self] settings in
  32. guard let self else { return }
  33. switch settings.authorizationStatus {
  34. case .authorized, .provisional, .ephemeral:
  35. isHidden = true
  36. break
  37. case .notDetermined:
  38. requestPermission()
  39. isHidden = false
  40. default:
  41. isHidden = false
  42. break
  43. }
  44. }
  45. }
  46. private func openNotificationSettings() {
  47. let settingsURL: URL? =
  48. if #available(iOS 16.0, *) {
  49. URL(string: UIApplication.openNotificationSettingsURLString)
  50. } else {
  51. URL(string: UIApplication.openSettingsURLString)
  52. }
  53. guard let settingsURL else { return }
  54. guard UIApplication.shared.canOpenURL(settingsURL) else { return }
  55. UIApplication.shared.open(settingsURL) { _ in }
  56. }
  57. }
  58. extension LNIMNotificationPermissionView {
  59. private func setupViews() {
  60. let container = UIView()
  61. container.backgroundColor = .fill
  62. container.layer.cornerRadius = 12
  63. addSubview(container)
  64. container.snp.makeConstraints { make in
  65. make.horizontalEdges.equalToSuperview().inset(16)
  66. make.verticalEdges.equalToSuperview()
  67. make.height.equalTo(50)
  68. }
  69. let close = UIButton()
  70. close.setImage(.init(systemName: "xmark")?.withRenderingMode(.alwaysTemplate), for: .normal)
  71. close.tintColor = .text_2
  72. close.addAction(UIAction(handler: { [weak self] _ in
  73. guard let self else { return }
  74. isHidden = true
  75. }), for: .touchUpInside)
  76. container.addSubview(close)
  77. close.snp.makeConstraints { make in
  78. make.leading.equalToSuperview().offset(10)
  79. make.centerY.equalToSuperview()
  80. make.width.height.equalTo(20)
  81. }
  82. let open = UIButton()
  83. open.setTitle(.init(key: "A00095"), for: .normal)
  84. open.titleLabel?.font = .body_s
  85. open.setTitleColor(.text_1, for: .normal)
  86. open.setBackgroundImage(.primary_7, for: .normal)
  87. open.contentEdgeInsets = .init(top: 5, left: 10, bottom: 5, right: 10)
  88. open.clipsToBounds = true
  89. open.layer.cornerRadius = 12
  90. open.addAction(UIAction(handler: { [weak self] _ in
  91. guard let self else { return }
  92. openNotificationSettings()
  93. }), for: .touchUpInside)
  94. container.addSubview(open)
  95. open.snp.makeConstraints { make in
  96. make.trailing.equalToSuperview().offset(-10)
  97. make.centerY.equalToSuperview()
  98. make.height.equalTo(24)
  99. }
  100. let textView = UIView()
  101. container.addSubview(textView)
  102. textView.snp.makeConstraints { make in
  103. make.centerY.equalToSuperview()
  104. make.leading.equalTo(close.snp.trailing).offset(12)
  105. make.trailing.lessThanOrEqualTo(open.snp.leading).offset(-12)
  106. }
  107. let titleLabel = UILabel()
  108. titleLabel.text = .init(key: "A00096")
  109. titleLabel.font = .heading_h5
  110. titleLabel.textColor = .text_5
  111. textView.addSubview(titleLabel)
  112. titleLabel.snp.makeConstraints { make in
  113. make.leading.top.equalToSuperview()
  114. }
  115. let textLabel = UILabel()
  116. textLabel.text = .init(key: "A00097")
  117. textLabel.font = .body_xs
  118. textLabel.textColor = .text_5
  119. textView.addSubview(textLabel)
  120. textLabel.snp.makeConstraints { make in
  121. make.horizontalEdges.equalToSuperview()
  122. make.bottom.equalToSuperview()
  123. make.top.equalTo(titleLabel.snp.bottom).offset(2)
  124. }
  125. }
  126. }