프로필 변경 페이지 추가
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// GetChangeNicknamePriceResponse.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/08/19.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct GetChangeNicknamePriceResponse: Decodable {
|
||||
let price: Int
|
||||
}
|
@@ -0,0 +1,110 @@
|
||||
//
|
||||
// NicknameUpdateView.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/08/19.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct NicknameUpdateView: View {
|
||||
|
||||
@StateObject var viewModel = NicknameUpdateViewModel()
|
||||
@StateObject var keyboardHandler = KeyboardHandler()
|
||||
|
||||
var body: some View {
|
||||
BaseView(isLoading: $viewModel.isLoading) {
|
||||
GeometryReader { proxy in
|
||||
VStack(spacing: 0) {
|
||||
DetailNavigationBar(title: "프로필 수정")
|
||||
|
||||
Text("닉네임 변경으로 인해 피해를 입는 사용자가 지속적으로 발생하여 닉네임 변경을 부득이하게 유료로 전환합니다.")
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.font(.custom(Font.medium.rawValue, size: 13.3))
|
||||
.foregroundColor(Color(hex: "eeeeee"))
|
||||
.frame(width: screenSize().width - 40, alignment: .leading)
|
||||
.padding(.top, 40)
|
||||
|
||||
Text("최초 1회에 한해서 무료로 변경이 가능하고, 그 이후부터는 유료로 전환됩니다.")
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.font(.custom(Font.medium.rawValue, size: 13.3))
|
||||
.foregroundColor(Color(hex: "dd4500"))
|
||||
.frame(width: screenSize().width - 40, alignment: .leading)
|
||||
|
||||
UserTextField(
|
||||
title: "닉네임(최대 12자)",
|
||||
hint: "닉네임",
|
||||
isSecure: false,
|
||||
variable: $viewModel.nickname
|
||||
)
|
||||
.frame(width: screenSize().width - 40)
|
||||
.padding(.top, 40)
|
||||
|
||||
Text("중복확인")
|
||||
.font(.custom(Font.bold.rawValue, size: 13.3))
|
||||
.foregroundColor(Color(hex: "eeeeee"))
|
||||
.frame(width: screenSize().width - 40)
|
||||
.padding(.vertical, 13.3)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.stroke(Color(hex: "9970ff"), lineWidth: 1)
|
||||
)
|
||||
.padding(.top, 21.3)
|
||||
.onTapGesture {
|
||||
hideKeyboard()
|
||||
viewModel.checkNickname()
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Text(viewModel.price > 0 ? "\(viewModel.price)코인으로 닉네임 변경하기" : "닉네임 변경하기")
|
||||
.font(.custom(Font.bold.rawValue, size: 18.3))
|
||||
.foregroundColor(Color.white)
|
||||
.padding(.vertical, 16)
|
||||
.frame(width: screenSize().width - 26.7)
|
||||
.background(Color(hex: "9970ff"))
|
||||
.cornerRadius(10)
|
||||
.padding(.vertical, 13.7)
|
||||
.frame(width: screenSize().width)
|
||||
.background(Color(hex: "222222"))
|
||||
.cornerRadius(16.7)
|
||||
.padding(.top, 13.3)
|
||||
.onTapGesture { viewModel.changeNickname() }
|
||||
|
||||
if proxy.safeAreaInsets.bottom > 0 {
|
||||
Rectangle()
|
||||
.foregroundColor(Color.black)
|
||||
.frame(width: proxy.size.width, height: 15.3)
|
||||
}
|
||||
}
|
||||
.edgesIgnoringSafeArea(.bottom)
|
||||
.onTapGesture {
|
||||
hideKeyboard()
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
viewModel.nickname = UserDefaults.string(forKey: .nickname)
|
||||
viewModel.getChangeNicknamePrice()
|
||||
}
|
||||
.popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
|
||||
GeometryReader { geo in
|
||||
HStack {
|
||||
Spacer()
|
||||
Text(viewModel.errorMessage)
|
||||
.padding(.vertical, 13.3)
|
||||
.padding(.horizontal, 6.7)
|
||||
.frame(width: geo.size.width - 66.7, alignment: .center)
|
||||
.font(.custom(Font.medium.rawValue, size: 12))
|
||||
.background(Color(hex: "9970ff"))
|
||||
.foregroundColor(Color.white)
|
||||
.multilineTextAlignment(.leading)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.cornerRadius(20)
|
||||
.padding(.top, 66.7)
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,168 @@
|
||||
//
|
||||
// NicknameUpdateViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/08/19.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
import Moya
|
||||
|
||||
final class NicknameUpdateViewModel: ObservableObject {
|
||||
private let repository = UserRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
@Published var isLoading = false
|
||||
@Published var errorMessage = ""
|
||||
@Published var isShowPopup = false
|
||||
|
||||
@Published var nickname = "" {
|
||||
didSet {
|
||||
if nickname.count > 12 {
|
||||
nickname = String(nickname.prefix(12))
|
||||
}
|
||||
|
||||
isCheckedNickname = false
|
||||
}
|
||||
}
|
||||
@Published var price = 0
|
||||
|
||||
var isCheckedNickname = false
|
||||
|
||||
func getChangeNicknamePrice() {
|
||||
isLoading = true
|
||||
|
||||
repository.getChangeNicknamePrice()
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { [unowned self] response in
|
||||
self.isLoading = false
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<GetChangeNicknamePriceResponse>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self.price = data.price
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self.errorMessage = message
|
||||
} else {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
|
||||
func checkNickname() {
|
||||
if !nickname.trimmingCharacters(in: .whitespaces).isEmpty {
|
||||
isLoading = true
|
||||
|
||||
repository.checkNickname(nickname: nickname)
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { [unowned self] response in
|
||||
self.isLoading = false
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponseWithoutData.self, from: responseData)
|
||||
|
||||
if decoded.success {
|
||||
self.isCheckedNickname = true
|
||||
self.errorMessage = "사용가능한 닉네임 입니다."
|
||||
self.isShowPopup = true
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self.errorMessage = message
|
||||
} else {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
} else {
|
||||
self.errorMessage = "닉네임을 입력하세요."
|
||||
self.isShowPopup = true
|
||||
}
|
||||
}
|
||||
|
||||
func changeNickname() {
|
||||
if isCheckedNickname {
|
||||
isLoading = true
|
||||
|
||||
let request = ProfileUpdateRequest(email: UserDefaults.string(forKey: .email), nickname: nickname)
|
||||
repository.changeNickname(request: request)
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { [unowned self] response in
|
||||
self.isLoading = false
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponseWithoutData.self, from: responseData)
|
||||
|
||||
if decoded.success {
|
||||
UserDefaults.set(nickname, forKey: .nickname)
|
||||
self.errorMessage = "닉네임이 변경되었습니다."
|
||||
self.isShowPopup = true
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
||||
AppState.shared.back()
|
||||
}
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self.errorMessage = message
|
||||
} else {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
|
||||
} else {
|
||||
self.errorMessage = "닉네임 중복체크를 해주세요."
|
||||
self.isShowPopup = true
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user