ActivityTableViewController.swift 8.04 KB
//
//  ActivityTableViewController.swift
//  styleteqios
//
//  Created by Alfonz Montelibano on 5/10/16.
//  Copyright © 2016 Allejo Chris Velarde. All rights reserved.
//

import UIKit

// This is a parent class extended by MainActivityViewController (activity feeds screen)
// and NotificationsTableViewController (notifications screen)

class ActivityTableViewController: UIViewController, WithProcessIndicatorController {
	
	let activityTableViewCellIdentifier = "activityTableViewCell"
	let specialActivityTableViewCellIdentifier = "specialActivityTableViewCell"
	var activityItems: [Activity] = []
    
    var adjustedHeights: [Int:CGFloat] = [:]
    
	
	// Used for pagination
	var currentPage: Int = 0
    var emptyCollectionLabel = UILabel()
    var refreshControl: UIRefreshControl!
	
	// Process Indicator
	var indicator = UIActivityIndicatorView()
	var processIndicator: UIActivityIndicatorView {
		get {
			return self.indicator
		}
	}
    
    @IBOutlet weak var tableView: UITableView!
	
	override func viewWillAppear(_ animated: Bool) {
		super.viewWillAppear(animated)
        
        self.clear()
        self.refreshControl?.beginRefreshing()
        self.fetchActivities()
        
        tableView.reloadData()
	}
    
    
	
	override func viewDidLoad() {
		super.viewDidLoad()
        
        
		
		self.view.backgroundColor = Theme.defaultBackgroundColor()
		
		self.registerCellNibs()
		
		self.tableView.tableFooterView = UIView()
        self.tableView.tableHeaderView = UIView()
        
        self.emptyCollectionLabel = UILabel(frame: CGRect(x: 20, y: 0, width: self.view.bounds.width - 40, height: self.view.bounds.height))
        self.emptyCollectionLabel.textAlignment = .center
        self.emptyCollectionLabel.numberOfLines = 0
        self.emptyCollectionLabel.isHidden = true
        self.view.addSubview(self.emptyCollectionLabel)
		
		self.activityItems = [Activity]()
		
		self.initProcessIndicator(self.view)
		
        self.refreshControl = UIRefreshControl()
		self.refreshControl?.addTarget(self, action: #selector(ActivityTableViewController.handleRefresh(_:)), for: UIControl.Event.valueChanged)
		self.refreshControl?.tintColor = UIColor.lightGray
        self.tableView.addSubview(self.refreshControl)
		
		
	}
    
    func registerCellNibs()
    {
        self.tableView.register(UINib(nibName: "ActivityTableViewCell", bundle: nil), forCellReuseIdentifier: self.activityTableViewCellIdentifier)
        self.tableView.register(UINib(nibName: "ActivitySpecialCellView", bundle: nil), forCellReuseIdentifier: self.specialActivityTableViewCellIdentifier)
        self.tableView.register(UINib(nibName: "StyleActivityTableViewCell", bundle: nil), forCellReuseIdentifier: "styleActivityTableViewCell")
    }
    
    func fetchActivities(){
		
	}
	
	@objc func handleRefresh(_ refreshControl: UIRefreshControl) {
		self.clear()
        self.fetchActivities()
		
	}
    
    func clear()
    {
        if nil != self.tableView {
            self.activityItems.removeAll()
            self.tableView.reloadData()
            self.currentPage = 1
        }
    }
	
	
}

extension ActivityTableViewController: UITableViewDelegate, UITableViewDataSource
{
    // MARK: Table View Delegate Functions
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let activityItem = self.activityItems[indexPath.row]
        
        switch activityItem.activityType{
        case .Style:
            self.loadStylePublicView(activityItem.object as! Style)
            break
            
        case .Special:
            self.loadSpecialPublicView(activityItem.object as! Special)
            break
            
        case .Booth:
            self.loadBoothPublicView(activityItem.object as! Booth)
            break
            
        case .User:
            self.loadUserProfilePublicView(activityItem.object as! User)
            break
        case .Salon:
            self.loadSalonPublicView(activityItem.object as! Salon)
        case .Page:
            self.loadPublicNotificationAnnouncementPage(activityItem.object as! NotificationAnnouncementPage)
            break
        case .Booking:
            guard let booking = activityItem.object as? Booking else {
                break
            }            
            self.loadBookingDetailView(booking)
            break
        default:
            break
        }
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if self.isLastIndexPath(indexPath) {
            self.fetchActivities()
        }
    }
    
    
    
    
    func isLastIndexPath(_ indexPath: IndexPath) -> Bool {
        let lastSectionIndex = self.tableView.numberOfSections - 1
        let lastItemIndex = self.tableView.numberOfRows(inSection: lastSectionIndex) - 1
        
        return (lastSectionIndex == indexPath.section && lastItemIndex == indexPath.row)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let activity = self.activityItems[indexPath.row]
        
        switch activity.activityType {
        case .Special:
            let cell = tableView.dequeueReusableCell(withIdentifier: self.specialActivityTableViewCellIdentifier) as! ActivitySpecialCellView
            cell.loadActivityItem(activity)
            cell.loadSpecial(activity.object as! Special)
            cell.delegate = self
            return cell
            
        case .Style:
            let cell = tableView.dequeueReusableCell(withIdentifier: "styleActivityTableViewCell") as! StyleActivityTableViewCell
            cell.loadActivityItem(activity)
            cell.delegate = self
            
            return cell
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: activityTableViewCellIdentifier) as! ActivityTableViewCell
            cell.loadActivityItem(activity)
            cell.delegate = self
            
            return cell
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.activityItems.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let activity = self.activityItems[indexPath.row]
        switch activity.activityType {
        case .Special:
            return 160
        case .Style:
            guard let preloadedSize = activity.carouselImageAspectSize, let itemStyle = activity.object as? Style else {
                return UIScreen.main.bounds.size.width + 100
            }
            
            let labelHeight = itemStyle.description.height(withConstrainedWidth: UIScreen.main.bounds.width, UIFont.systemFont(ofSize: 14))
            
            return preloadedSize.height + 64 + labelHeight
        default:
            
            if (activity as? NotificationEntity) != nil {
                return 80
            }
            
            if activity.withImage == false {
                return UIScreen.main.bounds.size.width + 100
            }
            else {
                guard let preloadedSize = activity.carouselImageAspectSize else {
                    return UIScreen.main.bounds.size.width + 100
                }
                
                return preloadedSize.height + 64
            }
            
        }
        
    }
    
    
}

extension ActivityTableViewController: ActivityTableViewCellDelegate {
    func shouldRefreshCell(_ cell: ActivityTableViewCell) {
    }
    
    func pushController(_ viewController: UIViewController) {
        self.navigationController?.pushViewController(viewController, animated: true)
    }
    func shouldRedirectToProfile(_ username: String) {
        UserRepository.instance.findByUsername(username) { (user, error) in
            if let user = user {
                self.loadUserProfilePublicView(user)
            } else {
                self.showAlert("Alert!", message: "User not found!", action: nil)
            }
        }
    }
    func didTapHashTag(_ hashtag: String) {
        self.loadHashTagSearchResult(hashtag)
    }

}