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
|
|
|
|
import MastodonKit
|
|
|
|
import SafariServices
|
|
|
|
|
2018-08-29 01:18:58 +00:00
|
|
|
class ProfileTableViewController: UITableViewController, PreferencesAdaptive {
|
2018-08-28 01:27:34 +00:00
|
|
|
|
|
|
|
static func create(for account: Account) -> UIViewController {
|
|
|
|
guard let profileController = UIStoryboard(name: "Profile", bundle: nil).instantiateInitialViewController() as? ProfileTableViewController else { fatalError() }
|
|
|
|
profileController.account = account
|
|
|
|
return profileController
|
|
|
|
}
|
|
|
|
|
|
|
|
var account: Account!
|
|
|
|
|
|
|
|
var statuses: [Status] = [] {
|
|
|
|
didSet {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.tableView.reloadData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var newer: RequestRange?
|
|
|
|
var older: RequestRange?
|
|
|
|
|
|
|
|
func request(for range: RequestRange? = .default) -> Request<[Status]> {
|
|
|
|
let range = range ?? .default
|
2018-08-28 23:16:17 +00:00
|
|
|
return Accounts.statuses(id: account.id, mediaOnly: false, pinnedOnly: false, excludeReplies: !Preferences.shared.showRepliesInProfiles, range: range)
|
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")
|
|
|
|
|
2018-08-29 01:18:58 +00:00
|
|
|
updateUIForPreferences()
|
2018-08-28 01:27:34 +00:00
|
|
|
|
2018-08-31 02:30:19 +00:00
|
|
|
|
|
|
|
|
2018-08-28 01:27:34 +00:00
|
|
|
MastodonController.shared.client.run(request()) { result in
|
|
|
|
guard case let .success(statuses, pagination) = result else { fatalError() }
|
|
|
|
self.statuses = statuses
|
|
|
|
self.newer = pagination?.previous
|
|
|
|
self.older = pagination?.next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:49:31 +00:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
2018-08-29 01:46:57 +00:00
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
2018-08-28 23:49:31 +00:00
|
|
|
for cell in tableView.visibleCells {
|
|
|
|
if let cell = cell as? PreferencesAdaptive {
|
|
|
|
cell.updateUIForPreferences()
|
|
|
|
}
|
|
|
|
}
|
2018-08-29 01:18:58 +00:00
|
|
|
updateUIForPreferences()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUIForPreferences() {
|
|
|
|
navigationItem.title = account.realDisplayName
|
2018-08-28 23:49:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 02:30:19 +00:00
|
|
|
func sendMessageMentioning() {
|
|
|
|
let vc = ComposeViewController.create(mentioning: account)
|
|
|
|
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:
|
|
|
|
return statuses.count
|
|
|
|
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
|
|
|
|
cell.updateUI(for: account)
|
|
|
|
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-08-28 01:27:34 +00:00
|
|
|
let status = statuses[indexPath.row]
|
|
|
|
cell.updateUI(for: status)
|
|
|
|
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) {
|
|
|
|
if indexPath.section == 1 && indexPath.row == statuses.count - 1 {
|
|
|
|
guard let older = older else { return }
|
|
|
|
|
|
|
|
MastodonController.shared.client.run(request(for: older)) { result in
|
|
|
|
guard case let .success(newStatuses, pagination) = result else { fatalError() }
|
|
|
|
self.older = pagination?.next
|
|
|
|
self.statuses.append(contentsOf: newStatuses)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func refreshStatuses(_ sender: Any) {
|
|
|
|
guard let newer = newer else { return }
|
|
|
|
|
|
|
|
MastodonController.shared.client.run(request(for: 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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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() {
|
|
|
|
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
|
|
|
|
alert.addAction(UIAlertAction(title: "Open in Safari...", style: .default, handler: { _ in
|
|
|
|
let vc = SFSafariViewController(url: URL(string: self.account.url)!)
|
|
|
|
self.present(vc, animated: true)
|
|
|
|
}))
|
|
|
|
alert.addAction(UIAlertAction(title: "Share...", style: .default, handler: { _ in
|
|
|
|
let vc = UIActivityViewController(activityItems: [URL(string: self.account.url)!], applicationActivities: nil)
|
|
|
|
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))
|
|
|
|
present(alert, animated: true)
|
|
|
|
}
|
|
|
|
|
2018-08-28 01:27:34 +00:00
|
|
|
}
|
2018-09-03 22:46:20 +00:00
|
|
|
|
|
|
|
extension ProfileTableViewController: LargeImageViewControllerDelegate {}
|