Skip to content

Instantly share code, notes, and snippets.

@rahul-inspired-iosdeveloper
Last active March 4, 2020 16:58
Show Gist options
  • Save rahul-inspired-iosdeveloper/2842031d3b53f3e95bc262fe54b89c7b to your computer and use it in GitHub Desktop.
Save rahul-inspired-iosdeveloper/2842031d3b53f3e95bc262fe54b89c7b to your computer and use it in GitHub Desktop.
class LiveNewsInteractor: PresentorToInteractorProtocol{
var presenter: InteractorToPresenterProtocol?
func fetchLiveNews() {
Alamofire.request(Constants.URL).response { response in
if(response.response?.statusCode == 200){
guard let data = response.data else { return }
do {
let decoder = JSONDecoder()
let newsResponse = try decoder.decode(NewsResponse.self, from: data)
guard let firstArticle = newsResponse.articles?[0] else {
return
}
self.presenter?.liveNewsFetched(news: firstArticle)
} catch let error {
print(error)
}
}
else {
self.presenter?.liveNewsFetchedFailed()
}
}
}
}
class LiveNewsPresenter: ViewToPresenterProtocol {
var view: PresenterToViewProtocol?
var interactor: PresentorToInteractorProtocol?
var router: PresenterToRouterProtocol?
func updateView() {
interactor?.fetchLiveNews()
}
}
extension LiveNewsPresenter: InteractorToPresenterProtocol {
func liveNewsFetched(news: LiveNewsModel) {
view?.showNews(news: news)
}
func liveNewsFetchedFailed(){
view?.showError()
}
}
protocol PresenterToViewProtocol: class {
func showNews(news: LiveNewsModel)
func showError()
}
protocol InteractorToPresenterProtocol: class {
func liveNewsFetched(news: LiveNewsModel)
func liveNewsFetchedFailed()
}
protocol PresentorToInteractorProtocol: class {
var presenter: InteractorToPresenterProtocol? {get set}
func fetchLiveNews()
}
protocol ViewToPresenterProtocol: class {
var view: PresenterToViewProtocol? {get set}
var interactor: PresentorToInteractorProtocol? {get set}
var router: PresenterToRouterProtocol? {get set}
func updateView()
}
protocol PresenterToRouterProtocol: class {
static func createModule() -> UIViewController
}
class LiveNewsRouter: PresenterToRouterProtocol{
class func createModule() ->UIViewController{
let view = mainstoryboard.instantiateViewController(withIdentifier: "LiveNewsViewController") as? LiveNewsViewController
//if let view = navController.childViewControllers.first as? LiveNewsViewController {
let presenter: ViewToPresenterProtocol & InteractorToPresenterProtocol = LiveNewsPresenter()
let interactor: PresentorToInteractorProtocol = LiveNewsInteractor()
let router: PresenterToRouterProtocol = LiveNewsRouter()
view?.presenter = presenter
presenter.view = view
presenter.router = router
presenter.interactor = interactor
interactor.presenter = presenter
return view!;
//}
//return UIViewController()
}
static var mainstoryboard: UIStoryboard{
return UIStoryboard(name:"Main",bundle: Bundle.main)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment