마이페이지 추가

This commit is contained in:
Yu Sung
2023-08-10 16:14:12 +09:00
parent 80ff04f825
commit d06f4d6a36
40 changed files with 979 additions and 3 deletions

View File

@@ -0,0 +1,44 @@
//
// AuthApi.swift
// SodaLive
//
// Created by klaus on 2023/08/10.
//
import Foundation
import Moya
enum AuthApi {
case auth(request: AuthVerifyRequest)
}
extension AuthApi: TargetType {
var baseURL: URL {
return URL(string: BASE_URL)!
}
var path: String {
switch self {
case .auth:
return "/auth"
}
}
var method: Moya.Method {
return .post
}
var task: Task {
switch self {
case .auth(let request):
return .requestJSONEncodable(request)
}
}
var headers: [String : String]? {
switch self {
default:
return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
}
}
}

View File

@@ -0,0 +1,27 @@
//
// AuthButtonView.swift
// SodaLive
//
// Created by klaus on 2023/08/10.
//
import SwiftUI
struct AuthButtonView: View {
var body: some View {
Text("본인인증")
.font(.custom(Font.bold.rawValue, size: 15.3))
.foregroundColor(Color(hex: "eeeeee"))
.padding(.horizontal, 13.3)
.padding(.vertical, 20)
.frame(width: screenSize().width - 26.7, alignment: .leading)
.background(Color(hex: "664aab"))
.cornerRadius(6.7)
}
}
struct AuthButtonView_Previews: PreviewProvider {
static var previews: some View {
AuthButtonView()
}
}

View File

@@ -0,0 +1,20 @@
//
// AuthRepository.swift
// SodaLive
//
// Created by klaus on 2023/08/10.
//
import Foundation
import CombineMoya
import Combine
import Moya
final class AuthRepository {
private let api = MoyaProvider<AuthApi>()
func authCertificate(receiptId: String) -> AnyPublisher<Response, MoyaError> {
return api.requestPublisher(.auth(request: AuthVerifyRequest(receiptId: receiptId)))
}
}

View File

@@ -0,0 +1,16 @@
//
// AuthVerifyRequest.swift
// SodaLive
//
// Created by klaus on 2023/08/10.
//
import Foundation
struct AuthVerifyRequest: Encodable {
let receiptId: String
enum CodingKeys : String, CodingKey {
case receiptId = "receipt_id"
}
}

View File

@@ -0,0 +1,77 @@
//
// CanCardView.swift
// SodaLive
//
// Created by klaus on 2023/08/10.
//
import SwiftUI
struct CanCardView: View {
let data: MyPageResponse
let refresh: () -> Void
var body: some View {
HStack(spacing: 0) {
Button(action: {}) {
HStack(spacing: 6.7) {
Text("\(data.chargeCan + data.rewardCan)")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color(hex: "eeeeee"))
Image("ic_can")
Image("ic_forward")
.resizable()
.frame(width: 20, height: 20)
}
}
Spacer()
Button(action: {}) {
HStack(spacing: 7) {
Image("ic_coin_w")
.resizable()
.frame(width: 26.7, height: 26.7)
Text("충전")
.font(.custom(Font.bold.rawValue, size: 12))
.foregroundColor(Color(hex: "fdca2f"))
}
.padding(.horizontal, 11.3)
.padding(.vertical, 7)
.overlay(
RoundedRectangle(cornerRadius: CGFloat(16.7))
.stroke(lineWidth: 1)
.foregroundColor(Color(hex: "fdca2f"))
)
.cornerRadius(16.7)
}
}
.padding(.horizontal, 13.3)
.padding(.vertical, 16.7)
.background(Color(hex: "222222"))
.cornerRadius(16.7)
}
}
struct CanCardView_Previews: PreviewProvider {
static var previews: some View {
CanCardView(
data: MyPageResponse(
nickname: "완다 막시모프",
profileUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
chargeCan: 0,
rewardCan: 150,
youtubeUrl: "",
instagramUrl: "",
websiteUrl: "",
blogUrl: "",
liveReservationCount: 0,
isAuth: false
),
refresh: {}
)
}
}

View File

@@ -0,0 +1,95 @@
//
// MyInfoCardView.swift
// SodaLive
//
// Created by klaus on 2023/08/10.
//
import SwiftUI
import Kingfisher
struct MyInfoCardView: View {
let data: MyPageResponse
let refresh: () -> Void
var body: some View {
HStack(spacing: 13.3) {
KFImage(URL(string: data.profileUrl))
.resizable()
.scaledToFill()
.frame(width: 90, height: 90, alignment: .top)
.clipShape(Circle())
VStack(alignment: .leading, spacing: 13.3) {
HStack(spacing: 0) {
Text(data.nickname)
.font(.custom(Font.bold.rawValue, size: 20))
.foregroundColor(Color(hex: "eeeeee"))
Spacer()
Button(action: {
if AppState.shared.roomId <= 0 {
}
}) {
Image("ic_myinfo_edit")
.resizable()
.frame(width: 20, height: 20)
}
}
HStack(spacing: 10) {
Spacer()
if let websiteUrl = data.websiteUrl, websiteUrl.count > 0, let url = URL(string: websiteUrl), UIApplication.shared.canOpenURL(url) {
Button(action: {UIApplication.shared.open(url)}) {
Image("ic_website_circle")
}
}
if let blogUrl = data.blogUrl, blogUrl.count > 0, let url = URL(string: blogUrl), UIApplication.shared.canOpenURL(url) {
Button(action: {UIApplication.shared.open(url)}) {
Image("ic_blog_circle")
}
}
if let instagramUrl = data.instagramUrl, instagramUrl.count > 0, let url = URL(string: instagramUrl), UIApplication.shared.canOpenURL(url) {
Button(action: {UIApplication.shared.open(url)}) {
Image("ic_instagram_circle")
}
}
if let youtubeUrl = data.youtubeUrl, youtubeUrl.count > 0, let url = URL(string: youtubeUrl), UIApplication.shared.canOpenURL(url) {
Button(action: {UIApplication.shared.open(url)}) {
Image("ic_youtube_circle")
}
}
}
}
}
.padding(20)
.background(Color(hex: "222222"))
.cornerRadius(16.7)
}
}
struct MyInfoCardView_Previews: PreviewProvider {
static var previews: some View {
MyInfoCardView(
data: MyPageResponse(
nickname: "완다 막시모프",
profileUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
chargeCan: 0,
rewardCan: 150,
youtubeUrl: "",
instagramUrl: "",
websiteUrl: "",
blogUrl: "",
liveReservationCount: 0,
isAuth: false
),
refresh: {}
)
}
}

View File

@@ -0,0 +1,22 @@
//
// MyPageResponse.swift
// SodaLive
//
// Created by klaus on 2023/08/10.
//
import Foundation
struct MyPageResponse: Decodable {
let nickname: String
let profileUrl: String
let chargeCan: Int
let rewardCan: Int
let youtubeUrl: String?
let instagramUrl: String?
let websiteUrl: String?
let blogUrl: String?
let liveReservationCount: Int
let isAuth: Bool
}

View File

@@ -7,9 +7,138 @@
import SwiftUI
import Bootpay
import BootpayUI
import PopupView
import RefreshableScrollView
struct MyPageView: View {
@StateObject var viewModel = MyPageViewModel()
@State private var payload = Payload()
var body: some View {
Text("MyPage")
BaseView(isLoading: $viewModel.isLoading) {
if viewModel.isShowAuthView {
BootpayUI(payload: payload, requestType: BootpayRequest.TYPE_AUTHENTICATION)
.onConfirm {
DEBUG_LOG("onConfirm: \($0)")
return true
}
.onCancel {
DEBUG_LOG("onCancel: \($0)")
}
.onError {
DEBUG_LOG("onError: \($0)")
viewModel.errorMessage = "본인인증 중 오류가 발생했습니다."
viewModel.isShowPopup = true
viewModel.isShowAuthView = false
}
.onDone {
DEBUG_LOG("onDone: \($0)")
viewModel.authVerify($0)
}
.onClose {
DEBUG_LOG("onClose")
viewModel.isShowAuthView = false
}
} else {
GeometryReader { geo in
VStack {
HomeNavigationBar(title: "마이 페이지") {
Image("ic_settings")
.resizable()
.frame(width: 20, height: 20)
.onTapGesture {
AppState.shared.setAppStep(step: .settings)
}
}
RefreshableScrollView(
refreshing: $viewModel.isLoading,
action: {
viewModel.getMypage()
}
) {
VStack(spacing: 0) {
if let data = viewModel.myPageResponse {
MyInfoCardView(data: data) {
viewModel.getMypage()
}
.frame(width: screenSize().width - 26.7)
.padding(.top, 13.3)
if UserDefaults.string(forKey: .role) == MemberRole.CREATOR.rawValue {
Text("내 채널 보기")
.frame(width: screenSize().width - 26.7, height: 46.7)
.font(.custom(Font.bold.rawValue, size: 15.3))
.foregroundColor(Color(hex: "eeeeee"))
.background(Color(hex: "352953"))
.cornerRadius(6.7)
.overlay(
RoundedRectangle(cornerRadius: 6.7)
.stroke(Color(hex: "9970ff"), lineWidth: 1.3)
)
.padding(.top, 26.7)
.onTapGesture {}
}
CanCardView(data: data) {
viewModel.getMypage()
}
.frame(width: screenSize().width - 26.7)
.padding(.top, 26.7)
ReservationStatusView(data: data)
.padding(.top, 33.3)
ServiceCenterButtonView()
.padding(.top, 40)
.padding(.bottom, data.isAuth ? 40 : 0)
if !data.isAuth {
AuthButtonView()
.padding(.vertical, 40)
.onTapGesture {
viewModel.isShowAuthView = true
}
}
}
}
}
}
.frame(width: geo.size.width, height: geo.size.height)
}
.onAppear {
viewModel.getMypage()
}
}
}
.popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
GeometryReader { geo in
HStack {
Spacer()
Text(viewModel.errorMessage)
.padding(.vertical, 13.3)
.frame(width: geo.size.width - 66.7, alignment: .center)
.font(.custom(Font.medium.rawValue, size: 12))
.background(Color(hex: "9970ff"))
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
.cornerRadius(20)
.padding(.top, 66.7)
Spacer()
}
}
}
.onAppear {
payload.applicationId = BOOTPAY_APP_ID
payload.price = 0
payload.pg = "다날"
payload.method = "본인인증"
payload.orderName = "본인인증"
payload.authenticationId = "\(UserDefaults.string(forKey: .nickname))__\(String(NSTimeIntervalSince1970))"
}
}
}

View File

@@ -0,0 +1,110 @@
//
// MyPageViewModel.swift
// SodaLive
//
// Created by klaus on 2023/08/10.
//
import Foundation
import Combine
final class MyPageViewModel: ObservableObject {
private let repository = UserRepository()
private let authRepository = AuthRepository()
private var subscription = Set<AnyCancellable>()
@Published var myPageResponse: MyPageResponse? = nil
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var isLoading = false
@Published var isShowAuthView = false
func getMypage() {
isLoading = true
repository.getMypage()
.sink { result in
switch result {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
}
} receiveValue: { response in
self.isLoading = false
let responseData = response.data
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<MyPageResponse>.self, from: responseData)
if let data = decoded.data, decoded.success {
self.myPageResponse = data
} 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)
}
func authVerify(_ data: [String: Any]) {
isLoading = true
let _data = data["data"] as? [String: Any]
if let data = _data {
let receiptId = data["receipt_id"] as! String
authRepository.authCertificate(receiptId: receiptId)
.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.errorMessage = "본인인증이 완료되었습니다."
self.isShowPopup = true
self.getMypage()
} 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 {
isLoading = false
errorMessage = "본인인증 중 오류가 발생했습니다."
isShowPopup = true
}
}
}

View File

@@ -0,0 +1,67 @@
//
// ReservationStatusView.swift
// SodaLive
//
// Created by klaus on 2023/08/10.
//
import SwiftUI
struct ReservationStatusView: View {
let data: MyPageResponse
var body: some View {
VStack(alignment: .leading, spacing: 13.3) {
Text("예약현황")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color(hex: "eeeeee"))
HStack(spacing: 0) {
let width = screenSize().width - 26.7
HStack(spacing: 6.7) {
Image("ic_tabbar_live_selected")
.resizable()
.frame(width: 20, height: 20)
Text("라이브")
.font(.custom(Font.medium.rawValue, size: 14.7))
.foregroundColor(Color(hex: "eeeeee"))
Text("\(data.liveReservationCount)")
.font(.custom(Font.medium.rawValue, size: 14.7))
.foregroundColor(Color(hex: "9970ff"))
}
.frame(width: width, height: 46.7)
.background(Color(hex: "352953"))
.cornerRadius(6.7)
.overlay(
RoundedRectangle(cornerRadius: 6.7)
.stroke(Color(hex: "9970ff"), lineWidth: 1.3)
)
.onTapGesture {
}
}
}
.frame(width: screenSize().width - 26.7, alignment: .leading)
}
}
struct ReservationStatusView_Previews: PreviewProvider {
static var previews: some View {
ReservationStatusView(
data: MyPageResponse(
nickname: "완다 막시모프",
profileUrl: "https://test-cf.sodalive.net/profile/default-profile.png",
chargeCan: 0,
rewardCan: 150,
youtubeUrl: "",
instagramUrl: "",
websiteUrl: "",
blogUrl: "",
liveReservationCount: 0,
isAuth: false
)
)
}
}

View File

@@ -0,0 +1,40 @@
//
// ServiceCenterButtonView.swift
// SodaLive
//
// Created by klaus on 2023/08/10.
//
import SwiftUI
struct ServiceCenterButtonView: View {
var body: some View {
HStack(spacing: 13.7) {
Image("ic_headphones")
.resizable()
.frame(width: 26.7, height: 26.7)
Text("소다라이브 고객센터")
.font(.custom(Font.bold.rawValue, size: 15.3))
.foregroundColor(.white)
Spacer()
Image("ic_forward")
.resizable()
.frame(width: 20, height: 20)
}
.padding(.horizontal, 13.3)
.frame(width: screenSize().width - 26.7, height: 66.7)
.background(Color(hex: "664aab"))
.cornerRadius(6.7)
.onTapGesture {
}
}
}
struct ServiceCenterButtonView_Previews: PreviewProvider {
static var previews: some View {
ServiceCenterButtonView()
}
}