라이브 상세 페이지 추가

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

@@ -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
}
}