[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.

154 lines
6.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
  1. //
  2. // ELStatusTableViewController.swift
  3. // elpha-ios
  4. //
  5. // Created by Dwayne Harris on 11/9/18.
  6. // Copyright © 2018 Elpha. All rights reserved.
  7. //
  8. import AVKit
  9. import CoreData
  10. import UIKit
  11. import SafariServices
  12. protocol AbstractStatusTableViewCell {
  13. var statusView: StatusView! { get set }
  14. }
  15. class AbstractStatusTableViewController: UITableViewController, StatusViewDelegate {
  16. let fetchLimit = 20
  17. var fetchedResultsController: NSFetchedResultsController<StatusMO>?
  18. var loading: Bool = false {
  19. didSet {
  20. DispatchQueue.main.async {
  21. if self.loading {
  22. self.refreshControl?.beginRefreshing()
  23. } else {
  24. self.refreshControl?.endRefreshing()
  25. }
  26. }
  27. }
  28. }
  29. var currentPaginationContext: String {
  30. get {
  31. return ""
  32. }
  33. }
  34. func storyboard() -> UIStoryboard {
  35. return UIStoryboard(name: "Main", bundle: nil)
  36. }
  37. func accountTapped(_ sender: Any, account: AccountMO) {
  38. if let controller = storyboard().instantiateViewController(withIdentifier: "AccountTableViewController") as? AccountTableViewController {
  39. controller.account = account
  40. self.navigationController?.pushViewController(controller, animated: true)
  41. }
  42. }
  43. func statusTapped(_ sender: Any, status: StatusMO) {
  44. if let controller = storyboard().instantiateViewController(withIdentifier: "StatusTableViewController") as? StatusTableViewController {
  45. controller.status = status
  46. self.navigationController?.pushViewController(controller, animated: true)
  47. }
  48. }
  49. func loadMoreTapped(_ sender: Any, status: StatusMO, direction: PaginationDirection) {
  50. }
  51. func replyTapped(_ sender: Any, status: StatusMO) {
  52. if let controller = storyboard().instantiateViewController(withIdentifier: "ComposeViewController") as? ComposeViewController {
  53. controller.replyToStatus = status
  54. present(controller, animated: true)
  55. }
  56. }
  57. func attachmentTapped(_ sender: Any, status: StatusMO, index: Int) {
  58. if let attachment = status.attachments?[index] as? AttachmentMO, let attachmentType = AttachmentType(rawValue: attachment.type!) {
  59. switch attachmentType {
  60. case .video, .gifv:
  61. let avController = AVPlayerViewController()
  62. avController.player = AVPlayer(url: attachment.url!)
  63. avController.entersFullScreenWhenPlaybackBegins = false
  64. present(avController, animated: true)
  65. default:
  66. if let controller = storyboard().instantiateViewController(withIdentifier: "AttachmentPageViewController") as? AttachmentPageViewController {
  67. controller.status = status
  68. controller.attachmentIndex = index
  69. present(controller, animated: false)
  70. }
  71. }
  72. }
  73. }
  74. func urlTapped(_ sender: Any, url: URL) {
  75. let controller = SFSafariViewController(url: url)
  76. controller.delegate = self
  77. controller.dismissButtonStyle = .done
  78. controller.preferredControlTintColor = UIColor(named: "Primary")
  79. present(controller, animated: true)
  80. }
  81. func revealTapped(_ sender: Any) {}
  82. func hideTapped(_ sender: Any) {}
  83. func boostTapped(_ sender: Any) {}
  84. func favoriteTapped(_ sender: Any) {}
  85. func updateCell(_ cell: AbstractStatusTableViewCell, withStatusAt indexPath: IndexPath) {
  86. guard let status = fetchedResultsController?.object(at: indexPath) else {
  87. fatalError("CoreData error")
  88. }
  89. cell.statusView.update(withStatus: status)
  90. cell.statusView.topDividerView.isHidden = indexPath.row == 0
  91. cell.statusView.topLoadMoreView.isHidden = true
  92. cell.statusView.bottomLoadMoreView.isHidden = true
  93. let statusCount = fetchedResultsController?.fetchedObjects?.count ?? 0
  94. if let markers = status.markers {
  95. markers.forEach { marker in
  96. if marker.context == self.currentPaginationContext {
  97. switch marker.item.direction {
  98. case .prev:
  99. if indexPath.row > 0, let previousStatus = fetchedResultsController?.object(at: IndexPath(row: indexPath.row - 1, section: indexPath.section)) {
  100. let previousMarkers = previousStatus.markers ?? []
  101. let previousMarker = previousMarkers.first { $0.context == self.currentPaginationContext && $0.item.direction == .next }
  102. if previousStatus.id! != marker.item.statusID && previousMarker != nil {
  103. cell.statusView.topLoadMoreView.isHidden = false
  104. cell.statusView.topDividerView.isHidden = true
  105. }
  106. }
  107. case .next:
  108. if indexPath.row < statusCount - 1, let nextStatus = fetchedResultsController?.object(at: IndexPath(row: indexPath.row + 1, section: indexPath.section)) {
  109. let nextMarkers = nextStatus.markers ?? []
  110. let nextMarker = nextMarkers.first { $0.context == self.currentPaginationContext && $0.item.direction == .prev }
  111. if nextStatus.id! != marker.item.statusID && nextMarker != nil {
  112. cell.statusView.bottomLoadMoreView.isHidden = false
  113. cell.statusView.bottomDividerView.isHidden = true
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. if indexPath.row == statusCount - 1 {
  121. cell.statusView.bottomLoadMoreView.isHidden = false
  122. cell.statusView.bottomDividerView.isHidden = true
  123. }
  124. }
  125. }
  126. extension AbstractStatusTableViewController: SFSafariViewControllerDelegate {
  127. func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
  128. controller.dismiss(animated: true)
  129. }
  130. }