콘텐츠 메인 - Api 분리
This commit is contained in:
122
SodaLive/Sources/Content/Main/Banner/ContentMainBannerView.swift
Normal file
122
SodaLive/Sources/Content/Main/Banner/ContentMainBannerView.swift
Normal file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// ContentMainBannerView.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/08/11.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import Kingfisher
|
||||
|
||||
struct ContentMainBannerView: View {
|
||||
|
||||
@StateObject private var viewModel = ContentMainBannerViewModel()
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
if !viewModel.bannerList.isEmpty {
|
||||
VStack(spacing: 0) {
|
||||
TabView(selection: $viewModel.currentIndex) {
|
||||
ForEach(0..<viewModel.bannerList.count, id: \.self) { index in
|
||||
let item = viewModel.bannerList[index]
|
||||
if let url = item.thumbnailImageUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
|
||||
KFImage(URL(string: url))
|
||||
.resizable()
|
||||
.scaledToFill()
|
||||
.frame(
|
||||
width: screenSize().width - 26.7,
|
||||
height: (screenSize().width - 26.7) * 0.53
|
||||
)
|
||||
.onTapGesture {
|
||||
switch item.type {
|
||||
case .EVENT:
|
||||
AppState.shared.setAppStep(step: .eventDetail(event: item.eventItem!))
|
||||
case .CREATOR:
|
||||
AppState.shared.setAppStep(step: .creatorDetail(userId: item.creatorId!))
|
||||
case .LINK:
|
||||
if let link = item.link, link.trimmingCharacters(in: .whitespaces).count > 0, let url = URL(string: link), UIApplication.shared.canOpenURL(url) {
|
||||
UIApplication.shared.open(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
.cornerRadius(4.7)
|
||||
} else {
|
||||
KFImage(URL(string: item.thumbnailImageUrl))
|
||||
.resizable()
|
||||
.scaledToFill()
|
||||
.frame(
|
||||
width: screenSize().width - 26.7,
|
||||
height: (screenSize().width - 26.7) * 0.53
|
||||
)
|
||||
.onTapGesture {
|
||||
switch item.type {
|
||||
case .EVENT:
|
||||
AppState.shared.setAppStep(step: .eventDetail(event: item.eventItem!))
|
||||
case .CREATOR:
|
||||
AppState.shared.setAppStep(step: .creatorDetail(userId: item.creatorId!))
|
||||
case .LINK:
|
||||
if let link = item.link, link.trimmingCharacters(in: .whitespaces).count > 0, let url = URL(string: link), UIApplication.shared.canOpenURL(url) {
|
||||
UIApplication.shared.open(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
.cornerRadius(4.7)
|
||||
}
|
||||
}
|
||||
}
|
||||
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
|
||||
.frame(
|
||||
width: screenSize().width - 26.7,
|
||||
height: (screenSize().width - 26.7) * 0.53
|
||||
)
|
||||
|
||||
HStack(spacing: 4) {
|
||||
ForEach(0..<viewModel.bannerList.count, id: \.self) { index in
|
||||
Capsule()
|
||||
.foregroundColor(index == viewModel.currentIndex ? Color(hex: "9970ff") : Color(hex: "909090"))
|
||||
.frame(
|
||||
width: index == viewModel.currentIndex ? 18 : 6,
|
||||
height: 6
|
||||
)
|
||||
.tag(index)
|
||||
}
|
||||
}
|
||||
.padding(.top, 13.3)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.onAppear {
|
||||
viewModel.timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
|
||||
}
|
||||
.onDisappear {
|
||||
viewModel.timer.upstream.connect().cancel()
|
||||
}
|
||||
.onReceive(viewModel.timer) { _ in
|
||||
DispatchQueue.main.async {
|
||||
withAnimation {
|
||||
if viewModel.currentIndex == viewModel.bannerList.count - 1 {
|
||||
viewModel.currentIndex = 0
|
||||
} else {
|
||||
viewModel.currentIndex += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if viewModel.isLoading {
|
||||
ActivityIndicatorView()
|
||||
.frame(width: 100, height: 100)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.onAppear {
|
||||
viewModel.getBannerList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ContentMainBannerView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
ContentMainBannerView()
|
||||
}
|
||||
}
|
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// ContentMainBannerViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/12/11.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
final class ContentMainBannerViewModel: ObservableObject {
|
||||
|
||||
private let repository = ContentRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
@Published var errorMessage = ""
|
||||
@Published var isShowPopup = false
|
||||
@Published var isLoading = false
|
||||
|
||||
@Published var currentIndex = 0
|
||||
@Published var timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
|
||||
|
||||
@Published var bannerList = [GetAudioContentBannerResponse]()
|
||||
|
||||
func getBannerList() {
|
||||
isLoading = true
|
||||
|
||||
repository.getMainBannerList()
|
||||
.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<[GetAudioContentBannerResponse]>.self, from: responseData)
|
||||
self.isLoading = false
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self.bannerList.removeAll()
|
||||
self.bannerList.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
|
||||
self.isLoading = false
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user