Created
April 11, 2017 19:46
-
-
Save gracietti/7a2f7cf03d9e6eca6b11046fc67dae8a to your computer and use it in GitHub Desktop.
ProductCategoriesViewController.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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