라이브 상세 페이지 추가

This commit is contained in:
Yu Sung
2023-08-14 19:22:23 +09:00
parent e0a5fb733d
commit 634f50d4f2
37 changed files with 2767 additions and 49 deletions

View File

@@ -11,15 +11,13 @@ struct GetRoomDetailResponse: Decodable {
let roomId: Int
let price: Int
let title: String
let content: String
let notice: String
let isPaid: Bool
let isPrivateRoom: Bool
let isSecretRoom: Bool
let password: Int?
let password: String?
let tags: [String]
let channelName: String?
let beginDateTime: String
let isNotification: Bool
let numberOfParticipants: Int
let numberOfParticipantsTotal: Int
let manager: GetRoomDetailManager
@@ -35,7 +33,7 @@ struct GetRoomDetailManager: Decodable {
let websiteUrl: String?
let blogUrl: String?
let profileImageUrl: String
let isCounselor: Bool
let isCreator: Bool
}
struct GetRoomDetailUser: Decodable, Hashable {

View File

@@ -0,0 +1,510 @@
//
// LiveDetailView.swift
// SodaLive
//
// Created by klaus on 2023/08/14.
//
import SwiftUI
import Kingfisher
struct LiveDetailView: View {
@ObservedObject var viewModel = LiveDetailViewModel()
@State private var isExpandParticipantArea = false
@State private var isShowCancelPopup = false
@StateObject var keyboardHandler = KeyboardHandler()
let columns = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
let roomId: Int
let onClickParticipant: () -> Void
let onClickReservation: () -> Void
let onClickStart: () -> Void
let onClickCancel: () -> Void
var onClickClose: (() -> Void)? = nil
var body: some View {
BaseView(isLoading: $viewModel.isLoading) {
Color.black.opacity(0.7)
.onTapGesture {
viewModel.onBack {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
hideView()
}
}
}
if isShowCancelPopup {
LiveCancelDialog(isShowCancelPopup: $isShowCancelPopup) { reason in
viewModel.liveCancel(roomId: roomId, reason: reason) {
viewModel.errorMessage = "예약이 취소되었습니다."
viewModel.isShowPopup = true
onClickCancel()
}
}
} else {
GeometryReader { proxy in
VStack {
Spacer()
VStack(spacing: 0) {
HStack {
Spacer()
Image("ic_close_white")
.resizable()
.frame(width: 20, height: 20)
.padding(.top, 13.3)
.padding(.trailing, 13.3)
.onTapGesture {
viewModel.onBack {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
hideView()
}
}
}
}
if let room = viewModel.room {
ScrollView(.vertical, showsIndicators: false) {
VStack(spacing: 0) {
Text(room.title)
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color(hex: "eeeeee"))
.frame(width: proxy.size.width - 26.7, alignment: .leading)
.padding(.top, 6.7)
HStack(spacing: 0) {
Text(room.beginDateTime)
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(Color(hex: "bbbbbb"))
Spacer()
if room.price > 0 {
Text("\(room.price)")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color(hex: "eeeeee"))
Image("ic_can")
.resizable()
.frame(width: 26.7, height: 26.7)
.padding(.leading, 6.7)
} else {
Text("무료")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color(hex: "eeeeee"))
}
}
.padding(.top, 16.7)
.frame(width: proxy.size.width - 26.7)
Rectangle()
.frame(height: 1)
.foregroundColor(Color(hex: "909090").opacity(0.5))
.padding(.top, 8)
.frame(width: proxy.size.width - 26.7)
ParticipantView(room: room)
.frame(width: proxy.size.width - 26.7)
Rectangle()
.frame(height: 1)
.foregroundColor(Color(hex: "909090").opacity(0.5))
.padding(.top, 13.3)
HStack(spacing: 13.3) {
let manager = room.manager
KFImage(URL(string: manager.profileImageUrl))
.resizable()
.scaledToFill()
.frame(width: 60, height: 60, alignment: .top)
.background(Color(hex: "3e3658"))
.clipShape(Circle())
VStack(spacing: 16.7) {
HStack(spacing: 6.7) {
Text(manager.nickname)
.font(.custom(Font.medium.rawValue, size: 16.7))
.foregroundColor(Color(hex: "eeeeee"))
Spacer()
if let websiteUrl = manager.websiteUrl, let url = URL(string: websiteUrl), UIApplication.shared.canOpenURL(url) {
Image("ic_website_purple")
.resizable()
.frame(width: 33.3, height: 33.3)
.onTapGesture {
UIApplication.shared.open(url)
}
}
if let blogUrl = manager.blogUrl, let url = URL(string: blogUrl), UIApplication.shared.canOpenURL(url) {
Image("ic_blog_purple")
.resizable()
.frame(width: 33.3, height: 33.3)
.onTapGesture {
UIApplication.shared.open(url)
}
}
if let instagramUrl = manager.instagramUrl, let url = URL(string: instagramUrl), UIApplication.shared.canOpenURL(url) {
Image("ic_instagram_purple")
.resizable()
.frame(width: 33.3, height: 33.3)
.onTapGesture {
UIApplication.shared.open(url)
}
}
if let youtubeUrl = manager.youtubeUrl, let url = URL(string: youtubeUrl), UIApplication.shared.canOpenURL(url) {
Image("ic_youtube_play_purple")
.resizable()
.frame(width: 33.3, height: 33.3)
.onTapGesture {
UIApplication.shared.open(url)
}
}
}
HStack(alignment: .center, spacing: 0) {
Text(manager.introduce)
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(Color(hex: "777777"))
.lineLimit(3)
.fixedSize(horizontal: false, vertical: true)
Spacer()
if manager.isCreator {
HStack(spacing: 3.3) {
Image("ic_thumb_play")
.resizable()
.frame(width: 13.3, height: 13.3)
Text("채널보기")
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(Color.white)
.onTapGesture {
AppState.shared.setAppStep(step: .creatorDetail(userId: manager.id))
}
}
.padding(.horizontal, 8.7)
.padding(.vertical, 10)
.background(Color(hex: "9970ff"))
.cornerRadius(16.7)
.onTapGesture {
}
}
}
}
}
.padding(.vertical, 20)
.padding(.horizontal, 13.3)
.frame(width: proxy.size.width)
.background(Color(hex: "111111"))
Rectangle()
.frame(height: 1)
.foregroundColor(Color(hex: "909090").opacity(0.5))
Text(room.tags.map { "#\($0)" }.joined(separator: " "))
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color(hex: "9970ff"))
.frame(width: proxy.size.width - 26, alignment: .leading)
.padding(.top, 26.7)
Text(room.notice)
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color(hex: "777777"))
.frame(width: proxy.size.width - 26, alignment: .leading)
.padding(.top, 26.7)
Rectangle()
.frame(width: proxy.size.width - 26.7, height: 1)
.foregroundColor(Color(hex: "909090").opacity(0.5))
.padding(.vertical, 40)
}
}
}
if !viewModel.isShowPopup {
JoinButton()
.padding(.bottom, 26.7)
}
if proxy.safeAreaInsets.bottom > 0 {
Rectangle()
.foregroundColor(Color(hex: "222222"))
.frame(width: screenSize().width, height: 15.3)
}
}
.frame(width: proxy.size.width, height: proxy.size.height * 0.9)
.background(Color(hex: "222222"))
.cornerRadius(16.7)
.offset(y: viewModel.showDetail ? 0 : proxy.size.height * 0.9)
.animation(.easeInOut(duration: 0.25), value: viewModel.showDetail)
}
.edgesIgnoringSafeArea(.bottom)
}
}
if viewModel.isLoading {
LoadingView()
}
}
.popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 1) {
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()
}
.onDisappear {
hideView()
}
}
}
.sheet(
isPresented: $viewModel.isShowShareView,
onDismiss: { viewModel.shareMessage = "" },
content: {
ActivityViewController(activityItems: [viewModel.shareMessage])
}
)
.onAppear {
viewModel.getDetail(roomId: roomId)
}
}
@ViewBuilder
private func JoinButton() -> some View {
if let room = viewModel.room {
HStack {
if room.channelName.isNullOrBlank() {
if room.manager.id == UserDefaults.int(forKey: .userId) {
VStack(spacing: 16.7) {
HStack(spacing: 13.3) {
Image("btn_big_share")
.onTapGesture {
viewModel.shareRoom(roomId: room.roomId)
}
Text("수정")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color.white)
.padding(.vertical, 16)
.padding(.horizontal, 27)
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.strokeBorder(lineWidth: 1)
.foregroundColor(Color(hex: "9970ff"))
)
.onTapGesture {
AppState.shared.back()
AppState.shared.setAppStep(step: .modifyLive(room: room))
}
Text("라이브 시작")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color.white)
.padding(.vertical, 16)
.frame(maxWidth: .infinity)
.background(Color(hex: "9970ff"))
.cornerRadius(10)
.onTapGesture {
onClickStart()
AppState.shared.back()
}
}
Text("예약삭제")
.font(.custom(Font.medium.rawValue, size: 14))
.foregroundColor(Color(hex: "ff5c49"))
.padding(5.3)
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.strokeBorder(lineWidth: 1)
.foregroundColor(Color(hex: "dd4500"))
)
.onTapGesture {
isShowCancelPopup = true
}
}
.frame(width: screenSize().width - 26.7)
} else if room.isPaid {
HStack(spacing: 13.3) {
Button {
viewModel.shareRoom(roomId: room.roomId)
} label: {
Image("btn_big_share")
}
Text("예약완료")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color(hex: "777777"))
.padding(.vertical, 16)
.padding(.horizontal, 99)
.background(Color(hex: "525252"))
.cornerRadius(10)
}
.frame(width: screenSize().width - 26.7)
} else {
HStack(spacing: 13.3) {
Button {
viewModel.shareRoom(roomId: room.roomId)
} label: {
Image("btn_big_share")
}
Button {
onClickReservation()
AppState.shared.back()
} label: {
Text("예약하기")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color.white)
.padding(.vertical, 16)
.padding(.horizontal, 99)
.background(Color(hex: "9970ff"))
.cornerRadius(10)
}
}
.frame(width: screenSize().width - 26.7)
}
} else {
HStack(spacing: 13.3) {
Button {
viewModel.shareRoom(roomId: room.roomId)
} label: {
Image("btn_big_share")
}
Button {
onClickParticipant()
AppState.shared.back()
} label: {
Text("지금 참여하기")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color.white)
.padding(.vertical, 16)
.padding(.horizontal, 79)
.background(Color(hex: "ff5c49"))
.cornerRadius(10)
}
}
.frame(width: screenSize().width - 26.7)
}
}
}
}
@ViewBuilder
private func ParticipantView(room: GetRoomDetailResponse) -> some View {
if isExpandParticipantArea {
HStack(spacing: 0) {
Text(room.channelName.isNullOrBlank() ? "예약자" : "참가자")
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(Color(hex: "eeeeee"))
Spacer()
Text("\(room.numberOfParticipants)")
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(Color(hex: "9970ff"))
Text("/\(room.numberOfParticipantsTotal)")
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(Color(hex: "bbbbbb"))
}
.padding(.top, 16.7)
LazyVGrid(columns: columns) {
ForEach(room.participatingUsers, id: \.self) { user in
VStack(spacing: 6.7) {
KFImage(URL(string: user.profileImageUrl))
.resizable()
.scaledToFill()
.frame(width: 46.7, height: 46.7, alignment: .top)
.clipShape(Circle())
Text(user.nickname)
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(Color(hex: "bbbbbb"))
.lineLimit(1)
}
}
}
.padding(.top, 16.7)
} else {
let userCount = room.numberOfParticipants > 10 ? 10 : room.numberOfParticipants
HStack(spacing: -13.3) {
ForEach(0..<userCount, id: \.self) { index in
let user = room.participatingUsers[index]
KFImage(URL(string: user.profileImageUrl))
.resizable()
.scaledToFill()
.frame(width: 33.3, height: 33.3, alignment: .top)
.clipShape(Circle())
}
Spacer()
HStack(spacing: 0) {
Text("\(room.numberOfParticipants)")
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(Color(hex: "9970ff"))
Text("/\(room.numberOfParticipantsTotal)")
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(Color(hex: "bbbbbb"))
}
}
.padding(.top, 22)
}
if room.numberOfParticipants > 0 {
HStack(spacing: 6.7) {
Image(isExpandParticipantArea ? "ic_suda_detail_top" : "ic_suda_detail_bottom")
.resizable()
.frame(width: 20, height: 20)
Text(isExpandParticipantArea ? "닫기" : "펼쳐보기")
.font(.custom(Font.medium.rawValue, size: 12))
.foregroundColor(Color(hex: "bbbbbb"))
}
.padding(.top, 13.3)
.onTapGesture {
isExpandParticipantArea.toggle()
}
}
}
private func hideView() {
if let close = onClickClose {
close()
} else {
AppState.shared.back()
}
}
}

View File

@@ -0,0 +1,162 @@
//
// LiveDetailViewModel.swift
// SodaLive
//
// Created by klaus on 2023/08/14.
//
import Foundation
import Combine
import FirebaseDynamicLinks
final class LiveDetailViewModel: ObservableObject {
private let repository = LiveRepository()
private var subscription = Set<AnyCancellable>()
@Published var isLoading = false
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var room: GetRoomDetailResponse? = nil
@Published var showDetail = false
@Published var shareMessage = ""
@Published var isShowShareView = false
func getDetail(roomId: Int) {
if !isLoading {
isLoading = true
repository.getRoomDetail(roomId: roomId)
.sink { result in
switch result {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
}
} receiveValue: { response in
let responseData = response.data
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<GetRoomDetailResponse>.self, from: responseData)
if let data = decoded.data, decoded.success {
DispatchQueue.main.async {
self.showDetail = true
self.room = data
}
} else {
if let message = decoded.message {
self.errorMessage = message
} else {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
}
self.isShowPopup = true
}
} catch {
print(error)
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowPopup = true
}
self.isLoading = false
}
.store(in: &subscription)
}
}
func onBack(afterExecute: () -> Void) {
showDetail = false
afterExecute()
}
func liveCancel(roomId: Int, reason: String, onSuccess: @escaping () -> Void) {
isLoading = true
repository.cancelRoom(roomId: roomId, reason: reason)
.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(ApiResponseWithoutData.self, from: responseData)
if decoded.success {
onSuccess()
} 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 shareRoom(roomId: Int) {
isLoading = true
guard let link = URL(string: "https://sodalive.net/?room_id=\(roomId)") else { return }
let dynamicLinksDomainURIPrefix = "https://sodalive.page.link"
guard let linkBuilder = DynamicLinkComponents(link: link, domainURIPrefix: dynamicLinksDomainURIPrefix) else {
self.errorMessage = "공유링크를 생성하지 못했습니다.\n다시 시도해 주세요."
self.isShowPopup = true
isLoading = false
return
}
linkBuilder.iOSParameters = DynamicLinkIOSParameters(bundleID: "kr.co.vividnext.sodalive")
linkBuilder.iOSParameters?.appStoreID = "1630284226"
linkBuilder.androidParameters = DynamicLinkAndroidParameters(packageName: "kr.co.vividnext.sodalive")
guard let longDynamicLink = linkBuilder.url else {
self.errorMessage = "공유링크를 생성하지 못했습니다.\n다시 시도해 주세요."
self.isShowPopup = true
isLoading = false
return
}
DEBUG_LOG("The long URL is: \(longDynamicLink)")
DynamicLinkComponents.shortenURL(longDynamicLink, options: nil) { [unowned self] url, warnings, error in
let shortUrl = url?.absoluteString
if let liveRoomInfo = self.room {
let urlString = shortUrl != nil ? shortUrl! : longDynamicLink.absoluteString
if liveRoomInfo.isPrivateRoom {
shareMessage = "\(UserDefaults.string(forKey: .nickname))님이 귀하를 소다라이브 비공개라이브에 초대하였습니다.\n" +
"※ 라이브 참여: \(urlString)\n" +
"(입장 비밀번호: \(liveRoomInfo.password!))"
} else {
shareMessage = "\(UserDefaults.string(forKey: .nickname))님이 귀하를 소다라이브 공개라이브에 초대하였습니다.\n" +
"※ 라이브 참여: \(urlString)"
}
isShowShareView = true
} else {
self.errorMessage = "공유링크를 생성하지 못했습니다.\n다시 시도해 주세요."
self.isShowPopup = true
return
}
isLoading = false
}
}
}

View File

@@ -0,0 +1,16 @@
//
// EditLiveRoomInfoRequest.swift
// SodaLive
//
// Created by klaus on 2023/08/14.
//
import Foundation
struct EditLiveRoomInfoRequest: Encodable {
let title: String?
let notice: String?
let numberOfPeople: Int?
let beginDateTimeString: String?
let timezone: String?
}

View File

@@ -0,0 +1,292 @@
//
// LiveRoomEditView.swift
// SodaLive
//
// Created by klaus on 2023/08/14.
//
import SwiftUI
struct LiveRoomEditView: View {
@StateObject var keyboardHandler = KeyboardHandler()
@StateObject var viewModel = LiveRoomEditViewModel()
@State private var isShowSelectDateView = false
@State private var isShowSelectTimeView = false
let room: GetRoomDetailResponse
init(room: GetRoomDetailResponse) {
UITextView.appearance().backgroundColor = .clear
UIScrollView.appearance().bounces = false
self.room = room
}
var body: some View {
BaseView(isLoading: $viewModel.isLoading) {
ZStack {
VStack(spacing: 0) {
DetailNavigationBar(title: "라이브 수정")
ScrollView(.vertical, showsIndicators: false) {
VStack(spacing: 0) {
TitleInputView()
.frame(width: screenSize().width - 26.7)
.padding(.top, 33.3)
ContentInputView()
.frame(width: screenSize().width - 26.7)
.padding(.top, 33.3)
ReservationDateTimeView(buttonWidth: (screenSize().width - 40) / 2)
.padding(.top, 22.7)
NumberOfPeopleLimitView()
.frame(width: screenSize().width - 26.7)
.padding(.top, 33.3)
if !viewModel.isLoading {
Text("라이브 수정")
.font(.custom(Font.bold.rawValue, size: 18.3))
.foregroundColor(Color.white)
.frame(width: screenSize().width - 26.7, height: 50)
.background(Color(hex: "9970ff"))
.cornerRadius(10)
.padding(.top, 30)
.onTapGesture {
viewModel.updateLiveRoom()
}
}
Rectangle()
.foregroundColor(Color(hex: "222222"))
.frame(width: screenSize().width, height: keyboardHandler.keyboardHeight)
}
}
}
if isShowSelectDateView {
SelectDateView()
}
if isShowSelectTimeView {
SelectTimeView()
}
}
.onTapGesture {
hideKeyboard()
}
.edgesIgnoringSafeArea(.bottom)
}
.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 {
viewModel.room = room
}
}
@ViewBuilder
func TitleInputView() -> some View {
VStack(spacing: 0) {
Text("제목")
.font(.custom(Font.bold.rawValue, size: 16.7))
.foregroundColor(Color(hex: "eeeeee"))
.padding(.horizontal, 13.3)
.frame(width: screenSize().width, alignment: .leading)
TextField("라이브 제목을 입력하세요", text: $viewModel.title)
.autocapitalization(.none)
.disableAutocorrection(true)
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color(hex: "eeeeee"))
.accentColor(Color(hex: "9970ff"))
.keyboardType(.default)
.padding(.top, 12)
.padding(.horizontal, 6.7)
Rectangle()
.frame(height: 1)
.foregroundColor(Color(hex: "909090").opacity(0.7))
.padding(.top, 8.3)
}
}
@ViewBuilder
func ContentInputView() -> some View {
VStack(spacing: 13.3) {
HStack(spacing: 0) {
Text("공지")
.font(.custom(Font.bold.rawValue, size: 16.7))
.foregroundColor(Color(hex: "eeeeee"))
Spacer()
Text("\(viewModel.notice.count)")
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color(hex: "ff5c49")) +
Text(" / 1000자")
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color(hex: "777777"))
}
TextViewWrapper(
text: $viewModel.notice,
placeholder: viewModel.placeholder,
textColorHex: "eeeeee",
backgroundColorHex: "222222"
)
.frame(width: screenSize().width - 26.7, height: 133.3)
.cornerRadius(6.7)
.padding(.top, 13.3)
}
}
@ViewBuilder
func ReservationDateTimeView(buttonWidth: CGFloat) -> some View {
HStack(spacing: 13.3) {
VStack(alignment: .leading, spacing: 6.7) {
Text("예약 날짜")
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color(hex: "eeeeee"))
Button(action: {
hideKeyboard()
self.isShowSelectDateView = true
}) {
Text(viewModel.reservationDateString)
.font(.custom(Font.medium.rawValue, size: 14.7))
.foregroundColor(Color(hex: "eeeeee"))
.frame(width: buttonWidth, height: 48.7)
.overlay(
RoundedRectangle(cornerRadius: 6.7)
.stroke(Color(hex: "9970ff"), lineWidth: 1.3)
)
}
}
VStack(alignment: .leading, spacing: 6.7) {
Text("예약 시간")
.font(.custom(Font.medium.rawValue, size: 13.3))
.foregroundColor(Color(hex: "eeeeee"))
Button(action: {
hideKeyboard()
self.isShowSelectTimeView = true
}) {
Text(viewModel.reservationTimeString)
.font(.custom(Font.medium.rawValue, size: 14.7))
.foregroundColor(Color(hex: "eeeeee"))
.frame(width: buttonWidth, height: 48.7)
.overlay(
RoundedRectangle(cornerRadius: 6.7)
.stroke(Color(hex: "9970ff"), lineWidth: 1.3)
)
}
}
}
.frame(width: screenSize().width)
.padding(.vertical, 13.3)
.background(Color(hex: "222222"))
}
@ViewBuilder
func NumberOfPeopleLimitView() -> some View {
VStack(spacing: 13.3) {
Text("참여인원 설정")
.font(.custom(Font.bold.rawValue, size: 16.7))
.foregroundColor(Color(hex: "eeeeee"))
.frame(width: screenSize().width - 26.7, alignment: .leading)
TextField("최대 인원 999명", text: $viewModel.numberOfPeople)
.autocapitalization(.none)
.disableAutocorrection(true)
.multilineTextAlignment(.center)
.font(.custom(Font.medium.rawValue, size: 14.7))
.foregroundColor(Color(hex: "eeeeee"))
.accentColor(Color(hex: "9970ff"))
.keyboardType(.numberPad)
.padding(.vertical, 15.7)
.frame(width: screenSize().width - 26.7, alignment: .center)
.background(Color(hex: "222222"))
.cornerRadius(6.7)
}
}
@ViewBuilder
func SelectDateView() -> some View {
GeometryReader { proxy in
ZStack {
Color
.black
.opacity(0.5)
.edgesIgnoringSafeArea(.all)
VStack(spacing: 0) {
DatePicker("", selection: $viewModel.reservationDate, in: Date()..., displayedComponents: .date)
.datePickerStyle(WheelDatePickerStyle())
.labelsHidden()
.environment(\.locale, Locale.init(identifier: "ko"))
.frame(width: proxy.size.width)
Button(action: { self.isShowSelectDateView = false }) {
Text("확인")
.font(.system(size: 16))
.foregroundColor(Color(hex: "eeeeee"))
.padding(.vertical, 10)
.frame(width: proxy.size.width - 53.4)
}
}
.background(Color(hex: "222222"))
.cornerRadius(6.7)
}
.frame(width: proxy.size.width)
}
}
@ViewBuilder
func SelectTimeView() -> some View {
GeometryReader { proxy in
ZStack {
Color
.black
.opacity(0.5)
.edgesIgnoringSafeArea(.all)
VStack(spacing: 0) {
DatePicker("", selection: $viewModel.reservationTime, displayedComponents: .hourAndMinute)
.datePickerStyle(WheelDatePickerStyle())
.labelsHidden()
.environment(\.locale, Locale.init(identifier: "ko"))
.frame(width: proxy.size.width - 53.4)
Button(action: { self.isShowSelectTimeView = false }) {
Text("확인")
.font(.system(size: 16))
.foregroundColor(Color(hex: "eeeeee"))
.padding(.vertical, 10)
.frame(width: proxy.size.width)
}
}
.background(Color(hex: "222222"))
.cornerRadius(6.7)
}
.frame(width: proxy.size.width)
}
}
}

View File

@@ -0,0 +1,178 @@
//
// LiveRoomEditViewModel.swift
// SodaLive
//
// Created by klaus on 2023/08/14.
//
import Foundation
import Moya
import Combine
final class LiveRoomEditViewModel: ObservableObject {
@Published var isLoading = false
@Published var title: String = ""
@Published var notice: String = "" {
didSet {
if notice.count > 1000 {
notice = String(notice.prefix(1000))
}
}
}
@Published var numberOfPeople = ""
@Published var reservationDateString: String = ""
@Published var reservationTimeString: String = ""
@Published var errorMessage = ""
@Published var isShowPopup = false
private let repository = LiveRepository()
private var subscription = Set<AnyCancellable>()
var reservationDate = Date() {
didSet {
reservationDateString = reservationDate.convertDateFormat(dateFormat: "yyyy.MM.dd")
}
}
var reservationTime = Date() {
didSet {
reservationTimeString = reservationTime.convertDateFormat(dateFormat: "a hh:mm")
}
}
let placeholder = "라이브 공지를 입력하세요"
var room: GetRoomDetailResponse? = nil {
didSet {
isLoading = true
title = room!.title
notice = room!.notice
numberOfPeople = String(room!.numberOfParticipantsTotal)
let fromFormatter = DateFormatter()
fromFormatter.dateFormat = "yyyy.MM.dd EEE hh:mm a"
fromFormatter.locale = Locale(identifier: "en_US_POSIX")
reservationDate = fromFormatter.date(from: room!.beginDateTime)!
reservationTime = fromFormatter.date(from: room!.beginDateTime)!
let beginDate = reservationDate.convertDateFormat(dateFormat: "yyyy-MM-dd")
let beginTime = reservationTime.convertDateFormat(dateFormat: "HH:mm")
beginDateTimeStr = "\(beginDate) \(beginTime)"
isLoading = false
}
}
var beginDateTimeStr: String = ""
func updateLiveRoom() {
if let room = room, !isLoading && validate() {
isLoading = true
let beginDate = reservationDate.convertDateFormat(dateFormat: "yyyy-MM-dd")
let beginTime = reservationTime.convertDateFormat(dateFormat: "HH:mm")
let beginDateTime = "\(beginDate) \(beginTime)"
let request = EditLiveRoomInfoRequest(
title: room.title != title ? title : nil,
notice: room.notice != notice ? notice : nil,
numberOfPeople: room.numberOfParticipantsTotal != Int(numberOfPeople)! ? Int(numberOfPeople)! : nil,
beginDateTimeString: beginDateTimeStr != beginDateTime ? beginDateTime : nil,
timezone: TimeZone.current.identifier
)
if (
request.title == nil &&
request.notice == nil &&
request.numberOfPeople == nil &&
request.beginDateTimeString == nil
) {
self.errorMessage = "변경사항이 없습니다."
self.isShowPopup = true
isLoading = false
return
}
var multipartData = [MultipartFormData]()
let encoder = JSONEncoder()
encoder.outputFormatting = .withoutEscapingSlashes
let jsonData = try? encoder.encode(request)
if let jsonData = jsonData {
multipartData.append(MultipartFormData(provider: .data(jsonData), name: "request"))
repository.editLiveRoomInfo(roomId: room.roomId, parameters: multipartData)
.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(ApiResponseWithoutData.self, from: responseData)
if decoded.success {
self.errorMessage = "라이브 정보가 수정되었습니다."
self.isShowPopup = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
AppState.shared.back()
}
} else {
if let message = decoded.message {
self.errorMessage = message
} else {
self.errorMessage = "라이브 정보를 수정 하지 못했습니다.\n다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
}
self.isShowPopup = true
}
} catch {
self.errorMessage = "라이브 정보를 수정 하지 못했습니다.\n다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowPopup = true
}
}
.store(in: &subscription)
} else {
self.errorMessage = "라이브 정보를 수정 하지 못했습니다.\n다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowPopup = true
self.isLoading = false
}
}
}
private func validate() -> Bool {
if title.trimmingCharacters(in: .whitespaces).isEmpty {
self.errorMessage = "제목을 입력해 주세요."
self.isShowPopup = true
return false
}
let notice = notice.trimmingCharacters(in: .whitespacesAndNewlines) != placeholder ? notice : ""
if notice.isEmpty && notice.count < 5 {
self.errorMessage = "공지를 5자 이상 입력해주세요."
self.isShowPopup = true
return false
}
guard let numberOfPeople = Int(numberOfPeople), (numberOfPeople >= 3 && numberOfPeople <= 999) else {
self.errorMessage = "인원을 3~999명 사이로 입력해주세요."
self.isShowPopup = true
return false
}
return true
}
}

View File

@@ -10,5 +10,5 @@ import Foundation
struct EnterOrQuitLiveRoomRequest: Encodable {
let roomId: Int
let container: String = "ios"
var password: Int? = nil
var password: String? = nil
}