From 9fa5ba36cf8aafe72333371b33c9587fbc2d8301 Mon Sep 17 00:00:00 2001 From: hyunjuntyler Date: Tue, 3 Dec 2024 00:28:27 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EC=86=8C?= =?UTF-8?q?=EC=8A=A4=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MainFeature/Sources/Models/Channel.swift | 3 -- .../BroadcastCollectionViewController.swift | 2 +- .../BroadcastCollectionViewModel.swift | 35 ++----------------- .../LargeBroadcastCollectionViewCell.swift | 13 ++++++- .../SmallBroadcastCollectionViewCell.swift | 13 ++++++- 5 files changed, 27 insertions(+), 39 deletions(-) diff --git a/Projects/Features/MainFeature/Sources/Models/Channel.swift b/Projects/Features/MainFeature/Sources/Models/Channel.swift index 26a1b574..9ef5989c 100644 --- a/Projects/Features/MainFeature/Sources/Models/Channel.swift +++ b/Projects/Features/MainFeature/Sources/Models/Channel.swift @@ -4,7 +4,6 @@ public struct Channel: Hashable { let id: String let name: String var thumbnailImageURLString: String - var thumbnailImage: UIImage? let owner: String let description: String @@ -12,14 +11,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..cbd96b79 100644 --- a/Projects/Features/MainFeature/Sources/ViewControllers/BroadcastCollectionViewController.swift +++ b/Projects/Features/MainFeature/Sources/ViewControllers/BroadcastCollectionViewController.swift @@ -280,7 +280,7 @@ extension BroadcastCollectionViewController { } } - dataSource?.apply(snapshot, animatingDifferences: true) { [weak self] in + dataSource?.applySnapshotUsingReloadData(snapshot) { [weak self] in if self?.refreshControl.isRefreshing == true { self?.refreshControl.endRefreshing() } diff --git a/Projects/Features/MainFeature/Sources/ViewModels/BroadcastCollectionViewModel.swift b/Projects/Features/MainFeature/Sources/ViewModels/BroadcastCollectionViewModel.swift index 07e51df4..c4d2748a 100644 --- a/Projects/Features/MainFeature/Sources/ViewModels/BroadcastCollectionViewModel.swift +++ b/Projects/Features/MainFeature/Sources/ViewModels/BroadcastCollectionViewModel.swift @@ -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( @@ -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..3c8e0d30 100644 --- a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/LargeBroadcastCollectionViewCell.swift +++ b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/LargeBroadcastCollectionViewCell.swift @@ -63,8 +63,19 @@ final class LargeBroadcastCollectionViewCell: BaseCollectionViewCell, ThumbnailV } func configure(channel: Channel) { - self.thumbnailView.configure(with: channel.thumbnailImage) + loadAsyncImage(with: channel.thumbnailImageURLString) self.titleLabel.text = channel.name self.descriptionLabel.text = channel.owner + (channel.description.isEmpty ? "" : " • \(channel.description)") } + + private func loadAsyncImage(with imageURLString: String) { + guard let url = URL(string: imageURLString) else { return } + URLSession.shared.dataTask(with: url) { [weak self] data, _, error in + guard error == nil, + let data else { return } + DispatchQueue.main.async { + self?.thumbnailView.configure(with: UIImage(data: data)) + } + }.resume() + } } diff --git a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift index 806fcc24..ea2b8ac9 100644 --- a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift +++ b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift @@ -68,9 +68,20 @@ final class SmallBroadcastCollectionViewCell: BaseCollectionViewCell, ThumbnailV } func configure(channel: Channel) { - self.thumbnailView.configure(with: channel.thumbnailImage) + loadAsyncImage(with: channel.thumbnailImageURLString) self.titleLabel.text = channel.name self.ownerLabel.text = channel.owner self.descriptionLabel.text = channel.description } + + private func loadAsyncImage(with imageURLString: String) { + guard let url = URL(string: imageURLString) else { return } + URLSession.shared.dataTask(with: url) { [weak self] data, _, error in + guard error == nil, + let data else { return } + DispatchQueue.main.async { + self?.thumbnailView.configure(with: UIImage(data: data)) + } + }.resume() + } } From 710edb15e847a14856b79d979fafa66ff03545a9 Mon Sep 17 00:00:00 2001 From: hyunjuntyler Date: Tue, 3 Dec 2024 04:54:51 +0900 Subject: [PATCH 2/4] =?UTF-8?q?chore=20Refresh=20=EC=BB=A8=ED=8A=B8?= =?UTF-8?q?=EB=A1=A4=20=EC=A2=85=EB=A3=8C=20=EC=8B=9C=EC=A0=90=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BroadcastCollectionViewController.swift | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Projects/Features/MainFeature/Sources/ViewControllers/BroadcastCollectionViewController.swift b/Projects/Features/MainFeature/Sources/ViewControllers/BroadcastCollectionViewController.swift index cbd96b79..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 Date: Tue, 3 Dec 2024 04:55:30 +0900 Subject: [PATCH 3/4] =?UTF-8?q?refactor=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EB=A1=9C=EB=94=A9=20=EC=A4=91=EB=B3=B5=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LargeBroadcastCollectionViewCell.swift | 13 +------------ .../SmallBroadcastCollectionViewCell.swift | 13 +------------ .../Sources/Views/BroadcastThumbnailView.swift | 10 ++++++++++ 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/LargeBroadcastCollectionViewCell.swift b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/LargeBroadcastCollectionViewCell.swift index 3c8e0d30..43b63243 100644 --- a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/LargeBroadcastCollectionViewCell.swift +++ b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/LargeBroadcastCollectionViewCell.swift @@ -63,19 +63,8 @@ final class LargeBroadcastCollectionViewCell: BaseCollectionViewCell, ThumbnailV } func configure(channel: Channel) { - loadAsyncImage(with: channel.thumbnailImageURLString) + self.thumbnailView.configure(with: channel.thumbnailImageURLString) self.titleLabel.text = channel.name self.descriptionLabel.text = channel.owner + (channel.description.isEmpty ? "" : " • \(channel.description)") } - - private func loadAsyncImage(with imageURLString: String) { - guard let url = URL(string: imageURLString) else { return } - URLSession.shared.dataTask(with: url) { [weak self] data, _, error in - guard error == nil, - let data else { return } - DispatchQueue.main.async { - self?.thumbnailView.configure(with: UIImage(data: data)) - } - }.resume() - } } diff --git a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift index ea2b8ac9..dbd5e961 100644 --- a/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift +++ b/Projects/Features/MainFeature/Sources/Views/BroadcastCollectionViewCell/SmallBroadcastCollectionViewCell.swift @@ -68,20 +68,9 @@ final class SmallBroadcastCollectionViewCell: BaseCollectionViewCell, ThumbnailV } func configure(channel: Channel) { - loadAsyncImage(with: channel.thumbnailImageURLString) + self.thumbnailView.configure(with: channel.thumbnailImageURLString) self.titleLabel.text = channel.name self.ownerLabel.text = channel.owner self.descriptionLabel.text = channel.description } - - private func loadAsyncImage(with imageURLString: String) { - guard let url = URL(string: imageURLString) else { return } - URLSession.shared.dataTask(with: url) { [weak self] data, _, error in - guard error == nil, - let data else { return } - DispatchQueue.main.async { - self?.thumbnailView.configure(with: UIImage(data: data)) - } - }.resume() - } } 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 } From 4c129a8e7f25b10126af8d610a75a6494a668e81 Mon Sep 17 00:00:00 2001 From: hyunjuntyler Date: Tue, 3 Dec 2024 13:53:43 +0900 Subject: [PATCH 4/4] =?UTF-8?q?chore=20import=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Projects/Features/MainFeature/Sources/Models/Channel.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/Projects/Features/MainFeature/Sources/Models/Channel.swift b/Projects/Features/MainFeature/Sources/Models/Channel.swift index 9ef5989c..dc09cc0a 100644 --- a/Projects/Features/MainFeature/Sources/Models/Channel.swift +++ b/Projects/Features/MainFeature/Sources/Models/Channel.swift @@ -1,5 +1,3 @@ -import UIKit - public struct Channel: Hashable { let id: String let name: String