Add my account tab

This commit is contained in:
Shadowfacts 2018-10-02 19:23:50 -04:00
parent 57cbebecbd
commit 05e747a6f4
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 80 additions and 38 deletions

View File

@ -54,11 +54,16 @@ class MastodonController {
}
}
func getOwnAccount() {
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)
}
}
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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
}

View File

@ -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,11 +55,12 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive {
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
tableView.register(UINib(nibName: "ProfileHeaderTableViewCell", bundle: nil), forCellReuseIdentifier: "headerCell")
if let accountID = accountID {
if MastodonCache.account(for: accountID) != nil {
updateAccountUI()
} else {
let loadingVC = LoadingViewController()
add(loadingVC)
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)
@ -57,12 +73,15 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive {
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 {}

View File

@ -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