Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
♻️[Refactor]: 전면 수정
Browse files Browse the repository at this point in the history
- 가독성을 개선 시켜봤습니다.
  • Loading branch information
doyeonjeong committed Apr 5, 2023
1 parent 52f15d0 commit 8518e8c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 54 deletions.
4 changes: 2 additions & 2 deletions doyeon/Album/Album/Applications/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) ]
Expand Down
68 changes: 49 additions & 19 deletions doyeon/Album/Album/Sources/AlbumListCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
38 changes: 12 additions & 26 deletions doyeon/Album/Album/Sources/AlbumListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
}
}

Expand All @@ -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)
}
}
Expand All @@ -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
}
}
28 changes: 21 additions & 7 deletions doyeon/Album/Album/Sources/AlbumViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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),
Expand Down

0 comments on commit 8518e8c

Please sign in to comment.