From af9768da67ab7e0e516426a50f1e0b717ffc4cf8 Mon Sep 17 00:00:00 2001 From: chopmozzi <44396392+chopmozzi@users.noreply.github.com> Date: Sun, 10 Dec 2023 18:08:43 +0900 Subject: [PATCH] =?UTF-8?q?:recycle:=20=EB=A6=AC=EB=B7=B0=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EB=B0=98=EC=98=812?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 유경님 리뷰사항 반영 --- .../Scenes/Playback/Cell/PlaybackCell.swift | 2 +- .../Scenes/Playback/PlaybackInteractor.swift | 42 ++++++++----------- .../Scenes/Playback/PlaybackModels.swift | 4 +- .../Scenes/Playback/PlaybackWorker.swift | 17 ++++++-- .../Workers/Mocks/MockPlaybackWorker.swift | 23 ++++++++++ 5 files changed, 58 insertions(+), 30 deletions(-) diff --git a/iOS/Layover/Layover/Scenes/Playback/Cell/PlaybackCell.swift b/iOS/Layover/Layover/Scenes/Playback/Cell/PlaybackCell.swift index cfcdc4f..549d2be 100644 --- a/iOS/Layover/Layover/Scenes/Playback/Cell/PlaybackCell.swift +++ b/iOS/Layover/Layover/Scenes/Playback/Cell/PlaybackCell.swift @@ -30,7 +30,7 @@ final class PlaybackCell: UICollectionViewCell { resetObserver() } - func setPlaybackContents(post: PlaybackModels.DisplayPost) { + func setPlaybackContents(post: PlaybackModels.DisplayedPost) { boardID = post.board.boardID playbackView.descriptionView.titleLabel.text = post.board.title playbackView.descriptionView.setText(post.board.description ?? "") diff --git a/iOS/Layover/Layover/Scenes/Playback/PlaybackInteractor.swift b/iOS/Layover/Layover/Scenes/Playback/PlaybackInteractor.swift index ed9315e..e27b708 100644 --- a/iOS/Layover/Layover/Scenes/Playback/PlaybackInteractor.swift +++ b/iOS/Layover/Layover/Scenes/Playback/PlaybackInteractor.swift @@ -43,7 +43,6 @@ final class PlaybackInteractor: PlaybackBusinessLogic, PlaybackDataStore { typealias Models = PlaybackModels var worker: PlaybackWorkerProtocol? - var userWorker: UserWorkerProtocol? var presenter: PlaybackPresentationLogic? var parentView: Models.ParentView? @@ -230,29 +229,24 @@ final class PlaybackInteractor: PlaybackBusinessLogic, PlaybackDataStore { return await withTaskGroup(of: Models.PlaybackVideo.self) { group -> [Models.PlaybackVideo] in for post in posts { guard let videoURL: URL = post.board.videoURL else { continue } - if let thumbnailImageURL = post.board.thumbnailImageURL { - group.addTask { - var profileImageData: Data? - let thumbnailImageData = await self.userWorker?.fetchImageData(with: thumbnailImageURL) - if let profileImageURL = post.member.profileImageURL { - profileImageData = await self.userWorker?.fetchImageData(with: profileImageURL) - } - let location: String? = await self.worker?.transLocation(latitude: post.board.latitude, longitude: post.board.longitude) - return Models.PlaybackVideo( - displayPost: Models.DisplayPost( - member: Models.Member( - memberID: post.member.identifier, - username: post.member.username, - profileImageData: profileImageData), - board: Models.Board( - boardID: post.board.identifier, - title: post.board.title, - description: post.board.description, - thumbnailImageData: thumbnailImageData, - videoURL: videoURL, - location: location), - tags: post.tag)) - } + group.addTask { + async let profileImageData = self.worker?.fetchImageData(with: post.member.profileImageURL) + async let thumbnailImageData: Data? = self.worker?.fetchImageData(with: post.board.thumbnailImageURL) + async let location: String? = self.worker?.transLocation(latitude: post.board.latitude, longitude: post.board.longitude) + return Models.PlaybackVideo( + displayPost: Models.DisplayedPost( + member: Models.Member( + memberID: post.member.identifier, + username: post.member.username, + profileImageData: await profileImageData), + board: Models.Board( + boardID: post.board.identifier, + title: post.board.title, + description: post.board.description, + thumbnailImageData: await thumbnailImageData, + videoURL: videoURL, + location: await location), + tags: post.tag)) } } var result = [Models.PlaybackVideo]() diff --git a/iOS/Layover/Layover/Scenes/Playback/PlaybackModels.swift b/iOS/Layover/Layover/Scenes/Playback/PlaybackModels.swift index acebc91..7c28dd0 100644 --- a/iOS/Layover/Layover/Scenes/Playback/PlaybackModels.swift +++ b/iOS/Layover/Layover/Scenes/Playback/PlaybackModels.swift @@ -12,7 +12,7 @@ enum PlaybackModels { // MARK: - Properties Type struct PlaybackVideo: Hashable { var id: UUID = UUID() - let displayPost: DisplayPost + let displayPost: DisplayedPost } enum ParentView { @@ -21,7 +21,7 @@ enum PlaybackModels { case other } - struct DisplayPost: Hashable { + struct DisplayedPost: Hashable { let member: PlaybackModels.Member let board: PlaybackModels.Board let tags: [String] diff --git a/iOS/Layover/Layover/Scenes/Playback/PlaybackWorker.swift b/iOS/Layover/Layover/Scenes/Playback/PlaybackWorker.swift index 9d69d0f..456f90c 100644 --- a/iOS/Layover/Layover/Scenes/Playback/PlaybackWorker.swift +++ b/iOS/Layover/Layover/Scenes/Playback/PlaybackWorker.swift @@ -15,6 +15,7 @@ protocol PlaybackWorkerProtocol { func deletePlaybackVideo(boardID: Int) async -> Bool func makeInfiniteScroll(posts: [Post]) -> [Post] func transLocation(latitude: Double, longitude: Double) async -> String? + func fetchImageData(with url: URL?) async -> Data? } final class PlaybackWorker: PlaybackWorkerProtocol { @@ -57,14 +58,24 @@ final class PlaybackWorker: PlaybackWorkerProtocol { func transLocation(latitude: Double, longitude: Double) async -> String? { let findLocation: CLLocation = CLLocation(latitude: latitude, longitude: longitude) let geoCoder: CLGeocoder = CLGeocoder() - let identifier = Locale.current.identifier == "en_KR" ? "ko_kr" : Locale.current.identifier - let local: Locale = Locale(identifier: identifier) + let localeIdentifier = Locale.preferredLanguages.first != nil ? Locale.preferredLanguages[0] : Locale.current.identifier + let locale = Locale(identifier: localeIdentifier) do { - let place = try await geoCoder.reverseGeocodeLocation(findLocation, preferredLocale: local) + let place = try await geoCoder.reverseGeocodeLocation(findLocation, preferredLocale: locale) return place.last?.administrativeArea } catch { os_log(.error, "convert location error: %@", error.localizedDescription) return nil } } + + func fetchImageData(with url: URL?) async -> Data? { + guard let url else { return nil } + do { + return try await provider.request(url: url) + } catch { + os_log(.error, log: .data, "Error: %s", error.localizedDescription) + return nil + } + } } diff --git a/iOS/Layover/Layover/Workers/Mocks/MockPlaybackWorker.swift b/iOS/Layover/Layover/Workers/Mocks/MockPlaybackWorker.swift index 97a5df7..ec23402 100644 --- a/iOS/Layover/Layover/Workers/Mocks/MockPlaybackWorker.swift +++ b/iOS/Layover/Layover/Workers/Mocks/MockPlaybackWorker.swift @@ -77,4 +77,27 @@ final class MockPlaybackWorker: PlaybackWorkerProtocol { return nil } } + + func fetchImageData(with url: URL?) async -> Data? { + guard let url else { return nil } + do { + guard let imageURL = Bundle.main.url(forResource: "sample", withExtension: "jpeg") else { + return nil + } + let mockData = try? Data(contentsOf: imageURL) + MockURLProtocol.requestHandler = { request in + let response = HTTPURLResponse(url: request.url!, + statusCode: 200, + httpVersion: nil, + headerFields: nil) + return (response, mockData, nil) + } + + let data = try await provider.request(url: url) + return data + } catch { + os_log(.error, log: .data, "%@", error.localizedDescription) + return nil + } + } }