[ABANDONED] Mastodon iOS client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.5 KiB

//
// ELStatusTableViewController.swift
// elpha-ios
//
// Created by Dwayne Harris on 11/9/18.
// Copyright © 2018 Elpha. All rights reserved.
//
import CoreData
import UIKit
import SafariServices
protocol AbstractStatusTableViewCell {
var statusView: StatusView! { get set }
}
class AbstractStatusTableViewController: UITableViewController, StatusViewDelegate {
let fetchLimit = 20
var fetchedResultsController: NSFetchedResultsController<StatusMO>? = nil
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(account: AccountMO) {
if let controller = storyboard().instantiateViewController(withIdentifier: "AccountTableViewController") as? AccountTableViewController {
controller.account = account
self.navigationController?.pushViewController(controller, animated: true)
}
}
func statusTapped(status: StatusMO) {
if let controller = storyboard().instantiateViewController(withIdentifier: "StatusTableViewController") as? StatusTableViewController {
controller.status = status
self.navigationController?.pushViewController(controller, animated: true)
}
}
func loadMoreTapped(status: StatusMO, direction: PaginationDirection) {
}
func replyTapped(status: StatusMO) {
if let controller = storyboard().instantiateViewController(withIdentifier: "ComposeViewController") as? ComposeViewController {
controller.replyToStatus = status
present(controller, animated: true)
}
}
func attachmentTapped(status: StatusMO, index: Int) {
if let controller = storyboard().instantiateViewController(withIdentifier: "AttachmentPageViewController") as? AttachmentPageViewController {
controller.status = status
controller.attachmentIndex = index
present(controller, animated: false)
}
}
func urlTapped(url: URL) {
let controller = SFSafariViewController(url: url)
controller.delegate = self
controller.dismissButtonStyle = .done
controller.preferredControlTintColor = UIColor(named: "Primary")
present(controller, animated: true)
}
func revealTapped() {}
func hideTapped() {}
func boostTapped() {}
func favoriteTapped() {}
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)
}
}