Skip to content

Commit

Permalink
Merge pull request #438 from hyun99999/feature/#437
Browse files Browse the repository at this point in the history
[Feat] #437 - 마이페이지 알림뷰 서버연결
  • Loading branch information
hyun99999 authored Mar 29, 2022
2 parents 688d489 + c207f32 commit 8f14a46
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 12 deletions.
8 changes: 8 additions & 0 deletions Spark-iOS/Spark-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@
F867B6B6278F86610024B1D4 /* UINavigationController+.swift in Sources */ = {isa = PBXBuildFile; fileRef = F867B6B5278F86610024B1D4 /* UINavigationController+.swift */; };
F86C68B727955ABA009A5296 /* SparkFlake.swift in Sources */ = {isa = PBXBuildFile; fileRef = F86C68B627955ABA009A5296 /* SparkFlake.swift */; };
F86C68B927958AA9009A5296 /* UIImageView+.swift in Sources */ = {isa = PBXBuildFile; fileRef = F86C68B827958AA9009A5296 /* UIImageView+.swift */; };
F86FC47A27F1B835004478F3 /* NoticeSetting.swift in Sources */ = {isa = PBXBuildFile; fileRef = F86FC47927F1B835004478F3 /* NoticeSetting.swift */; };
F86FC47C27F1C8B3004478F3 /* NotificationCellDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F86FC47B27F1C8B3004478F3 /* NotificationCellDelegate.swift */; };
F8902FB927ED84060034E8F9 /* Withdrawal.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F8902FB827ED84060034E8F9 /* Withdrawal.storyboard */; };
F8902FBD27ED844E0034E8F9 /* WithdrawalVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8902FBC27ED844E0034E8F9 /* WithdrawalVC.swift */; };
F8ABAEFD279615DA00C0F774 /* splash_bg_iOS.json in Resources */ = {isa = PBXBuildFile; fileRef = F8ABAEFC279615DA00C0F774 /* splash_bg_iOS.json */; };
Expand Down Expand Up @@ -416,6 +418,8 @@
F867B6B5278F86610024B1D4 /* UINavigationController+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UINavigationController+.swift"; sourceTree = "<group>"; };
F86C68B627955ABA009A5296 /* SparkFlake.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SparkFlake.swift; sourceTree = "<group>"; };
F86C68B827958AA9009A5296 /* UIImageView+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView+.swift"; sourceTree = "<group>"; };
F86FC47927F1B835004478F3 /* NoticeSetting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoticeSetting.swift; sourceTree = "<group>"; };
F86FC47B27F1C8B3004478F3 /* NotificationCellDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationCellDelegate.swift; sourceTree = "<group>"; };
F8902FB827ED84060034E8F9 /* Withdrawal.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Withdrawal.storyboard; sourceTree = "<group>"; };
F8902FBC27ED844E0034E8F9 /* WithdrawalVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WithdrawalVC.swift; sourceTree = "<group>"; };
F8ABAEFC279615DA00C0F774 /* splash_bg_iOS.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = splash_bg_iOS.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -693,6 +697,7 @@
children = (
2BE907E327D9F1BB00B36002 /* ActiveNotice.swift */,
2B2D31B727DA68EE000EDD28 /* ServiceNotice.swift */,
F86FC47927F1B835004478F3 /* NoticeSetting.swift */,
);
path = Notice;
sourceTree = "<group>";
Expand Down Expand Up @@ -1106,6 +1111,7 @@
EBD0D09327DBBC270007E5AF /* SendSparkCellDelegate.swift */,
F821812F27DE225200DDFBF8 /* SparkTabBar */,
F8B18E0227EF9903001A1A7D /* WithdrawalCellDelegate.swift */,
F86FC47B27F1C8B3004478F3 /* NotificationCellDelegate.swift */,
);
path = Protocols;
sourceTree = "<group>";
Expand Down Expand Up @@ -1727,9 +1733,11 @@
F8E3496A27969B67001B67E7 /* AuthService.swift in Sources */,
2BA98FB227CB44E800C53820 /* HabitRoomLeaveVC.swift in Sources */,
2BE907E127D9EE9700B36002 /* NoticeService.swift in Sources */,
F86FC47C27F1C8B3004478F3 /* NotificationCellDelegate.swift in Sources */,
F821813327DE512D00DDFBF8 /* SparkTabBarItem.swift in Sources */,
F8096F3027841F9200B71D38 /* HomeVC.swift in Sources */,
F8902FBD27ED844E0034E8F9 /* WithdrawalVC.swift in Sources */,
F86FC47A27F1B835004478F3 /* NoticeSetting.swift in Sources */,
F8250CFA27D25F2B0073FFCA /* UserService.swift in Sources */,
F82F5808279289B2003E4174 /* HomeAPI.swift in Sources */,
EBA93A7F279080E2009AE771 /* JoinCheckVC.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ extension SendSparkCVC {

@objc
func touchSendSparkButton(_ sender: SendSparkButton) {
print(sender.tag)
if sender.type == .message {
sendSparkCellDelegate?.showTextField()
} else {
Expand Down
23 changes: 21 additions & 2 deletions Spark-iOS/Spark-iOS/Source/Cells/Mypage/NotificationTVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class NotificationTVC: UITableViewCell {
private let notificationSwitch = UISwitch()
private let lineView = UIView()

weak var delegate: notificationCellDelegate?

// MARK: - View Life Cycle

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
Expand Down Expand Up @@ -62,22 +64,27 @@ extension NotificationTVC {
titleLabel.text = "습관방 시작"
subtitleLabel.text = "대기 중이었던 방이 시작되면 바로 알 수 있어요."
lineView.isHidden = false
notificationSwitch.tag = 0
case .spark:
titleLabel.text = "스파크 보내기"
subtitleLabel.text = "받은 스파크를 확인할 수 있어요."
lineView.isHidden = true
notificationSwitch.tag = 1
case .consider:
titleLabel.text = "고민중"
subtitleLabel.text = "다른 스파커가 고민중인 경우 바로 알 수 있어요."
lineView.isHidden = true
notificationSwitch.tag = 2
case .certification:
titleLabel.text = "인증 완료"
subtitleLabel.text = "스파커들의 인증 사진을 바로 확인할 수 있어요."
lineView.isHidden = false
notificationSwitch.tag = 3
case .remind:
titleLabel.text = "미완료 습관방"
subtitleLabel.text = "21:00에 미완료된 방이 있을 경우 알 수 있어요."
lineView.isHidden = true
notificationSwitch.tag = 4
}
}

Expand All @@ -88,8 +95,20 @@ extension NotificationTVC {
// MARK: - @objc Methods

@objc
private func touchNotificationSwitch() {
// TODO: - network
private func touchNotificationSwitch(_ sender: UISwitch) {
guard let category = NotificationTableRow(rawValue: sender.tag) else { return }
switch category {
case .roomStart:
delegate?.notificationSwitchToggle(category: "roomStart")
case .spark:
delegate?.notificationSwitchToggle(category: "spark")
case .consider:
delegate?.notificationSwitchToggle(category: "consider")
case .certification:
delegate?.notificationSwitchToggle(category: "certification")
case .remind:
delegate?.notificationSwitchToggle(category: "remind")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// NoticeSetting.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/03/28.
//

import Foundation

// MARK: - NoticeSetting

struct NoticeSetting: Codable {
let roomStart: Bool
let spark: Bool
let consider: Bool
let certification: Bool
let remind: Bool

enum CodingKeys: String, CodingKey {
case roomStart, spark, consider, certification, remind
}
}
44 changes: 44 additions & 0 deletions Spark-iOS/Spark-iOS/Source/NetworkServices/Notice/NoticeAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,48 @@ public class NoticeAPI {
return .networkFail
}
}

func settingFetch(completion: @escaping(NetworkResult<Any>) -> Void) {
noticeProvider.request(.settingFetch) { result in
switch result {
case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeSettingFetchStatus(by: statusCode, data)
completion(networkResult)
case .failure(let err):
print(err)
}
}
}

private func judgeSettingFetchStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<NoticeSetting>.self, from: data)
else { return .pathErr }
switch statusCode {
case 200:
return .success(decodedData.data ?? "None-Data")
case 400..<500:
return .requestErr(decodedData.message)
case 500:
return .serverErr
default:
return .networkFail
}
}

func settingPatch(category: String, completion: @escaping(NetworkResult<Any>) -> Void) {
noticeProvider.request(.settingPatch(category: category)) { result in
switch result {
case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeStatus(by: statusCode, data)
completion(networkResult)
case .failure(let err):
print(err)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ enum NoticeService {
case serviceFetch(lastID: Int, size: Int)
case activeRead
case serviceRead
case settingFetch
case settingPatch(category: String)
}

extension NoticeService: TargetType {
Expand All @@ -31,6 +33,10 @@ extension NoticeService: TargetType {
return "/notice/active/read"
case .serviceRead:
return "/notice/service/read"
case .settingFetch:
return "/notice/setting"
case .settingPatch:
return "/notice/setting"
}
}

Expand All @@ -44,6 +50,10 @@ extension NoticeService: TargetType {
return .patch
case .serviceRead:
return .patch
case .settingFetch:
return .get
case .settingPatch:
return .patch
}
}

Expand All @@ -61,6 +71,11 @@ extension NoticeService: TargetType {
return .requestPlain
case .serviceRead:
return .requestPlain
case .settingFetch:
return .requestPlain
case .settingPatch(category: let category):
return .requestParameters(parameters: ["category": category],
encoding: URLEncoding.queryString)
}
}

Expand All @@ -74,6 +89,10 @@ extension NoticeService: TargetType {
return Const.Header.authorizationHeader
case .serviceRead:
return Const.Header.authorizationHeader
case .settingFetch:
return Const.Header.authorizationHeader
case .settingPatch:
return Const.Header.authorizationHeader
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// NotificationCellDelegate.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/03/28.
//

import Foundation

protocol notificationCellDelegate: AnyObject {
func notificationSwitchToggle(category: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class NotificationVC: UIViewController {
private let customNavigationBar = LeftButtonNavigaitonBar()
private let tableView = UITableView()

private var noticeSetting: NoticeSetting?

// MARK: - View Life Cycle

override func viewDidLoad() {
Expand All @@ -40,6 +42,7 @@ class NotificationVC: UIViewController {
setUI()
setTableView()
setLayout()
settingFetchWithAPI()
}
}

Expand All @@ -52,6 +55,8 @@ extension NotificationVC {
.leftButtonAction {
self.navigationController?.popViewController(animated: true)
}

tableView.isHidden = true
}

private func setTableView() {
Expand Down Expand Up @@ -130,31 +135,88 @@ extension NotificationVC: UITableViewDataSource {

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let section = NotificationTableSection(rawValue: indexPath.section) else { return UITableViewCell() }
guard let noticeSetting = noticeSetting else { return UITableViewCell() }
guard let cell = tableView.dequeueReusableCell(withIdentifier: Const.Cell.Identifier.notificationTVC, for: indexPath) as? NotificationTVC else { return UITableViewCell() }
cell.selectionStyle = .none
cell.delegate = self

switch section {
case .information:
cell.initCell(with: .roomStart, isOn: false)
cell.initCell(with: .roomStart, isOn: noticeSetting.roomStart)

return cell
case .sparkerActivity:
/*
NotificationTableRow(rawValue: 1) 는 .spark 이다.
NotificationTableRow(rawValue: 2) 는 .consider 이다.
NotificationTableRow(rawValue: 3) 는 .certification 이다.
*/
guard let row = NotificationTableRow(rawValue: indexPath.section + indexPath.row) else { return UITableViewCell() }
cell.initCell(with: row, isOn: false)
if indexPath.row == 0 {
cell.initCell(with: .spark, isOn: noticeSetting.spark)
} else if indexPath.row == 1 {
cell.initCell(with: .consider, isOn: noticeSetting.consider)
} else {
cell.initCell(with: .certification, isOn: noticeSetting.certification)
}

return cell
case .remind:
cell.initCell(with: .remind, isOn: false)
cell.initCell(with: .remind, isOn: noticeSetting.remind)

return cell
}
}
}

// MARK: - Network

extension NotificationVC {
private func settingFetchWithAPI() {
NoticeAPI.shared.settingFetch { response in
switch response {
case .success(let data):
if let noticeSetting = data as? NoticeSetting {
self.noticeSetting = noticeSetting
self.tableView.reloadData()
self.tableView.isHidden = false
}
case .requestErr(let message):
print("profileFetchWithAPI - requestErr: \(message)")
case .pathErr:
print("profileFetchWithAPI - pathErr")
case .serverErr:
print("profileFetchWithAPI - serverErr")
case .networkFail:
print("profileFetchWithAPI - networkFail")
}
}
}

private func settingPatchWithAPI(category: String) {
NoticeAPI.shared.settingPatch(category: category) { response in
switch response {
case .success(let data):
if let noticeSetting = data as? NoticeSetting {
self.noticeSetting = noticeSetting
self.tableView.reloadData()
self.tableView.isHidden = false
}
case .requestErr(let message):
print("profileFetchWithAPI - requestErr: \(message)")
case .pathErr:
print("profileFetchWithAPI - pathErr")
case .serverErr:
print("profileFetchWithAPI - serverErr")
case .networkFail:
print("profileFetchWithAPI - networkFail")
}
}
}
}

// MARK: - notificationCellDelegate

extension NotificationVC: notificationCellDelegate {
func notificationSwitchToggle(category: String) {
settingPatchWithAPI(category: category)
}
}

// MARK: - Layout

extension NotificationVC {
Expand Down

0 comments on commit 8f14a46

Please sign in to comment.