Skip to content

Instantly share code, notes, and snippets.

@volkanbicer
Last active April 30, 2019 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volkanbicer/60d129c27b42e712da7de1b4e62f96de to your computer and use it in GitHub Desktop.
Save volkanbicer/60d129c27b42e712da7de1b4e62f96de to your computer and use it in GitHub Desktop.
protocol BookService {
typealias Handler = (Books) -> Void
// Global popular books (Anonymous)
func fetchPopularBooks(then handler: Handler)
// Special books based on logged in user preferences
func fetchRelatedBooks(then handler: Handler)
}
protocol Auth {
var isAuthenticated: Bool { get }
}
class BooksViewContoller: UIViewController {
private let bookService: BookService
private let auth: Auth
init(bookService: BookService, auth: Auth) {
self.bookService = bookService
self.auth = auth
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func viewDidLoad() {
super.viewDidLoad()
fetchBooks()
}
func fetchBooks() {
let hander: (Books) -> Void = { [weak self] books in
self?.display(books)
}
if auth.isAuthenticated {
bookService.fetchRelatedBooks(then: hander)
} else {
bookService.fetchPopularBooks(then: hander)
}
}
private func display(_ books: Books) {
print(books)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment