diff --git a/Projects/Features/MainFeature/Sources/Models/Channel.swift b/Projects/Features/MainFeature/Sources/Models/Channel.swift index 26a1b574..dc09cc0a 100644 --- a/Projects/Features/MainFeature/Sources/Models/Channel.swift +++ b/Projects/Features/MainFeature/Sources/Models/Channel.swift @@ -1,10 +1,7 @@ -import UIKit - public struct Channel: Hashable { let id: String let name: String var thumbnailImageURLString: String - var thumbnailImage: UIImage? let owner: String let description: String @@ -12,14 +9,12 @@ public struct Channel: Hashable { id: String, title: String, thumbnailImageURLString: String = "", - thumbnailImage: UIImage? = nil, owner: String = "", description: String = "" ) { self.id = id self.name = title self.thumbnailImageURLString = thumbnailImageURLString - self.thumbnailImage = thumbnailImage self.owner = owner self.description = description } diff --git a/Projects/Features/MainFeature/Sources/ViewControllers/BroadcastCollectionViewController.swift b/Projects/Features/MainFeature/Sources/ViewControllers/BroadcastCollectionViewController.swift index 944df009..bd0867e9 100644 --- a/Projects/Features/MainFeature/Sources/ViewControllers/BroadcastCollectionViewController.swift +++ b/Projects/Features/MainFeature/Sources/ViewControllers/BroadcastCollectionViewController.swift @@ -111,6 +111,7 @@ public class BroadcastCollectionViewController: BaseViewController [Channel] in + .map { channelEntities, broadcastInfoEntities in channelEntities.map { channelEntity in let broadcast = broadcastInfoEntities.first { $0.id == channelEntity.id } return Channel( @@ -82,30 +82,8 @@ public class BroadcastCollectionViewModel: ViewModel { ) } } - .flatMap { [weak self] channels -> AnyPublisher<[Channel], Never> in - guard let self else { return Just([]).eraseToAnyPublisher() } - - return channels.publisher - .flatMap { channel -> AnyPublisher in - self.loadAsyncImage(with: channel.thumbnailImageURLString) - .replaceError(with: nil) - .map { image in - var updatedChannel = channel - updatedChannel.thumbnailImage = image - return updatedChannel - } - .eraseToAnyPublisher() - } - .collect() - .eraseToAnyPublisher() - } .sink( - receiveCompletion: { completion in - switch completion { - case .finished: break - case .failure(let error): print("Error: \(error)") - } - }, + receiveCompletion: { _ in }, receiveValue: { [weak self] channels in let filteredChannels = channels.filter { !($0.id == self?.channelID) } self?.output.channels.send(filteredChannels) @@ -113,13 +91,4 @@ public class BroadcastCollectionViewModel: ViewModel { ) .store(in: &cancellables) } - - private func loadAsyncImage(with imageURLString: String) -> AnyPublisher { - guard let url = URL(string: imageURLString) else { - return Just(nil).setFailureType(to: URLError.self).eraseToAnyPublisher() - } - return URLSession.shared.dataTaskPublisher(for: url) - .map { data, _ in UIImage(data: data) } - .eraseToAnyPublisher() - } } diff --git a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/LargeBroadcastCollectionViewCell.swift b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/LargeBroadcastCollectionViewCell.swift index e546e991..43b63243 100644 --- a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/LargeBroadcastCollectionViewCell.swift +++ b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/LargeBroadcastCollectionViewCell.swift @@ -63,7 +63,7 @@ final class LargeBroadcastCollectionViewCell: BaseCollectionViewCell, ThumbnailV } func configure(channel: Channel) { - self.thumbnailView.configure(with: channel.thumbnailImage) + self.thumbnailView.configure(with: channel.thumbnailImageURLString) self.titleLabel.text = channel.name self.descriptionLabel.text = channel.owner + (channel.description.isEmpty ? "" : " • \(channel.description)") } diff --git a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift index 806fcc24..dbd5e961 100644 --- a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift +++ b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift @@ -68,7 +68,7 @@ final class SmallBroadcastCollectionViewCell: BaseCollectionViewCell, ThumbnailV } func configure(channel: Channel) { - self.thumbnailView.configure(with: channel.thumbnailImage) + self.thumbnailView.configure(with: channel.thumbnailImageURLString) self.titleLabel.text = channel.name self.ownerLabel.text = channel.owner self.descriptionLabel.text = channel.description diff --git a/Projects/Features/MainFeature/Sources/Views/BroadcastThumbnailView.swift b/Projects/Features/MainFeature/Sources/Views/BroadcastThumbnailView.swift index 87c1ea61..57096727 100644 --- a/Projects/Features/MainFeature/Sources/Views/BroadcastThumbnailView.swift +++ b/Projects/Features/MainFeature/Sources/Views/BroadcastThumbnailView.swift @@ -75,6 +75,16 @@ final class ThumbnailView: BaseView { } } + func configure(with imageURLString: String) { + guard let url = URL(string: imageURLString) else { return } + URLSession.shared.dataTask(with: url) { [weak self] data, _, _ in + guard let data else { return } + DispatchQueue.main.async { + self?.imageView.image = UIImage(data: data) + } + }.resume() + } + func configure(with image: UIImage?) { imageView.image = image }