Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ProductCategoriesViewController.swift
import Foundation
import SMPageControl
class ProductCategoriesViewController: BaseViewController {
// MARK: Constants
let categories = Constants.categories
// MARK: Properties
var presenter: ProductCategoriesPresentation?
var pageViewController: ProductCategoriesPageViewController?
var product: Product?
var viewControllers: [UIViewController?]?
var viewIsLoaded = false
var firstPage: Int? {
didSet {
if viewIsLoaded {
pageControl.currentPage = firstPage ?? 0
pageViewController?.showViewController(for: firstPage ?? 0)
}
}
}
// MARK: Outlets
@IBOutlet weak var pageControl: SMPageControl!
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setupPageControl()
viewIsLoaded = true
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == Constants.Segue.productCategoriesPage) {
pageViewController = segue.destination as? ProductCategoriesPageViewController
pageViewController?.categoriesViewPageDelegate = self
}
}
// MARK: IBActions
@IBAction func onPageControlClicked(_ sender: UIControl) {
if let pager = sender as? SMPageControl {
let newPageIndex = pager.currentPage
onPageChanged(newPageIndex)
setupView(for: newPageIndex)
}
}
// MARK: Private Methods
fileprivate func setupPageControl() {
pageControl.numberOfPages = categories.count
pageControl.currentPageIndicatorImage = .pageActiveIcon
pageControl.pageIndicatorImage = .pageInactiveIcon
pageControl.currentPage = firstPage ?? 0
guard let viewControllers = viewControllers else { return }
pageViewController?.setupPages(with: viewControllers, andShow: firstPage)
}
}
extension ProductCategoriesViewController: ProductCategoriesPageDelegate {
func onPageChanged(_ newPageIndex: Int) {
pageControl.currentPage = newPageIndex
presenter?.onUserScrolled(to: newPageIndex)
}
}
extension ProductCategoriesViewController: ProductCategoriesView {
func setupPages(with viewControllers: [UIViewController?]) {
self.viewControllers = viewControllers
}
func setupView(for firstPage: Int?) {
self.firstPage = firstPage
}
}
extension ProductCategoriesViewController: ReusableView { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.