// // ELStatusTableViewController.swift // elpha-ios // // Created by Dwayne Harris on 11/9/18. // Copyright © 2018 Elpha. All rights reserved. // import AVKit import CoreData import UIKit import SafariServices protocol AbstractStatusTableViewCell { var statusView: StatusView! { get set } } class AbstractStatusTableViewController: UITableViewController, StatusViewDelegate { let fetchLimit = 20 var fetchedResultsController: NSFetchedResultsController? var loading: Bool = false { didSet { DispatchQueue.main.async { if self.loading { self.refreshControl?.beginRefreshing() } else { self.refreshControl?.endRefreshing() } } } } var currentPaginationContext: String { get { return "" } } func storyboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: nil) } func accountTapped(_ sender: Any, account: AccountMO) { if let controller = storyboard().instantiateViewController(withIdentifier: "AccountTableViewController") as? AccountTableViewController { controller.account = account self.navigationController?.pushViewController(controller, animated: true) } } func statusTapped(_ sender: Any, status: StatusMO) { if let controller = storyboard().instantiateViewController(withIdentifier: "StatusTableViewController") as? StatusTableViewController { controller.status = status self.navigationController?.pushViewController(controller, animated: true) } } func loadMoreTapped(_ sender: Any, status: StatusMO, direction: PaginationDirection) { } func replyTapped(_ sender: Any, status: StatusMO) { if let controller = storyboard().instantiateViewController(withIdentifier: "ComposeViewController") as? ComposeViewController { controller.replyToStatus = status present(controller, animated: true) } } func attachmentTapped(_ sender: Any, status: StatusMO, index: Int) { if let attachment = status.attachments?[index] as? AttachmentMO, let attachmentType = AttachmentType(rawValue: attachment.type!) { switch attachmentType { case .video, .gifv: let avController = AVPlayerViewController() avController.player = AVPlayer(url: attachment.url!) avController.entersFullScreenWhenPlaybackBegins = false present(avController, animated: true) default: if let controller = storyboard().instantiateViewController(withIdentifier: "AttachmentPageViewController") as? AttachmentPageViewController { controller.status = status controller.attachmentIndex = index present(controller, animated: false) } } } } func urlTapped(_ sender: Any, url: URL) { let controller = SFSafariViewController(url: url) controller.delegate = self controller.dismissButtonStyle = .done controller.preferredControlTintColor = UIColor(named: "Primary") present(controller, animated: true) } func revealTapped(_ sender: Any) {} func hideTapped(_ sender: Any) {} func boostTapped(_ sender: Any) {} func favoriteTapped(_ sender: Any) {} func updateCell(_ cell: AbstractStatusTableViewCell, withStatusAt indexPath: IndexPath) { guard let status = fetchedResultsController?.object(at: indexPath) else { fatalError("CoreData error") } cell.statusView.update(withStatus: status) cell.statusView.topDividerView.isHidden = indexPath.row == 0 cell.statusView.topLoadMoreView.isHidden = true cell.statusView.bottomLoadMoreView.isHidden = true let statusCount = fetchedResultsController?.fetchedObjects?.count ?? 0 if let markers = status.markers { markers.forEach { marker in if marker.context == self.currentPaginationContext { switch marker.item.direction { case .prev: if indexPath.row > 0, let previousStatus = fetchedResultsController?.object(at: IndexPath(row: indexPath.row - 1, section: indexPath.section)) { let previousMarkers = previousStatus.markers ?? [] let previousMarker = previousMarkers.first { $0.context == self.currentPaginationContext && $0.item.direction == .next } if previousStatus.id! != marker.item.statusID && previousMarker != nil { cell.statusView.topLoadMoreView.isHidden = false cell.statusView.topDividerView.isHidden = true } } case .next: if indexPath.row < statusCount - 1, let nextStatus = fetchedResultsController?.object(at: IndexPath(row: indexPath.row + 1, section: indexPath.section)) { let nextMarkers = nextStatus.markers ?? [] let nextMarker = nextMarkers.first { $0.context == self.currentPaginationContext && $0.item.direction == .prev } if nextStatus.id! != marker.item.statusID && nextMarker != nil { cell.statusView.bottomLoadMoreView.isHidden = false cell.statusView.bottomDividerView.isHidden = true } } } } } } if indexPath.row == statusCount - 1 { cell.statusView.bottomLoadMoreView.isHidden = false cell.statusView.bottomDividerView.isHidden = true } } } extension AbstractStatusTableViewController: SFSafariViewControllerDelegate { func safariViewControllerDidFinish(_ controller: SFSafariViewController) { controller.dismiss(animated: true) } }