From 36326e4469eb7d3d3016ae382cda13a8c0ca35e1 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 15 Jun 2020 19:39:44 -0400 Subject: [PATCH] Make network requests in viewWillAppear instead of viewDidLoad --- .../BookmarksTableViewController.swift | 34 ++++++++++------ .../NotificationsTableViewController.swift | 36 ++++++++++------- .../Profile/ProfileTableViewController.swift | 39 +++++++++---------- .../TimelineTableViewController.swift | 9 +++++ 4 files changed, 73 insertions(+), 45 deletions(-) diff --git a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift index 18dc5cc3..c842594d 100644 --- a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift +++ b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift @@ -15,6 +15,8 @@ class BookmarksTableViewController: EnhancedTableViewController { let mastodonController: MastodonController + private var loaded = false + var statuses: [(id: String, state: StatusState)] = [] var newer: RequestRange? @@ -41,22 +43,30 @@ class BookmarksTableViewController: EnhancedTableViewController { tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: statusCell) tableView.prefetchDataSource = self + + userActivity = UserActivityManager.bookmarksActivity() + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) - let request = Client.getBookmarks() - mastodonController.run(request) { (response) in - guard case let .success(statuses, pagination) = response else { fatalError() } - self.mastodonController.persistentContainer.addAll(statuses: statuses) { - self.statuses.append(contentsOf: statuses.map { ($0.id, .unknown) }) - self.newer = pagination?.newer - self.older = pagination?.older - - DispatchQueue.main.async { - self.tableView.reloadData() + if !loaded { + loaded = true + + let request = Client.getBookmarks() + mastodonController.run(request) { (response) in + guard case let .success(statuses, pagination) = response else { fatalError() } + self.mastodonController.persistentContainer.addAll(statuses: statuses) { + self.statuses.append(contentsOf: statuses.map { ($0.id, .unknown) }) + self.newer = pagination?.newer + self.older = pagination?.older + + DispatchQueue.main.async { + self.tableView.reloadData() + } } } } - - userActivity = UserActivityManager.bookmarksActivity() } // MARK: - Table view data source diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index a9b5d98f..d11cb1f9 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -21,6 +21,8 @@ class NotificationsTableViewController: EnhancedTableViewController { let excludedTypes: [Pachyderm.Notification.Kind] let groupTypes = [Notification.Kind.favourite, .reblog, .follow] + + private var loaded = false var groups: [NotificationGroup] = [] @@ -54,21 +56,29 @@ class NotificationsTableViewController: EnhancedTableViewController { tableView.register(UINib(nibName: "BasicTableViewCell", bundle: .main), forCellReuseIdentifier: unknownCell) tableView.prefetchDataSource = self + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) - let request = Client.getNotifications(excludeTypes: excludedTypes) - mastodonController.run(request) { result in - guard case let .success(notifications, pagination) = result else { fatalError() } + if !loaded { + loaded = true - let groups = NotificationGroup.createGroups(notifications: notifications, only: self.groupTypes) - - self.groups.append(contentsOf: groups) - - self.newer = pagination?.newer - self.older = pagination?.older - - self.mastodonController.persistentContainer.addAll(notifications: notifications) { - DispatchQueue.main.async { - self.tableView.reloadData() + let request = Client.getNotifications(excludeTypes: excludedTypes) + mastodonController.run(request) { result in + guard case let .success(notifications, pagination) = result else { fatalError() } + + let groups = NotificationGroup.createGroups(notifications: notifications, only: self.groupTypes) + + self.groups.append(contentsOf: groups) + + self.newer = pagination?.newer + self.older = pagination?.older + + self.mastodonController.persistentContainer.addAll(notifications: notifications) { + DispatchQueue.main.async { + self.tableView.reloadData() + } } } } diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index f7b1b539..ec85650c 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -14,15 +14,7 @@ class ProfileTableViewController: EnhancedTableViewController { weak var mastodonController: MastodonController! - var accountID: String! { - didSet { - if shouldLoadOnAccountIDSet { - DispatchQueue.main.async { - self.updateAccountUI() - } - } - } - } + var accountID: String! var pinnedStatuses: [(id: String, state: StatusState)] = [] { didSet { @@ -42,8 +34,8 @@ class ProfileTableViewController: EnhancedTableViewController { var older: RequestRange? var newer: RequestRange? - var shouldLoadOnAccountIDSet = false - var loadingVC: LoadingViewController? = nil + private var loadingVC: LoadingViewController? = nil + private var loaded = false init(accountID: String?, mastodonController: MastodonController) { self.mastodonController = mastodonController @@ -80,7 +72,22 @@ class ProfileTableViewController: EnhancedTableViewController { tableView.prefetchDataSource = self - if let accountID = accountID { + if accountID == nil { + loadingVC = LoadingViewController() + embedChild(loadingVC!) + } + + NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + if !loaded, let accountID = accountID { + loaded = true + loadingVC?.removeViewAndController() + loadingVC = nil + if mastodonController.persistentContainer.account(for: accountID) != nil { updateAccountUI() } else { @@ -106,18 +113,10 @@ class ProfileTableViewController: EnhancedTableViewController { } } } - } else { - loadingVC = LoadingViewController() - embedChild(loadingVC!) - shouldLoadOnAccountIDSet = true } - - NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } func updateAccountUI() { - loadingVC?.removeViewAndController() - updateUIForPreferences() getStatuses(onlyPinned: true) { (response) in diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index 4a9a8d98..b88ceafb 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -14,6 +14,8 @@ class TimelineTableViewController: EnhancedTableViewController { var timeline: Timeline! weak var mastodonController: MastodonController! + private var loaded = false + var timelineSegments: [[(id: String, state: StatusState)]] = [] var newer: RequestRange? @@ -63,11 +65,18 @@ class TimelineTableViewController: EnhancedTableViewController { tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell") tableView.prefetchDataSource = self + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) loadInitialStatuses() } func loadInitialStatuses() { + guard !loaded else { return } + loaded = true + let request = Client.getStatuses(timeline: timeline) mastodonController.run(request) { response in guard case let .success(statuses, pagination) = response else { fatalError() }