커뮤니티 게시물 등록 API 적용,
채널 프로필 API - 커뮤니티 게시물 추가
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// CreateCommunityPostRequest.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/12/19.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct CreateCommunityPostRequest: Encodable {
|
||||
let content: String
|
||||
let isAdult: Bool
|
||||
let isCommentAvailable: Bool
|
||||
}
|
@@ -10,9 +10,13 @@ import SwiftUI
|
||||
struct CreatorCommunityWriteView: View {
|
||||
|
||||
@StateObject var keyboardHandler = KeyboardHandler()
|
||||
@StateObject private var viewModel = CreatorCommunityWriteViewModel()
|
||||
|
||||
@State private var isShowPhotoPicker = false
|
||||
let onSuccess: () -> Void
|
||||
|
||||
var body: some View {
|
||||
BaseView {
|
||||
BaseView(isLoading: $viewModel.isLoading) {
|
||||
GeometryReader { proxy in
|
||||
ZStack {
|
||||
VStack(spacing: 0) {
|
||||
@@ -27,13 +31,24 @@ struct CreatorCommunityWriteView: View {
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
ZStack {
|
||||
Image("ic_logo2")
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.padding(13.3)
|
||||
.frame(width: 107, height: 107)
|
||||
.background(Color(hex: "13181B"))
|
||||
.cornerRadius(8)
|
||||
if let selectedImage = viewModel.postImage {
|
||||
Image(uiImage: selectedImage)
|
||||
.resizable()
|
||||
.scaledToFill()
|
||||
.frame(width: 107, height: 107)
|
||||
.background(Color(hex: "3e3358"))
|
||||
.cornerRadius(8)
|
||||
.clipped()
|
||||
} else {
|
||||
Image("ic_logo2")
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.padding(13.3)
|
||||
.frame(width: 107, height: 107)
|
||||
.background(Color(hex: "13181B"))
|
||||
.cornerRadius(8)
|
||||
.clipped()
|
||||
}
|
||||
|
||||
Image("ic_camera")
|
||||
.padding(10)
|
||||
@@ -42,6 +57,8 @@ struct CreatorCommunityWriteView: View {
|
||||
.offset(x: 50, y: 36)
|
||||
}
|
||||
.frame(alignment: .bottomTrailing)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture { isShowPhotoPicker = true }
|
||||
|
||||
HStack(alignment: .top, spacing: 0) {
|
||||
Text("※ ")
|
||||
@@ -62,7 +79,7 @@ struct CreatorCommunityWriteView: View {
|
||||
|
||||
Spacer()
|
||||
|
||||
Text("0자")
|
||||
Text("\(viewModel.content.count)자")
|
||||
.font(.custom(Font.medium.rawValue, size: 13.3))
|
||||
.foregroundColor(Color(hex: "ff5c49")) +
|
||||
Text(" / 최대 500자")
|
||||
@@ -72,8 +89,8 @@ struct CreatorCommunityWriteView: View {
|
||||
.padding(.top, 26.7)
|
||||
|
||||
TextViewWrapper(
|
||||
text: .constant(""),
|
||||
placeholder: "내용을 입력하세요",
|
||||
text: $viewModel.content,
|
||||
placeholder: viewModel.placeholder,
|
||||
textColorHex: "eeeeee",
|
||||
backgroundColorHex: "222222"
|
||||
)
|
||||
@@ -88,10 +105,22 @@ struct CreatorCommunityWriteView: View {
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
HStack(spacing: 13.3) {
|
||||
SelectButtonView(title: "댓글 가능", isChecked: true) {
|
||||
SelectButtonView(
|
||||
title: "댓글 가능",
|
||||
isChecked: viewModel.isAvailableComment
|
||||
) {
|
||||
if !viewModel.isAvailableComment {
|
||||
viewModel.isAvailableComment = true
|
||||
}
|
||||
}
|
||||
|
||||
SelectButtonView(title: "댓글 불가", isChecked: false) {
|
||||
SelectButtonView(
|
||||
title: "댓글 불가",
|
||||
isChecked: !viewModel.isAvailableComment
|
||||
) {
|
||||
if viewModel.isAvailableComment {
|
||||
viewModel.isAvailableComment = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,10 +133,22 @@ struct CreatorCommunityWriteView: View {
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
HStack(spacing: 13.3) {
|
||||
SelectButtonView(title: "전체 연령", isChecked: true) {
|
||||
SelectButtonView(
|
||||
title: "전체 연령",
|
||||
isChecked: !viewModel.isAdult
|
||||
) {
|
||||
if viewModel.isAdult {
|
||||
viewModel.isAdult = false
|
||||
}
|
||||
}
|
||||
|
||||
SelectButtonView(title: "19세 이상", isChecked: false) {
|
||||
SelectButtonView(
|
||||
title: "19세 이상",
|
||||
isChecked: viewModel.isAdult
|
||||
) {
|
||||
if !viewModel.isAdult {
|
||||
viewModel.isAdult = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,6 +171,7 @@ struct CreatorCommunityWriteView: View {
|
||||
)
|
||||
.onTapGesture {
|
||||
hideKeyboard()
|
||||
AppState.shared.back()
|
||||
}
|
||||
|
||||
Text("등록")
|
||||
@@ -141,6 +183,13 @@ struct CreatorCommunityWriteView: View {
|
||||
.cornerRadius(10)
|
||||
.onTapGesture {
|
||||
hideKeyboard()
|
||||
viewModel.createCommunityPost {
|
||||
AppState.shared.back()
|
||||
|
||||
DispatchQueue.main.async {
|
||||
onSuccess()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(13.3)
|
||||
@@ -164,8 +213,34 @@ struct CreatorCommunityWriteView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if isShowPhotoPicker {
|
||||
ImagePicker(
|
||||
isShowing: $isShowPhotoPicker,
|
||||
selectedImage: $viewModel.postImage,
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,6 +248,6 @@ struct CreatorCommunityWriteView: View {
|
||||
|
||||
struct CreatorCommunityWriteView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
CreatorCommunityWriteView()
|
||||
CreatorCommunityWriteView {}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,108 @@
|
||||
//
|
||||
// CreatorCommunityWriteViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/12/19.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
import Moya
|
||||
import Combine
|
||||
|
||||
final class CreatorCommunityWriteViewModel: ObservableObject {
|
||||
private let repository = CreatorCommunityRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
@Published var isLoading = false
|
||||
@Published var errorMessage = ""
|
||||
@Published var isShowPopup = false
|
||||
|
||||
@Published var content = ""
|
||||
@Published var isAdult = false
|
||||
@Published var isAvailableComment = true
|
||||
@Published var postImage: UIImage? = nil
|
||||
|
||||
var placeholder = "내용을 입력하세요"
|
||||
|
||||
func createCommunityPost(onSuccess: @escaping () -> Void) {
|
||||
if !isLoading && validateData() {
|
||||
isLoading = true
|
||||
|
||||
let request = CreateCommunityPostRequest(content: content, 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 postImage = postImage, let imageData = postImage.jpegData(compressionQuality: 0.8) {
|
||||
multipartData.append(
|
||||
MultipartFormData(
|
||||
provider: .data(imageData),
|
||||
name: "postImage",
|
||||
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
|
||||
.createCommunityPost(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
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
||||
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 {
|
||||
return true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user