Skip to content

Commit d91d581

Browse files
committed
Don't maintain replyID locally, use comment.replyID. Only update reply indicator view if the reply changes.
1 parent f9221c6 commit d91d581

1 file changed

Lines changed: 30 additions & 26 deletions

File tree

WordPress/Classes/ViewRelated/Comments/CommentDetailViewController.swift

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,7 @@ class CommentDetailViewController: UIViewController {
4343
}
4444

4545
private var replyID: Int32 {
46-
didSet {
47-
// toggle reply indicator cell visibility only when the value changes from 0 to any positive number, or vice versa.
48-
if oldValue == 0 && replyID > 0 {
49-
// show the reply indicator row.
50-
// update the rows first so replyIndicator is present in `rows`.
51-
configureRows()
52-
guard let replyIndicatorRow = rows.firstIndex(of: .replyIndicator) else {
53-
tableView.reloadData()
54-
return
55-
}
56-
tableView.insertRows(at: [IndexPath(row: replyIndicatorRow, section: .zero)], with: .fade)
57-
58-
} else if oldValue > 0 && replyID == 0 {
59-
// hide the reply indicator row.
60-
// get the reply indicator row first before it is removed via `configureRows`.
61-
guard let replyIndicatorRow = rows.firstIndex(of: .replyIndicator) else {
62-
return
63-
}
64-
configureRows()
65-
tableView.deleteRows(at: [IndexPath(row: replyIndicatorRow, section: .zero)], with: .fade)
66-
}
67-
}
46+
return comment.replyID
6847
}
6948

7049
private var isCommentReplied: Bool {
@@ -204,7 +183,6 @@ class CommentDetailViewController: UIViewController {
204183
self.comment = comment
205184
self.isLastInList = isLastInList
206185
self.managedObjectContext = managedObjectContext
207-
self.replyID = comment.replyID
208186
super.init(nibName: nil, bundle: nil)
209187
}
210188

@@ -214,7 +192,6 @@ class CommentDetailViewController: UIViewController {
214192
self.comment = comment
215193
self.notification = notification
216194
self.managedObjectContext = managedObjectContext
217-
self.replyID = comment.replyID
218195
super.init(nibName: nil, bundle: nil)
219196
}
220197

@@ -266,6 +243,7 @@ class CommentDetailViewController: UIViewController {
266243
self.isLastInList = isLastInList
267244
replyTextView?.placeholder = String(format: .replyPlaceholderFormat, comment.authorForDisplay())
268245
refreshData()
246+
refreshCommentReplyIfNeeded()
269247
}
270248
}
271249

@@ -527,15 +505,41 @@ private extension CommentDetailViewController {
527505
self.comment.replyID = Int32(replyID)
528506
ContextManager.sharedInstance().saveContextAndWait(context)
529507

530-
// update local replyID to trigger table view updates.
531-
self.replyID = self.comment.replyID
508+
self.updateReplyIndicator()
532509

533510
} failure: { error in
534511
DDLogError("Failed fetching latest comment reply ID: \(String(describing: error))")
535512
}
536513

537514
}
538515

516+
func updateReplyIndicator() {
517+
518+
// If there is a reply, add reply indicator if it is not being shown.
519+
if replyID > 0 && !rows.contains(.replyIndicator) {
520+
// Update the rows first so replyIndicator is present in `rows`.
521+
configureRows()
522+
guard let replyIndicatorRow = rows.firstIndex(of: .replyIndicator) else {
523+
tableView.reloadData()
524+
return
525+
}
526+
527+
tableView.insertRows(at: [IndexPath(row: replyIndicatorRow, section: .zero)], with: .fade)
528+
return
529+
}
530+
531+
// If there is not a reply, remove reply indicator if it is being shown.
532+
if replyID == 0 && rows.contains(.replyIndicator) {
533+
// Get the reply indicator row first before it is removed via `configureRows`.
534+
guard let replyIndicatorRow = rows.firstIndex(of: .replyIndicator) else {
535+
return
536+
}
537+
538+
configureRows()
539+
tableView.deleteRows(at: [IndexPath(row: replyIndicatorRow, section: .zero)], with: .fade)
540+
}
541+
}
542+
539543
// MARK: Actions and navigations
540544

541545
// Shows the comment thread with the parent comment highlighted.

0 commit comments

Comments
 (0)