From e3b62c8e83cff568323dc7123350d93c83b1e1ab Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 2 Oct 2018 19:23:50 -0400 Subject: [PATCH] Add my account tab --- Tusker/Controllers/MastodonController.swift | 15 ++-- .../Main/MainTabBarViewController.swift | 28 ++++++-- .../NotificationsTableViewController.swift | 2 +- .../PreferencesTableViewController.swift | 2 +- .../Profile/ProfileTableViewController.swift | 69 ++++++++++++------- .../TimelineTableViewController.swift | 2 +- 6 files changed, 80 insertions(+), 38 deletions(-) diff --git a/Tusker/Controllers/MastodonController.swift b/Tusker/Controllers/MastodonController.swift index d2c997fd..5cf7c0d8 100644 --- a/Tusker/Controllers/MastodonController.swift +++ b/Tusker/Controllers/MastodonController.swift @@ -54,11 +54,16 @@ class MastodonController { } } - func getOwnAccount() { - let request = client.getSelfAccount() - client.run(request) { response in - guard case let .success(account, _) = response else { fatalError() } - self.account = account + func getOwnAccount(completion: ((Account) -> Void)? = nil) { + if account != nil { + completion?(account) + } else { + let request = client.getSelfAccount() + client.run(request) { response in + guard case let .success(account, _) = response else { fatalError() } + self.account = account + completion?(account) + } } } diff --git a/Tusker/Screens/Main/MainTabBarViewController.swift b/Tusker/Screens/Main/MainTabBarViewController.swift index 396667e8..06e3f718 100644 --- a/Tusker/Screens/Main/MainTabBarViewController.swift +++ b/Tusker/Screens/Main/MainTabBarViewController.swift @@ -13,13 +13,31 @@ class MainTabBarViewController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() + let home = TimelineTableViewController.create(for: .home) + + let federated = TimelineTableViewController.create(for: .public(local: false)) + + let local = TimelineTableViewController.create(for: .public(local: true)) + + let ownProfile = ProfileTableViewController.createForPending() + ownProfile.title = "My Profile" + + let notifications = NotificationsTableViewController.create() + + let preferences = PreferencesTableViewController.create() + viewControllers = [ - navigationController(for: TimelineTableViewController.create(for: .home)), - navigationController(for: TimelineTableViewController.create(for: .public(local: false))), - navigationController(for: TimelineTableViewController.create(for: .public(local: true))), - NotificationsTableViewController.create(), - PreferencesTableViewController.create() + navigationController(for: home), + navigationController(for: federated), + navigationController(for: local), + navigationController(for: ownProfile), + notifications, + preferences ] + + MastodonController.shared.getOwnAccount { (account) in + ownProfile.accountID = account.id + } } func navigationController(for vc: UIViewController) -> UINavigationController { diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index 80c3a791..96ee9764 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -11,7 +11,7 @@ import Pachyderm class NotificationsTableViewController: UITableViewController { - static func create() -> UIViewController { + static func create() -> UINavigationController { guard let navigationController = UIStoryboard(name: "Notifications", bundle: nil).instantiateInitialViewController() as? UINavigationController else { fatalError() } return navigationController } diff --git a/Tusker/Screens/Preferences/PreferencesTableViewController.swift b/Tusker/Screens/Preferences/PreferencesTableViewController.swift index efa44086..02a22241 100644 --- a/Tusker/Screens/Preferences/PreferencesTableViewController.swift +++ b/Tusker/Screens/Preferences/PreferencesTableViewController.swift @@ -10,7 +10,7 @@ import UIKit class PreferencesTableViewController: UITableViewController { - static func create() -> UIViewController { + static func create() -> UINavigationController { guard let navigationController = UIStoryboard(name: "Preferences", bundle: nil).instantiateInitialViewController() as? UINavigationController else { fatalError() } return navigationController } diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index fc34c899..e2504518 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -12,13 +12,25 @@ import SafariServices class ProfileTableViewController: UITableViewController, PreferencesAdaptive { - static func create(for accountID: String) -> UIViewController { + static func create(for accountID: String) -> ProfileTableViewController { guard let profileController = UIStoryboard(name: "Profile", bundle: nil).instantiateInitialViewController() as? ProfileTableViewController else { fatalError() } profileController.accountID = accountID return profileController } - var accountID: String! + static func createForPending() -> ProfileTableViewController { + guard let profileController = UIStoryboard(name: "Profile", bundle: nil).instantiateInitialViewController() as? ProfileTableViewController else { fatalError() } + profileController.accountID = nil + return profileController + } + + var accountID: String! { + didSet { + if shouldLoadOnAccountIDSet { + updateAccountUI() + } + } + } var statusIDs: [String] = [] { didSet { @@ -31,6 +43,9 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { var older: RequestRange? var newer: RequestRange? + var shouldLoadOnAccountIDSet = false + var loadingVC: LoadingViewController? = nil + override func viewDidLoad() { super.viewDidLoad() @@ -40,29 +55,33 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell") tableView.register(UINib(nibName: "ProfileHeaderTableViewCell", bundle: nil), forCellReuseIdentifier: "headerCell") - if MastodonCache.account(for: accountID) != nil { - updateAccountUI() - } else { - let loadingVC = LoadingViewController() - add(loadingVC) - 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) + if let accountID = accountID { + if MastodonCache.account(for: accountID) != nil { + updateAccountUI() + } else { + loadingVC = LoadingViewController() + add(loadingVC!) + 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 { + self.updateAccountUI() + self.tableView.reloadData() } - return - } - DispatchQueue.main.async { - loadingVC.remove() - self.updateAccountUI() - self.tableView.reloadData() } } - + } else { + loadingVC = LoadingViewController() + add(loadingVC!) + shouldLoadOnAccountIDSet = true } } @@ -81,6 +100,8 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { } func updateAccountUI() { + loadingVC?.remove() + updateUIForPreferences() getStatuses() { response in @@ -127,7 +148,7 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case 0: - return MastodonCache.account(for: accountID) == nil ? 0 : 1 + return accountID == nil || MastodonCache.account(for: accountID) == nil ? 0 : 1 case 1: return statusIDs.count default: @@ -201,7 +222,6 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive { extension ProfileTableViewController: StatusTableViewCellDelegate {} extension ProfileTableViewController: ProfileHeaderTableViewCellDelegate { - func showMoreOptions() { let account = MastodonCache.account(for: accountID)! @@ -245,6 +265,5 @@ extension ProfileTableViewController: ProfileHeaderTableViewCellDelegate { } } } - } extension ProfileTableViewController: LargeImageViewControllerDelegate {} diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index 614dd7fb..0f631cf0 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -11,7 +11,7 @@ import Pachyderm class TimelineTableViewController: UITableViewController { - static func create(for timeline: Timeline) -> UIViewController { + static func create(for timeline: Timeline) -> TimelineTableViewController { guard let timelineController = UIStoryboard(name: "Timeline", bundle: nil).instantiateInitialViewController() as? TimelineTableViewController else { fatalError() } timelineController.timeline = timeline