Skip to content

[IDLE-000] 알림 동의 로직을 Domain에서 제거 #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading