Last active
April 30, 2019 20:38
-
-
Save volkanbicer/60d129c27b42e712da7de1b4e62f96de to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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