Skip to content

Commit

Permalink
✨[feat]: 프로필 api 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy-wonji committed Nov 22, 2023
1 parent 7c6923b commit 279de3b
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// ProfileUserPrefModel.swift
// Model
//
// Created by 서원지 on 11/22/23.
// Copyright © 2023 Wonji Suh. All rights reserved.
//

import Foundation

// MARK: - Welcome
public struct ProfileUserPrefModel: Codable {
public let status: Int?
public let data: ProfileUserPrefModelResponse?
public let message: String?

public init(status: Int?, data: ProfileUserPrefModelResponse?, message: String?) {
self.status = status
self.data = data
self.message = message
}
}

// MARK: - DataClass
public struct ProfileUserPrefModelResponse: Codable {
public let regDttm, modDttm: String?
public let regrID, regrNm, modrID, modrNm: String?
public let rmk, rowStatus: String?
public let userPrefID, userID: Int?
public let flavors, sources: [String]?

enum CodingKeys: String, CodingKey {
case regDttm, modDttm
case regrID = "regrId"
case regrNm
case modrID = "modrId"
case modrNm, rmk, rowStatus
case userPrefID = "userPrefId"
case userID = "userId"
case flavors, sources
}

public init(regDttm: String?, modDttm: String?, regrID: String?, regrNm: String?, modrID: String?, modrNm: String?, rmk: String?, rowStatus: String?, userPrefID: Int?, userID: Int?, flavors: [String]?, sources: [String]?) {
self.regDttm = regDttm
self.modDttm = modDttm
self.regrID = regrID
self.regrNm = regrNm
self.modrID = modrID
self.modrNm = modrNm
self.rmk = rmk
self.rowStatus = rowStatus
self.userPrefID = userPrefID
self.userID = userID
self.flavors = flavors
self.sources = sources
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Moya

public enum MyPageService {
case myPageUsePref(userId: String)
case myPageEditUserPref(userId: String, userPrefId: String, flavors: [String], sources: [String])
case myPageEditUserPref(userPrefId: String, flavors: [String], sources: [String])
case myPageLikes(userId: String)
case deleteLike(likeId: Int)
}
Expand All @@ -23,8 +23,8 @@ extension MyPageService: BaseTargetType {
switch self {
case .myPageUsePref(let userId):
return "\(PingPongAPIMyPage.myPageEditUserPrefURL)\(userId)"
case .myPageEditUserPref(let userId, _, _, _):
return "\(PingPongAPIMyPage.myPageEditUserPrefURL)\(userId)"
case .myPageEditUserPref(let userPrefId, _, _):
return "\(PingPongAPIMyPage.myPageEditUserPrefURL)\(userPrefId)"
case .myPageLikes(let userId):
return "\(PingPongAPIMyPage.myPageLikeURL)\(userId)"
case .deleteLike(let likeId):
Expand All @@ -47,16 +47,14 @@ extension MyPageService: BaseTargetType {

public var task: Moya.Task {
switch self {
case .myPageUsePref(let userId):
case .myPageUsePref(_):
let parameters : [String : Any] = [
"userId": userId
:
]
return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)

case .myPageEditUserPref(let userId, let userPrefId, let flavors, let sources):
case .myPageEditUserPref(_, let flavors, let sources):
let parameters : [String : Any] = [
"userId": userId,
"userPrefId": userPrefId,
"flavors": flavors,
"sources" : sources
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public struct OnBoardingPushView: View {
.edgesIgnoringSafeArea(.all)
VStack {
onboardingBackButton()

ScrollView(showsIndicators: false) {
exampleWiseSayingView()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public struct ProfileView: View {
await profileViewModel.randomNameRequest(commCdTpCd: .userDesc)
}

await profileViewModel.profileUserPrefRequset(userid: "\(authViewModel.userid)", completion: {})

print("\(profileViewModel.randomNickName)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,41 +45,133 @@ public class ProfileViewViewModel: ObservableObject {

@Published var isFirstRequestCompleted = false
@AppStorage("randomNickName") var randomNickName: String = ""
let unicodeArray: [Character] = CheckRegister.generateUnicodeArray()

@Published var nicknameValidation: NicknameValidationType = .notValidated
@Published public var changeValidationColor: Color = .basicGray4
@Published public var changeValidationImageName: String?
@Published public var changeValidationText: String = " "
@Published public var userNickName: String = ""
@AppStorage("userPrefId") public var userPrefId: Int = .zero

let unicodeArray: [Character] = CheckRegister.generateUnicodeArray()
var withDrawCancellable: AnyCancellable?

@Published public var profileUserPrefModel: ProfileUserPrefModel?
var profileUserPrefCancellable: AnyCancellable?

@Published public var withDrawModel: WithDrawModel?
var changeNickNameCancellable: AnyCancellable?

@Published public var baseModel: BaseModel?
var changeUserPrefEditCancellable: AnyCancellable?

public init() {
saveDate = UserDefaults.standard.string(forKey: "saveDate") ?? ""
saveDateHour = UserDefaults.standard.string(forKey: "saveDateHour") ?? ""
selectedChangeTimeView = UserDefaults.standard.bool(forKey: "selectedChangeTimeView")
randomNickName = UserDefaults.standard.string(forKey: "randomNickName") ?? ""
userPrefId = UserDefaults.standard.integer(forKey: "userPrefId")

}





public func withDrawToViewModel(_ list: WithDrawModel) {
self.withDrawModel = list
}

let profileViewListArray: [ProfileViewComponentModel] = [
ProfileViewComponentModel(isDevider: true, imageName: "notificationImage", content: "명언 알림 메시지", detail: "당신을 위한 명언을 배송해드릴게요."),
ProfileViewComponentModel(isDevider: true, imageName: "reviewImage", content: "앱 리뷰 작성", detail: "서비스에 대한 평가를 남겨주세요."),
ProfileViewComponentModel(isDevider: true, imageName: "bugImage", content: "버그 신고 및 의견 공유", detail: "더 좋은 서비스를 함께 만들어가요."),
ProfileViewComponentModel(isDevider: false, imageName: "settingImage", content: "기타 설정", detail: "각종 설정들을 관리해요."),
]


//MARK: - 프로필 취향 조회
private func profileUserPrefToViewModel(_ list: ProfileUserPrefModel) {
self.profileUserPrefModel = list
}


public func profileUserPrefRequset(userid: String, completion: @escaping () -> Void) async {
if let cancellable = profileUserPrefCancellable {
cancellable.cancel()
}

let provider = MoyaProvider<MyPageService>(plugins: [MoyaLoggingPlugin()])
profileUserPrefCancellable = provider.requestWithProgressPublisher(.myPageUsePref(userId: userid))
.compactMap{$0.response?.data}
.receive(on: DispatchQueue.main)
.decode(type: ProfileUserPrefModel.self, decoder: JSONDecoder())
.sink(receiveCompletion: { [weak self] result in
switch result {
case .finished:
break
case .failure(let error):
Log.network("네트워크 에러", error.localizedDescription)
}
}, receiveValue: { [weak self] model in
if let status = model.status {
if status == NetworkCode.success.status {
self?.profileUserPrefToViewModel(model)
Log.network("회원정보 조회", model)
self?.userPrefId = model.data?.userPrefID ?? .zero
completion()
} else {
self?.profileUserPrefToViewModel(model)
Log.network("회원정보 조회", model)
}
}
})

}


//MARK: - 취향 수정
private func profileUserPrefEditToViewModel(_ list: BaseModel) {
self.baseModel = list
}


public func profileUserPrefEditPUT(
userPrefId: Int,
flavors: [String],
sources: [String],
successCompletion: @escaping () -> Void
) async {
if let cancellable = changeUserPrefEditCancellable {
cancellable.cancel()
}

let provider = MoyaProvider<MyPageService>(plugins: [MoyaLoggingPlugin()])
changeUserPrefEditCancellable = provider.requestWithProgressPublisher(.myPageEditUserPref(userPrefId: "\(userPrefId)", flavors: flavors, sources: sources))
.compactMap{$0.response?.data}
.receive(on: DispatchQueue.main)
.decode(type: BaseModel.self, decoder: JSONDecoder())
.sink(receiveCompletion: { [weak self] result in
switch result {
case .finished:
break
case .failure(let error):
Log.network("네트워크 에러", error.localizedDescription)
}
}, receiveValue: { [weak self] model in
if let status = model.status {
if status == model.status {
self?.profileUserPrefEditToViewModel(model)
Log.network("취향 수정 ", model)
successCompletion()
} else {
self?.profileUserPrefEditToViewModel(model)
Log.network("취향 수정 ", model)
}
}
})
}

//MARK: - 회원탈퇴

public func withDrawToViewModel(_ list: WithDrawModel) {
self.withDrawModel = list
}

public func withDrawPost(
userID: String,
reason: String,
Expand Down

0 comments on commit 279de3b

Please sign in to comment.