Skip to content

Commit

Permalink
[IDLE-000] 알림 동의 로직을 Domain에서 제거
Browse files Browse the repository at this point in the history
BaseFeature의 싱글톤 객체가 담당하도록 구현
  • Loading branch information
J0onYEong committed Oct 9, 2024
1 parent e82481f commit 3f5ef64
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import UserNotifications
import Core


Expand All @@ -23,56 +22,6 @@ public class DefaultSettingUseCase: SettingScreenUseCase {

public init() { }

public func checkPushNotificationApproved() -> Single<Bool> {
Single<Bool>.create { single in
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { settings in
switch settings.authorizationStatus {
case .notDetermined, .denied:
single(.success(false))
case .authorized, .provisional, .ephemeral:
single(.success(true))
@unknown default:
single(.success(false))
}
}

return Disposables.create { }
}
}

public func requestNotificationPermission() -> Maybe<NotificationApproveAction> {
Maybe<NotificationApproveAction>.create { maybe in

let current = UNUserNotificationCenter.current()

current.getNotificationSettings { [maybe] settings in
switch settings.authorizationStatus {
case .notDetermined:
// Request permission since the user hasn't decided yet.
current.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if error != nil {
maybe(.success(.error(message: "알람동의를 수행할 수 없습니다.")))
} else {
maybe(.success(.granted))
}
}
case .denied:
// 사용자가 요청을 거부했던 상태로 설정앱을 엽니다.
maybe(.success(.openSystemSetting))
return
case .authorized, .provisional, .ephemeral:
maybe(.success(.granted))
default:
maybe(.completed)
break
}
}

return Disposables.create { }
}
}

// MARK: 회원탈퇴 & 로그아웃
// 센터 회원탈퇴
public func deregisterCenterAccount(reasons: [String], password: String) -> RxSwift.Single<Result<Void, DomainError>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,8 @@ import Foundation

import RxSwift

public enum NotificationApproveAction: Equatable {
case openSystemSetting
case granted
case error(message: String)
}

public protocol SettingScreenUseCase: BaseUseCase {

/// 현재 알람수신 동의 여부를 확인합니다.
func checkPushNotificationApproved() -> Single<Bool>

/// 알림동의를 요청합니다.
func requestNotificationPermission() -> Maybe<NotificationApproveAction>


// MARK: Worker

/// 요양보호사 회원 탈퇴
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// RemoteNotificationAuthHelper.swift
// BaseFeature
//
// Created by choijunios on 10/9/24.
//

import Foundation
import UserNotifications


import RxSwift


public enum RemoteNotificationApproveAction: Equatable {
case openSystemSetting
case granted
case error(message: String)
}

public class RemoteNotificationAuthHelper {

public static let shared: RemoteNotificationAuthHelper = .init()

private init() { }

public func checkPushNotificationApproved() -> Single<Bool> {
Single<Bool>.create { single in
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { settings in
switch settings.authorizationStatus {
case .notDetermined, .denied:
single(.success(false))
case .authorized, .provisional, .ephemeral:
single(.success(true))
@unknown default:
single(.success(false))
}
}

return Disposables.create { }
}
}

public func requestNotificationPermission() -> Maybe<RemoteNotificationApproveAction> {
Maybe<RemoteNotificationApproveAction>.create { maybe in

let current = UNUserNotificationCenter.current()

current.getNotificationSettings { [maybe] settings in
switch settings.authorizationStatus {
case .notDetermined:
// Request permission since the user hasn't decided yet.
current.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if error != nil {
maybe(.success(.error(message: "알람동의를 수행할 수 없습니다.")))
} else {
maybe(.success(.granted))
}
}
case .denied:
// 사용자가 요청을 거부했던 상태로 설정앱을 엽니다.
maybe(.success(.openSystemSetting))
return
case .authorized, .provisional, .ephemeral:
maybe(.success(.granted))
default:
maybe(.completed)
break
}
}

return Disposables.create { }
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,16 @@ class CenterSettingViewModel: BaseViewModel, CenterSettingVMable {
)

let currentNotificationAuthStatus = refreshNotificationStatusRequest
.flatMap { [settingUseCase] _ in
settingUseCase.checkPushNotificationApproved()
.flatMap { _ in
RemoteNotificationAuthHelper.shared.checkPushNotificationApproved()
}

let requestApproveNotification = approveToPushNotification.filter { $0 }.map { _ in () }
let requestDenyNotification = approveToPushNotification.filter { !$0 }.map { _ in () }

let approveRequestResult = requestApproveNotification
.flatMap { [settingUseCase] _ in
settingUseCase
.requestNotificationPermission()
.flatMap { _ in
RemoteNotificationAuthHelper.shared.requestNotificationPermission()
}

let approveGranted = approveRequestResult.filter { $0 == .granted }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
//

import UIKit
import UserNotifications
import BaseFeature
import PresentationCore
import Domain
import DSKit
import UserNotifications
import Core


Expand Down Expand Up @@ -54,16 +54,16 @@ class SettingPageViewModel: BaseViewModel {
)

let currentNotificationAuthStatus = refreshNotificationStatusRequest
.flatMap { [settingUseCase] _ in
settingUseCase.checkPushNotificationApproved()
.flatMap { _ in
RemoteNotificationAuthHelper.shared.checkPushNotificationApproved()
}

let requestApproveNotification = approveToPushNotification.filter { $0 }.map { _ in () }
let requestDenyNotification = approveToPushNotification.filter { !$0 }.map { _ in () }

let approveRequestResult = requestApproveNotification
.flatMap { [settingUseCase] _ in
settingUseCase
.flatMap { _ in
RemoteNotificationAuthHelper.shared
.requestNotificationPermission()
}

Expand Down

0 comments on commit 3f5ef64

Please sign in to comment.