Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Projects/Features/MainFeature/Sources/Models/Channel.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import UIKit

public struct Channel: Hashable {
let id: String
let name: String
var thumbnailImageURLString: String
var thumbnailImage: UIImage?
let owner: String
let description: String

public init(
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public class BroadcastCollectionViewController: BaseViewController<BroadcastColl
.receive(on: DispatchQueue.main)
.sink { [weak self] channels in
self?.applySnapshot(with: channels)
self?.refreshControl.endRefreshing()
}
.store(in: &cancellables)

Expand Down Expand Up @@ -280,11 +281,7 @@ extension BroadcastCollectionViewController {
}
}

dataSource?.apply(snapshot, animatingDifferences: true) { [weak self] in
if self?.refreshControl.isRefreshing == true {
self?.refreshControl.endRefreshing()
}
}
dataSource?.applySnapshotUsingReloadData(snapshot)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class BroadcastCollectionViewModel: ViewModel {
private func fetchData() {
fetchChannelListUsecase.execute()
.zip(fetchAllBroadcastUsecase.execute())
.map { channelEntities, broadcastInfoEntities -> [Channel] in
.map { channelEntities, broadcastInfoEntities in
channelEntities.map { channelEntity in
let broadcast = broadcastInfoEntities.first { $0.id == channelEntity.id }
return Channel(
Expand All @@ -82,44 +82,13 @@ 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<Channel, Never> 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)
}
)
.store(in: &cancellables)
}

private func loadAsyncImage(with imageURLString: String) -> AnyPublisher<UIImage?, URLError> {
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading