Advertisement
Guest User

FlickrPhotosViewController

a guest
Jan 10th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.42 KB | None | 0 0
  1. //
  2. //  FlickerPhotosViewController.swift
  3. //  MyFlicker1
  4. //
  5. //  Created by דקל איפרגן on 10/01/2019.
  6. //  Copyright © 2019 Dekel. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11.  
  12.  
  13. class FlickerPhotosViewController: UICollectionViewController {
  14.    
  15.     private let reuseIdentifier = "FlickerCell"
  16.     private let sectionInset = UIEdgeInsets(top: 50.0, left: 20.0, bottom: 50.0, right: 20.0)
  17.     private var searches:[FlickrSearchResults] = []
  18.     private let flickr = Flickr()
  19.    
  20.    
  21.     override func viewDidLoad() {
  22.         super.viewDidLoad()
  23.  
  24.         // Uncomment the following line to preserve selection between presentations
  25.         // self.clearsSelectionOnViewWillAppear = false
  26.  
  27.         // Register cell classes
  28.         self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
  29.  
  30.         // Do any additional setup after loading the view.
  31.     }
  32.  
  33.     /*
  34.     // MARK: - Navigation
  35.  
  36.     // In a storyboard-based application, you will often want to do a little preparation before navigation
  37.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  38.         // Get the new view controller using [segue destinationViewController].
  39.         // Pass the selected object to the new view controller.
  40.     }
  41.     */
  42.  
  43.     // MARK: UICollectionViewDataSource
  44.  
  45.     override func numberOfSections(in collectionView: UICollectionView) -> Int {
  46.        
  47.         return searches.count
  48.     }
  49.  
  50.  
  51.     override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  52.        
  53.         return searches[section].searchResults.count
  54.     }
  55.  
  56.     override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  57.         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
  58.    
  59.         // Configure the cell
  60.         cell.backgroundColor = .black
  61.         return cell
  62.     }
  63.  
  64.     // MARK: UICollectionViewDelegate
  65.  
  66.     /*
  67.     // Uncomment this method to specify if the specified item should be highlighted during tracking
  68.     override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
  69.         return true
  70.     }
  71.     */
  72.  
  73.     /*
  74.     // Uncomment this method to specify if the specified item should be selected
  75.     override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
  76.         return true
  77.     }
  78.     */
  79.  
  80.     /*
  81.     // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
  82.     override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
  83.         return false
  84.     }
  85.  
  86.     override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
  87.         return false
  88.     }
  89.  
  90.     override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
  91.    
  92.     }
  93.     */
  94.  
  95. }
  96.  
  97.  
  98.  
  99. private extension FlickerPhotosViewController{
  100.     func photo(for indexPath: IndexPath) -> FlickrPhoto{
  101.         return searches[indexPath.section].searchResults[indexPath.row]
  102.     }
  103. }
  104.  
  105. extension FlickerPhotosViewController : UITextFieldDelegate {
  106.     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  107.         //1
  108.         let activityIndicator = UIActivityIndicatorView(style: .gray)
  109.         activityIndicator.frame = textField.bounds
  110.         activityIndicator.startAnimating()
  111.        
  112.         flickr.searchFlickr(for: textField.text!){
  113.             searchResults in
  114.             activityIndicator.removeFromSuperview()
  115.            
  116.             switch searchResults{
  117.             case .error(let error):
  118.                 //2
  119.                 print("error searching: \(error)")
  120.                 //3
  121.             case .results(let results):
  122.                 print("found \(results.searchResults.count) matching \(results.searchTerm)")
  123.                 self.searches.insert(results, at: 0)
  124.                 //4
  125.                 self.collectionView?.reloadData()
  126.                
  127.             }
  128.         }
  129.         textField.text = nil
  130.         textField.resignFirstResponder()
  131.         return true
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement