// // ExploreTableViewController.swift // Gallerink // // Created by Denis Lunyov on 1/20/17. // Copyright © 2017 Denis Lunyov. All rights reserved. // import UIKit import Crashlytics class ExploreTableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate, UISearchControllerDelegate { @IBOutlet weak var recentTitleLabel: UILabel! @IBOutlet weak var recentAllButton: UIButton! @IBOutlet weak var blockedUsersButton: UIButton! @IBOutlet weak var recentCollectionView: UICollectionView! @IBOutlet weak var recentPlaceholder: UIView! @IBOutlet weak var recentPlaceholderLabel: UILabel! @IBOutlet weak var stylesTitleLabel: UILabel! @IBOutlet weak var stylesAllButton: UIButton! @IBOutlet weak var stylesCollectionView: UICollectionView! @IBOutlet weak var categoriesTitleLabel: UILabel! @IBOutlet weak var categoriesAllButton: UIButton! @IBOutlet weak var categoriesCollectionView: UICollectionView! @IBOutlet weak var followingTitleLabel: UILabel! @IBOutlet weak var followingAllButton: UIButton! @IBOutlet weak var followingFindButton: UIButton! @IBOutlet weak var followingCollectionView: UICollectionView! @IBOutlet weak var followingPlaceholder: UIView! @IBOutlet weak var followingPlaceholderLabel: UILabel! let stylesArray = [9,11,8,2,3,10] let categoriesArray = [11,5,10,6] var recentPostsArray: Array? var followingPostsArray: Array? var searchBar = UISearchBar() var searchController: UISearchController! var cellsCount: Int = 0 let spinner = UIActivityIndicatorView() var recentIsDownloaded: Bool = false var followingIsDownloaded: Bool = false { didSet { if (recentIsDownloaded == true) || (followingIsDownloaded == true) { stopActivityIndicator() } } } override func viewDidLoad() { super.viewDidLoad() UIApplication.shared.statusBarStyle = .lightContent self.title = NSLocalizedString("exploreLabel", comment: "") self.refreshControl?.tintColor = UIColor(red: (222/255), green: (222/255), blue: (222/255), alpha: 1) self.refreshControl?.backgroundColor = UIColor.white self.refreshControl?.addTarget(self, action: #selector(refresh), for: UIControlEvents.valueChanged) // Recent Cell recentTitleLabel.text = NSLocalizedString("recentLabel", comment: "") recentTitleLabel.text = recentTitleLabel.text?.uppercased() recentAllButton.setTitle(NSLocalizedString("allButton", comment: ""), for: UIControlState.normal) blockedUsersButton.setTitle(NSLocalizedString("blockedUsersLabel2", comment: ""), for: UIControlState.normal) recentPlaceholderLabel.textColor = UIColor.greyLabel() recentPlaceholderLabel.text = NSLocalizedString("blockedAllUsers", comment: "") self.recentCollectionView.register(UINib(nibName: "ImageCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ImageCollectionViewCell") recentCollectionView.delegate = self // Styles Cell stylesTitleLabel.text = NSLocalizedString("stylesLabel", comment: "") stylesTitleLabel.text = stylesTitleLabel.text?.uppercased() stylesAllButton.setTitle(NSLocalizedString("allButton", comment: ""), for: UIControlState.normal) self.stylesCollectionView.register(UINib(nibName: "StylesSmallCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "StylesSmallCollectionViewCell") stylesCollectionView.delegate = self // Categories Cell categoriesTitleLabel.text = NSLocalizedString("categoriesLabel", comment: "") categoriesTitleLabel.text = categoriesTitleLabel.text?.uppercased() categoriesAllButton.setTitle(NSLocalizedString("allButton", comment: ""), for: UIControlState.normal) self.categoriesCollectionView.register(UINib(nibName: "CategoriesSmallCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CategoriesSmallCollectionViewCell") categoriesCollectionView.delegate = self // Following Cell followingTitleLabel.text = NSLocalizedString("followingLabel", comment: "") followingTitleLabel.text = followingTitleLabel.text?.uppercased() followingAllButton.setTitle(NSLocalizedString("allButton", comment: ""), for: UIControlState.normal) followingFindButton.setTitle(NSLocalizedString("findFriendsLabel", comment: ""), for: UIControlState.normal) followingPlaceholderLabel.textColor = UIColor.greyLabel() followingPlaceholderLabel.text = NSLocalizedString("findLabel", comment: "") self.followingCollectionView.register(UINib(nibName: "ImageCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ImageCollectionViewCell") followingCollectionView.delegate = self activityIndicator(downloadPosts()) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func refresh(sender:AnyObject) { PostApiClient().getRecentPosts(limit: 6, offset: 0, success: { (recentPosts: Array) in self.recentPostsArray = recentPosts if self.recentPostsArray?.count == 0 { self.recentAllButton.isHidden = true self.recentPlaceholder.isHidden = false self.blockedUsersButton.isHidden = false } else { self.recentAllButton.isHidden = false self.recentPlaceholder.isHidden = true self.blockedUsersButton.isHidden = true } PostApiClient().getFollowingPosts(limit: 6, offset: 0, success: { (followingPosts: Array) in self.followingPostsArray = followingPosts self.followingIsDownloaded = true self.recentIsDownloaded = true if self.followingPostsArray?.count == 0 { self.followingAllButton.isHidden = true self.followingPlaceholder.isHidden = false self.followingFindButton.isHidden = false } else { self.followingAllButton.isHidden = false self.followingPlaceholder.isHidden = true self.followingFindButton.isHidden = true } self.tableView.reloadData() self.recentCollectionView.reloadData() self.followingCollectionView.reloadData() self.refreshControl?.endRefreshing() }, failure: { (Void) in self.failureAlert() }) }, failure: { (Void) in self.failureAlert() }) } // MARK: - Collection view data source func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if collectionView.tag == 1 { return recentPostsArray!.count } else if collectionView.tag == 2 { return 6 } else if collectionView.tag == 3 { return 4 } else if collectionView.tag == 4 { return followingPostsArray!.count } return 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if collectionView.tag == 1 { let cell: ImageCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell let row = (indexPath as NSIndexPath).row if recentPostsArray!.count != 0 { cell.postUuid = self.recentPostsArray![row].postUuid cell.imageView.kf.setImage(with: URL(string: "\(self.recentPostsArray![row].postImageUuid!)")) } return cell } else if collectionView.tag == 2 { let cell: StylesSmallCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "StylesSmallCollectionViewCell", for: indexPath) as! StylesSmallCollectionViewCell let row = (indexPath as NSIndexPath).row cell.styleImage.image = StylesAndCategoriesHelper.getStyle(style: stylesArray[row]).image cell.styleTitle.text = StylesAndCategoriesHelper.getStyle(style: stylesArray[row]).title return cell } else if collectionView.tag == 3 { let cell: CategoriesSmallCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoriesSmallCollectionViewCell", for: indexPath) as! CategoriesSmallCollectionViewCell let row = (indexPath as NSIndexPath).row cell.categoryImage.image = StylesAndCategoriesHelper.getCategory(category: categoriesArray[row]).image cell.categoryTitle.text = StylesAndCategoriesHelper.getCategory(category: categoriesArray[row]).title return cell } else { let cell: ImageCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell let row = (indexPath as NSIndexPath).row if followingPostsArray!.count != 0 { cell.postUuid = self.followingPostsArray![row].postUuid cell.imageView.kf.setImage(with: URL(string: "\(self.followingPostsArray![row].postImageUuid!)")) } return cell } } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if collectionView.tag == 1 { let row = (indexPath as NSIndexPath).row let storyboard = UIStoryboard(name: "Main", bundle: nil) let detailsViewController = storyboard.instantiateViewController(withIdentifier: "ExplorePostsDetailsViewController") as! ExplorePostsDetailsViewController detailsViewController.requestType = 1 detailsViewController.offset = row self.navigationController?.pushViewController(detailsViewController, animated: true) } else if collectionView.tag == 2 { let row = (indexPath as NSIndexPath).row let storyboard = UIStoryboard(name: "Main", bundle: nil) let styleViewController = storyboard.instantiateViewController(withIdentifier: "StyleViewController") as! StyleViewController styleViewController.style = stylesArray[row] self.navigationController?.pushViewController(styleViewController, animated: true) } else if collectionView.tag == 3 { let row = (indexPath as NSIndexPath).row let storyboard = UIStoryboard(name: "Main", bundle: nil) let categoryViewController = storyboard.instantiateViewController(withIdentifier: "CategoryViewController") as! CategoryViewController categoryViewController.category = categoriesArray[row] self.navigationController?.pushViewController(categoryViewController, animated: true) } else { let row = (indexPath as NSIndexPath).row let storyboard = UIStoryboard(name: "Main", bundle: nil) let detailsViewController = storyboard.instantiateViewController(withIdentifier: "ExplorePostsDetailsViewController") as! ExplorePostsDetailsViewController detailsViewController.requestType = 4 detailsViewController.offset = row self.navigationController?.pushViewController(detailsViewController, animated: true) } } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return cellsCount } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.row == 0 { return 172 } if indexPath.row == 1 { return 142 } if indexPath.row == 2 { return 172 } if indexPath.row == 3 { return 172 } return 0 } // MARK: - View buttons actions @IBAction func recentAllButtonDidTap(_ sender: Any) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier: "RecentViewController") self.navigationController?.pushViewController(vc, animated: true) } @IBAction func blockedUsersButtonDidTap(_ sender: Any) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier: "BlockedUsersViewController") self.navigationController?.pushViewController(vc, animated: true) } @IBAction func stylesAllButtonDidTap(_ sender: Any) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier: "StylesViewController") self.navigationController?.pushViewController(vc, animated: true) } @IBAction func categoriesAllButtonDidTap(_ sender: Any) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier: "CategoriesViewController") self.navigationController?.pushViewController(vc, animated: true) } @IBAction func followingAllButtonDidTap(_ sender: Any) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier: "FollowingViewController") self.navigationController?.pushViewController(vc, animated: true) } @IBAction func followingFindButtonDidTap(_ sender: Any) { showSearchBar() } @IBAction func searchButtonDidTap(_ sender: AnyObject) { showSearchBar() } func showSearchBar() { Answers.logSearch(withQuery: "mobile analytics", customAttributes: [:]) // Result Table let searchResultController = storyboard!.instantiateViewController(withIdentifier: "SearchResultTableViewController") as! SearchResultTableViewController searchController = UISearchController(searchResultsController: searchResultController) searchController.searchResultsUpdater = searchResultController // Search Controller searchController.hidesNavigationBarDuringPresentation = false searchController.searchBar.barTintColor = UIColor.panelsColor() searchController.searchBar.tintColor = UIColor.mainColor() searchController.searchBar.isTranslucent = false let textField = searchController.searchBar.value(forKey: "searchField") as! UITextField textField.backgroundColor = UIColor(red: 30/255, green: 36/255, blue: 51/255, alpha: 1) textField.textColor = UIColor.white self.searchController.searchBar.delegate = self present(searchController, animated: true, completion: nil) searchController.delegate = self // Hide Tab Bar UIView.animate(withDuration: 1) { self.tabBarController?.tabBar.alpha = 0.0 } searchResultController.selectedProfile = { profileUuid in UIView.animate(withDuration: 0.3) { self.tabBarController?.tabBar.alpha = 1.0 } self.dismiss(animated: true, completion: nil) let storyboard = UIStoryboard(name: "Main", bundle: nil) let userProfileTableViewController = storyboard.instantiateViewController(withIdentifier: "UserProfileTableViewController") as! UserProfileTableViewController userProfileTableViewController.profileUuid = profileUuid self.navigationController?.pushViewController(userProfileTableViewController, animated: true) } } func willDismissSearchController(_ searchController: UISearchController) { UIView.animate(withDuration: 0.3) { self.tabBarController?.tabBar.alpha = 1.0 } } func didDismissSearchController(_ searchController: UISearchController) { UIView.animate(withDuration: 0.3) { self.tabBarController?.tabBar.alpha = 1.0 } } func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { UIView.animate(withDuration: 0.3) { self.tabBarController?.tabBar.alpha = 1.0 } } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { let searchResultTableViewController = searchController.searchResultsController as! SearchResultTableViewController searchResultTableViewController.requestString = searchText } func activityIndicator() { let width: CGFloat = 30 let height: CGFloat = 30 let x = (self.view.frame.width / 2) - (width / 2) let y = (self.view.frame.height / 2) - (height / 2) - (self.tabBarController?.tabBar.frame.height)! - 10 self.spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray self.spinner.frame = CGRect(x: x, y: y, width: 30, height: 30) self.view.addSubview(self.spinner) self.spinner.startAnimating() tableView.isScrollEnabled = false } func stopActivityIndicator() { self.spinner.stopAnimating() self.spinner.hidesWhenStopped = true cellsCount = 4 tableView.reloadData() tableView.isScrollEnabled = true } func downloadPosts() { PostApiClient().getRecentPosts(limit: 6, offset: 0, success: { (recentPosts: Array) in self.recentPostsArray = recentPosts if self.recentPostsArray?.count == 0 { self.recentAllButton.isHidden = true self.recentPlaceholder.isHidden = false self.blockedUsersButton.isHidden = false } else { self.recentAllButton.isHidden = false self.recentPlaceholder.isHidden = true self.blockedUsersButton.isHidden = true } PostApiClient().getFollowingPosts(limit: 6, offset: 0, success: { (followingPosts: Array) in self.followingPostsArray = followingPosts self.followingIsDownloaded = true self.recentIsDownloaded = true if self.followingPostsArray?.count == 0 { self.followingAllButton.isHidden = true self.followingPlaceholder.isHidden = false self.followingFindButton.isHidden = false } else { self.followingAllButton.isHidden = false self.followingPlaceholder.isHidden = true self.followingFindButton.isHidden = true } self.tableView.reloadData() }, failure: { (Void) in self.failureAlert() }) }, failure: { (Void) in self.failureAlert() }) } func failureAlert() { let alertController = UIAlertController(title: NSLocalizedString("errorLabel", comment: ""), message: NSLocalizedString("serverError", comment: ""), preferredStyle: .alert) let OKAction = UIAlertAction(title: "OK", style: .default) { (action) in } alertController.addAction(OKAction) self.present(alertController, animated: true) } }