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

422 lines
16 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
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
4 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
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
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
4 years ago
6 years ago
4 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. // TimelineTableViewController.swift
  3. // elpha-ios
  4. //
  5. // Created by Dwayne Harris on 10/1/18.
  6. // Copyright © 2018 Elpha. All rights reserved.
  7. //
  8. import CoreData
  9. import Kingfisher
  10. import UIKit
  11. import SafariServices
  12. class TimelineTableViewController: AbstractStatusTableViewController {
  13. var feedbackGenerator: UINotificationFeedbackGenerator?
  14. override var currentPaginationContext: String {
  15. get {
  16. guard let timeline = AuthenticationManager.session?.timeline, let categoryString = timeline.category else {
  17. return ""
  18. }
  19. switch TimelineCategory(rawValue: categoryString)! {
  20. case .home:
  21. return "timeline:home"
  22. case .local:
  23. return "timeline:local"
  24. case .federated:
  25. return "timeline:federated"
  26. case .tag:
  27. return "timeline:tag:\(timeline.subcategory!)"
  28. case .favorites:
  29. return "timeline:favorites"
  30. }
  31. }
  32. }
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35. feedbackGenerator = UINotificationFeedbackGenerator()
  36. initializeFetchedResultsController()
  37. let scale = UIScreen.main.scale
  38. let avatarButtonSize: CGFloat = 30
  39. let avatarButtonScaledSize = avatarButtonSize * scale
  40. let moreButtonItem = UIBarButtonItem(image: UIImage(named: "More"), style: .plain, target: self, action: #selector(more))
  41. let composeButtonItem = UIBarButtonItem(image: UIImage(named: "Compose"), style: .plain, target: self, action: #selector(compose))
  42. let avatarButton = UIButton()
  43. avatarButton.setBackgroundImage(UIImage(named: "Account"), for: .normal)
  44. avatarButton.addTarget(self, action: #selector(self.openSettings), for: .touchUpInside)
  45. NSLayoutConstraint.activate([
  46. avatarButton.widthAnchor.constraint(equalToConstant: avatarButtonSize),
  47. avatarButton.heightAnchor.constraint(equalToConstant: avatarButtonSize)
  48. ])
  49. let avatarButtonItem = UIBarButtonItem(customView: avatarButton)
  50. navigationItem.leftBarButtonItems = [avatarButtonItem, moreButtonItem]
  51. navigationItem.rightBarButtonItems = [composeButtonItem]
  52. if let account = AuthenticationManager.session?.account {
  53. let processor = ResizingImageProcessor(referenceSize: CGSize(width: avatarButtonScaledSize, height: avatarButtonScaledSize), mode: .aspectFill) >> RoundCornerImageProcessor(cornerRadius: avatarButtonScaledSize / 2)
  54. ImageDownloader.default.downloadImage(with: account.avatarURL!, retrieveImageTask: nil, options: [.processor(processor)], progressBlock: nil) { image, error, url, data in
  55. if let image = image {
  56. avatarButton.setBackgroundImage(image.withRenderingMode(.alwaysOriginal), for: .normal)
  57. }
  58. }
  59. }
  60. if let timeline = AuthenticationManager.session?.timeline {
  61. navigationItem.title = timeline.name
  62. } else {
  63. navigationItem.title = "Home"
  64. }
  65. refreshControl?.addTarget(self, action: #selector(self.fetch), for: .valueChanged)
  66. NotificationCenter.default.addObserver(self, selector: #selector(onDidAuthenticate), name: .didAuthenticate, object: nil)
  67. NotificationCenter.default.addObserver(self, selector: #selector(onDidUnauthenticate), name: .didUnauthenticate, object: nil)
  68. NotificationCenter.default.addObserver(self, selector: #selector(onDidBecomeActive), name: .didBecomeActive, object: nil)
  69. }
  70. override func viewDidAppear(_ animated: Bool) {
  71. super.viewDidAppear(animated)
  72. if SettingsManager.automaticallyRefreshTimelines {
  73. fetch()
  74. }
  75. }
  76. override func loadMoreTapped(_ sender: Any, status: StatusMO, direction: PaginationDirection) {
  77. var markerIndex = -1
  78. if let markers = status.markers {
  79. for (index, marker) in markers.enumerated() {
  80. if marker.context == self.currentPaginationContext && marker.item.direction == direction {
  81. markerIndex = index
  82. }
  83. }
  84. if markerIndex > -1 {
  85. status.markers?.remove(at: markerIndex)
  86. CoreDataManager.shared.saveContext()
  87. fetchTimeline(withPagination: markers[markerIndex].item) { error in
  88. if error != nil {
  89. AlertManager.shared.show(message: error!.localizedDescription, category: .error)
  90. }
  91. }
  92. }
  93. }
  94. }
  95. @objc func openSettings() {
  96. let storyboard = UIStoryboard(name: "Main", bundle: nil)
  97. if let controller = storyboard.instantiateViewController(withIdentifier: "SettingsTableViewController") as? SettingsTableViewController {
  98. self.navigationController?.pushViewController(controller, animated: true)
  99. }
  100. }
  101. @objc func more() {
  102. performSegue(withIdentifier: "TimelinesSegue", sender: self)
  103. }
  104. @objc func compose() {
  105. let storyboard = UIStoryboard(name: "Main", bundle: nil)
  106. if let controller = storyboard.instantiateViewController(withIdentifier: "ComposeViewController") as? ComposeViewController {
  107. present(controller, animated: true)
  108. }
  109. }
  110. @objc func onDidAuthenticate(_ notification: Notification) {
  111. initializeFetchedResultsController()
  112. tableView.reloadData()
  113. fetch()
  114. }
  115. @objc func onDidUnauthenticate(_ notification: Notification) {
  116. performSegue(withIdentifier: "AuthenticateSegue", sender: self)
  117. }
  118. @objc func onDidBecomeActive() {
  119. if SettingsManager.automaticallyRefreshTimelines {
  120. fetch()
  121. }
  122. }
  123. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  124. if segue.identifier == "TimelinesSegue" {
  125. if let destination = segue.destination as? TimelinesViewController {
  126. destination.delegate = self
  127. }
  128. }
  129. }
  130. func createDefaultTimelines(account: AccountMO) {
  131. let homeTimeline = TimelineMO(context: CoreDataManager.shared.context)
  132. homeTimeline.name = "Home"
  133. homeTimeline.category = TimelineCategory.home.rawValue
  134. homeTimeline.account = account
  135. homeTimeline.order = 1
  136. let localTimeline = TimelineMO(context: CoreDataManager.shared.context)
  137. localTimeline.name = "Local"
  138. localTimeline.category = TimelineCategory.local.rawValue
  139. localTimeline.account = account
  140. localTimeline.order = 2
  141. let federatedTimeline = TimelineMO(context: CoreDataManager.shared.context)
  142. federatedTimeline.name = "Federated"
  143. federatedTimeline.category = TimelineCategory.federated.rawValue
  144. federatedTimeline.account = account
  145. federatedTimeline.order = 3
  146. let favoritesTimeline = TimelineMO(context: CoreDataManager.shared.context)
  147. favoritesTimeline.name = "Favorites"
  148. favoritesTimeline.category = TimelineCategory.favorites.rawValue
  149. favoritesTimeline.account = account
  150. favoritesTimeline.order = 4
  151. CoreDataManager.shared.saveContext()
  152. }
  153. @objc func fetch() {
  154. fetchTimeline { error in
  155. if error != nil {
  156. AlertManager.shared.show(message: error!.localizedDescription, category: .error)
  157. }
  158. }
  159. }
  160. func fetchTimeline(withPagination pagination: PaginationItem? = nil, completion: @escaping (Error?) -> Void) {
  161. guard let session = AuthenticationManager.session, let account = session.account else {
  162. completion(nil)
  163. return
  164. }
  165. if session.timeline == nil {
  166. if let timelines = account.timelines, timelines.count == 0 {
  167. createDefaultTimelines(account: account)
  168. }
  169. let request = NSFetchRequest<TimelineMO>(entityName: "Timeline")
  170. request.predicate = NSPredicate(format: "category == %@", TimelineCategory.home.rawValue)
  171. do {
  172. let results = try CoreDataManager.shared.context.fetch(request)
  173. session.timeline = results.first
  174. CoreDataManager.shared.saveContext()
  175. } catch {
  176. print("\(error)")
  177. }
  178. }
  179. guard let timeline = session.timeline else {
  180. completion(nil)
  181. return
  182. }
  183. func requestCompletion(data: [JSONObject]?, pagination: [PaginationItem]?, error: Error?) {
  184. guard let data = data, error == nil else {
  185. completion(error)
  186. return
  187. }
  188. DispatchQueue.main.async {
  189. var newStatusCount = 0
  190. self.feedbackGenerator?.prepare()
  191. for (index, status) in data.enumerated() {
  192. if let upsertResult = MastodonDataManager.upsertStatus(status) {
  193. let status = upsertResult.object
  194. if upsertResult.new {
  195. newStatusCount = newStatusCount + 1
  196. }
  197. if let pagination = pagination {
  198. var markers: [PaginationMarker] = status.markers ?? []
  199. markers = Array(markers.drop { $0.context == self.currentPaginationContext })
  200. if index == 0 {
  201. pagination.forEach { item in
  202. if item.direction == .prev {
  203. markers.append(PaginationMarker(context: self.currentPaginationContext, item: item))
  204. }
  205. }
  206. }
  207. if index == data.count - 1 {
  208. pagination.forEach { item in
  209. if item.direction == .next {
  210. markers.append(PaginationMarker(context: self.currentPaginationContext, item: item))
  211. }
  212. }
  213. }
  214. status.markers = markers
  215. }
  216. timeline.addToStatuses(status)
  217. }
  218. }
  219. if newStatusCount > 0 {
  220. let pluralization = newStatusCount == 1 ? "" : "s"
  221. AlertManager.shared.show(message: "\(newStatusCount) new toot\(pluralization)", category: .newStatuses)
  222. self.feedbackGenerator?.notificationOccurred(.success)
  223. }
  224. CoreDataManager.shared.saveContext()
  225. self.loading = false
  226. completion(nil)
  227. }
  228. }
  229. loading = true
  230. switch TimelineCategory(rawValue: timeline.category!)! {
  231. case .home:
  232. MastodonAPI.homeTimeline(
  233. limit: fetchLimit,
  234. pagination: pagination,
  235. completion: requestCompletion
  236. )
  237. case .local:
  238. MastodonAPI.publicTimeline(
  239. local: true,
  240. limit: fetchLimit,
  241. pagination: pagination,
  242. completion: requestCompletion
  243. )
  244. case .federated:
  245. MastodonAPI.publicTimeline(
  246. local: false,
  247. limit: fetchLimit,
  248. pagination: pagination,
  249. completion: requestCompletion
  250. )
  251. case .tag:
  252. MastodonAPI.tagTimeline(
  253. tag: timeline.subcategory!,
  254. local: false,
  255. limit: fetchLimit,
  256. pagination: pagination,
  257. completion: requestCompletion
  258. )
  259. case .favorites:
  260. MastodonAPI.favorites(
  261. limit: fetchLimit,
  262. pagination: pagination,
  263. completion: requestCompletion
  264. )
  265. }
  266. }
  267. }
  268. extension TimelineTableViewController: NSFetchedResultsControllerDelegate {
  269. func initializeFetchedResultsController() {
  270. guard let timeline = AuthenticationManager.session?.timeline else {
  271. return
  272. }
  273. let request = NSFetchRequest<StatusMO>(entityName: "Status")
  274. request.predicate = NSPredicate(format: "ANY timelines = %@", timeline)
  275. request.sortDescriptors = [
  276. NSSortDescriptor(key: "createdAt", ascending: false),
  277. ]
  278. fetchedResultsController = NSFetchedResultsController(
  279. fetchRequest: request,
  280. managedObjectContext: CoreDataManager.shared.context,
  281. sectionNameKeyPath: nil,
  282. cacheName: nil
  283. )
  284. fetchedResultsController!.delegate = self
  285. do {
  286. try fetchedResultsController!.performFetch()
  287. } catch {
  288. print("\(error)")
  289. }
  290. }
  291. func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
  292. tableView.beginUpdates()
  293. }
  294. func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
  295. switch type {
  296. case NSFetchedResultsChangeType.insert:
  297. tableView.insertRows(at: [newIndexPath!], with: UITableView.RowAnimation.none)
  298. case NSFetchedResultsChangeType.delete:
  299. tableView.deleteRows(at: [indexPath!], with: UITableView.RowAnimation.none)
  300. case NSFetchedResultsChangeType.update:
  301. if let cell = tableView.cellForRow(at: indexPath!) as? TimelineTableViewCell {
  302. updateCell(cell, withStatusAt: indexPath!)
  303. }
  304. case NSFetchedResultsChangeType.move:
  305. tableView.deleteRows(at: [indexPath!], with: UITableView.RowAnimation.fade)
  306. tableView.insertRows(at: [newIndexPath!], with: UITableView.RowAnimation.fade)
  307. }
  308. }
  309. func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
  310. tableView.endUpdates()
  311. }
  312. }
  313. extension TimelineTableViewController {
  314. override func numberOfSections(in tableView: UITableView) -> Int {
  315. return 1
  316. }
  317. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  318. return fetchedResultsController?.fetchedObjects?.count ?? 0
  319. }
  320. // override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  321. // if let cell = cell as? TimelineTableViewCell {
  322. // CellHeightManager.set(status: cell.statusView.status, height: cell.frame.size.height)
  323. // }
  324. // }
  325. // override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  326. // return CellHeightManager.get(status: fetchedResultsController?.object(at: indexPath))
  327. // }
  328. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  329. guard let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineTableViewCell", for: indexPath) as? TimelineTableViewCell else {
  330. fatalError("Unable to find reusable cell")
  331. }
  332. updateCell(cell, withStatusAt: indexPath)
  333. cell.statusView.delegate = self
  334. return cell
  335. }
  336. }
  337. extension TimelineTableViewController: TimelinesViewControllerDelegate {
  338. func didSelect(_ sender: Any, timeline: TimelineMO) {
  339. navigationItem.title = timeline.name
  340. AuthenticationManager.session?.timeline = timeline
  341. CoreDataManager.shared.saveContext()
  342. initializeFetchedResultsController()
  343. tableView.reloadData()
  344. if SettingsManager.automaticallyRefreshTimelines {
  345. fetch()
  346. }
  347. }
  348. }