From 8518e8c752f62dd1ef46d1cab8d0093ec389b8a4 Mon Sep 17 00:00:00 2001 From: doyeonjeong Date: Wed, 5 Apr 2023 20:46:58 +0900 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F[Refactor]:=20=EC=A0=84?= =?UTF-8?q?=EB=A9=B4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 가독성을 개선 시켜봤습니다. --- .../Album/Applications/SceneDelegate.swift | 4 +- .../Album/Album/Sources/AlbumListCell.swift | 68 +++++++++++++------ .../Sources/AlbumListViewController.swift | 38 ++++------- .../Album/Album/Sources/AlbumViewModel.swift | 28 ++++++-- 4 files changed, 84 insertions(+), 54 deletions(-) diff --git a/doyeon/Album/Album/Applications/SceneDelegate.swift b/doyeon/Album/Album/Applications/SceneDelegate.swift index 20e6b22..0bdfaca 100644 --- a/doyeon/Album/Album/Applications/SceneDelegate.swift +++ b/doyeon/Album/Album/Applications/SceneDelegate.swift @@ -13,8 +13,8 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { - let viewController = ViewController() - let navigationController = UINavigationController(rootViewController: viewController) + let mainViewController = AlbumListViewController() + let navigationController = UINavigationController(rootViewController: mainViewController) let appearance = UINavigationBarAppearance() appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black] appearance.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: .bold) ] diff --git a/doyeon/Album/Album/Sources/AlbumListCell.swift b/doyeon/Album/Album/Sources/AlbumListCell.swift index 378926b..9258c24 100644 --- a/doyeon/Album/Album/Sources/AlbumListCell.swift +++ b/doyeon/Album/Album/Sources/AlbumListCell.swift @@ -13,38 +13,68 @@ class AlbumListCell: UICollectionViewCell { // MARK: - Properties static let identifier: String = String(describing: AlbumListCell.self) - var LastImage = UIImageView(frame: .zero) - var albumNameLabel = UILabel(frame: .zero) - var albumCount = UILabel(frame: .zero) + private let lastImage: UIImageView = { + let imageView = UIImageView() + imageView.backgroundColor = .gray + imageView.layer.cornerRadius = 10 + return imageView + }() + + private let albumNameLabel : UILabel = { + let label = UILabel() + label.font = .systemFont(ofSize: 18) + return label + }() + + private let albumCountLabel: UILabel = { + let label = UILabel() + label.textColor = .gray + return label + }() // MARK: - Life Cycle override init(frame: CGRect) { super.init(frame: frame) + addSubviews() + setLastImageLayout() + setAlbumNameLabel() + setAlbumCountLabel() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func configure(with albumItemViewModel: AlbumItemViewModel) { + albumNameLabel.text = albumItemViewModel.name + albumCountLabel.text = String(albumItemViewModel.count) + } +} - albumNameLabel.font = .systemFont(ofSize: 18) - albumCount.textColor = .gray - LastImage.backgroundColor = .gray - LastImage.layer.cornerRadius = 10 - - addSubview(LastImage) +extension AlbumListCell { + private func addSubviews() { + addSubview(lastImage) addSubview(albumNameLabel) - addSubview(albumCount) - - LastImage.snp.makeConstraints { make in + addSubview(albumCountLabel) + } + + private func setLastImageLayout() { + lastImage.snp.makeConstraints { make in make.width.top.leading.trailing.equalToSuperview() make.bottom.equalTo(albumNameLabel.snp.top) } - + } + + private func setAlbumNameLabel() { albumNameLabel.snp.makeConstraints { make in make.leading.equalToSuperview() - make.bottom.equalTo(albumCount.snp.top) - } - albumCount.snp.makeConstraints { make in - make.leading.bottom.equalToSuperview() + make.bottom.equalTo(albumCountLabel.snp.top) } } - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") + private func setAlbumCountLabel() { + albumCountLabel.snp.makeConstraints { make in + make.leading.bottom.equalToSuperview() + } } } diff --git a/doyeon/Album/Album/Sources/AlbumListViewController.swift b/doyeon/Album/Album/Sources/AlbumListViewController.swift index bad7761..028ad22 100644 --- a/doyeon/Album/Album/Sources/AlbumListViewController.swift +++ b/doyeon/Album/Album/Sources/AlbumListViewController.swift @@ -11,10 +11,9 @@ import SnapKit final class AlbumListViewController: UIViewController { // MARK: - Properties - let viewModel = AlbumViewModel() - var albums: [Album] = [] + private let viewModel = DefaultAlbumViewModel() - private let collectionView: UICollectionView = { + private let albumCollectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .vertical layout.itemSize = CGSize(width: (UIScreen.main.bounds.width / 2) - 20, height: 220) @@ -30,9 +29,10 @@ final class AlbumListViewController: UIViewController { super.viewDidLoad() title = "앨범" setupUI() - - viewModel.requestAlbumsData() - print("Data Flow1 - view model에게 데이터 요청") + viewModel.loadAlbumItems() + DispatchQueue.main.async { + self.albumCollectionView.reloadData() + } } } @@ -45,22 +45,16 @@ extension AlbumListViewController { } private func setDelegates() { - collectionView.delegate = self - collectionView.dataSource = self - viewModel.delegate = self + albumCollectionView.delegate = self + albumCollectionView.dataSource = self } private func setSubviews() { - let views = [ - collectionView - ] - views.forEach { - view.addSubview($0) - } + view.addSubview(albumCollectionView) } private func setConstraints() { - collectionView.snp.makeConstraints { make in + albumCollectionView.snp.makeConstraints { make in make.edges.equalToSuperview().inset(10) } } @@ -76,21 +70,13 @@ extension AlbumListViewController: UICollectionViewDelegate { extension AlbumListViewController: UICollectionViewDataSource { /// 지정된 섹션에 표시할 항목의 개수 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return albums.count + return viewModel.items.count } /// 컬렉션뷰의 지정된 위치에 표시할 셀을 요청 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: AlbumListCell.identifier, for: indexPath) as? AlbumListCell else { return AlbumListCell() } - cell.LastImage.image = UIImage(named: "photo") - cell.albumNameLabel.text = albums[indexPath.item].name - cell.albumCount.text = "\(albums[indexPath.item].count)" + cell.configure(with: viewModel.items[indexPath.row]) return cell } } - -extension AlbumListViewController: AlbumViewModelDelegate { - func fetchAlbumInfo(_ albums: [Album]) { - self.albums = albums - } -} diff --git a/doyeon/Album/Album/Sources/AlbumViewModel.swift b/doyeon/Album/Album/Sources/AlbumViewModel.swift index c3f7a66..971a661 100644 --- a/doyeon/Album/Album/Sources/AlbumViewModel.swift +++ b/doyeon/Album/Album/Sources/AlbumViewModel.swift @@ -7,13 +7,16 @@ import Foundation -protocol AlbumViewModelDelegate: AnyObject { - func fetchAlbumInfo(_ albums: [Album]) +protocol AlbumViewModel: AnyObject { + var items: [AlbumItemViewModel] { get } + + func loadAlbumItems() } -class AlbumViewModel { - weak var delegate: AlbumViewModelDelegate? +final class DefaultAlbumViewModel: AlbumViewModel { + private var albums: [Album] = [] + private(set) var items: [AlbumItemViewModel] = [] init() { self.addAlbums() @@ -23,13 +26,24 @@ class AlbumViewModel { albums = dummyAlbumList } - func requestAlbumsData() { - delegate?.fetchAlbumInfo(albums) + func loadAlbumItems() { + items = albums.map { .init(name: $0.name, count: $0.count) } } } -// 더미데이터 +struct AlbumItemViewModel { + let name: String + let count: Int +} + +extension AlbumItemViewModel { + init(album: Album) { + self.name = album.name + self.count = album.count + } +} + fileprivate let dummyAlbumList = [ Album(name: "앨범 1", count: 23), Album(name: "앨범 2", count: 9),