Monday, August 15, 2022
HomeiOS Developmentios - Working with VIPER shedding information within the View

ios – Working with VIPER shedding information within the View

[ad_1]

Greetings after I cross my information from the interactor to view, I can catch it within the func “replace” I assign the infoRes however in the mean time to assign this information to the desk and even name in viewdidload all information is nil
im shedding the information in some level however i dont know the place, might you please assist me

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 format = UICollectionViewFlowLayout()
      format.sectionInset = UIEdgeInsets(high: 5, left: 0, backside: 5, proper: 0)
      format.estimatedItemSize = .zero
        let collectionview = UICollectionView(body: CGRect(x: 0, y: 0, width: 0, top: 0), collectionViewLayout: format)
      format.minimumLineSpacing = 10
      format.minimumInteritemSpacing = 10
      format.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 Reveals"
    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.fundamental.async {
        self.infoRes = resul
        //print("(self.infoRes?.outcomes.depend)")
    }
    //print("(infoRes?.outcomes.depend)")
}

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, format collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 160, top: 350)
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
    return infoRes?[0].outcomes.depend ?? 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, format collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat {
    5
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    DispatchQueue.fundamental.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.fundamental.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: "record.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 wish to do?", preferredStyle: .actionSheet)
            alert.addAction(UIAlertAction(title: "View Profile", model: .default, handler: { (_) in
                DispatchQueue.fundamental.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: "Log off", model: .harmful, handler: { (_) in
                self.navigationController?.popToRootViewController(animated: true)
                               DispatchQueue.fundamental.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", model: .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 activity = URLSession.shared.dataTask(with: url) { information, response, error in
        if let information = information {
            if let res = attempt? JSONDecoder().decode(Welcome.self, from: information) {
                print("interactor (res.outcomes.depend)")
                    self.presenter?.view?.replace(with: [res])
            } else {
                print("Invalid Response")
            }
        } else if let error = error {
            print("HTTP Request Failed (error)")
        }
    }
    activity.resume()
}

[ad_2]

RELATED ARTICLES

Most Popular

Recent Comments