This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 282
/
CardsViewController.swift
107 lines (78 loc) · 3.02 KB
/
CardsViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// CardsViewController.swift
// CollectionViewPagingLayout
//
// Created by Amir Khorsandi on 01/10/19.
// Copyright © 2019 Amir Khorsandi. All rights reserved.
//
import Foundation
import UIKit
import CollectionViewPagingLayout
class CardsViewController: UIViewController, NibBased, ViewModelBased {
// MARK: Constants
private struct Constants {
static let infiniteNumberOfItems = 100_000
}
// MARK: Properties
var viewModel: CardsViewModel!
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
private let layout = CollectionViewPagingLayout()
private var didScrollCollectionViewToMiddle = false
@IBOutlet private weak var backButton: UIButton!
@IBOutlet private weak var collectionView: UICollectionView!
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
configureViews()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
layout.invalidateLayout()
if !didScrollCollectionViewToMiddle {
layout.setCurrentPage(Constants.infiniteNumberOfItems / 2, animated: false)
didScrollCollectionViewToMiddle = true
}
}
// MARK: Event listener
@IBAction private func onBackTouched() {
navigationController?.popViewController(animated: true)
}
@IBAction private func onNextTouched() {
layout.goToNextPage()
}
@IBAction private func onPreviousTouched() {
layout.goToPreviousPage()
}
// MARK: Private functions
private func configureViews() {
view.backgroundColor = #colorLiteral(red: 0.05882352941, green: 0.06274509804, blue: 0.1137254902, alpha: 1)
view.clipsToBounds = true
configureCollectionView()
}
private func configureCollectionView() {
collectionView.register(CardCollectionViewCell.self)
collectionView.isPagingEnabled = true
collectionView.dataSource = self
layout.numberOfVisibleItems = 7
layout.scrollDirection = .vertical
collectionView.collectionViewLayout = layout
collectionView.showsVerticalScrollIndicator = false
collectionView.clipsToBounds = false
collectionView.backgroundColor = .clear
collectionView.scrollsToTop = false
}
}
extension CardsViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
Constants.infiniteNumberOfItems
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let index = indexPath.row % viewModel.itemViewModels.count
let itemViewModel = viewModel.itemViewModels[index]
let cell: CardCollectionViewCell = collectionView.dequeueReusableCell(for: indexPath)
cell.viewModel = itemViewModel
return cell
}
}