Skip to content

Commit d44173c

Browse files
committed
Add new iOS 17 empty states
1 parent 30fffda commit d44173c

File tree

4 files changed

+61
-36
lines changed

4 files changed

+61
-36
lines changed

GHFollowers/Screens/FavoritesListViewController.swift

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,26 @@ class FavoritesListViewController: GFDataLoadingViewController {
2626

2727
getFavorites()
2828
}
29-
29+
30+
override func updateContentUnavailableConfiguration(
31+
using state: UIContentUnavailableConfigurationState
32+
) {
33+
super.updateContentUnavailableConfiguration(using: state)
34+
35+
if favorites.isEmpty {
36+
var configuration = UIContentUnavailableConfiguration.empty()
37+
configuration.image = .init(systemName: "star")
38+
configuration.text = String(localized: "No Favourites", comment: "Empty state message.")
39+
configuration.secondaryText = String(
40+
localized: "Add a favorite on the follower list screen",
41+
comment: "Empty state secondary message."
42+
)
43+
contentUnavailableConfiguration = configuration
44+
} else {
45+
contentUnavailableConfiguration = nil
46+
}
47+
}
48+
3049
func configureViewController() {
3150
view.backgroundColor = .systemBackground
3251
title = NSLocalizedString("Favorites", comment: "Faforites screen title (appears in the navigation bar).")
@@ -63,22 +82,13 @@ class FavoritesListViewController: GFDataLoadingViewController {
6382
}
6483
}
6584
}
66-
85+
6786
func updateUI(with favorites: [Follower]) {
68-
if favorites.isEmpty {
69-
self.showEmptyStateView(
70-
withMessage:
71-
NSLocalizedString("No Favorites", comment: "Title for no favorites message.")
72-
+ "\n"
73-
+ NSLocalizedString("No Favorites Body", comment: "Something like: Add one from the followers screen."),
74-
in: self.view
75-
)
76-
} else {
77-
self.favorites = favorites
78-
DispatchQueue.main.async {
79-
self.tableView.reloadData()
80-
self.view.bringSubviewToFront(self.tableView)
81-
}
87+
self.favorites = favorites
88+
setNeedsUpdateContentUnavailableConfiguration()
89+
DispatchQueue.main.async {
90+
self.tableView.reloadData()
91+
self.view.bringSubviewToFront(self.tableView)
8292
}
8393
}
8494

@@ -120,16 +130,7 @@ extension FavoritesListViewController: UITableViewDataSource, UITableViewDelegat
120130
guard let error else {
121131
self.favorites.remove(at: indexPath.row)
122132
tableView.deleteRows(at: [indexPath], with: .automatic)
123-
124-
if self.favorites.isEmpty {
125-
self.showEmptyStateView(
126-
withMessage:
127-
NSLocalizedString("No Favorites", comment: "Title for no favorites message.")
128-
+ "\n"
129-
+ NSLocalizedString("No Favorites Body", comment: "Something like: Add one from the followers screen."),
130-
in: self.view
131-
)
132-
}
133+
setNeedsUpdateContentUnavailableConfiguration()
133134

134135
return
135136
}

GHFollowers/Screens/FollowersListViewController.swift

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,28 @@ class FollowersListViewController: GFDataLoadingViewController {
5454

5555
navigationController?.setNavigationBarHidden(false, animated: animated)
5656
}
57-
57+
58+
override func updateContentUnavailableConfiguration(
59+
using state: UIContentUnavailableConfigurationState
60+
) {
61+
super.updateContentUnavailableConfiguration(using: state)
62+
63+
if followers.isEmpty, !isLoadingMoreFollowers {
64+
var configuration = UIContentUnavailableConfiguration.empty()
65+
configuration.image = .init(systemName: "person.slash")
66+
configuration.text = String(localized: "No Followers", comment: "Empty state message.")
67+
configuration.secondaryText = String(
68+
localized: "This user has no followers. Go follow them!",
69+
comment: "Empty state secondary message."
70+
)
71+
contentUnavailableConfiguration = configuration
72+
} else if isSearching, filteredFollowers.isEmpty {
73+
contentUnavailableConfiguration = UIContentUnavailableConfiguration.search()
74+
} else {
75+
contentUnavailableConfiguration = nil
76+
}
77+
}
78+
5879
func configureViewController() {
5980
view.backgroundColor = .systemBackground
6081
navigationController?.navigationBar.prefersLargeTitles = true
@@ -86,36 +107,38 @@ class FollowersListViewController: GFDataLoadingViewController {
86107
Task {
87108
do {
88109
let followers = try await NetworkManager.shared.getFollowers(for: username, page: page)
110+
dismissLoadingView()
111+
isLoadingMoreFollowers = false
89112
updateUI(with: followers)
90113
} catch let error as GFError {
114+
dismissLoadingView()
115+
isLoadingMoreFollowers = false
91116
presentAlert(
92117
title: NSLocalizedString("Bad stuff happened", comment: "Error message title."),
93118
message: error.localizedDescription,
94119
buttonTitle: NSLocalizedString("OK", comment: "Button: OK.")
95120
)
96121
} catch {
122+
dismissLoadingView()
123+
isLoadingMoreFollowers = false
97124
presentDefaultAlert()
98125
}
99-
100-
dismissLoadingView()
101-
isLoadingMoreFollowers = false
102126
}
103127
}
104128

105129
func updateUI(with followers: [Follower]) {
106130
if followers.count < 100 {
107131
self.hasMoreFollowers = false
108132
}
133+
109134
self.followers.append(contentsOf: followers)
110-
if self.followers.isEmpty {
111-
let message = NSLocalizedString("No Followers Body", comment: "This user doesn't have any followers. Go follow them 😀")
112-
DispatchQueue.main.async { self.showEmptyStateView(withMessage: message, in: self.view) }
113-
return
114-
}
135+
115136
DispatchQueue.main.async {
116137
self.searchController.searchBar.isHidden = false
117138
}
118-
self.updateData(on: self.followers)
139+
140+
updateData(on: self.followers)
141+
setNeedsUpdateContentUnavailableConfiguration()
119142
}
120143

121144
func configureDataSource() {
@@ -269,6 +292,7 @@ extension FollowersListViewController: UISearchResultsUpdating {
269292
isSearching = true
270293
filteredFollowers = followers.filter { $0.login.lowercased().contains(filter.lowercased()) }
271294
updateData(on: filteredFollowers)
295+
setNeedsUpdateContentUnavailableConfiguration()
272296
}
273297

274298

514 Bytes
Binary file not shown.
528 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)