콘텐츠 추가
This commit is contained in:
260
SodaLive/Sources/Content/Modify/ContentModifyView.swift
Normal file
260
SodaLive/Sources/Content/Modify/ContentModifyView.swift
Normal file
@@ -0,0 +1,260 @@
|
||||
//
|
||||
// ContentModifyView.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/08/13.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Kingfisher
|
||||
|
||||
struct ContentModifyView: View {
|
||||
|
||||
let contentId: Int
|
||||
@StateObject var keyboardHandler = KeyboardHandler()
|
||||
@StateObject private var viewModel = ContentModifyViewModel()
|
||||
|
||||
@State private var isShowPhotoPicker = false
|
||||
|
||||
var body: some View {
|
||||
BaseView(isLoading: $viewModel.isLoading) {
|
||||
GeometryReader { proxy in
|
||||
ZStack {
|
||||
VStack(spacing: 0) {
|
||||
DetailNavigationBar(title: "콘텐츠 수정")
|
||||
|
||||
ScrollView(.vertical, showsIndicators: false) {
|
||||
VStack(spacing: 0) {
|
||||
Text("썸네일")
|
||||
.font(.custom(Font.bold.rawValue, size: 16.7))
|
||||
.foregroundColor(Color(hex: "eeeeee"))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
ZStack {
|
||||
if let selectedImage = viewModel.coverImage {
|
||||
Image(uiImage: selectedImage)
|
||||
.resizable()
|
||||
.scaledToFill()
|
||||
.frame(width: 107, height: 107)
|
||||
.background(Color(hex: "3e3358"))
|
||||
.cornerRadius(8)
|
||||
.clipped()
|
||||
} else if let coverImageUrl = viewModel.coverImageUrl {
|
||||
KFImage(URL(string: coverImageUrl))
|
||||
.resizable()
|
||||
.scaledToFill()
|
||||
.frame(width: 107, height: 107)
|
||||
.background(Color(hex: "3e3358"))
|
||||
.cornerRadius(8)
|
||||
.clipped()
|
||||
} else {
|
||||
Image("ic_logo")
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.padding(13.3)
|
||||
.frame(width: 107, height: 107)
|
||||
.background(Color(hex: "3e3358"))
|
||||
.cornerRadius(8)
|
||||
}
|
||||
|
||||
Image("ic_camera")
|
||||
.padding(10)
|
||||
.background(Color(hex: "9970ff"))
|
||||
.cornerRadius(30)
|
||||
.offset(x: 50, y: 36)
|
||||
}
|
||||
.frame(alignment: .bottomTrailing)
|
||||
.onTapGesture { isShowPhotoPicker = true }
|
||||
}
|
||||
.padding(.top, 13.3)
|
||||
.padding(.horizontal, 13.3)
|
||||
|
||||
Rectangle()
|
||||
.foregroundColor(Color(hex: "232323"))
|
||||
.frame(height: 6.7)
|
||||
.padding(.top, 26.7)
|
||||
|
||||
VStack(spacing: 0) {
|
||||
Text("제목")
|
||||
.font(.custom(Font.bold.rawValue, size: 16.7))
|
||||
.foregroundColor(Color(hex: "eeeeee"))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
TextField("제목을 입력하세요", text: $viewModel.title)
|
||||
.autocapitalization(.none)
|
||||
.disableAutocorrection(true)
|
||||
.font(.custom(Font.medium.rawValue, size: 13.3))
|
||||
.foregroundColor(Color(hex: "eeeeee"))
|
||||
.padding(.vertical, 16.7)
|
||||
.padding(.horizontal, 13.3)
|
||||
.background(Color(hex: "222222"))
|
||||
.cornerRadius(6.7)
|
||||
.keyboardType(.default)
|
||||
.padding(.top, 13.3)
|
||||
|
||||
HStack(spacing: 0) {
|
||||
Text("내용")
|
||||
.font(.custom(Font.bold.rawValue, size: 16.7))
|
||||
.foregroundColor(Color(hex: "eeeeee"))
|
||||
|
||||
Spacer()
|
||||
|
||||
Text("\(viewModel.detail.count)자")
|
||||
.font(.custom(Font.medium.rawValue, size: 13.3))
|
||||
.foregroundColor(Color(hex: "ff5c49")) +
|
||||
Text(" / 최대 500자")
|
||||
.font(.custom(Font.medium.rawValue, size: 13.3))
|
||||
.foregroundColor(Color(hex: "777777"))
|
||||
}
|
||||
.padding(.top, 26.7)
|
||||
|
||||
TextViewWrapper(
|
||||
text: $viewModel.detail,
|
||||
placeholder: viewModel.placeholder,
|
||||
textColorHex: "eeeeee",
|
||||
backgroundColorHex: "222222"
|
||||
)
|
||||
.frame(height: 184)
|
||||
.cornerRadius(6.7)
|
||||
.padding(.top, 13.3)
|
||||
}
|
||||
.padding(.top, 26.7)
|
||||
.padding(.horizontal, 13.3)
|
||||
|
||||
Rectangle()
|
||||
.foregroundColor(Color(hex: "232323"))
|
||||
.frame(height: 6.7)
|
||||
.padding(.top, 26.7)
|
||||
|
||||
if viewModel.isAdultShowUi {
|
||||
VStack(spacing: 13.3) {
|
||||
Text("연령 제한")
|
||||
.font(.custom(Font.bold.rawValue, size: 16.7))
|
||||
.foregroundColor(Color(hex: "eeeeee"))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
HStack(spacing: 13.3) {
|
||||
SelectButtonView(title: "전체 연령", isChecked: !viewModel.isAdult) {
|
||||
if viewModel.isAdult {
|
||||
viewModel.isAdult = false
|
||||
}
|
||||
}
|
||||
|
||||
SelectButtonView(title: "19세 이상", isChecked: viewModel.isAdult) {
|
||||
if !viewModel.isAdult {
|
||||
viewModel.isAdult = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Text("성인콘텐츠를 전체관람가로 등록할 시 발생하는 법적 책임은 회사와 상관없이 콘텐츠를 등록한 본인에게 있습니다.\n콘텐츠 내용은 물론 제목도 19금 여부를 체크해 주시기 바랍니다.")
|
||||
.font(.custom(Font.medium.rawValue, size: 13.3))
|
||||
.foregroundColor(Color(hex: "DD4500"))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.padding(.top, 13.3)
|
||||
}
|
||||
.padding(.top, 26.7)
|
||||
.padding(.horizontal, 13.3)
|
||||
}
|
||||
|
||||
VStack(spacing: 13.3) {
|
||||
Text("댓글 가능 여부")
|
||||
.font(.custom(Font.bold.rawValue, size: 16.7))
|
||||
.foregroundColor(Color(hex: "eeeeee"))
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
HStack(spacing: 13.3) {
|
||||
SelectButtonView(title: "댓글 가능", isChecked: viewModel.isAvailableComment) {
|
||||
if !viewModel.isAvailableComment {
|
||||
viewModel.isAvailableComment = true
|
||||
}
|
||||
}
|
||||
|
||||
SelectButtonView(title: "댓글 불가", isChecked: !viewModel.isAvailableComment) {
|
||||
if viewModel.isAvailableComment {
|
||||
viewModel.isAvailableComment = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.top, 26.7)
|
||||
.padding(.horizontal, 13.3)
|
||||
|
||||
VStack(spacing: 0) {
|
||||
HStack(alignment: .top, spacing: 0) {
|
||||
Text("등록")
|
||||
.font(.custom(Font.bold.rawValue, size: 18.3))
|
||||
.foregroundColor(Color.white)
|
||||
.frame(height: 50)
|
||||
.frame(maxWidth: .infinity)
|
||||
.background(Color(hex: "9970ff"))
|
||||
.cornerRadius(10)
|
||||
.padding(13.3)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.background(Color(hex: "222222"))
|
||||
.cornerRadius(16.7, corners: [.topLeft, .topRight])
|
||||
.onTapGesture {
|
||||
hideKeyboard()
|
||||
viewModel.modifyAudioContent {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||
AppState.shared.back()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Rectangle()
|
||||
.foregroundColor(Color(hex: "222222"))
|
||||
.frame(height: keyboardHandler.keyboardHeight)
|
||||
.frame(maxWidth: .infinity)
|
||||
|
||||
if proxy.safeAreaInsets.bottom > 0 {
|
||||
Rectangle()
|
||||
.foregroundColor(Color(hex: "222222"))
|
||||
.frame(height: 15.3)
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
}
|
||||
.padding(.top, 30)
|
||||
}
|
||||
}
|
||||
|
||||
if isShowPhotoPicker {
|
||||
ImagePicker(
|
||||
isShowing: $isShowPhotoPicker,
|
||||
selectedImage: $viewModel.coverImage,
|
||||
sourceType: .photoLibrary
|
||||
)
|
||||
}
|
||||
}
|
||||
.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: screenSize().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.contentId = contentId
|
||||
viewModel.getAudioContentDetail {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||
AppState.shared.back()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
177
SodaLive/Sources/Content/Modify/ContentModifyViewModel.swift
Normal file
177
SodaLive/Sources/Content/Modify/ContentModifyViewModel.swift
Normal file
@@ -0,0 +1,177 @@
|
||||
//
|
||||
// ContentModifyViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/08/13.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import Moya
|
||||
import Combine
|
||||
|
||||
final class ContentModifyViewModel: ObservableObject {
|
||||
private let repository = ContentRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
@Published var errorMessage = ""
|
||||
@Published var isShowPopup = false
|
||||
@Published var isLoading = false
|
||||
|
||||
@Published private(set) var audioContent: GetAudioContentDetailResponse?
|
||||
|
||||
@Published var title: String = ""
|
||||
@Published var detail: String = ""
|
||||
@Published var coverImage: UIImage? = nil
|
||||
@Published var coverImageUrl: String? = nil
|
||||
|
||||
@Published var isAvailableComment = true
|
||||
@Published var isAdult = false
|
||||
@Published var isAdultShowUi = false
|
||||
|
||||
var contentId: Int = 0
|
||||
var placeholder = "내용을 입력하세요"
|
||||
|
||||
func getAudioContentDetail(onFailure: (() -> Void)? = nil) {
|
||||
audioContent = nil
|
||||
isLoading = true
|
||||
|
||||
repository.getAudioContentDetail(audioContentId: contentId)
|
||||
.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<GetAudioContentDetailResponse>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self.audioContent = data
|
||||
|
||||
self.title = data.title
|
||||
self.detail = data.detail
|
||||
self.isAdult = data.isAdult
|
||||
self.isAdultShowUi = !data.isAdult
|
||||
self.coverImageUrl = data.coverImageUrl
|
||||
self.isAvailableComment = data.isCommentAvailable
|
||||
} 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 modifyAudioContent(onSuccess: @escaping () -> Void) {
|
||||
if !isLoading && contentId > 0 && validateData() {
|
||||
isLoading = true
|
||||
|
||||
let request = ModifyContentRequest(
|
||||
audioContentId: contentId,
|
||||
title: title != audioContent!.title ? title : nil,
|
||||
detail: detail != audioContent!.detail ? detail : nil,
|
||||
isAdult: isAdult,
|
||||
isCommentAvailable: isAvailableComment
|
||||
)
|
||||
|
||||
var multipartData = [MultipartFormData]()
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .withoutEscapingSlashes
|
||||
let jsonData = try? encoder.encode(request)
|
||||
|
||||
if let jsonData = jsonData {
|
||||
if let coverImage = coverImage {
|
||||
if let imageData = coverImage.jpegData(compressionQuality: 0.8) {
|
||||
multipartData.append(
|
||||
MultipartFormData(
|
||||
provider: .data(imageData),
|
||||
name: "coverImage",
|
||||
fileName: "\(UUID().uuidString)_\(Date().timeIntervalSince1970 * 1000).jpg",
|
||||
mimeType: "image/*")
|
||||
)
|
||||
} else {
|
||||
errorMessage = "커버이미지를 업로드 하지 못했습니다.\n다시 선택해 주세요"
|
||||
isShowPopup = true
|
||||
isLoading = false
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
multipartData.append(MultipartFormData(provider: .data(jsonData), name: "request"))
|
||||
|
||||
repository
|
||||
.modifyAudioContent(parameters: multipartData)
|
||||
.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
|
||||
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)
|
||||
} else {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
self.isLoading = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func validateData() -> Bool {
|
||||
if title != audioContent!.title && title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||
errorMessage = "제목을 입력해 주세요."
|
||||
isShowPopup = true
|
||||
return false
|
||||
}
|
||||
|
||||
if detail != audioContent!.detail && (detail.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || detail.count < 5) {
|
||||
errorMessage = "내용을 5자 이상 입력해 주세요."
|
||||
isShowPopup = true
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
16
SodaLive/Sources/Content/Modify/ModifyContentRequest.swift
Normal file
16
SodaLive/Sources/Content/Modify/ModifyContentRequest.swift
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// ModifyContentRequest.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/08/13.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct ModifyContentRequest: Encodable {
|
||||
let audioContentId: Int
|
||||
let title: String?
|
||||
let detail: String?
|
||||
let isAdult: Bool
|
||||
let isCommentAvailable: Bool
|
||||
}
|
Reference in New Issue
Block a user