diff --git a/Tusker/Screens/Profile/ProfileStatusesViewController.swift b/Tusker/Screens/Profile/ProfileStatusesViewController.swift index 24d48d4d40..9c3d84ca63 100644 --- a/Tusker/Screens/Profile/ProfileStatusesViewController.swift +++ b/Tusker/Screens/Profile/ProfileStatusesViewController.swift @@ -25,7 +25,7 @@ class ProfileStatusesViewController: EnhancedTableViewController { private var older: RequestRange? private var newer: RequestRange? - private var loaded = false + var loaded = false init(accountID: String?, kind: Kind, mastodonController: MastodonController) { self.accountID = accountID @@ -61,12 +61,14 @@ class ProfileStatusesViewController: EnhancedTableViewController { if !loaded, let accountID = accountID, let account = mastodonController.persistentContainer.account(for: accountID) { - loaded = true updateUI(account: account) } } - private func updateUI(account: AccountMO) { + func updateUI(account: AccountMO) { + guard !loaded else { return } + loaded = true + if kind == .statuses { getPinnedStatuses { (response) in guard case let .success(statuses, _) = response else { diff --git a/Tusker/Screens/Profile/ProfileViewController.swift b/Tusker/Screens/Profile/ProfileViewController.swift index 545984ae78..512863b296 100644 --- a/Tusker/Screens/Profile/ProfileViewController.swift +++ b/Tusker/Screens/Profile/ProfileViewController.swift @@ -48,6 +48,10 @@ class ProfileViewController: UIPageViewController { required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } + + deinit { + mastodonController.persistentContainer.account(for: accountID)?.decrementReferenceCount() + } override func viewDidLoad() { super.viewDidLoad() @@ -56,7 +60,6 @@ class ProfileViewController: UIPageViewController { headerView = ProfileHeaderView.create() headerView.delegate = self - headerView.updateUI(for: accountID) selectPage(at: 0, animated: false) @@ -70,6 +73,24 @@ class ProfileViewController: UIPageViewController { .filter { [weak self] in $0 == self?.accountID } .receive(on: DispatchQueue.main) .sink { [weak self] (_) in self?.updateAccountUI() } + + if mastodonController.persistentContainer.account(for: accountID) != nil { + headerView.updateUI(for: accountID) + } else { + let req = Client.getAccount(id: accountID) + mastodonController.run(req) { [weak self] (response) in + guard let self = self else { return } + guard case let .success(account, _) = response else { fatalError() } + self.mastodonController.persistentContainer.addOrUpdate(account: account, incrementReferenceCount: true) { (account) in + DispatchQueue.main.async { + self.headerView.updateUI(for: self.accountID) + self.pageControllers.forEach { + $0.updateUI(account: account) + } + } + } + } + } } private func updateAccountUI() {