Skip to content

Commit

Permalink
[feat] FeedVC API 및 데이터 연결 (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
BAEKYUJEONG committed Aug 30, 2024
1 parent c2bf450 commit 3266dc9
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions PLUV/Feed/FeedViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa

class FeedViewController: UIViewController {

let viewModel = FeedViewModel()

private let titleView = UIView()
private let feedTitleLabel = UILabel().then {
$0.text = "최신 플레이리스트"
Expand All @@ -17,23 +22,26 @@ class FeedViewController: UIViewController {
}
private var feedCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 16
layout.minimumInteritemSpacing = 16
layout.minimumLineSpacing = 24
layout.minimumInteritemSpacing = 14
layout.scrollDirection = .vertical
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)

let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.register(SelectPlaylistCollectionViewCell.self, forCellWithReuseIdentifier: SelectPlaylistCollectionViewCell.identifier)
cv.register(FeedCollectionViewCell.self, forCellWithReuseIdentifier: FeedCollectionViewCell.identifier)

return cv
}()
private let disposeBag = DisposeBag()

override func viewDidLoad() {
super.viewDidLoad()

setUI()
setFeedAPI()
}


override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: false)
Expand Down Expand Up @@ -64,9 +72,56 @@ class FeedViewController: UIViewController {
feedCollectionView.snp.makeConstraints { make in
make.top.equalTo(titleView.snp.bottom)
make.leading.trailing.equalToSuperview()
make.bottom.equalToSuperview().inset(80)
make.bottom.equalToSuperview().inset(90)
}
feedCollectionView.showsVerticalScrollIndicator = false
feedCollectionView.allowsMultipleSelection = false
}

private func setData() {
self.feedCollectionView.rx.setDelegate(self)
.disposed(by: disposeBag)

/// CollectionView에 들어갈 Cell에 정보 제공
self.viewModel.feedItems
.observe(on: MainScheduler.instance)
.bind(to: self.feedCollectionView.rx.items(cellIdentifier: FeedCollectionViewCell.identifier, cellType: FeedCollectionViewCell.self)) { index, item, cell in
cell.prepare(feed: item)
}
.disposed(by: disposeBag)

/// 아이템 선택 시 다음으로 넘어갈 VC에 정보 제공
self.feedCollectionView.rx.modelSelected(Feed.self)
.subscribe(onNext: { [weak self] feedItem in
/// pushVC
})
.disposed(by: disposeBag)
}

private func setFeedAPI() {
let url = EndPoint.feed.path

APIService().get(of: APIResponse<[Feed]>.self, url: url) { response in
switch response.code {
case 200:
self.viewModel.feedItems = Observable.just(response.data)
self.setData()
self.view.layoutIfNeeded()
default:
AlertController(message: response.msg).show()
}
}
}
}

@available(iOS 14.0, *)
extension FeedViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else {
return CGSize.zero
}

let value = (collectionView.frame.width - (flowLayout.sectionInset.left + flowLayout.sectionInset.right + flowLayout.minimumInteritemSpacing)) / 2
return CGSize(width: value, height: value / 172 * 272)
}
}

0 comments on commit 3266dc9

Please sign in to comment.