Skip to content

Commit d001035

Browse files
Fix empty feed cells
1 parent 42997b4 commit d001035

2 files changed

Lines changed: 12 additions & 20 deletions

File tree

FyreplaceLegacy/Sources/ViewControllers/FeedViewController.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class FeedViewController: UITableViewController {
99
@IBOutlet
1010
var help: UIBarButtonItem!
1111

12-
private var postCount = 0
12+
private var posts: [FPPost] = []
1313
private var isAuthenticated = false
1414
private var cancellables = Set<AnyCancellable>()
1515

@@ -57,7 +57,7 @@ class FeedViewController: UITableViewController {
5757
let cell = sender as? UITableViewCell,
5858
let position = tableView.indexPath(for: cell)?.row
5959
{
60-
postController.post = vm.post(at: position)
60+
postController.post = posts[position]
6161
}
6262
}
6363

@@ -103,18 +103,18 @@ class FeedViewController: UITableViewController {
103103

104104
extension FeedViewController {
105105
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
106-
tableView.backgroundView = postCount == 0 ? emptyPlaceholder : nil
107-
return postCount
106+
tableView.backgroundView = posts.count == 0 ? emptyPlaceholder : nil
107+
return posts.count
108108
}
109109

110110
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
111-
let post = vm.post(at: indexPath.row)
111+
let post = posts[indexPath.row]
112112
let cell = tableView.dequeueReusableCell(
113-
withIdentifier: post?.chapters.first?.text.isEmpty ?? false ? "Image" : "Text",
113+
withIdentifier: post.chapters.first?.text.isEmpty ?? false ? "Image" : "Text",
114114
for: indexPath
115115
)
116116

117-
guard let cell = cell as? FeedTableViewCell, let post else { return cell }
117+
guard let cell = cell as? FeedTableViewCell else { return cell }
118118
cell.delegate = self
119119
cell.setup(withPost: post)
120120
return cell
@@ -132,34 +132,30 @@ extension FeedViewController: FeedViewModelDelegate {
132132
}
133133

134134
func feedViewModel(_ viewModel: FeedViewModel, didReceivePostAtPosition position: Int) {
135+
guard let post = vm.post(at: position) else { return }
135136
DispatchQueue.main.async { [self] in
136-
postCount += 1
137+
posts.append(post)
137138
tableView.insertRows(at: .init(row: position, section: 0), with: .automatic)
138139
stopRefreshing()
139140
}
140141
}
141142

142143
func feedViewModel(_ viewModel: FeedViewModel, didUpdatePostAtPosition position: Int) {
144+
guard let post = vm.post(at: position) else { return }
143145
DispatchQueue.main.async { [self] in
146+
posts[position] = post
144147
tableView.reloadRows(at: .init(row: position, section: 0), with: .automatic)
145148
stopRefreshing()
146149
}
147150
}
148151

149152
func feedViewModel(_ viewModel: FeedViewModel, didDismissPostAtPosition position: Int) {
150153
DispatchQueue.main.async { [self] in
151-
postCount -= 1
154+
posts.remove(at: position)
152155
tableView.deleteRows(at: .init(row: position, section: 0), with: .automatic)
153156
}
154157
}
155158

156-
func didDismissAllPosts(_ viewModel: FeedViewModel) {
157-
DispatchQueue.main.async { [self] in
158-
postCount = 0
159-
tableView.reloadData()
160-
}
161-
}
162-
163159
func didFinishListing(_ viewModel: FeedViewModel) {
164160
DispatchQueue.main.async { self.stopRefreshing() }
165161
}

FyreplaceLegacy/Sources/ViewModels/FeedViewModel.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ class FeedViewModel: ViewModel {
4242

4343
func refresh() {
4444
stopListing()
45-
posts.removeAll()
46-
delegate?.didDismissAllPosts(self)
4745
startListing()
4846
}
4947

@@ -75,7 +73,5 @@ protocol FeedViewModelDelegate: ViewModelDelegate {
7573

7674
func feedViewModel(_ viewModel: FeedViewModel, didDismissPostAtPosition position: Int)
7775

78-
func didDismissAllPosts(_ viewModel: FeedViewModel)
79-
8076
func didFinishListing(_ viewModel: FeedViewModel)
8177
}

0 commit comments

Comments
 (0)