protocol RemoteAPIDelegate: class {
func remoteAPIDidLoadPosts(_ posts: NSArray)
}
class RemoteAPI {
weak var delegate: RemoteAPIDelegate?
func loadPosts() {
}
}
class FeedViewModel: RemoteAPIDelegate {
private let remoteAPI: RemoteAPI
private var initialPostsLoaded = false
init(remoteAPI: RemoteAPI) {
self.remoteAPI = remoteAPI
}
func loadInitialPosts() {
// how to test this method?
if (initialPostsLoaded) {
return
}
remoteAPI.loadPosts()
}
func remoteAPIDidLoadPosts(_ posts: NSArray) {
initialPostsLoaded = true
}
}
// factory code
let remoteAPI = RemoteAPI()
let viewModel = FeedViewModel(remoteAPI: remoteAPI)
remoteAPI.delegate = viewModel