Skip to content

Instantly share code, notes, and snippets.

Avatar

Shabib Hossain shabib87

View GitHub Profile
@shabib87
shabib87 / Routing.swift
Created Jun 12, 2019
UI navigation using protocol oriented programming
View Routing.swift
protocol CanDisplayHeroDetailsScreen {
func displayHeroDetailsScreen(presenter aPresenter: Presenter)
}
extension CanDisplayHeroDetailsScreen where Self: UIViewController {
func displayHeroDetailsScreen(presenter aPresenter: Presenter) {
let detailsVC = HeroDetailsViewController.instantiateFromStoryboard(presenter: aPresenter)
navigationController?.pushViewController(detailsVC, animated: true)
}
@shabib87
shabib87 / HeroListPresenter.swift
Created Jun 12, 2019
Hero list presenter class
View HeroListPresenter.swift
final class HeroListPresenter: Presenter {
private let service: HeroService
weak private var view : HeroListView?
...
init(service: HeroService) {
self.service = service
}
func attachView(view: HeroListView) {
@shabib87
shabib87 / HeroListView.swift
Created Jun 12, 2019
Passive view protocol for HeroListViewController
View HeroListView.swift
protocol HeroListView: NSObjectProtocol {
func startLoading()
func finishLoading()
func reloadHeroList()
func showEmptyListMessage()
func gotoHeroDetails(presenter aPresenter: HeroDetailsPresenter)
}
@shabib87
shabib87 / HeroDetailsViewController.swift
Created May 10, 2019
Overriding root view controller confronting to StoryboardInitializable to initialize from storyboard
View HeroDetailsViewController.swift
final class HeroDetailsViewController: RootViewController {
override class func instantiateFromStoryboard(presenter aPresenter: Presenter) -> UIViewController {
let vc = UIStoryboard(name: "Hero", bundle: nil)
.instantiateViewController(withIdentifier: "HeroDetailsViewController")
as! HeroDetailsViewController
vc.presenter = aPresenter as? HeroDetailsPresenter
return vc
}
}
@shabib87
shabib87 / RootViewController.swift
Created May 10, 2019
Helper root view controller class to enforce storyboard initialization without segues and handling dependency.
View RootViewController.swift
class RootViewController: UIViewController, StoryboardInitializable {
class func instantiateFromStoryboard(presenter aPresenter: Presenter) -> UIViewController {
fatalError("Override failed. Dependency injection not processed. Presenter injection hampered.")
}
// Preventing segue to support dependency injection
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
fatalError("Segues should not be used. Presenter injection hampered.")
}
@shabib87
shabib87 / StoryboardInitializable.swift
Created May 10, 2019
Helper protocol to enforce storyboard initialization without segues and handling dependency.
View StoryboardInitializable.swift
class Presenter {}
protocol StoryboardInitializable {
static func instantiateFromStoryboard(presenter aPresenter: Presenter) -> UIViewController
}
@shabib87
shabib87 / RetainCycle3.swift
Created Mar 13, 2017
In support to blog post
View RetainCycle3.swift
class Person {
...
}
class Apartment {
...
weak var person: Person?
...
}
@shabib87
shabib87 / RetainCycle2.swift
Created Mar 13, 2017
In support to blog post
View RetainCycle2.swift
class Person {
let name: String
init(name: String) {
self.name = name
}
deinit {
print("\(name) is being deinitialized")
}
}
@shabib87
shabib87 / RetainCycle8.swift
Last active Mar 13, 2017
In support to blog post
View RetainCycle8.swift
class HTMLElement {
...
lazy var asHTML: () -> String = { [weak self] in
guard let htmlElement = self else { return "" }
return "<\(htmlElement.name)>\(htmlElement.text)</\(htmlElement.name)>"
}
...
}
@shabib87
shabib87 / RetainCycle7.swift
Last active Mar 13, 2017
In support to blog post
View RetainCycle7.swift
class HTMLElement {
let name: String
let text: String?
lazy var asHTML: () -> String = {
return "<\(self.name)>\(self.text)</\(self.name)>"
}
init(name: String, text: String? = nil) {