Skip to content

Instantly share code, notes, and snippets.

@djds23
Created September 4, 2018 20:19
Show Gist options
  • Select an option

  • Save djds23/b4a7b411f5049f9968c5f8d0a355f144 to your computer and use it in GitHub Desktop.

Select an option

Save djds23/b4a7b411f5049f9968c5f8d0a355f144 to your computer and use it in GitHub Desktop.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
protocol GreetService {
func greeting() -> String
}
class Greeter: GreetService{
func greeting() -> String {
return "Hello, World!"
}
}
protocol StyleService {
func textColor() -> UIColor
func backgroundColor() -> UIColor
}
class DefaultStyle: StyleService {
func textColor() -> UIColor {
return .green
}
func backgroundColor() -> UIColor {
return .blue
}
}
protocol HasStyleService {
var styleService: StyleService { get }
}
protocol HasGreetService {
var greetService: GreetService { get }
}
struct Dependencies: HasStyleService, HasGreetService {
var styleService: StyleService
var greetService: GreetService
}
class MyViewController : UIViewController {
typealias Dependencies = HasGreetService & HasStyleService
let dependencies: Dependencies
init(dependencies: Dependencies) {
self.dependencies = dependencies
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
let view = UIView()
view.backgroundColor = dependencies.styleService.backgroundColor()
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = dependencies.greetService.greeting()
label.textColor = dependencies.styleService.textColor()
view.addSubview(label)
self.view = view
}
}
let AppDepenencies = Dependencies(styleService: DefaultStyle(), greetService: Greeter())
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController(dependencies: AppDepenencies)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment