Tusker/Tusker/Screens/Notifications/NotificationsTableViewContr...

158 lines
6.5 KiB
Swift

//
// NotificationsTableViewController.swift
// Tusker
//
// Created by Shadowfacts on 9/2/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
class NotificationsTableViewController: UITableViewController {
static func create() -> UIViewController {
guard let navigationController = UIStoryboard(name: "Notifications", bundle: nil).instantiateInitialViewController() as? UINavigationController else { fatalError() }
return navigationController
}
var notifications: [Pachyderm.Notification] = [] {
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
var newer: RequestRange?
var older: RequestRange?
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 140
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
tableView.register(UINib(nibName: "ActionNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "actionCell")
tableView.register(UINib(nibName: "FollowNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "followCell")
let request = MastodonController.shared.client.getNotifications()
MastodonController.shared.client.run(request) { result in
guard case let .success(notifications, pagination) = result else { fatalError() }
self.notifications = notifications
StatusCache.addAll(notifications.compactMap { $0.status })
self.newer = pagination?.newer
self.older = pagination?.older
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
for cell in tableView.visibleCells {
if let cell = cell as? PreferencesAdaptive {
cell.updateUIForPreferences()
}
}
}
/*
// 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.
}
*/
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notifications.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let notification = notifications[indexPath.row]
switch notification.kind {
case .mention:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() }
let status = notification.status!
cell.updateUI(for: status.id)
cell.delegate = self
return cell
case .favourite, .reblog:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath) as? ActionNotificationTableViewCell else { fatalError() }
cell.updateUI(for: notification)
cell.delegate = self
return cell
case .follow:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "followCell", for: indexPath) as? FollowNotificationTableViewCell else { fatalError() }
cell.updateUI(for: notification)
cell.delegate = self
return cell
}
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == notifications.count - 1 {
guard let older = older else { return }
let request = MastodonController.shared.client.getNotifications(range: older)
MastodonController.shared.client.run(request) { result in
guard case let .success(newNotifications, pagination) = result else { fatalError() }
self.older = pagination?.older
self.notifications.append(contentsOf: newNotifications)
}
}
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider
}
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
}
@IBAction func refreshNotifications(_ sender: Any) {
guard let newer = newer else { return }
let request = MastodonController.shared.client.getNotifications(range: newer)
MastodonController.shared.client.run(request) { result in
guard case let .success(newNotifications, pagination) = result else { fatalError() }
self.newer = pagination?.newer
StatusCache.addAll(newNotifications.compactMap { $0.status })
self.notifications.insert(contentsOf: newNotifications, at: 0)
DispatchQueue.main.async {
self.refreshControl?.endRefreshing()
// maintain the current position in the list (don't scroll to top)
self.tableView.scrollToRow(at: IndexPath(row: newNotifications.count, section: 0), at: .top, animated: false)
}
}
}
}
extension NotificationsTableViewController: StatusTableViewCellDelegate {}
extension NotificationsTableViewController: LargeImageViewControllerDelegate {}