Skip to content

Instantly share code, notes, and snippets.

View shabib87's full-sized avatar

Shabib Hossain shabib87

View GitHub Profile
@shabib87
shabib87 / Routing.swift
Created June 12, 2019 18:24
UI navigation using protocol oriented programming
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 June 12, 2019 18:15
Hero list presenter class
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 June 12, 2019 18:09
Passive view protocol for HeroListViewController
protocol HeroListView: NSObjectProtocol {
func startLoading()
func finishLoading()
func reloadHeroList()
func showEmptyListMessage()
func gotoHeroDetails(presenter aPresenter: HeroDetailsPresenter)
}
@shabib87
shabib87 / HeroDetailsViewController.swift
Created May 10, 2019 23:56
Overriding root view controller confronting to StoryboardInitializable to initialize from storyboard
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 23:49
Helper root view controller class to enforce storyboard initialization without segues and handling dependency.
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 23:33
Helper protocol to enforce storyboard initialization without segues and handling dependency.
class Presenter {}
protocol StoryboardInitializable {
static func instantiateFromStoryboard(presenter aPresenter: Presenter) -> UIViewController
}
@shabib87
shabib87 / RetainCycle3.swift
Created March 13, 2017 10:23
In support to blog post
class Person {
...
}
class Apartment {
...
weak var person: Person?
...
}
@shabib87
shabib87 / RetainCycle5.swift
Last active March 13, 2017 10:18
In support to blog post
var person: Person? = Person(name: "John Doe")
var apartment: Apartment? = Apartment(number: 123)
person?.apartment = apartment
apartment?.person = person
person = nil
apartment = nil
// output:
//
@shabib87
shabib87 / RetainCycle6.swift
Last active March 13, 2017 10:17
In support to blog post
class Person {
...
}
class Apartment {
...
weak var person: Person?
...
}
@shabib87
shabib87 / RetainCycle7.swift
Last active March 13, 2017 10:17
In support to blog post
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) {