<file_sep>//
//  RequestGeneric.swift
//  TheMovieApp
//
//  Created by <NAME> on 17/04/19.
//  Copyright © 2019 Rafael. All rights reserved.
//

import Foundation

enum Language: String, Codable {
    case US = "en-US"
}
class GenericRequest : Codable {
    var page: Int?
    var language: Language?
}
<file_sep>//
//  HomeWorker.swift
//  TheMovieApp
//
//  Created by <NAME> on 17/04/19.
//  Copyright (c) 2019 Rafael. All rights reserved.
//
//  This file was generated by the Clean Swift Xcode Templates so
//  you can apply clean architecture to your iOS and Mac projects,
//  see http://clean-swift.com
//
import Foundation

class HomeWorker
{
    private var base = Api.BASE_URL
    private let api_key = Api.API_KEY
    
    func fetchDiscovers(completion: @escaping ((Home.Fetch.ResponseDiscover) -> Void), failure: @escaping ((GenericError) -> Void)) {
        let service = ServiceClient<Home.Fetch.ResponseDiscover>(baseUrl: self.base)
        service.fetch(resource: "discover/movie", method: .GET, queryParams: ["api_key": self.api_key], completion: { (response) in
            completion(response)
        }) { (code, message)  in
            failure(GenericError(cod: code, message: message))
        }
    }
    
    func fetchTrends(completion: @escaping ((Home.Fetch.ResponseTrending) -> Void), failure: @escaping ((GenericError) -> Void)) {
        let service = ServiceClient<Home.Fetch.ResponseTrending>(baseUrl: self.base)
        service.fetch(resource: "trending/movie/day", method: .GET, queryParams:["api_key": self.api_key], completion: { (response) in
            completion(response)
        }) { (code, message) in
            failure(GenericError(cod: code, message: message))
        }
    }
    
    
    
    
    
    
}
<file_sep>//
//  Response.swift
//  TheMovieApp
//
//  Created by <NAME> on 17/04/19.
//  Copyright © 2019 Rafael. All rights reserved.
//

import Foundation

class ResponseGeneric<T: Codable> : Codable {
    var page: Int?
    var total_results: Int?
    var total_pages: Int?
    var results = [T]()
}
<file_sep>//
//  HomePresenter.swift
//  TheMovieApp
//
//  Created by <NAME> on 17/04/19.
//  Copyright (c) 2019 Rafael. All rights reserved.
//
//  This file was generated by the Clean Swift Xcode Templates so
//  you can apply clean architecture to your iOS and Mac projects,
//  see http://clean-swift.com
//

import UIKit

protocol HomePresentationLogic
{
    func presentSuccessDiscover(response: Home.Fetch.ResponseDiscover)
    func presentFailedDiscover(error: GenericError)
    
    func presentSuccessTrends(response: Home.Fetch.ResponseTrending)
    func presentFailedTrends(error: GenericError)
    
}

class HomePresenter: HomePresentationLogic
{
    weak var viewController: HomeDisplayLogic?
    
    // MARK: Do something
    
    func presentSuccessDiscover(response: Home.Fetch.ResponseDiscover) {
        self.viewController?.successDisplayDiscoveredMovies(response: response)
    }
    
    func presentFailedDiscover(error: GenericError) {
        self.viewController?.failureDisplayDiscoveredMovies(error: error)
    }
    
    func presentSuccessTrends(response: Home.Fetch.ResponseTrending) {
        self.viewController?.successDisplayTrendsMovies(response: response)
    }
    
    func presentFailedTrends(error: GenericError) {
        self.viewController?.failureDisplayTrendsMovies(error: error)
    }
}
<file_sep>//
//  HomeInteractor.swift
//  TheMovieApp
//
//  Created by <NAME> on 17/04/19.
//  Copyright (c) 2019 Rafael. All rights reserved.
//
//  This file was generated by the Clean Swift Xcode Templates so
//  you can apply clean architecture to your iOS and Mac projects,
//  see http://clean-swift.com
//

import UIKit

protocol HomeBusinessLogic
{
    func fetchDiscovered(request: Home.Fetch.Request)
    func fetchTrends(request: Home.Fetch.Request)
}

protocol HomeDataStore
{
    //var name: String { get set }
}

class HomeInteractor: HomeBusinessLogic, HomeDataStore
{
    var presenter: HomePresentationLogic?
    var trendsPresenter: HomePresentationLogic?
    var discoverWorker: HomeWorker?
    var trendsWorker: HomeWorker?
    //var name: String = ""
    
    // MARK: Do something
    
    func fetchDiscovered(request: Home.Fetch.Request) {
        discoverWorker = HomeWorker()
        discoverWorker?.fetchDiscovers(completion: { (response) in
            self.presenter?.presentSuccessDiscover(response: response)
        }, failure: { (error) in
            self.presenter?.presentFailedDiscover(error: error)
        })
    }
    
    func fetchTrends(request: Home.Fetch.Request) {
        trendsWorker = HomeWorker()
        trendsWorker?.fetchTrends(completion: { (response) in
            self.presenter?.presentSuccessTrends(response: response)
        }, failure: { (error) in
            self.presenter?.presentFailedTrends(error: error)
        })
    }
}
<file_sep>//
//  HomeModels.swift
//  TheMovieApp
//
//  Created by <NAME> on 17/04/19.
//  Copyright (c) 2019 Rafael. All rights reserved.
//
//  This file was generated by the Clean Swift Xcode Templates so
//  you can apply clean architecture to your iOS and Mac projects,
//  see http://clean-swift.com
//

import UIKit

struct Home
{
  // MARK: Use cases
  struct Fetch
  {
    class Request : GenericRequest {}
    
    class ResponseDiscover : ResponseGeneric<DiscoverEntity>{}
    class DiscoverEntity : GenericMovie{}
    
    class ResponseTrending : ResponseGeneric<TrendingEntity>{}
    class TrendingEntity : GenericMovie{}
  }
}
<file_sep>//
//  API.swift
//  TheMovieApp
//
//  Created by <NAME> on 17/04/19.
//  Copyright © 2019 Rafael. All rights reserved.
//

import Foundation

class Api {
    static let BASE_URL = "https://api.themoviedb.org/3/"
    static let API_KEY = "<KEY>"
}
<file_sep>//
//  GenericError.swift
//  TheMovieApp
//
//  Created by <NAME> on 17/04/19.
//  Copyright © 2019 Rafael. All rights reserved.
//

import Foundation

struct GenericError {
    var cod: Int?
    var message: String?
}
<file_sep>//
//  ServiceClient.swift
//  TheMovieApp
//
//  Created by <NAME> on 17/04/19.
//  Copyright © 2019 Rafael. All rights reserved.
//

import Foundation

enum Method : String {
    case GET = "GET"
    case POST = "POST"
}

class ServiceClient<T : Codable> {
    
    typealias Completion = ((T) -> Void)
    typealias Failure = ((_ code: Int, _ message: String) -> Void)
    
    private let BASE_URL: String
    
    init(baseUrl: String) {
        self.BASE_URL = baseUrl
    }
    
    private func url(resource: String) -> URL? {
        let url_string = BASE_URL + resource
        guard let URL = URL(string: url_string) else {
            return nil
        }
        return URL
    }
    
    func fetch(resource: String, method: Method, queryParams: Dictionary<String, String>, completion: @escaping Completion, failure: @escaping Failure) {
        
        var urlQuery = String()
        for param in queryParams {
            urlQuery = urlQuery + "\(param.key)=\(param.value)&"
        }
        
        urlQuery = String(urlQuery.dropLast())
        
        guard let url = url(resource: "\(resource)?\(urlQuery)") else {
            return
        }
        
        var urlRequest = URLRequest(url: url)
        urlRequest.httpMethod = method.rawValue
        
        let session = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
            if let error = error {
                if let httpResponse = response as? HTTPURLResponse {
                   failure(httpResponse.statusCode, error.localizedDescription)
                }
            }
            if let data = data {
                do {
                    let json = try JSONDecoder().decode(T.self, from: data)
                    DispatchQueue.main.async {
                        completion(json)
                    }
                } catch let error {
                    DispatchQueue.main.async {
                        failure(1, error.localizedDescription)
                    }
                }
            }
        }
        session.resume()
    }
}
<file_sep>//
//  HomeViewController.swift
//  TheMovieApp
//
//  Created by <NAME> on 17/04/19.
//  Copyright (c) 2019 Rafael. All rights reserved.
//
//  This file was generated by the Clean Swift Xcode Templates so
//  you can apply clean architecture to your iOS and Mac projects,
//  see http://clean-swift.com
//

import UIKit

protocol HomeDisplayLogic: class
{
    func successDisplayDiscoveredMovies(response: Home.Fetch.ResponseDiscover)
    func failureDisplayDiscoveredMovies(error: GenericError)
    
    func successDisplayTrendsMovies(response: Home.Fetch.ResponseTrending)
    func failureDisplayTrendsMovies(error: GenericError)
}

class HomeViewController: UIViewController, HomeDisplayLogic
{
    let nibControllerName = "HomeViewController"
    
    var interactor: HomeBusinessLogic?
    var router: (NSObjectProtocol & HomeRoutingLogic & HomeDataPassing)?
    
    // MARK: Object lifecycle
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
    {
        super.init(nibName: nibControllerName, bundle: Bundle.main)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
        setup()
    }
    
    // MARK: Setup
    
    private func setup()
    {
        let viewController = self
        let interactor = HomeInteractor()
        let presenter = HomePresenter()
        let router = HomeRouter()
        viewController.interactor = interactor
        viewController.router = router
        interactor.presenter = presenter
        presenter.viewController = viewController
        router.viewController = viewController
        router.dataStore = interactor
    }
    
    // MARK: Routing
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if let scene = segue.identifier {
            let selector = NSSelectorFromString("routeTo\(scene)WithSegue:")
            if let router = router, router.responds(to: selector) {
                router.perform(selector, with: segue)
            }
        }
    }
    
    // MARK: View lifecycle
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        doSomething()
    }
    
    // MARK: Do something
    
    //@IBOutlet weak var nameTextField: UITextField!
    
    func doSomething()
    {
        let request = Home.Fetch.Request()
        interactor?.fetchDiscovered(request: request)
        
        interactor?.fetchTrends(request: request)
    }
    
    func successDisplayDiscoveredMovies(response: Home.Fetch.ResponseDiscover)
    {
        for i in response.results {
            print("DEU BOM NESSA MERDA \(i.title)")
        }
    }
    func failureDisplayDiscoveredMovies(error: GenericError) {
        print("ERROUUU \(error.cod ?? 0)")
    }
    
    func successDisplayTrendsMovies(response: Home.Fetch.ResponseTrending) {
        for i in response.results {
            print("DEU BOM NESSA MERDA DE TRENDS \(i.title)")
        }
    }
    
    func failureDisplayTrendsMovies(error: GenericError) {
        print("ERROUUU \(error.cod ?? 0)")
    }
}
