Skip to content

Commit

Permalink
✨ 여정 기록의 이미지를 네트워크로부터 가져오는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
SwiftyJunnos committed Nov 27, 2023
1 parent b4c9512 commit dc75e7b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,46 @@ private extension JourneyListViewController {
return section
}

private func fetchImage(url: URL) async throws -> Data {
let request = URLRequest(url: url)
let (data, response) = try await URLSession.shared.data(for: request)

guard let statusCode = (response as? HTTPURLResponse)?.statusCode,
(200...299).contains(statusCode) else { throw NSError(domain: "fetch error", code: 1004) }

return data
}

func configureDataSource() -> JourneyListDataSource {
// TODO: 최적화 & 캐싱
let cellRegistration = JourneyCellRegistration { cell, _, itemIdentifier in
let cellModel = JourneyCellModel(location: itemIdentifier.location,
date: itemIdentifier.date,
songTitle: itemIdentifier.song.title,
songArtist: itemIdentifier.song.artist)
cell.update(with: cellModel)
// cell.updateImages(images: itemIdentifier.spot.photoURLs)
let photoURLs = itemIdentifier.spots
.flatMap { $0.photoURLs }
.compactMap { URL(string: $0) }

Task {
cell.addImageView(count: photoURLs.count)

await withTaskGroup(of: (index: Int, imageData: Data).self) { group in
for (index, photoURL) in photoURLs.enumerated() {
group.addTask(priority: .background) { [weak self] in
guard let imageData = try? await self?.fetchImage(url: photoURL) else {
return (0, Data())
}
return (index, imageData)
}
}

for await (index, imageData) in group {
cell.updateImages(imageData: imageData, atIndex: index)
}
}
}
}

let dataSource = JourneyListDataSource(collectionView: self.collectionView,
Expand Down
24 changes: 21 additions & 3 deletions iOS/MSUIKit/Sources/MSUIKit/Cells/JourneyCell/JourneyCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public final class JourneyCell: UICollectionViewCell {
fatalError("MusicSpot은 code-based로만 작업 중입니다.")
}

public override func prepareForReuse() {
self.spotImageStack.arrangedSubviews.forEach {
$0.removeFromSuperview()
}
}

// MARK: - Functions

public func update(with model: JourneyCellModel) {
Expand All @@ -55,14 +61,26 @@ public final class JourneyCell: UICollectionViewCell {
artist: model.song.artist)
}

public func updateImages(images: [Data]) {
images.forEach { data in
@MainActor
public func addImageView(count: Int) {
(1...count).forEach { _ in
let imageView = SpotPhotoImageView()
imageView.update(with: data)
self.spotImageStack.addArrangedSubview(imageView)
}
}

@MainActor
public func updateImages(imageData: Data, atIndex index: Int) {
guard self.spotImageStack.arrangedSubviews.count > index else {
return
}
guard let imageView = self.spotImageStack.arrangedSubviews[index] as? SpotPhotoImageView else {
return
}

imageView.update(with: imageData)
}

}

// MARK: - UI Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import UIKit

import MSDesignSystem

final class SpotPhotoImageView: UIView {

// MARK: - Constants
Expand Down Expand Up @@ -50,6 +52,7 @@ final class SpotPhotoImageView: UIView {
private extension SpotPhotoImageView {

func configureStyle() {
self.backgroundColor = .msColor(.secondaryBackground)
self.layer.cornerRadius = Metric.cornerRadius
self.clipsToBounds = true
}
Expand Down

0 comments on commit dc75e7b

Please sign in to comment.