-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from AckeeCZ/improve_example
Improve example
- Loading branch information
Showing
10 changed files
with
382 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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
41 changes: 41 additions & 0 deletions
41
ACKategories-Example/Flow coordinators/AppFlowCoordinator.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// AppFlowCoordinator.swift | ||
// ACKategories-Example | ||
// | ||
// Created by Jakub Olejník on 12/09/2018. | ||
// Copyright © 2018 Ackee, s.r.o. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import ACKategories | ||
|
||
final class AppFlowCoordinator: FlowCoordinator { | ||
override func start(in window: UIWindow) { | ||
super.start(in: window) | ||
|
||
let exampleListVM = ExampleListViewModel() | ||
let exampleListVC = ExampleListViewController(viewModel: exampleListVM) | ||
exampleListVC.flowDelegate = self | ||
|
||
let navigationController = UINavigationController(rootViewController: exampleListVC) | ||
window.rootViewController = navigationController | ||
|
||
self.rootViewController = navigationController | ||
self.navigationController = navigationController | ||
} | ||
} | ||
|
||
extension AppFlowCoordinator: ExampleListFlowDelegate { | ||
func exampleItemSelected(_ item: ExampleItem, in viewController: ExampleListViewController) { | ||
let itemVC = controller(for: item) | ||
itemVC.title = item.title | ||
navigationController?.pushViewController(itemVC, animated: true) | ||
} | ||
|
||
private func controller(for item: ExampleItem) -> UIViewController { | ||
switch item { | ||
case .uiControlBlocks: return UIControlBlocksViewController() | ||
case .viewControllerComposition: return VCCompositionViewController() | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// ExampleItem.swift | ||
// ACKategories-Example | ||
// | ||
// Created by Jakub Olejník on 12/09/2018. | ||
// Copyright © 2018 Ackee, s.r.o. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
enum ExampleItem { | ||
case uiControlBlocks | ||
case viewControllerComposition | ||
|
||
static var allCases: [ExampleItem] { return [.uiControlBlocks, .viewControllerComposition] } | ||
|
||
var title: String { return data.title } | ||
var subtitle: String { return data.subtitle } | ||
|
||
private var data: (title: String, subtitle: String) { | ||
switch self { | ||
case .uiControlBlocks: return ("UIControl blocks", "Use closures instead of target - selector pattern") | ||
case .viewControllerComposition: return ("View controller composition", "Simply embed view controller into another one") | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
ACKategories-Example/Screens/Example list/ExampleListViewController.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// ExampleListViewController.swift | ||
// ACKategories-Example | ||
// | ||
// Created by Jakub Olejník on 12/09/2018. | ||
// Copyright © 2018 Ackee, s.r.o. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
protocol ExampleListFlowDelegate: class { | ||
func exampleItemSelected(_ item: ExampleItem, in viewController: ExampleListViewController) | ||
} | ||
|
||
final class ExampleListViewController: UIViewController { | ||
weak var flowDelegate: ExampleListFlowDelegate? | ||
|
||
private weak var tableView: UITableView! | ||
|
||
private let viewModel: ExampleListViewModeling | ||
|
||
// MARK: Initializers | ||
|
||
init(viewModel: ExampleListViewModeling) { | ||
self.viewModel = viewModel | ||
super.init(nibName: nil, bundle: nil) | ||
self.title = "ACKategories" | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: View life cycle | ||
|
||
override func loadView() { | ||
super.loadView() | ||
|
||
let tableView = UITableView() | ||
view.addSubview(tableView) | ||
tableView.snp.makeConstraints { (make) in | ||
make.edges.equalToSuperview() | ||
} | ||
self.tableView = tableView | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
tableView.delegate = self | ||
tableView.dataSource = self | ||
} | ||
|
||
override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
|
||
tableView.indexPathsForVisibleRows?.forEach { tableView.deselectRow(at: $0, animated: true) } | ||
} | ||
} | ||
|
||
extension ExampleListViewController: UITableViewDataSource { | ||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
return viewModel.items.count | ||
} | ||
|
||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
let item = viewModel.items[indexPath.row] | ||
let cell: TitleSubtitleTableViewCell = tableView.dequeueCell(for: indexPath) | ||
cell.textLabel?.text = item.title | ||
cell.detailTextLabel?.text = item.subtitle | ||
return cell | ||
} | ||
} | ||
|
||
extension ExampleListViewController: UITableViewDelegate { | ||
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
flowDelegate?.exampleItemSelected(viewModel.items[indexPath.row], in: self) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
ACKategories-Example/Screens/Example list/ExampleListViewModel.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// ExampleListViewModel.swift | ||
// ACKategories | ||
// | ||
// Created by Jakub Olejník on 12/09/2018. | ||
// Copyright © 2018 Ackee, s.r.o. All rights reserved. | ||
// | ||
|
||
protocol ExampleListViewModelingActions { | ||
|
||
} | ||
|
||
protocol ExampleListViewModeling { | ||
var actions: ExampleListViewModelingActions { get } | ||
|
||
var items: [ExampleItem] { get } | ||
} | ||
|
||
extension ExampleListViewModeling where Self: ExampleListViewModelingActions { | ||
var actions: ExampleListViewModelingActions { return self } | ||
} | ||
|
||
final class ExampleListViewModel: ExampleListViewModeling, ExampleListViewModelingActions { | ||
let items = ExampleItem.allCases | ||
} |
This file contains 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
45 changes: 45 additions & 0 deletions
45
ACKategories-Example/Screens/VC composition/TitleViewController.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// TitleViewController.swift | ||
// ACKategories | ||
// | ||
// Created by Jakub Olejník on 12/09/2018. | ||
// Copyright © 2018 Ackee, s.r.o. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class TitleViewController: UIViewController { | ||
private(set) weak var nameLabel: UILabel! | ||
|
||
private let name: String | ||
private let color: UIColor | ||
|
||
// MARK: Initializers | ||
|
||
init(name: String, color: UIColor) { | ||
self.name = name | ||
self.color = color | ||
super.init(nibName: nil, bundle: nil) | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: View life cycle | ||
|
||
override func loadView() { | ||
super.loadView() | ||
|
||
view.backgroundColor = color | ||
|
||
let nameLabel = UILabel() | ||
nameLabel.textAlignment = .center | ||
nameLabel.text = name | ||
view.addSubview(nameLabel) | ||
nameLabel.snp.makeConstraints { (make) in | ||
make.leading.trailing.top.equalTo(safeArea).inset(20) | ||
} | ||
self.nameLabel = nameLabel | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
ACKategories-Example/Screens/VC composition/VCCompositionViewController.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// VCCompositionViewController.swift | ||
// ACKategories | ||
// | ||
// Created by Jakub Olejník on 12/09/2018. | ||
// Copyright © 2018 Ackee, s.r.o. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import SnapKit | ||
|
||
final class VCCompositionViewController: TitleViewController { | ||
|
||
// MARK: Initializers | ||
|
||
init() { | ||
super.init(name: "Parent", color: .lightGray) | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: View life cycle | ||
|
||
override func loadView() { | ||
super.loadView() | ||
|
||
let containerView = UIView() | ||
view.addSubview(containerView) | ||
containerView.snp.makeConstraints { (make) in | ||
make.leading.trailing.bottom.equalToSuperview() | ||
make.top.equalTo(nameLabel.snp.bottom).offset(30) | ||
} | ||
|
||
let childVC = TitleViewController(name: "Child", color: .blue) | ||
display(childViewController: childVC, in: containerView) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
ACKategories-Example/View/TitleSubtitleTableViewCell.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// TitleSubtitleTableViewCell.swift | ||
// ACKategories-Example | ||
// | ||
// Created by Jakub Olejník on 12/09/2018. | ||
// Copyright © 2018 Ackee, s.r.o. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
final class TitleSubtitleTableViewCell: UITableViewCell { | ||
|
||
// MARK: Initializers | ||
|
||
override init(style: UITableViewCellStyle, reuseIdentifier: String?) { | ||
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
} |
Oops, something went wrong.