Tusker/Tusker/View Controllers/StatusesTableViewController...

108 lines
3.5 KiB
Swift
Raw Normal View History

2018-08-16 11:46:19 +00:00
//
// StatusesTableViewController.swift
// Tusker
//
// Created by Shadowfacts on 8/15/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
2018-08-17 02:39:16 +00:00
import MastodonKit
import SwiftSoup
2018-08-16 11:46:19 +00:00
class StatusesTableViewController: UITableViewController {
2018-08-17 02:39:16 +00:00
var statuses: [Status] = [] {
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
var newer: RequestRange?
var older: RequestRange?
2018-08-17 02:39:16 +00:00
override func viewWillAppear(_ animated: Bool) {
2018-08-19 20:14:04 +00:00
guard MastodonController.shared.client?.accessToken != nil else { return }
2018-08-17 02:39:16 +00:00
MastodonController.shared.client.run(Timelines.home()) { result in
guard case let .success(statuses, pagination) = result else { fatalError() }
2018-08-17 02:39:16 +00:00
self.statuses = statuses
self.newer = pagination?.previous
self.older = pagination?.next
DispatchQueue.main.async {
self.tableView.reloadData()
}
2018-08-17 02:39:16 +00:00
}
}
2018-08-16 11:46:19 +00:00
override func viewDidLoad() {
super.viewDidLoad()
2018-08-17 02:39:16 +00:00
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 140
2018-08-16 11:46:19 +00:00
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
2018-08-16 11:46:19 +00:00
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
2018-08-17 02:39:16 +00:00
return 1
2018-08-16 11:46:19 +00:00
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
2018-08-17 02:39:16 +00:00
return statuses.count
2018-08-16 11:46:19 +00:00
}
2018-08-17 02:39:16 +00:00
2018-08-16 11:46:19 +00:00
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
2018-08-17 02:39:16 +00:00
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else {
fatalError()
}
let status = statuses[indexPath.row]
cell.updateUI(for: status)
2018-08-16 11:46:19 +00:00
return cell
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == statuses.count - 1 {
guard let older = older else { return }
MastodonController.shared.client.run(Timelines.home(range: older)) { result in
guard case let .success(newStatuses, pagination) = result else { fatalError() }
self.older = pagination?.next
self.statuses.append(contentsOf: newStatuses)
}
}
2018-08-16 11:46:19 +00:00
}
@IBAction func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return }
MastodonController.shared.client.run(Timelines.home(range: newer)) { result in
guard case let .success(newStatuses, pagination) = result else { fatalError() }
self.newer = pagination?.previous
self.statuses.insert(contentsOf: newStatuses, at: 0)
DispatchQueue.main.async {
self.refreshControl?.endRefreshing()
// maintain the current position in the list (don't scroll to the top)
self.tableView.scrollToRow(at: IndexPath(row: newStatuses.count, section: 0), at: .top, animated: false)
}
}
2018-08-16 11:46:19 +00:00
}
2018-08-16 11:46:19 +00:00
}