[ad_1]
Greatings when i cross my knowledge from the interactor to view, i can catch it within the func “replace” i assign the infoRes however on the momento to assign this knowledge to the tableview and even name in viewdidload all knowledge is nil
i share my code of viewcontroller and interactor
protocol HomeAnyView {
var presenter: HomeAnyPresenter? { get set }
func replace(with resul: [Welcome])
func replace(with error: String)
}
class HomeViewController: UIViewController, HomeAnyView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var presenter: HomeAnyPresenter?
var window: UIWindow?
var infoRes: [Welcome]?
let segmentMenu: UISegmentedControl = {
let segmenu = UISegmentedControl(objects: ["Popular", "Top Rated", "On Tv", "Airing Today"])
segmenu.backgroundColor = .darkGray
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
segmenu.setTitleTextAttributes(titleTextAttributes, for: .regular)
segmenu.setTitleTextAttributes(titleTextAttributes, for: .chosen)
segmenu.selectedSegmentIndex = 0
segmenu.selectedSegmentTintColor = UIColor.systemGray4
segmenu.translatesAutoresizingMaskIntoConstraints = false
segmenu.addTarget(self, motion: #selector(HomeViewController.indexChanged(_:)), for: .valueChanged)
return segmenu
}()
let collectionview :UICollectionView = {
let structure = UICollectionViewFlowLayout()
structure.sectionInset = UIEdgeInsets(high: 5, left: 0, backside: 5, proper: 0)
structure.estimatedItemSize = .zero
let collectionview = UICollectionView(body: CGRect(x: 0, y: 0, width: 0, top: 0), collectionViewLayout: structure)
structure.minimumLineSpacing = 10
structure.minimumInteritemSpacing = 10
structure.scrollDirection = .vertical
collectionview.translatesAutoresizingMaskIntoConstraints = false
collectionview.register(MovieCollectionViewCell.self, forCellWithReuseIdentifier: MovieCollectionViewCell.identifier)
collectionview.showsVerticalScrollIndicator = false
collectionview.backgroundColor = UIColor.clear
collectionview.layer.cornerRadius = 15
collectionview.layoutIfNeeded()
return collectionview
}()
override func viewDidLoad() {
tremendous.viewDidLoad()
navigationController?.navigationBar.tintColor = .white
view.backgroundColor = .systemGray5
title = "TV Exhibits"
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
setupViewContraints()
btnNavBar()
collectionview.delegate = self
collectionview.dataSource = self
getMovies()
}
@objc func indexChanged(_ sender: UISegmentedControl) {
swap segmentMenu.selectedSegmentIndex {
case 0:
print("0")
case 1:
print("1")
case 2:
print("2")
case 3:
print("3")
default:
print("default")
}
}
func replace(with resul: [Welcome]) {
DispatchQueue.predominant.async {
self.infoRes = resul
//print("(self.infoRes?.outcomes.rely)")
}
//print("(infoRes?.outcomes.rely)")
}
func replace(with error: String) {
print("omgError")
}
func formattedDateFromString(dateString: String, withFormat format: String) -> String? {
let inputFormatter = DateFormatter()
inputFormatter.dateFormat = "yyyy-MM-dd"
if let date = inputFormatter.date(from: dateString) {
let outputFormatter = DateFormatter()
outputFormatter.dateFormat = format
return outputFormatter.string(from: date)
}
return nil
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 160, top: 350)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
return infoRes?[0].outcomes.rely ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionview.dequeueReusableCell(withReuseIdentifier: MovieCollectionViewCell.identifier, for: indexPath as IndexPath) as! MovieCollectionViewCell
cell.myLbl.textual content = infoRes?[0].outcomes[indexPath.row].originalTitle
cell.myImg.downloaded(from: "https://picture.tmdb.org/t/p/w500/(infoRes![0].outcomes[indexPath.row].posterPath)")
print(infoRes?[0].outcomes[indexPath.row].posterPath)
cell.myImg.clipsToBounds = true
cell.myImg.layer.cornerRadius = 30
cell.myLblDes.textual content = infoRes?[0].outcomes[indexPath.row].overview
let stringB = formattedDateFromString( dateString: infoRes?[0].outcomes[indexPath.row].releaseDate ?? "date", withFormat: "MMM dd, yyyy")
cell.myLblDate.textual content = stringB
return cell
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat {
5
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
DispatchQueue.predominant.async { [self] in
let destinationVC = ModalViewController()
destinationVC.texto1 = infoRes?[0].outcomes[indexPath.row].originalTitle ?? ""
destinationVC.texto2 = infoRes?[0].outcomes[indexPath.row].overview ?? ""
destinationVC.texto3 = infoRes?[0].outcomes[indexPath.row].voteAverage ?? 0.0
destinationVC.imgPath1 = "https://picture.tmdb.org/t/p/w500/(String(describing: infoRes?[0].outcomes[indexPath.row].posterPath))"
let stringB = formattedDateFromString( dateString: infoRes?[0].outcomes[indexPath.row].releaseDate ?? "date", withFormat: "MMM dd, yyyy")
destinationVC.texto4 = stringB ?? ""
current(destinationVC, animated: true, completion: nil)
let userRouter = ModalRouter.begin()
let initialVC = userRouter.entry
initialVC?.presenter?.view?.replace(with: infoRes?[0].outcomes[indexPath.row].originalTitle ?? "")
let window = UIWindow()
window.rootViewController = initialVC
self.window = window
window.makeKeyAndVisible()
}
}
func newView(onViewC: UIViewController) {
DispatchQueue.predominant.async {
let navController = UINavigationController(rootViewController: onViewC)
navController.modalPresentationStyle = .popover
navController.navigationBar.backgroundColor = .clear
self.current(navController, animated: true, completion: nil)
}
}
}
extension HomeViewController{
func btnNavBar(){
let button = UIButton(kind: UIButton.ButtonType.customized)
button.setImage(UIImage(systemName: "listing.sprint"), for: .regular)
button.body = CGRect(x: 0, y: 0, width: 30, top: 30)
button.addTarget(self, motion: #selector(menuItem), for: .touchDown)
button.showsMenuAsPrimaryAction = true
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItems = [barButton]
}
@objc func menuItem() {
let alert = UIAlertController(title: "", message: "what do you need to do?", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "View Profile", fashion: .default, handler: { (_) in
DispatchQueue.predominant.async {
self.current(ProfileViewController(), animated: true, completion: nil)
let userRouter = ProfileRouter.begin()
let initialVC = userRouter.entry
let window = UIWindow()
window.rootViewController = initialVC
self.window = window
window.makeKeyAndVisible()
}
}))
alert.addAction(UIAlertAction(title: "Sign off", fashion: .harmful, handler: { (_) in
self.navigationController?.popToRootViewController(animated: true)
DispatchQueue.predominant.async {
let navController = UINavigationController(rootViewController: LogViewController())
navController.modalPresentationStyle = .fullScreen
navController.navigationBar.backgroundColor = .clear
self.current(navController, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Cancel", fashion: .cancel, handler: { (_) in
print("Consumer click on Dismiss button")
}))
self.current(alert, animated: true, completion: {
print("completion block")
})
}
func setupViewContraints() {
view.addSubview(segmentMenu)
view.addSubview(collectionview)
segmentMenu.translatesAutoresizingMaskIntoConstraints = false
segmentMenu.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, fixed: 30).isActive = true
segmentMenu.centerXAnchor.constraint(equalTo: view.centerXAnchor, fixed: 0).isActive = true
segmentMenu.leftAnchor.constraint(equalTo: view.leftAnchor, fixed: 20).isActive = true
collectionview.translatesAutoresizingMaskIntoConstraints = false
collectionview.topAnchor.constraint(equalTo: segmentMenu.bottomAnchor, fixed: 20).isActive = true
collectionview.centerXAnchor.constraint(equalTo: view.centerXAnchor, fixed: 0).isActive = true
collectionview.leftAnchor.constraint(equalTo: view.leftAnchor, fixed: 15).isActive = true
collectionview.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, fixed: 0).isActive = true
}
}
That is the interactor code
var presenter: HomeAnyPresenter?
func getMovies() {
print("entraGetMovies")
guard let url = URL(string: "https://api.themoviedb.org/3/film/standard?api_key=34c5d1ffcfea821c6c7269f28caafa11&language=en-US&web page=1") else { return }
let process = URLSession.shared.dataTask(with: url) { knowledge, response, error in
if let knowledge = knowledge {
if let res = attempt? JSONDecoder().decode(Welcome.self, from: knowledge) {
print("interactor (res.outcomes.rely)")
self.presenter?.view?.replace(with: [res])
} else {
print("Invalid Response")
}
} else if let error = error {
print("HTTP Request Failed (error)")
}
}
process.resume()
}
[ad_2]
