feat(creator): 라이브 탭 기반을 추가한다
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user