Skip to content

Commit

Permalink
add CellIdentifirable protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Macostik committed Jan 30, 2020
1 parent eee336b commit d15f4b4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
2 changes: 0 additions & 2 deletions FisherMan/CommonClasses/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ struct Constants {
static let iOS13Version = ProcessInfo.processInfo.operatingSystemVersion.majorVersion >= 13
static let screenWidth = UIScreen.main.bounds.width
static let screenHeight = UIScreen.main.bounds.height
static let mainCollectionViewCell = "mainCollectionViewCell"
static let newsCollectionViewCell = "newsCollectionViewCell"
static let serviceID = "1fa7ce2d8fde588ac8fc"
static let localizeNames = "localizationsShortNames"
static let localizeName = "localizationShortName"
Expand Down
10 changes: 10 additions & 0 deletions FisherMan/Views/BaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,13 @@ class BaseTabBarController<C>: BaseSceneCoordinator<UINavigationController> {
return Observable<UINavigationController>.just(navigationController)
}
}

protocol CellIdentifierable {
static var identifier: String { get }
}

extension CellIdentifierable where Self: UICollectionViewCell {
static var identifier: String {
return NSStringFromClass(Self.self)
}
}
14 changes: 6 additions & 8 deletions FisherMan/Views/FavoriteSceneViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ class FavoriteSceneViewController: BaseViewController<FavoriteSceneViewModel> {
collectionViewLayout: self.collectionViewLayout)) {
$0.backgroundColor = .systemBackground
$0.register(FavoriteCell.self,
forCellWithReuseIdentifier: FavoriteCell.reuseIdentifier)
forCellWithReuseIdentifier: FavoriteCell.identifier)
}
private lazy var collectionViewLayout: UICollectionViewLayout = {
let layout = UICollectionViewCompositionalLayout { _, _ in
let leadingSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5),
let leadingSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.75),
heightDimension: .fractionalHeight(1.0))
let leadingItem = NSCollectionLayoutItem(layoutSize: leadingSize)
let trailingSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let trailingItem = NSCollectionLayoutItem(layoutSize: trailingSize)
let trailingGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5),
let trailingGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.25),
heightDimension: .fractionalHeight(1.0))
let trailingGroup = NSCollectionLayoutGroup.vertical(layoutSize: trailingGroupSize,
subitem: trailingItem,
Expand All @@ -49,7 +49,7 @@ class FavoriteSceneViewController: BaseViewController<FavoriteSceneViewModel> {
let dataSource = UICollectionViewDiffableDataSource
<Section, Int>(collectionView: self.favoriteGrid) { collectionView, indexPath, identifier in
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: FavoriteCell.reuseIdentifier,
withReuseIdentifier: FavoriteCell.identifier,
for: indexPath) as? FavoriteCell else { fatalError("Cannot create new cell") }

cell.setupEntry(identifier)
Expand All @@ -65,7 +65,6 @@ class FavoriteSceneViewController: BaseViewController<FavoriteSceneViewModel> {
}()

override func setupUI() {
view.backgroundColor = .systemBackground
view.add(favoriteGrid, layoutBlock: { $0.edges() })
dataSource.apply(snapShot, animatingDifferences: false)
}
Expand All @@ -77,8 +76,7 @@ class FavoriteSceneViewController: BaseViewController<FavoriteSceneViewModel> {
}
}

final class FavoriteCell: UICollectionViewCell {
static let reuseIdentifier = NSStringFromClass(FavoriteCell.self)
final class FavoriteCell: UICollectionViewCell, CellIdentifierable {

let titleLabel = specify(UILabel()) {
$0.textAlignment = .center
Expand All @@ -88,6 +86,6 @@ final class FavoriteCell: UICollectionViewCell {
add(titleLabel, layoutBlock: { $0.edges() })
titleLabel.text = "\(entry)"
layer.borderColor = UIColor.black.cgColor
layer.borderWidth = 1.0
layer.borderWidth = 0.5
}
}
6 changes: 3 additions & 3 deletions FisherMan/Views/MainSceneViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MainScreenViewController: BaseViewController<MainSceneViewModel> {
collectionView.bounces = false
collectionView.contentInsetAdjustmentBehavior = .never
collectionView.register(MainCollectionViewCell.self,
forCellWithReuseIdentifier: Constants.mainCollectionViewCell)
forCellWithReuseIdentifier: MainCollectionViewCell.identifier)
return collectionView
}()

Expand All @@ -35,14 +35,14 @@ class MainScreenViewController: BaseViewController<MainSceneViewModel> {

override func setupBindings() {
viewModel?.items?.asObservable()
.bind(to: mainCollectionView.rx.items(cellIdentifier: Constants.mainCollectionViewCell,
.bind(to: mainCollectionView.rx.items(cellIdentifier: MainCollectionViewCell.identifier,
cellType: MainCollectionViewCell.self)) { _, data, cell in
cell.setupEntry(data)
}.disposed(by: disposeBag)
}
}

class MainCollectionViewCell: UICollectionViewCell {
class MainCollectionViewCell: UICollectionViewCell, CellIdentifierable {

public func setupEntry(_ entry: UIViewController) {
add(entry.view, layoutBlock: { $0.edges() })
Expand Down
6 changes: 3 additions & 3 deletions FisherMan/Views/NewsSceneViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class NewsSceneViewController: BaseViewController<NewsSceneViewModel> {
collectionView.backgroundColor = .clear
collectionView.showsVerticalScrollIndicator = false
collectionView.register(NewsCollectionViewCell.self,
forCellWithReuseIdentifier: Constants.newsCollectionViewCell)
forCellWithReuseIdentifier: NewsCollectionViewCell.identifier)
self.view.add(collectionView, layoutBlock: { $0.edges() })
return collectionView
}()
Expand All @@ -51,7 +51,7 @@ class NewsSceneViewController: BaseViewController<NewsSceneViewModel> {
return DataSource(animationConfiguration: animationConfiguration,
configureCell: { _, collectionView, indexPath, data in
let cell = collectionView
.dequeueReusableCell(withReuseIdentifier: Constants.newsCollectionViewCell,
.dequeueReusableCell(withReuseIdentifier: NewsCollectionViewCell.identifier,
for: indexPath) as? NewsCollectionViewCell
cell?.setupEntry(data)
return cell ?? UICollectionViewCell()
Expand All @@ -75,7 +75,7 @@ class NewsSceneViewController: BaseViewController<NewsSceneViewModel> {
}
}

class NewsCollectionViewCell: UICollectionViewCell {
class NewsCollectionViewCell: UICollectionViewCell, CellIdentifierable {

private let disposeBag = DisposeBag()

Expand Down

0 comments on commit d15f4b4

Please sign in to comment.