Skip to content

Commit 71e57d2

Browse files
committed
[#78] 푸시 알림 토글 스위치 생성
1 parent ffc378e commit 71e57d2

File tree

4 files changed

+57
-21
lines changed

4 files changed

+57
-21
lines changed

EATSSU_MVC/EATSSU_MVC/Sources/Notification/NotificationManager.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class NotificationManager {
1616

1717
// MARK: - Methods
1818

19-
/// 평일 11시에 앱의 유입을 유도하는 알림을 발송하는 메소드
19+
/// 평일 11시에 앱의 유입을 유도하는 푸시 알림을 발송하는 메소드
2020
///
2121
/// - Title : 🤔 오늘 밥 뭐 먹지…
2222
/// - Body : 오늘의 학식을 확인해보세요!
@@ -31,7 +31,6 @@ class NotificationManager {
3131
content.sound = .default
3232

3333
// 반복할 요일 및 시간 설정 (평일 오전 11시)
34-
let calendar = Calendar.current
3534
let weekdays = [2, 3, 4, 5, 6] // 월, 화, 수, 목, 금 (Calendar에서 1이 일요일)
3635

3736
for weekday in weekdays {
@@ -55,6 +54,15 @@ class NotificationManager {
5554
}
5655
}
5756
}
57+
58+
/// 평일 11시에 앱의 유입을 유도하는 푸시 알림을 취소하는 메소드
59+
func cancelWeekday11AMNotification() {
60+
let weekday = [2, 3, 4, 5, 6]
61+
let identifier = "weekdayNotification-\(weekday)"
62+
63+
let center = UNUserNotificationCenter.current()
64+
center.removePendingNotificationRequests(withIdentifiers: [identifier])
65+
}
5866

5967
/// 앱 실행 시 알림 발송 권한을 요청하는 팝업 호출 메소드
6068
func requestNotificationPermission() {

EATSSU_MVC/EATSSU_MVC/Sources/Presentation/MyPage/View/MyPageView/Cell/NotificationSettingTableViewCell.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class NotificationSettingTableViewCell: UITableViewCell {
4343
internal let toggleSwitch: UISwitch = {
4444
let toggleSwitch = UISwitch()
4545
toggleSwitch.onTintColor = EATSSUAsset.Color.Main.primary.color
46+
toggleSwitch.isOn = UserDefaults.standard.bool(forKey: TextLiteral.MyPage.pushNotificationUserSettingKey)
4647
return toggleSwitch
4748
}()
4849

EATSSU_MVC/EATSSU_MVC/Sources/Presentation/MyPage/ViewController/MyPageViewController.swift

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ final class MyPageViewController: BaseViewController {
3333

3434
private let myProvider = MoyaProvider<MyRouter>(plugins: [MoyaLoggingPlugin()])
3535
private var nickName = ""
36+
private var switchState = false
37+
private let userDefaultsKey = TextLiteral.MyPage.pushNotificationUserSettingKey
3638
private let myPageTableLabelList = MyPageLocalData.myPageTableLabelList
3739

3840
// MARK: - UI Components
@@ -44,14 +46,15 @@ final class MyPageViewController: BaseViewController {
4446
override func viewDidLoad() {
4547
super.viewDidLoad()
4648

47-
setDelegate()
48-
Analytics.logEvent("MypageViewControllerLoad", parameters: nil)
49+
Analytics.logEvent("MypageViewControllerLoad", parameters: nil)
50+
setTableViewDelegate()
51+
loadSwitchStateFromUserDefaults()
4952
}
5053

5154
override func viewWillAppear(_ animated: Bool) {
5255
super.viewWillAppear(animated)
5356

54-
nickName = UserInfoManager.shared.getCurrentUserInfo()?.nickname ?? "fail"
57+
nickName = UserInfoManager.shared.getCurrentUserInfo()?.nickname ?? "실패"
5558
mypageView.setUserInfo(nickname: nickName)
5659
}
5760

@@ -79,28 +82,20 @@ final class MyPageViewController: BaseViewController {
7982
action: #selector(didTappedChangeNicknameButton),
8083
for: .touchUpInside)
8184
}
82-
83-
@objc
84-
func toggleSwitchTapped() {
85-
86-
}
8785

8886
@objc
89-
func didTappedChangeNicknameButton() {
90-
87+
private func didTappedChangeNicknameButton() {
9188
let setNickNameVC = SetNickNameViewController()
9289
self.navigationController?.pushViewController(setNickNameVC, animated: true)
9390
}
9491

95-
func setDelegate() {
92+
/// TableViewDelegate & DataSource를 해당 클래스로 할당합니다.
93+
private func setTableViewDelegate() {
9694
mypageView.myPageTableView.dataSource = self
9795
mypageView.myPageTableView.delegate = self
9896
}
9997

100-
/*
101-
해야 할 일
102-
- 알림 팝업을 띄우는 코드를 모듈화
103-
*/
98+
/// 로그아웃 Alert를 스크린에 표시하는 메소드
10499
private func logoutShowAlert() {
105100
let alert = UIAlertController(title: "로그아웃",
106101
message: "정말 로그아웃 하시겠습니까?",
@@ -128,6 +123,18 @@ final class MyPageViewController: BaseViewController {
128123

129124
present(alert, animated: true, completion: nil)
130125
}
126+
127+
/// UserDefaults에 스위치 상태 저장
128+
private func saveSwitchStateToUserDefaults() {
129+
print("사용자 푸시 알림 값을 앱 저장소에 보관합니다.")
130+
UserDefaults.standard.set(switchState, forKey: userDefaultsKey)
131+
}
132+
133+
/// UserDefaults에서 스위치 상태 불러오기
134+
private func loadSwitchStateFromUserDefaults() {
135+
print("사용자 푸시 알림 값을 앱 저장소에서 불러옵니다.")
136+
switchState = UserDefaults.standard.bool(forKey: userDefaultsKey)
137+
}
131138
}
132139

133140
// MARK: - TableView DataSource
@@ -145,7 +152,7 @@ extension MyPageViewController: UITableViewDataSource {
145152
withIdentifier: NotificationSettingTableViewCell.identifier,
146153
for: indexPath) as! NotificationSettingTableViewCell
147154

148-
cell.toggleSwitch.addTarget(self, action: #selector(toggleSwitchTapped), for: .valueChanged)
155+
cell.toggleSwitch.isOn = switchState
149156

150157
return cell
151158
} else {
@@ -173,6 +180,7 @@ extension MyPageViewController: UITableViewDelegate {
173180
tableView.deselectRow(at: indexPath, animated: true)
174181

175182
switch indexPath.row {
183+
176184
// "내가 쓴 리뷰" 스크린으로 이동
177185
case MyPageLabels.MyReview.rawValue:
178186
let myReviewViewController = MyReviewViewController()
@@ -181,8 +189,24 @@ extension MyPageViewController: UITableViewDelegate {
181189
// "푸시 알림 설정" 스위치 토글
182190
case MyPageLabels.NotificationSetting.rawValue:
183191
if let cell = tableView.cellForRow(at: indexPath) as? NotificationSettingTableViewCell {
184-
// TODO: 스위치 값을 앱 저장소에 보관하고, 값에 따라 알림을 보내는 여부를 제어하는 코드를 설계할 것
185-
cell.toggleSwitch.isOn.toggle()
192+
193+
// 현재 스위치 상태를 반전
194+
let newSwitchState = !switchState
195+
cell.toggleSwitch.setOn(newSwitchState, animated: true)
196+
197+
// 스위치 상태를 업데이트
198+
switchState = newSwitchState
199+
200+
if switchState {
201+
print("푸시 알림을 발송합니다.")
202+
NotificationManager.shared.scheduleWeekday11AMNotification()
203+
} else {
204+
print("푸시 알림을 발송하지 않습니다.")
205+
NotificationManager.shared.cancelWeekday11AMNotification()
206+
}
207+
208+
// UserDefaults에 상태 저장
209+
saveSwitchStateToUserDefaults()
186210
}
187211

188212
// "문의하기" 스크린으로 이동

EATSSU_MVC/EATSSU_MVC/Sources/Utility/Literal/TextLiteral.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ enum TextLiteral {
7070

7171
/// "마이페이지" 텍스트 리터럴
7272
enum MyPage {
73-
73+
74+
/// "푸시 알림 사용자 설정 접근 키"
75+
static let pushNotificationUserSettingKey: String = "pushNotificationUserSettingKey"
76+
7477
/// "푸시 알림 설정"
7578
static let pushNotificationSetting: String = "푸시 알림 설정"
7679

0 commit comments

Comments
 (0)