Tusker/Tusker/Screens/Profile/ProfileTableViewController....

221 lines
8.7 KiB
Swift
Raw Normal View History

2018-08-28 01:27:34 +00:00
//
// ProfileTableViewController.swift
// Tusker
//
// Created by Shadowfacts on 8/27/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
2018-09-11 14:52:21 +00:00
import Pachyderm
2018-08-28 01:27:34 +00:00
import SafariServices
class ProfileTableViewController: UITableViewController, PreferencesAdaptive {
2018-08-28 01:27:34 +00:00
2018-09-18 16:59:07 +00:00
static func create(for accountID: String) -> UIViewController {
2018-08-28 01:27:34 +00:00
guard let profileController = UIStoryboard(name: "Profile", bundle: nil).instantiateInitialViewController() as? ProfileTableViewController else { fatalError() }
2018-09-18 16:59:07 +00:00
profileController.accountID = accountID
2018-08-28 01:27:34 +00:00
return profileController
}
2018-09-18 16:59:07 +00:00
var accountID: String!
2018-08-28 01:27:34 +00:00
2018-09-18 01:57:46 +00:00
var statusIDs: [String] = [] {
2018-08-28 01:27:34 +00:00
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
var older: RequestRange?
2018-09-11 14:52:21 +00:00
var newer: RequestRange?
2018-08-28 01:27:34 +00:00
2018-09-11 14:52:21 +00:00
func getStatuses(for range: RequestRange = .default, completion: @escaping Client.Callback<[Status]>) {
2018-09-18 16:59:07 +00:00
let request = Account.getStatuses(accountID, range: range, onlyMedia: false, pinned: false, excludeReplies: !Preferences.shared.showRepliesInProfiles)
MastodonController.shared.client.run(request, completion: completion)
2018-08-28 01:27:34 +00:00
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 140
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
tableView.register(UINib(nibName: "ProfileHeaderTableViewCell", bundle: nil), forCellReuseIdentifier: "headerCell")
updateUIForPreferences()
2018-08-28 01:27:34 +00:00
2018-09-11 14:52:21 +00:00
getStatuses { response in
guard case let .success(statuses, pagination) = response else { fatalError() }
2018-09-18 16:59:07 +00:00
MastodonCache.addAll(statuses: statuses)
2018-09-18 01:57:46 +00:00
self.statusIDs = statuses.map { $0.id }
2018-09-11 14:52:21 +00:00
self.older = pagination?.older
self.newer = pagination?.newer
2018-08-28 01:27:34 +00:00
}
}
2018-08-28 23:49:31 +00:00
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
2018-08-28 23:49:31 +00:00
for cell in tableView.visibleCells {
if let cell = cell as? PreferencesAdaptive {
cell.updateUIForPreferences()
}
}
updateUIForPreferences()
}
func updateUIForPreferences() {
2018-09-18 16:59:07 +00:00
guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }
navigationItem.title = account.realDisplayName
2018-08-28 23:49:31 +00:00
}
2018-08-31 02:30:19 +00:00
func sendMessageMentioning() {
2018-09-18 16:59:07 +00:00
guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }
let vc = ComposeViewController.create(mentioning: account.acct)
2018-08-31 02:30:19 +00:00
present(vc, animated: true)
}
2018-08-28 01:27:34 +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.
}
*/
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
case 1:
2018-09-18 01:57:46 +00:00
return statusIDs.count
2018-08-28 01:27:34 +00:00
default:
return 0
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
2018-08-28 18:29:06 +00:00
guard let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath) as? ProfileHeaderTableViewCell else { fatalError() }
2018-08-28 01:27:34 +00:00
cell.selectionStyle = .none
2018-09-18 16:59:07 +00:00
cell.updateUI(for: accountID)
2018-08-28 01:27:34 +00:00
cell.delegate = self
return cell
case 1:
2018-08-28 18:29:06 +00:00
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() }
2018-09-18 01:57:46 +00:00
let statusID = statusIDs[indexPath.row]
cell.updateUI(for: statusID)
2018-08-28 01:27:34 +00:00
cell.delegate = self
return cell
default:
fatalError("Invalid section \(indexPath.section) for profile VC")
}
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
2018-09-18 01:57:46 +00:00
if indexPath.section == 1 && indexPath.row == statusIDs.count - 1 {
2018-08-28 01:27:34 +00:00
guard let older = older else { return }
2018-09-11 14:52:21 +00:00
getStatuses(for: older) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.older = pagination?.older
2018-09-18 16:59:07 +00:00
MastodonCache.addAll(statuses: newStatuses)
2018-09-18 01:57:46 +00:00
self.statusIDs.append(contentsOf: newStatuses.map { $0.id })
2018-08-28 01:27:34 +00:00
}
}
}
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()
}
2018-08-28 01:27:34 +00:00
@IBAction func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return }
2018-09-11 14:52:21 +00:00
getStatuses(for: newer) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.newer = pagination?.newer
2018-09-18 16:59:07 +00:00
MastodonCache.addAll(statuses: newStatuses)
2018-09-18 01:57:46 +00:00
self.statusIDs.insert(contentsOf: newStatuses.map { $0.id }, at: 0)
2018-08-28 01:27:34 +00:00
DispatchQueue.main.async {
self.refreshControl?.endRefreshing()
}
}
}
2018-08-31 02:30:19 +00:00
@IBAction func composePressed(_ sender: Any) {
sendMessageMentioning()
}
2018-08-28 01:27:34 +00:00
}
extension ProfileTableViewController: ProfileHeaderTableViewCellDelegate {
2018-08-28 21:53:59 +00:00
func showMoreOptions() {
2018-09-18 16:59:07 +00:00
let account = MastodonCache.account(for: accountID)!
2018-08-28 21:53:59 +00:00
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Open in Safari...", style: .default, handler: { _ in
2018-09-18 16:59:07 +00:00
let vc = SFSafariViewController(url: account.url)
2018-08-28 21:53:59 +00:00
self.present(vc, animated: true)
}))
alert.addAction(UIAlertAction(title: "Share...", style: .default, handler: { _ in
2018-09-18 16:59:07 +00:00
let vc = UIActivityViewController(activityItems: [account.url], applicationActivities: nil)
2018-08-28 21:53:59 +00:00
self.present(vc, animated: true)
}))
alert.addAction(UIAlertAction(title: "Send Message...", style: .default, handler: { _ in
2018-08-31 02:30:19 +00:00
self.sendMessageMentioning()
2018-08-28 21:53:59 +00:00
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
2018-09-24 12:49:39 +00:00
MastodonCache.relationship(for: account.id) { (relationship) in
guard let relationship = relationship else {
DispatchQueue.main.async {
self.present(alert, animated: true)
}
return
}
let title = relationship.following ? "Unfollow": "Follow"
DispatchQueue.main.async {
alert.addAction(UIAlertAction(title: title, style: .default, handler: { (_) in
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
let request = (relationship.following ? Account.unfollow : Account.follow)(account.id)
MastodonController.shared.client.run(request, completion: { (response) in
if case let .success(relationship, _) = response {
MastodonCache.add(relationship: relationship)
} else {
// todo: display error message
UINotificationFeedbackGenerator().notificationOccurred(.error)
fatalError()
}
})
}))
self.present(alert, animated: true)
}
}
2018-08-28 21:53:59 +00:00
}
2018-08-28 01:27:34 +00:00
}
extension ProfileTableViewController: LargeImageViewControllerDelegate {}