Skip to content

Commit 21b6f28

Browse files
committed
Fix an issue with CommentComposer using the outdated text
1 parent d33bd39 commit 21b6f28

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

WordPress/Classes/ViewRelated/Comments/Controllers/Composer/CommentComposerViewController.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ final class CommentComposerViewController: UIViewController {
111111
private func sendComment() async {
112112
do {
113113
setLoading(true)
114+
await editor?.refresh()
114115
try await viewModel.save(text)
115116
UINotificationFeedbackGenerator().notificationOccurred(.success)
116117
presentingViewController?.dismiss(animated: true)
@@ -128,10 +129,15 @@ final class CommentComposerViewController: UIViewController {
128129
}
129130

130131
@objc private func buttonCancelTapped() {
131-
if text.isEmpty {
132-
presentingViewController?.dismiss(animated: true)
133-
} else {
134-
showCloseDraftConfirmationAlert(content: text)
132+
navigationItem.leftBarButtonItem?.isEnabled = false
133+
Task { @MainActor in
134+
await editor?.refresh()
135+
navigationItem.leftBarButtonItem?.isEnabled = true
136+
if text.isEmpty {
137+
presentingViewController?.dismiss(animated: true)
138+
} else {
139+
showCloseDraftConfirmationAlert(content: text)
140+
}
135141
}
136142
}
137143

WordPress/Classes/ViewRelated/Comments/Controllers/Composer/CommentGutenbergEditorViewController.swift

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,7 @@ final class CommentGutenbergEditorViewController: UIViewController, CommentEdito
1010

1111
var initialContent: String?
1212

13-
var text: String {
14-
set {
15-
wpAssertionFailure("not supported")
16-
}
17-
get {
18-
currentText
19-
}
20-
}
21-
22-
private var currentText = ""
13+
private(set) var text = ""
2314

2415
var isEnabled: Bool = true {
2516
didSet {
@@ -56,22 +47,24 @@ final class CommentGutenbergEditorViewController: UIViewController, CommentEdito
5647

5748
editorDidUpdate
5849
.throttle(for: 1.0, scheduler: DispatchQueue.main, latest: true)
59-
.sink { [weak self] in self?.refreshText() }
50+
.sink { [weak self] in
51+
Task {
52+
await self?.refresh()
53+
}
54+
}
6055
.store(in: &cancellables)
6156
}
6257

63-
private func refreshText() {
64-
guard let editorVC else { return }
65-
Task { @MainActor in
66-
do {
67-
let text = try await editorVC.getContent()
68-
if text != self.currentText {
69-
self.currentText = text
70-
self.delegate?.commentEditor(self, didUpateText: text)
71-
}
72-
} catch {
73-
// TODO: handle errors
58+
func refresh() async {
59+
do {
60+
guard let editorVC else { return }
61+
let text = try await editorVC.getContent()
62+
if text != self.text {
63+
self.text = text
64+
self.delegate?.commentEditor(self, didUpateText: text)
7465
}
66+
} catch {
67+
wpAssertionFailure("failed to refresh content", userInfo: ["error": "\(error)"])
7568
}
7669
}
7770
}

WordPress/Classes/ViewRelated/Comments/Controllers/Composer/CommentPlainTextEditorViewController.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import UIKit
22
import WordPressUI
33

4+
@MainActor
45
protocol CommentEditor {
5-
var text: String { get set }
6+
/// - warning: The latest cached text can be a bit behind. Use `refresh()`
7+
/// to ensure the editor has the latest content.
8+
var text: String { get }
69
var isEnabled: Bool { get set }
10+
11+
func refresh() async
12+
}
13+
14+
extension CommentEditor {
15+
func refresh() async {
16+
// Do nothing
17+
}
718
}
819

920
protocol CommentEditorDelegate: AnyObject {

0 commit comments

Comments
 (0)