feat(creator): 라이브 탭 기반을 추가한다
This commit is contained in:
@@ -3162,6 +3162,17 @@ If you block this user, the following features will be restricted.
|
||||
}
|
||||
}
|
||||
|
||||
enum CreatorChannelLive {
|
||||
static var totalLabel: String { pick(ko: "전체", en: "Total", ja: "全体") }
|
||||
enum Sort {
|
||||
static var latest: String { pick(ko: "최신순", en: "Latest", ja: "最新順") }
|
||||
static var popular: String { pick(ko: "인기순", en: "Popular", ja: "人気順") }
|
||||
static var owned: String { pick(ko: "소장중", en: "Owned", ja: "所持中") }
|
||||
static var priceHigh: String { pick(ko: "높은 가격순", en: "Highest price", ja: "価格が高い順") }
|
||||
static var priceLow: String { pick(ko: "낮은 가격순", en: "Lowest price", ja: "価格が低い順") }
|
||||
}
|
||||
}
|
||||
|
||||
enum CreatorChannelHome {
|
||||
static var currentLive: String { pick(ko: "현재 라이브", en: "Live now", ja: "現在ライブ") }
|
||||
static var latestAudio: String { pick(ko: "최신 오디오 콘텐츠", en: "Latest audio", ja: "最新オーディオ") }
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import SwiftUI
|
||||
|
||||
struct CreatorChannelSortBar: View {
|
||||
let totalCount: Int
|
||||
let selectedSort: ContentSort
|
||||
let onTapSort: () -> Void
|
||||
|
||||
var body: some View {
|
||||
HStack(alignment: .center, spacing: SodaSpacing.s8) {
|
||||
HStack(spacing: SodaSpacing.s4) {
|
||||
Text(I18n.CreatorChannelLive.totalLabel)
|
||||
.appFont(size: 14, weight: .medium)
|
||||
.foregroundColor(Color.gray500)
|
||||
|
||||
Text(totalCount.comma())
|
||||
.appFont(size: 14, weight: .medium)
|
||||
.foregroundColor(Color.gray400)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Button(action: onTapSort) {
|
||||
HStack(spacing: SodaSpacing.s4) {
|
||||
Text(selectedSort.title)
|
||||
.appFont(size: 14, weight: .medium)
|
||||
.foregroundColor(Color.white)
|
||||
|
||||
Image(systemName: "chevron.down")
|
||||
.font(.system(size: 12, weight: .semibold))
|
||||
.foregroundColor(Color.gray400)
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
.padding(.horizontal, SodaSpacing.s20)
|
||||
.frame(height: 52)
|
||||
.background(Color.black)
|
||||
}
|
||||
}
|
||||
|
||||
struct CreatorChannelSortBar_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
CreatorChannelSortBar(totalCount: 100, selectedSort: .latest, onTapSort: {})
|
||||
.previewLayout(.sizeThatFits)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import SwiftUI
|
||||
|
||||
struct CreatorChannelSortBottomSheet: View {
|
||||
let selectedSort: ContentSort
|
||||
let sorts: [ContentSort]
|
||||
let onSelect: (ContentSort) -> Void
|
||||
|
||||
init(
|
||||
selectedSort: ContentSort,
|
||||
sorts: [ContentSort] = ContentSort.allCases,
|
||||
onSelect: @escaping (ContentSort) -> Void
|
||||
) {
|
||||
self.selectedSort = selectedSort
|
||||
self.sorts = sorts
|
||||
self.onSelect = onSelect
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
ForEach(sorts, id: \.self) { sort in
|
||||
Button {
|
||||
onSelect(sort)
|
||||
} label: {
|
||||
Text(sort.title)
|
||||
.appFont(size: 16, weight: selectedSort == sort ? .bold : .medium)
|
||||
.foregroundColor(selectedSort == sort ? Color.white : Color.gray400)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.padding(.horizontal, SodaSpacing.s20)
|
||||
.frame(height: 52)
|
||||
.background(selectedSort == sort ? Color.gray900 : Color.black)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
.padding(.vertical, SodaSpacing.s8)
|
||||
.background(Color.black)
|
||||
}
|
||||
}
|
||||
|
||||
struct CreatorChannelSortBottomSheet_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
CreatorChannelSortBottomSheet(selectedSort: .latest, onSelect: { _ in })
|
||||
.previewLayout(.sizeThatFits)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
final class CreatorChannelLiveViewModel: ObservableObject {
|
||||
private let repository = CreatorChannelLiveRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
private let pageSize = 20
|
||||
|
||||
@Published var isLoading = false
|
||||
@Published var isLoadingNextPage = false
|
||||
@Published var response: CreatorChannelLiveTabResponse?
|
||||
@Published var liveReplayContents = [CreatorChannelAudioContentResponse]()
|
||||
@Published var selectedSort: ContentSort = .latest
|
||||
@Published var page = 0
|
||||
@Published var size = 20
|
||||
@Published var hasNext = false
|
||||
@Published var hasLoaded = false
|
||||
@Published var errorMessage = ""
|
||||
@Published var isShowPopup = false
|
||||
|
||||
func fetchFirstPage(creatorId: Int) {
|
||||
fetchLive(creatorId: creatorId, page: 0, isNextPage: false)
|
||||
}
|
||||
|
||||
func fetchNextPageIfNeeded(creatorId: Int, currentItem: CreatorChannelAudioContentResponse) {
|
||||
guard liveReplayContents.last?.audioContentId == currentItem.audioContentId else { return }
|
||||
guard hasNext, isLoadingNextPage == false, isLoading == false else { return }
|
||||
|
||||
fetchLive(creatorId: creatorId, page: page + 1, isNextPage: true)
|
||||
}
|
||||
|
||||
func selectSort(_ sort: ContentSort, creatorId: Int) {
|
||||
guard selectedSort != sort else { return }
|
||||
|
||||
selectedSort = sort
|
||||
fetchFirstPage(creatorId: creatorId)
|
||||
}
|
||||
|
||||
private func fetchLive(creatorId: Int, page requestPage: Int, isNextPage: Bool) {
|
||||
if isNextPage {
|
||||
isLoadingNextPage = true
|
||||
} else {
|
||||
isLoading = true
|
||||
}
|
||||
|
||||
repository.getLive(creatorId: creatorId, page: requestPage, size: pageSize, sort: selectedSort)
|
||||
.sink { [weak self] result in
|
||||
guard let self else { return }
|
||||
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
self.applyFailureState()
|
||||
}
|
||||
} receiveValue: { [weak self] response in
|
||||
guard let self else { return }
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<CreatorChannelLiveTabResponse>.self, from: response.data)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self.applySuccessState(data, isNextPage: isNextPage)
|
||||
} else {
|
||||
self.errorMessage = decoded.message ?? I18n.Common.commonError
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
self.errorMessage = I18n.Common.commonError
|
||||
self.isShowPopup = true
|
||||
}
|
||||
|
||||
self.hasLoaded = true
|
||||
self.isLoading = false
|
||||
self.isLoadingNextPage = false
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
|
||||
private func applySuccessState(_ data: CreatorChannelLiveTabResponse, isNextPage: Bool) {
|
||||
response = data
|
||||
page = data.page
|
||||
size = data.size
|
||||
hasNext = data.hasNext
|
||||
|
||||
if isNextPage {
|
||||
liveReplayContents.append(contentsOf: data.liveReplayContents)
|
||||
} else {
|
||||
liveReplayContents = data.liveReplayContents
|
||||
}
|
||||
}
|
||||
|
||||
private func applyFailureState() {
|
||||
errorMessage = I18n.Common.commonError
|
||||
isShowPopup = true
|
||||
hasLoaded = true
|
||||
isLoading = false
|
||||
isLoadingNextPage = false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import Foundation
|
||||
|
||||
struct CreatorChannelLiveTabResponse: Decodable {
|
||||
let liveReplayContentCount: Int
|
||||
let currentLive: CreatorChannelLiveResponse?
|
||||
let liveReplayContents: [CreatorChannelAudioContentResponse]
|
||||
let sort: ContentSort
|
||||
let page: Int
|
||||
let size: Int
|
||||
let hasNext: Bool
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import Foundation
|
||||
|
||||
import Moya
|
||||
|
||||
enum CreatorChannelLiveApi {
|
||||
case getLive(creatorId: Int, page: Int, size: Int, sort: ContentSort)
|
||||
}
|
||||
|
||||
extension CreatorChannelLiveApi: TargetType {
|
||||
var baseURL: URL {
|
||||
return URL(string: BASE_URL)!
|
||||
}
|
||||
|
||||
var path: String {
|
||||
switch self {
|
||||
case .getLive(let creatorId, _, _, _):
|
||||
return "/api/v2/creator-channels/\(creatorId)/live"
|
||||
}
|
||||
}
|
||||
|
||||
var method: Moya.Method {
|
||||
switch self {
|
||||
case .getLive:
|
||||
return .get
|
||||
}
|
||||
}
|
||||
|
||||
var task: Moya.Task {
|
||||
switch self {
|
||||
case .getLive(_, let page, let size, let sort):
|
||||
return .requestParameters(
|
||||
parameters: ["page": page, "size": size, "sort": sort.rawValue],
|
||||
encoding: URLEncoding.queryString
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
var headers: [String: String]? {
|
||||
return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
import CombineMoya
|
||||
import Moya
|
||||
|
||||
final class CreatorChannelLiveRepository {
|
||||
private let api = MoyaProvider<CreatorChannelLiveApi>()
|
||||
|
||||
func getLive(creatorId: Int, page: Int, size: Int, sort: ContentSort) -> AnyPublisher<Response, MoyaError> {
|
||||
return api.requestPublisher(.getLive(creatorId: creatorId, page: page, size: size, sort: sort))
|
||||
}
|
||||
}
|
||||
24
SodaLive/Sources/V2/CreatorChannel/Models/ContentSort.swift
Normal file
24
SodaLive/Sources/V2/CreatorChannel/Models/ContentSort.swift
Normal file
@@ -0,0 +1,24 @@
|
||||
import Foundation
|
||||
|
||||
enum ContentSort: String, Decodable, CaseIterable, Hashable {
|
||||
case latest = "LATEST"
|
||||
case popular = "POPULAR"
|
||||
case owned = "OWNED"
|
||||
case priceHigh = "PRICE_HIGH"
|
||||
case priceLow = "PRICE_LOW"
|
||||
|
||||
var title: String {
|
||||
switch self {
|
||||
case .latest:
|
||||
return I18n.CreatorChannelLive.Sort.latest
|
||||
case .popular:
|
||||
return I18n.CreatorChannelLive.Sort.popular
|
||||
case .owned:
|
||||
return I18n.CreatorChannelLive.Sort.owned
|
||||
case .priceHigh:
|
||||
return I18n.CreatorChannelLive.Sort.priceHigh
|
||||
case .priceLow:
|
||||
return I18n.CreatorChannelLive.Sort.priceLow
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user