Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…apsule into develop
  • Loading branch information
pyj9748 committed Dec 8, 2022
2 parents 5d90199 + 6c293c7 commit d388b57
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -68,4 +72,10 @@ final class CapsuleDetailCoordinator: Coordinator {

parent.hideTabBar()
}

private func setupNavigationItem() {
let backButtonItem = UIBarButtonItem()
backButtonItem.title = "목록"
navigationController?.navigationBar.topItem?.backBarButtonItem = backButtonItem
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void>()
var viewDidDisappear = PublishSubject<Void>()
let frameWidth = PublishSubject<CGFloat>()
let tapCapsuleSettings = PublishRelay<Void>()
let tapImage = PublishSubject<Int>()
}

struct Output {
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class DetailImageCoordinator: Coordinator {

var index: Int?
var dataArray: [Data]?
var urlArray: [String]?

init(navigationController: CustomNavigationController?) {
self.navigationController = navigationController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Data>?
private var dataSource: UICollectionViewDiffableDataSource<Int, ImageSource>?

override func loadView() {
view = mainView
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Int, Data>()
private func applySnapshot(items: [ImageSource]) {
var snapshot = NSDiffableDataSourceSnapshot<Int, ImageSource>()
snapshot.appendSections([0])
snapshot.appendItems(items, toSection: 0)
dataSource?.apply(snapshot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void>()
}

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
Expand All @@ -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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d388b57

Please sign in to comment.