From 32b8ceed6668e109ad406fcdc8385a3e2ecb917e Mon Sep 17 00:00:00 2001 From: trumanfromkorea Date: Thu, 8 Dec 2022 15:45:13 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EC=83=81=EC=84=B8=20=ED=99=94?= =?UTF-8?q?=EB=A9=B4=EC=97=90=EC=84=9C=EB=8F=84=20=EC=9D=B4=EB=AF=B8?= =?UTF-8?q?=EC=A7=80=20=ED=99=95=EB=8C=80=20=EA=B0=80=EB=8A=A5=20(#72)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CapsuleDetailCoordinator.swift | 18 ++++++-- .../CapsuleDetailViewController.swift | 6 +++ .../CapsuleDetailViewModel.swift | 10 +++++ .../DetailImage/DetailImageCoordinator.swift | 1 + .../DetailImageViewController.swift | 23 ++++++---- .../DetailImage/DetailImageViewModel.swift | 43 ++++++++++++++++--- .../DetailImage/ZoomableImageCell.swift | 4 ++ 7 files changed, 86 insertions(+), 19 deletions(-) diff --git a/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailCoordinator.swift b/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailCoordinator.swift index 80fac53..715d364 100644 --- a/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailCoordinator.swift +++ b/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailCoordinator.swift @@ -47,10 +47,14 @@ final class CapsuleDetailCoordinator: Coordinator { children.append(capsuleSettingsCooridnator) } - private func setupNavigationItem() { - let backButtonItem = UIBarButtonItem() - backButtonItem.title = "목록" - navigationController?.navigationBar.topItem?.backBarButtonItem = backButtonItem + func showDetailImage(index: Int, urlArray: [String]) { + let detailImageCoordinator = DetailImageCoordinator(navigationController: navigationController) + detailImageCoordinator.parent = self + detailImageCoordinator.index = index + detailImageCoordinator.urlArray = urlArray + detailImageCoordinator.start() + + children.append(detailImageCoordinator) } func finish() { @@ -68,4 +72,10 @@ final class CapsuleDetailCoordinator: Coordinator { parent.hideTabBar() } + + private func setupNavigationItem() { + let backButtonItem = UIBarButtonItem() + backButtonItem.title = "목록" + navigationController?.navigationBar.topItem?.backBarButtonItem = backButtonItem + } } diff --git a/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailViewController.swift b/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailViewController.swift index 9b68b9a..7df37e2 100644 --- a/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailViewController.swift +++ b/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailViewController.swift @@ -72,6 +72,12 @@ final class CapsuleDetailViewController: UIViewController, BaseViewController { self?.viewModel?.input.tapCapsuleSettings.accept(()) }) .disposed(by: disposeBag) + + mainView.imageCollectionView.rx.itemSelected + .subscribe(onNext: { [weak self] indexPath in + self?.viewModel?.input.tapImage.onNext(indexPath.item) + }) + .disposed(by: disposeBag) } private func addSettingButton() { diff --git a/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailViewModel.swift b/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailViewModel.swift index da9e2d4..ca180b6 100644 --- a/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailViewModel.swift +++ b/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAccess/CapsuleDetail/CapsuleDetailViewModel.swift @@ -19,11 +19,14 @@ final class CapsuleDetailViewModel: BaseViewModel { lazy var mapSnapshotInfo = Observable.zip(input.frameWidth, output.mapCoordinate) + lazy var detailImageData = Observable.combineLatest(input.tapImage, output.imageCell) + struct Input { var viewWillAppear = PublishSubject() var viewDidDisappear = PublishSubject() let frameWidth = PublishSubject() let tapCapsuleSettings = PublishRelay() + let tapImage = PublishSubject() } struct Output { @@ -65,6 +68,13 @@ final class CapsuleDetailViewModel: BaseViewModel { self?.coordinator?.showCapsuleSettings() }) .disposed(by: disposeBag) + + detailImageData + .subscribe(onNext: { [weak self] (index: Int, imageCell: [DetailImageCell.Cell]) in + let urlArray = imageCell.compactMap { $0.imageURL } + self?.coordinator?.showDetailImage(index: index, urlArray: urlArray) + }) + .disposed(by: disposeBag) } func fetchCapsule() { diff --git a/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageCoordinator.swift b/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageCoordinator.swift index 2a305e6..d98715c 100644 --- a/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageCoordinator.swift +++ b/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageCoordinator.swift @@ -16,6 +16,7 @@ final class DetailImageCoordinator: Coordinator { var index: Int? var dataArray: [Data]? + var urlArray: [String]? init(navigationController: CustomNavigationController?) { self.navigationController = navigationController diff --git a/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageViewController.swift b/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageViewController.swift index 5411bb0..78bd018 100644 --- a/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageViewController.swift +++ b/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageViewController.swift @@ -12,9 +12,8 @@ final class DetailImageViewController: UIViewController { var disposeBag = DisposeBag() var viewModel: DetailImageViewModel? -// private let mainView = ZoomableImageView() private let mainView = DetailImageView() - private var dataSource: UICollectionViewDiffableDataSource? + private var dataSource: UICollectionViewDiffableDataSource? override func loadView() { view = mainView @@ -30,12 +29,13 @@ final class DetailImageViewController: UIViewController { } private func bind() { - viewModel?.output.imageData + viewModel?.output.imageSources .subscribe(onNext: { [weak self] imageData in let index = imageData.index - self?.applySnapshot(items: imageData.data) + print(index) + self?.applySnapshot(items: imageData.sources) - self?.mainView.itemCount = imageData.data.count + self?.mainView.itemCount = imageData.sources.count self?.mainView.currentIndex = index DispatchQueue.main.async { @@ -66,14 +66,21 @@ extension DetailImageViewController { return UICollectionViewCell() } - cell.configure(data: item) + switch item { + case let .data(value): + cell.configure(data: value) + + case let .url(value): + cell.configrue(url: value) + } + return cell }) } - private func applySnapshot(items: [Data]) { - var snapshot = NSDiffableDataSourceSnapshot() + private func applySnapshot(items: [ImageSource]) { + var snapshot = NSDiffableDataSourceSnapshot() snapshot.appendSections([0]) snapshot.appendItems(items, toSection: 0) dataSource?.apply(snapshot) diff --git a/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageViewModel.swift b/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageViewModel.swift index cec705d..525e5b2 100644 --- a/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageViewModel.swift +++ b/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/DetailImageViewModel.swift @@ -8,25 +8,47 @@ import Foundation import RxSwift +enum ImageSource: Hashable { + case data(value: Data) + case url(value: String) + + var dataValue: Data? { + switch self { + case let .data(value): return value + default: return nil + } + } + + var urlValue: String? { + switch self { + case let .url(value): return value + default: return nil + } + } +} + final class DetailImageViewModel: BaseViewModel { var disposeBag = DisposeBag() var coordinator: DetailImageCoordinator? let input = Input() let output = Output() - + struct Input { let tapClose = PublishSubject() } struct Output { - let imageData = PublishSubject<(data: [Data], index: Int)>() +// let imageData = PublishSubject<(data: [Data], index: Int)>() +// let urlData = PublishSubject<(data: [String], index: Int)>() + + let imageSources = PublishSubject<(sources: [ImageSource], index: Int)>() } init() { bind() } - + private func bind() { input.tapClose .subscribe(onNext: { [weak self] in @@ -36,11 +58,18 @@ final class DetailImageViewModel: BaseViewModel { } func fetchData() { - guard let index = coordinator?.index, - let dataArray = coordinator?.dataArray else { + guard let index = coordinator?.index else { return } - - output.imageData.onNext((data: dataArray, index: index)) + + if let dataArray = coordinator?.dataArray { + let sources = dataArray.map { ImageSource.data(value: $0) } + output.imageSources.onNext((sources: sources, index: index)) + } + + if let urlArray = coordinator?.urlArray { + let sources = urlArray.map { ImageSource.url(value: $0) } + output.imageSources.onNext((sources: sources, index: index)) + } } } diff --git a/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/ZoomableImageCell.swift b/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/ZoomableImageCell.swift index 96097c7..b53c9de 100644 --- a/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/ZoomableImageCell.swift +++ b/SpaceCapsule/SpaceCapsule/Scene/TabBar/DetailImage/ZoomableImageCell.swift @@ -32,6 +32,10 @@ final class ZoomableImageCell: UICollectionViewCell { func configure(data: Data) { zoomableImageView.imageView.image = UIImage(data: data) } + + func configrue(url: String) { + zoomableImageView.imageView.kr.setImage(with: url, resizing: false) + } private func addSubViews() { addSubview(zoomableImageView)