Files
SodaLive
Preview Content
Resources
Sources
Agora
App
Common
Content
All
ContentNewAllItemView.swift
ContentNewAllView.swift
ContentNewAllViewModel.swift
ContentRankingAllView.swift
ContentRankingAllViewModel.swift
GetNewContentAllResponse.swift
Create
Curation
Detail
Donation
Main
Modify
Order
AddAllPlaybackTrackingRequest.swift
ContentApi.swift
ContentListItemView.swift
ContentListView.swift
ContentListViewModel.swift
ContentPlayManager.swift
ContentRepository.swift
PlaybackTracking.swift
PlaybackTrackingRepository.swift
Debug
Dialog
Explorer
Extensions
Follow
Font
FortuneWheel
IAP
ImagePicker
Keyboard
Live
Main
Message
MyPage
NavigationBar
Onboarding
Report
Settings
Shape
Splash
User
Utils
ContentView.swift
SodaLive.entitlements
SodaLive.xcworkspace
generated
.gitignore
Podfile
Podfile.lock
SodaLive-dev.entitlements
model-SodaLive-dev.json
model-SodaLive.json
sodalive-ios/SodaLive/Sources/Content/All/ContentRankingAllViewModel.swift
2023-11-03 14:45:51 +09:00

125 lines
4.9 KiB
Swift

//
// ContentRankingAllViewModel.swift
// SodaLive
//
// Created by klaus on 2023/10/15.
//
import Foundation
import Combine
final class ContentRankingAllViewModel: ObservableObject {
private let repository = ContentRepository()
private var subscription = Set<AnyCancellable>()
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var isLoading = false
@Published var dateString = ""
@Published var contentRankingItemList = [GetAudioContentRankingItem]()
@Published var contentRankingSortList = [String]()
@Published var selectedContentRankingSort = "매출" {
didSet {
page = 1
isLast = false
getContentRanking()
}
}
var page = 1
var isLast = false
private let pageSize = 10
func getContentRanking() {
if (!isLast && !isLoading && page <= 5) {
isLoading = true
repository.getContentRanking(page: page, size: pageSize, sortType: selectedContentRankingSort)
.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<GetAudioContentRanking>.self, from: responseData)
self.isLoading = false
if let data = decoded.data, decoded.success {
if page == 1 {
contentRankingItemList.removeAll()
}
dateString = "\(data.startDate)~\(data.endDate)"
if !data.items.isEmpty {
page += 1
self.contentRankingItemList.append(contentsOf: data.items)
} else {
isLast = true
}
} else {
if let message = decoded.message {
self.errorMessage = message
} else {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
}
self.isShowPopup = true
}
} catch {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowPopup = true
self.isLoading = false
}
}
.store(in: &subscription)
}
}
func getContentRankingSortType() {
repository.getContentRankingSortType()
.sink { result in
switch result {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
}
} receiveValue: { [unowned self] response in
let responseData = response.data
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<[String]>.self, from: responseData)
if let data = decoded.data, decoded.success {
self.contentRankingSortList.removeAll()
self.contentRankingSortList.append(contentsOf: data)
} 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)
}
}