Skip to content

Commit

Permalink
[Feat] #219 - 알림권한 토글 기능 추가
Browse files Browse the repository at this point in the history
[Feat] #219 - 알림권한 토글 기능 추가
  • Loading branch information
yungu0010 authored Jan 11, 2024
2 parents 2dc3299 + 59e766e commit 6b963a6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import UIKit

import SnapKit
import Then
import UserNotifications

class MyInfoAccountStackView: UIView {
final class MyInfoAccountStackView: UIView {

// MARK: - UI Components

private let stackView = UIView()
Expand All @@ -20,6 +21,7 @@ class MyInfoAccountStackView: UIView {
let notificationSwitch = UISwitch()
private let lineView = UIView()
var isTapped: Bool = false
private var notificationSettings: UNNotificationSettings?
var switchClosure: ((_ isTapped: Bool) -> Void)?

// MARK: - View Life Cycle
Expand All @@ -28,11 +30,27 @@ class MyInfoAccountStackView: UIView {
super.init(frame: .zero)
setUI(title: title, isHidden: isHidden)
setLayout(isHidden: isHidden)
UNUserNotificationCenter.current().getNotificationSettings { settings in
self.isTapped = settings.authorizationStatus == .authorized
}

NotificationCenter.default.addObserver(
self,
selector: #selector(appWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

deinit {
NotificationCenter.default.removeObserver(
self,
name: UIApplication.willEnterForegroundNotification,
object: nil)
}
}

// MARK: - Methods
Expand All @@ -57,7 +75,7 @@ extension MyInfoAccountStackView {
}

notificationSwitch.do {
$0.isOn = true
$0.isOn = isTapped
$0.onTintColor = .green2
$0.addTarget(self, action: #selector(switchTapped), for: .valueChanged)
}
Expand All @@ -67,7 +85,7 @@ extension MyInfoAccountStackView {
$0.isHidden = isHidden ? true : false
}
}

private func setLayout(isHidden: Bool) {
addSubviews(lineView, stackView)
stackView.addSubviews(titleLabel, isHidden ? notificationSwitch : contentLabel)
Expand All @@ -82,7 +100,7 @@ extension MyInfoAccountStackView {
titleLabel.snp.makeConstraints {
$0.centerY.equalToSuperview()
}

if !isHidden {
contentLabel.snp.makeConstraints {
$0.centerY.equalToSuperview()
Expand All @@ -105,9 +123,48 @@ extension MyInfoAccountStackView {
}

@objc func switchTapped(_ sender: Any) {
isTapped.toggle()
switchClosure?(isTapped)

AmplitudeAnalyticsService.shared.send(event: isTapped ? AnalyticsEvent.AccountInfo.completePushOn : AnalyticsEvent.AccountInfo.completePushOff)

DispatchQueue.main.async {
do {
try self.toggleNotificationPermission()
} catch {
print("Error toggling notification permission: \(error)")
}
}
}

private func toggleNotificationPermission() throws {
do {
try self.openAppSettings()
} catch {
throw error
}
}

private func openAppSettings() throws {
let settingsUrl: URL
if #available(iOS 16.0, *) {
settingsUrl = URL(string: UIApplication.openNotificationSettingsURLString) ?? URL(string: "")!
} else {
settingsUrl = URL(string: UIApplication.openSettingsURLString) ?? URL(string: "")!
}

if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { success in
print("iOS 설정 앱 열기: \(success)")
})
}
}

@objc
private func appWillEnterForeground() {
UNUserNotificationCenter.current().getNotificationSettings { [weak self] settings in
DispatchQueue.main.async {
self?.notificationSettings = settings
self?.isTapped = settings.authorizationStatus == .authorized
self?.switchClosure?(self!.isTapped)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UIKit

import KakaoSDKUser

class MyInfoAccountViewController: UIViewController {
final class MyInfoAccountViewController: UIViewController {

// MARK: - UI Components

Expand Down Expand Up @@ -41,11 +41,8 @@ class MyInfoAccountViewController: UIViewController {

private extension MyInfoAccountViewController {
func setUI() {

self.notificationView.switchClosure = { result in
if result {
self.notificationView.notificationSwitch.setOn(result, animated: true)
}
self.notificationView.notificationSwitch.setOn(result, animated: true)
}

view.backgroundColor = .ntdBlack
Expand Down

0 comments on commit 6b963a6

Please sign in to comment.