Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FlickerPhotosViewController.swift
- // MyFlicker1
- //
- // Created by דקל איפרגן on 10/01/2019.
- // Copyright © 2019 Dekel. All rights reserved.
- //
- import UIKit
- class FlickerPhotosViewController: UICollectionViewController {
- private let reuseIdentifier = "FlickerCell"
- private let sectionInset = UIEdgeInsets(top: 50.0, left: 20.0, bottom: 50.0, right: 20.0)
- private var searches:[FlickrSearchResults] = []
- private let flickr = Flickr()
- override func viewDidLoad() {
- super.viewDidLoad()
- // Uncomment the following line to preserve selection between presentations
- // self.clearsSelectionOnViewWillAppear = false
- // Register cell classes
- self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
- // Do any additional setup after loading the view.
- }
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- // MARK: UICollectionViewDataSource
- override func numberOfSections(in collectionView: UICollectionView) -> Int {
- return searches.count
- }
- override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return searches[section].searchResults.count
- }
- override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
- // Configure the cell
- cell.backgroundColor = .black
- return cell
- }
- // MARK: UICollectionViewDelegate
- /*
- // Uncomment this method to specify if the specified item should be highlighted during tracking
- override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
- return true
- }
- */
- /*
- // Uncomment this method to specify if the specified item should be selected
- override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
- return true
- }
- */
- /*
- // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
- return false
- }
- override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
- return false
- }
- override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
- }
- */
- }
- private extension FlickerPhotosViewController{
- func photo(for indexPath: IndexPath) -> FlickrPhoto{
- return searches[indexPath.section].searchResults[indexPath.row]
- }
- }
- extension FlickerPhotosViewController : UITextFieldDelegate {
- func textFieldShouldReturn(_ textField: UITextField) -> Bool {
- //1
- let activityIndicator = UIActivityIndicatorView(style: .gray)
- activityIndicator.frame = textField.bounds
- activityIndicator.startAnimating()
- flickr.searchFlickr(for: textField.text!){
- searchResults in
- activityIndicator.removeFromSuperview()
- switch searchResults{
- case .error(let error):
- //2
- print("error searching: \(error)")
- //3
- case .results(let results):
- print("found \(results.searchResults.count) matching \(results.searchTerm)")
- self.searches.insert(results, at: 0)
- //4
- self.collectionView?.reloadData()
- }
- }
- textField.text = nil
- textField.resignFirstResponder()
- return true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement