From 9de90310022fc66b048df8f6597fe4b5a04aeac3 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 30 Sep 2018 21:12:23 -0400 Subject: [PATCH] Add tapping mentions to show profiles --- Tusker/Screens/Profile/Profile.storyboard | 4 +- .../Profile/ProfileTableViewController.swift | 62 ++++++++++++++----- Tusker/TuskerNavigationDelegate.swift | 6 +- 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/Tusker/Screens/Profile/Profile.storyboard b/Tusker/Screens/Profile/Profile.storyboard index 17d944c7..c51cae35 100644 --- a/Tusker/Screens/Profile/Profile.storyboard +++ b/Tusker/Screens/Profile/Profile.storyboard @@ -1,10 +1,10 @@ - + - + diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index 9e2a12b1..0708bcb9 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -31,11 +31,6 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { var older: RequestRange? var newer: RequestRange? - 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) - MastodonController.shared.client.run(request, completion: completion) - } - override func viewDidLoad() { super.viewDidLoad() @@ -45,14 +40,33 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell") tableView.register(UINib(nibName: "ProfileHeaderTableViewCell", bundle: nil), forCellReuseIdentifier: "headerCell") - updateUIForPreferences() - - getStatuses { response in - guard case let .success(statuses, pagination) = response else { fatalError() } - MastodonCache.addAll(statuses: statuses) - self.statusIDs = statuses.map { $0.id } - self.older = pagination?.older - self.newer = pagination?.newer + if MastodonCache.account(for: accountID) != nil { + updateAccountUI() + } else { + let activityIndicator = UIActivityIndicatorView(style: .whiteLarge) + activityIndicator.color = .darkGray + activityIndicator.startAnimating() + activityIndicator.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(activityIndicator) + activityIndicator.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor) + activityIndicator.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor) + 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 + } + DispatchQueue.main.async { + activityIndicator.removeFromSuperview() + self.updateAccountUI() + self.tableView.reloadData() + } + } } } @@ -64,7 +78,22 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { cell.updateUIForPreferences() } } + + if MastodonCache.account(for: accountID) != nil { + updateUIForPreferences() + } + } + + func updateAccountUI() { updateUIForPreferences() + + getStatuses() { response in + guard case let .success(statuses, pagination) = response else { fatalError() } + MastodonCache.addAll(statuses: statuses) + self.statusIDs = statuses.map { $0.id } + self.older = pagination?.older + self.newer = pagination?.newer + } } func updateUIForPreferences() { @@ -72,6 +101,11 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { navigationItem.title = account.realDisplayName } + 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) + MastodonController.shared.client.run(request, completion: completion) + } + func sendMessageMentioning() { guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } let vc = ComposeViewController.create(mentioning: account.acct) @@ -97,7 +131,7 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case 0: - return 1 + return MastodonCache.account(for: accountID) == nil ? 0 : 1 case 1: return statusIDs.count default: diff --git a/Tusker/TuskerNavigationDelegate.swift b/Tusker/TuskerNavigationDelegate.swift index 181b5aef..c44aaa56 100644 --- a/Tusker/TuskerNavigationDelegate.swift +++ b/Tusker/TuskerNavigationDelegate.swift @@ -45,7 +45,11 @@ extension TuskerNavigationDelegate where Self: UIViewController { } func selected(mention: Mention) { - + guard let navigationController = navigationController else { + fatalError("Can't show profile VC from mention when not in navigation controller") + } + let vc = ProfileTableViewController.create(for: mention.id) + navigationController.pushViewController(vc, animated: true) } func selected(tag: Hashtag) {