Untitled
unknown
swift
3 years ago
4.3 kB
34
Indexable
// // DetailedVC.swift // DownloadImageWithAPI // // Created by Vlad Katsubo on 6.10.22. // import UIKit import RealmSwift class DetailedVC: UIViewController { let realm = try! Realm() lazy var layout: DetailedVCLayout = .init() var unsplashPhotoManager = UnsplashPhotoManager() var photoObject: PhotoModel? = nil var fromSearchCollection = false var fullScreen: Bool = false override func loadView() { super.loadView() view = layout } override func viewDidLoad() { super.viewDidLoad() unsplashPhotoManager.delegate = self fetchDownloadsLocationInfo(fromSearchCollection) configureNavBar() } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) self.hidesBottomBarWhenPushed = true } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func configureNavBar() { navigationItem.title = photoObject?.authorName let info = UIBarButtonItem(image: UIImage(systemName: Symbols.info.rawValue), style: .plain, target: self, action: #selector(showInfo)) let favorite = UIBarButtonItem(image: UIImage(systemName: Symbols.favorite.rawValue), style: .plain, target: self, action: #selector(addToFavorite)) navigationItem.rightBarButtonItems = [favorite, info] } //show infoVC at the bottom of the page @objc func showInfo() { let infoVC = InfoVC() infoVC.modalPresentationStyle = .custom infoVC.transitioningDelegate = self infoVC.photoObject = photoObject present(infoVC, animated: true) } // add photo to the RealmDB and FavoriteVC @objc func addToFavorite() { guard let photoObject = photoObject else { return } if realm.objects(PhotoModel.self).contains(where: { $0.id == photoObject.id }) { deleteFromDB(photoObject) } else { saveToDB(photoObject) } } func fetchDownloadsLocationInfo(_ proceed: Bool) { if proceed { if let photoObject = photoObject { if photoObject.downloads == nil { unsplashPhotoManager.getInfoForID(with: photoObject.id) } } } } //MARK: - Hide NavigationBar on Tap to make image fullscreen override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.hidesBarsOnTap = true } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) navigationController?.hidesBarsOnTap = false } //MARK: - Make Photo "fullscreen" override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { scaleAspect() } func scaleAspect() { fullScreen.toggle() layout.photoView.contentMode = fullScreen ? .scaleAspectFill : .scaleAspectFit } } //MARK: - Extension to present InfoVC in DetailedVC extension DetailedVC: UIViewControllerTransitioningDelegate { func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? { PresentationController(presentedViewController: presented, presenting: presenting) } } extension DetailedVC: UnsplashDataDelegate { func updateUI(with photoModels: [PhotoModel]) { self.photoObject?.location = photoModels[0].location self.photoObject?.downloads = photoModels[0].downloads } } //MARK: - Realm Database methods extension DetailedVC { func saveToDB(_ photoModel: PhotoModel) { do { try realm.write({ realm.add(photoModel) }) } catch { print("Error while saving photo data: \(error)") } } func deleteFromDB(_ photoModel: PhotoModel) { do { try realm.write { realm.delete(photoModel) } } catch { print("Error while deleting object from arra: \(error)") } } }
Editor is loading...