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
|
|
|
|
|
2019-08-03 00:05:47 +00:00
|
|
|
class ProfileTableViewController: EnhancedTableViewController {
|
2018-10-02 23:23:50 +00:00
|
|
|
|
|
|
|
var accountID: String! {
|
|
|
|
didSet {
|
|
|
|
if shouldLoadOnAccountIDSet {
|
2018-10-20 16:03:18 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.updateAccountUI()
|
|
|
|
}
|
2018-10-02 23:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 01:27:34 +00:00
|
|
|
|
2019-08-03 00:23:45 +00:00
|
|
|
var timelineSegments: [TimelineSegment<Status>] = [] {
|
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-10-02 23:23:50 +00:00
|
|
|
var shouldLoadOnAccountIDSet = false
|
|
|
|
var loadingVC: LoadingViewController? = nil
|
|
|
|
|
2019-01-19 19:31:31 +00:00
|
|
|
init(accountID: String?) {
|
2018-10-20 16:03:18 +00:00
|
|
|
self.accountID = accountID
|
2018-10-21 02:07:04 +00:00
|
|
|
|
|
|
|
super.init(style: .plain)
|
|
|
|
|
2018-10-20 16:03:18 +00:00
|
|
|
self.refreshControl = UIRefreshControl()
|
|
|
|
refreshControl!.addTarget(self, action: #selector(refreshStatuses(_:)), for: .valueChanged)
|
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(composePressed(_:)))
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemeneted")
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
|
2019-02-08 18:37:38 +00:00
|
|
|
tableView.prefetchDataSource = self
|
|
|
|
|
2018-10-02 23:23:50 +00:00
|
|
|
if let accountID = accountID {
|
|
|
|
if MastodonCache.account(for: accountID) != nil {
|
|
|
|
updateAccountUI()
|
|
|
|
} else {
|
|
|
|
loadingVC = LoadingViewController()
|
2019-06-15 00:23:03 +00:00
|
|
|
embedChild(loadingVC!)
|
2018-10-02 23:23:50 +00:00
|
|
|
MastodonCache.account(for: accountID) { (account) in
|
|
|
|
guard account != nil else {
|
|
|
|
let alert = UIAlertController(title: "Something Went Wrong", message: "Couldn't load the selected account", preferredStyle: .alert)
|
|
|
|
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
|
|
|
|
self.navigationController!.popViewController(animated: true)
|
|
|
|
}))
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.present(alert, animated: true)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-10-01 01:12:23 +00:00
|
|
|
DispatchQueue.main.async {
|
2018-10-02 23:23:50 +00:00
|
|
|
self.updateAccountUI()
|
|
|
|
self.tableView.reloadData()
|
2018-10-01 01:12:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-02 23:23:50 +00:00
|
|
|
} else {
|
|
|
|
loadingVC = LoadingViewController()
|
2019-06-15 00:23:03 +00:00
|
|
|
embedChild(loadingVC!)
|
2018-10-02 23:23:50 +00:00
|
|
|
shouldLoadOnAccountIDSet = true
|
2018-08-28 01:27:34 +00:00
|
|
|
}
|
2018-10-01 01:12:23 +00:00
|
|
|
|
2019-08-03 00:05:47 +00:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
|
2018-10-01 01:12:23 +00:00
|
|
|
}
|
2019-08-03 00:05:47 +00:00
|
|
|
|
2018-10-12 01:52:11 +00:00
|
|
|
override var previewActionItems: [UIPreviewActionItem] {
|
|
|
|
var actions = [UIPreviewActionItem]()
|
|
|
|
if let account = MastodonCache.account(for: accountID) {
|
|
|
|
actions.append(UIPreviewAction(title: "Open in Safari", style: .default, handler: { (_, _) in
|
2019-01-19 19:31:31 +00:00
|
|
|
let vc = SFSafariViewController(url: account.url)
|
2018-10-12 01:52:11 +00:00
|
|
|
UIApplication.shared.delegate!.window!!.rootViewController!.present(vc, animated: true)
|
2019-01-19 19:31:31 +00:00
|
|
|
// TODO: kill this ^ with fire
|
2018-10-12 01:52:11 +00:00
|
|
|
}))
|
|
|
|
actions.append(UIPreviewAction(title: "Share", style: .default, handler: { (_, _) in
|
|
|
|
let vc = UIActivityViewController(activityItems: [account.url], applicationActivities: nil)
|
|
|
|
UIApplication.shared.delegate!.window!!.rootViewController!.present(vc, animated: true)
|
|
|
|
}))
|
|
|
|
actions.append(UIPreviewAction(title: "Send Message", style: .default, handler: { (_, _) in
|
2019-01-19 19:31:31 +00:00
|
|
|
let vc = UINavigationController(rootViewController: ComposeViewController(mentioningAcct: account.acct))
|
2018-10-12 01:52:11 +00:00
|
|
|
UIApplication.shared.delegate!.window!!.rootViewController!.present(vc, animated: true)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
return actions
|
|
|
|
}
|
|
|
|
|
2018-10-01 01:12:23 +00:00
|
|
|
func updateAccountUI() {
|
2019-06-15 00:23:03 +00:00
|
|
|
loadingVC?.removeViewAndController()
|
2018-10-02 23:23:50 +00:00
|
|
|
|
2018-08-29 01:18:58 +00:00
|
|
|
updateUIForPreferences()
|
2018-10-01 01:12:23 +00:00
|
|
|
|
|
|
|
getStatuses() { response in
|
|
|
|
guard case let .success(statuses, pagination) = response else { fatalError() }
|
2019-08-03 00:23:45 +00:00
|
|
|
|
2018-10-01 01:12:23 +00:00
|
|
|
MastodonCache.addAll(statuses: statuses)
|
2019-08-03 00:23:45 +00:00
|
|
|
self.timelineSegments.append(TimelineSegment(objects: statuses))
|
|
|
|
|
2018-10-01 01:12:23 +00:00
|
|
|
self.older = pagination?.older
|
|
|
|
self.newer = pagination?.newer
|
|
|
|
}
|
2018-08-29 01:18:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 00:05:47 +00:00
|
|
|
@objc func updateUIForPreferences() {
|
2018-09-18 16:59:07 +00:00
|
|
|
guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }
|
2018-08-29 01:18:58 +00:00
|
|
|
navigationItem.title = account.realDisplayName
|
2018-08-28 23:49:31 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 01:12:23 +00:00
|
|
|
func getStatuses(for range: RequestRange = .default, completion: @escaping Client.Callback<[Status]>) {
|
|
|
|
let request = Account.getStatuses(accountID, range: range, onlyMedia: false, pinned: false, excludeReplies: !Preferences.shared.showRepliesInProfiles)
|
2018-10-02 23:31:00 +00:00
|
|
|
MastodonController.client.run(request, completion: completion)
|
2018-10-01 01:12:23 +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!)") }
|
2019-01-19 19:31:31 +00:00
|
|
|
let vc = UINavigationController(rootViewController: ComposeViewController(mentioningAcct: account.acct))
|
2018-08-31 02:30:19 +00:00
|
|
|
present(vc, animated: true)
|
|
|
|
}
|
|
|
|
|
2018-08-28 01:27:34 +00:00
|
|
|
// MARK: - Table view data source
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2019-08-03 00:23:45 +00:00
|
|
|
return 1 + timelineSegments.count
|
2018-08-28 01:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-08-03 00:23:45 +00:00
|
|
|
if section == 0 {
|
2018-10-02 23:23:50 +00:00
|
|
|
return accountID == nil || MastodonCache.account(for: accountID) == nil ? 0 : 1
|
2019-08-03 00:23:45 +00:00
|
|
|
} else {
|
|
|
|
return timelineSegments[section - 1].count
|
2018-08-28 01:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2019-08-03 00:23:45 +00:00
|
|
|
if indexPath.section == 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
|
|
|
|
cell.delegate = self
|
2019-04-01 23:34:50 +00:00
|
|
|
cell.updateUI(for: accountID)
|
2018-08-28 01:27:34 +00:00
|
|
|
return cell
|
2019-08-03 00:23:45 +00:00
|
|
|
} else {
|
2018-08-28 18:29:06 +00:00
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() }
|
2019-08-03 00:23:45 +00:00
|
|
|
let statusID = timelineSegments[indexPath.section - 1][indexPath.row]
|
|
|
|
cell.updateUI(statusID: statusID)
|
2018-08-28 01:27:34 +00:00
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
2019-08-03 00:23:45 +00:00
|
|
|
if indexPath.section == timelineSegments.count && indexPath.row == timelineSegments[indexPath.section - 1].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() }
|
2019-08-03 00:23:45 +00:00
|
|
|
|
2018-09-18 16:59:07 +00:00
|
|
|
MastodonCache.addAll(statuses: newStatuses)
|
2019-08-03 00:23:45 +00:00
|
|
|
self.timelineSegments[indexPath.section - 1].append(objects: newStatuses)
|
|
|
|
|
|
|
|
self.older = pagination?.older
|
2018-08-28 01:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:01:13 +00:00
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
2019-06-14 02:31:36 +00:00
|
|
|
return true
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2019-06-14 02:31:36 +00:00
|
|
|
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2019-06-14 02:31:36 +00:00
|
|
|
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-20 16:03:18 +00:00
|
|
|
@objc func refreshStatuses(_ sender: Any) {
|
2018-08-28 01:27:34 +00:00
|
|
|
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() }
|
2019-08-03 00:23:45 +00:00
|
|
|
|
2018-09-18 16:59:07 +00:00
|
|
|
MastodonCache.addAll(statuses: newStatuses)
|
2019-08-03 00:23:45 +00:00
|
|
|
self.timelineSegments[0].insertAtBeginning(objects: newStatuses)
|
|
|
|
|
|
|
|
self.newer = pagination?.newer
|
|
|
|
|
2018-08-28 01:27:34 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.refreshControl?.endRefreshing()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 02:30:19 +00:00
|
|
|
|
2018-10-20 16:03:18 +00:00
|
|
|
@objc func composePressed(_ sender: Any) {
|
2018-08-31 02:30:19 +00:00
|
|
|
sendMessageMentioning()
|
|
|
|
}
|
|
|
|
|
2018-08-28 01:27:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:29:52 +00:00
|
|
|
extension ProfileTableViewController: StatusTableViewCellDelegate {}
|
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)
|
2018-10-12 01:52:11 +00:00
|
|
|
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)
|
|
|
|
}))
|
2018-10-12 01:52:11 +00:00
|
|
|
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)
|
|
|
|
}))
|
2018-10-12 01:52:11 +00:00
|
|
|
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)
|
2018-10-02 23:31:00 +00:00
|
|
|
MastodonController.client.run(request, completion: { (response) in
|
2018-09-24 12:49:39 +00:00
|
|
|
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
|
|
|
}
|
2019-02-08 18:37:38 +00:00
|
|
|
|
|
|
|
extension ProfileTableViewController: UITableViewDataSourcePrefetching {
|
|
|
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
2019-08-03 00:23:45 +00:00
|
|
|
for indexPath in indexPaths where indexPath.section > 0 {
|
|
|
|
let statusID = timelineSegments[indexPath.section - 1][indexPath.row]
|
|
|
|
guard let status = MastodonCache.status(for: statusID) else { continue }
|
2019-02-08 18:37:38 +00:00
|
|
|
ImageCache.avatars.get(status.account.avatar, completion: nil)
|
|
|
|
for attachment in status.attachments {
|
|
|
|
ImageCache.attachments.get(attachment.url, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
2019-08-03 00:23:45 +00:00
|
|
|
for indexPath in indexPaths where indexPath.section > 0 {
|
|
|
|
let statusID = timelineSegments[indexPath.section - 1][indexPath.row]
|
|
|
|
guard let status = MastodonCache.status(for: statusID) else { continue }
|
2019-02-08 18:37:38 +00:00
|
|
|
ImageCache.avatars.cancel(status.account.avatar)
|
|
|
|
for attachment in status.attachments {
|
|
|
|
ImageCache.attachments.cancel(attachment.url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|