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

172 lines
5.8 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
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
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
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
  1. //
  2. // InstancesTableViewController.swift
  3. // elpha-ios
  4. //
  5. // Created by Dwayne Harris on 8/29/18.
  6. // Copyright © 2018 Elpha. All rights reserved.
  7. //
  8. import Alamofire
  9. import CoreData
  10. import Kingfisher
  11. import UIKit
  12. class InstancesTableViewController: UITableViewController {
  13. @IBOutlet var mainNavigationItem: UINavigationItem!
  14. let fetchLimit = 20
  15. var fetchedResultsController: NSFetchedResultsController<ISInstanceMO>?
  16. var loading: Bool = false {
  17. didSet {
  18. DispatchQueue.main.async {
  19. if self.loading {
  20. self.refreshControl?.beginRefreshing()
  21. } else {
  22. self.refreshControl?.endRefreshing()
  23. }
  24. }
  25. }
  26. }
  27. func initializeFetchedResultsController() {
  28. let request = NSFetchRequest<ISInstanceMO>(entityName: "ISInstance")
  29. request.sortDescriptors = [
  30. NSSortDescriptor(key: "users", ascending: false),
  31. ]
  32. fetchedResultsController = NSFetchedResultsController(
  33. fetchRequest: request,
  34. managedObjectContext: CoreDataManager.shared.context,
  35. sectionNameKeyPath: nil,
  36. cacheName: nil
  37. )
  38. fetchedResultsController!.delegate = self
  39. do {
  40. try fetchedResultsController!.performFetch()
  41. } catch {
  42. print("\(error)")
  43. }
  44. }
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. initializeFetchedResultsController()
  48. mainNavigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done))
  49. registerForPreviewing(with: self, sourceView: tableView)
  50. refreshControl?.addTarget(self, action: #selector(self.refetchInstances), for: .valueChanged)
  51. if tableView.numberOfRows(inSection: 0) == 0 {
  52. fetchInstances()
  53. }
  54. }
  55. @objc func done() {
  56. dismiss(animated: true, completion: nil)
  57. }
  58. @objc func refetchInstances() {
  59. loading = true
  60. DispatchQueue.main.async {
  61. InstancesDataManager.shared.clearInstances()
  62. CoreDataManager.shared.saveContext()
  63. InstancesDataManager.shared.reloadInstances() { error in
  64. self.loading = false
  65. }
  66. }
  67. }
  68. func fetchInstances() {
  69. loading = true
  70. DispatchQueue.main.async {
  71. InstancesDataManager.shared.loadInstances() { error in
  72. self.loading = false
  73. }
  74. }
  75. }
  76. override func numberOfSections(in tableView: UITableView) -> Int {
  77. return 1
  78. }
  79. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  80. guard let count = fetchedResultsController?.fetchedObjects?.count else {
  81. return 0
  82. }
  83. return count
  84. }
  85. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  86. guard let cell = tableView.dequeueReusableCell(withIdentifier: "InstancesTableViewCell", for: indexPath) as? InstancesTableViewCell else {
  87. fatalError("Unable to find reusable cell")
  88. }
  89. guard let instance = fetchedResultsController?.object(at: indexPath) else {
  90. fatalError("CoreData error")
  91. }
  92. cell.instanceNameLabel.text = instance.name
  93. cell.statusesLabel.text = NumberFormatter.localizedString(from: NSNumber(value: instance.statuses), number: .decimal)
  94. cell.usersLabel.text = NumberFormatter.localizedString(from: NSNumber(value: instance.users), number: .decimal)
  95. if let thumbnail = instance.thumbnail {
  96. cell.thumbnailImageView.kf.setImage(with: thumbnail)
  97. }
  98. if indexPath.row > 0 && indexPath.row % fetchLimit == 0 && !loading {
  99. fetchInstances()
  100. }
  101. return cell
  102. }
  103. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  104. if segue.identifier == "InstanceDetailSegue", let cell = segue.destination as? InstanceViewController {
  105. if let indexPath = tableView.indexPathForSelectedRow {
  106. guard let instance = fetchedResultsController?.object(at: indexPath) else {
  107. fatalError("CoreData error")
  108. }
  109. cell.instance = instance
  110. }
  111. }
  112. }
  113. }
  114. extension InstancesTableViewController: UIViewControllerPreviewingDelegate {
  115. public func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
  116. navigationController?.pushViewController(viewControllerToCommit, animated: true)
  117. }
  118. public func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
  119. guard let indexPath = tableView.indexPathForRow(at: location) else {
  120. return nil
  121. }
  122. guard let instance = fetchedResultsController?.object(at: indexPath) else {
  123. fatalError("CoreData error")
  124. }
  125. let storyboard = UIStoryboard(name: "Main", bundle: nil)
  126. if let detailViewController = storyboard.instantiateViewController(withIdentifier: "InstanceViewController") as? InstanceViewController {
  127. detailViewController.instance = instance
  128. return detailViewController
  129. } else {
  130. return nil
  131. }
  132. }
  133. }
  134. extension InstancesTableViewController: NSFetchedResultsControllerDelegate {
  135. func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
  136. self.tableView.reloadData()
  137. }
  138. }