Files
buzz/mobile/ios/Runner/NativeMessagePresentationCoordinator.swift
Tom Brow bc0b552894 fix(mobile): report failed reaction publishes instead of throwing (#9813)
Tapping a reaction while the relay is disconnected threw an uncaught
`RelayNotConnectedError` (a `StateError`) from
`RelaySessionNotifier.publish`, reported by Sentry from 1.5.0 builds.
Every reaction call site fires `addReaction` or `removeReaction` without
awaiting it, so any publish failure escaped as an unhandled error and
the pill just didn't change.

Now each of those call sites (pill taps, the `+` picker, the long-press
quick-reaction row and grouped system rows) hands its publish to
`reportReactionFailure`, which shows an error notice. It's a toast drawn
above any Flutter route or sheet, including the Android reaction details
sheet. While the native iOS reaction details sheet is open, that sheet
shows the notice itself as an alert, since UIKit covers Flutter's
overlay:

- "Couldn’t add reaction. No connection." (or "remove") when the relay
isn't connected;
- "Reaction not confirmed. Connection lost." or "… The relay didn't
respond." (or "Removal not confirmed. …") when the acknowledgement was
lost or late, since the relay may have accepted it;
- nothing for the relay's `duplicate:` reply to a reaction you already
have (a double tap, or re-picking it from the picker), which is a
success;
- "Couldn’t add reaction. Try again." for anything else. Errors that
aren't relay outcomes (for example the signer failing) still escape
afterwards with their stack, so the uncaught-error handler reports them
as before.

| Before (offline tap) | After (offline tap) |
| --- | --- |
| <img alt="before"
src="https://github.com/user-attachments/assets/7745ec5b-8f54-41a2-ba3d-b8dd599f127c"
width="300"> | <img alt="after"
src="https://github.com/user-attachments/assets/066fba0b-9580-4250-966e-cd0b99831ecc"
width="300"> |

### Regressions

- Successful reactions behave exactly as before: the UI still doesn't
await the publish, and the notice only appears on failure.
- Removing a reaction from a grouped system row publishes every deletion
at once as before, and shows at most one notice if any fail.
- Unexpected local errors reach diagnostics just as they did on main.
- No new state or retries: a failed reaction isn't queued. It's reported
instead of thrown.

### Validation

- Added tests in
[`reaction_row_test.dart`](https://github.com/block/buzz/blob/fix-reaction-publish-stateerror/mobile/test/features/channels/reaction_row_test.dart):
offline add and remove, a lost ack, a duplicate and an unexpected signer
failure, each with the notice it should show (or none), and whether the
error escapes; plus a failed toggle from the reaction details sheet on
Android and iOS.
- Simulator run (iPhone 17 Pro, signed in to a Builderlab community, in
a QA chat): add and remove online through the long-press quick-reaction
row, the `+` picker and the reaction details sheet. The before and after
captures come from builds that force reaction publishes to fail as if
offline (the simulator can't drop its own network). The after build
shows the toast once the quick-reaction sheet has closed.
- A failed removal from the reaction details sheet shows its notice
above the sheet, which stays open. Widget tests cover the Flutter sheet
and the native-sheet `notice` call; a simulator run with a build that
forces removals to fail showed the native alert on the iOS sheet:

After: failed removal in the native reaction details sheet

https://github.com/user-attachments/assets/b8d808f0-e86b-411f-82c0-c3d19a51b231

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
2026-11-20 15:52:59 -05:00

116 lines
4.7 KiB
Swift

import Flutter
import UIKit
/// Owns one UIKit reaction-details surface and completes its Flutter request on dismissal.
final class NativeMessagePresentationCoordinator: NSObject, UIAdaptivePresentationControllerDelegate {
private let channel: FlutterMethodChannel
private weak var parent: UIViewController?
private weak var presented: UIViewController?
private var pendingResult: FlutterResult?
private var requestID: String?
private var terminalValue: [String: String]?
init(messenger: FlutterBinaryMessenger, parentViewController: UIViewController?) {
channel = FlutterMethodChannel(name: "buzz/native_message_presentation", binaryMessenger: messenger)
parent = parentViewController
super.init()
channel.setMethodCallHandler { [weak self] call, result in
guard let self else { result(nil); return }
self.handle(call, result: result)
}
}
private func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard let data = call.arguments as? [String: Any] else { result(nil); return }
if call.method == "updateReactions" {
if data["requestId"] as? String == requestID,
let sheet = presented as? NativeReactionDetailsViewController,
let reactions = data["reactions"] as? [[String: Any]] {
sheet.updateReactions(reactions)
}
result([:])
return
}
if call.method == "updateProfiles" {
if data["requestId"] as? String == requestID,
let sheet = presented as? NativeReactionDetailsViewController,
let profiles = data["profiles"] as? [String: [String: Any]] {
sheet.updateProfiles(profiles)
}
result([:])
return
}
if call.method == "notice" {
if data["requestId"] as? String == requestID,
let sheet = presented as? NativeReactionDetailsViewController,
let message = data["message"] as? String {
sheet.showNotice(message)
}
result(nil)
return
}
guard call.method == "reactions" else {
result(FlutterMethodNotImplemented)
return
}
// Ignore a second long press while presentation/dismissal is in flight.
guard pendingResult == nil else { result(["busy": true]); return }
let root = parent ?? UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.filter { $0.activationState == .foregroundActive }
.flatMap(\.windows).first(where: \.isKeyWindow)?.rootViewController
guard let parent = root, parent.view.window != nil else { result(nil); return }
guard parent.presentedViewController == nil else { result(["busy": true]); return }
let controller = NativeReactionDetailsViewController(data: data)
controller.onClose = { [weak self] in self?.dismiss([:]) }
controller.onAddReaction = { [weak self] in
self?.dismiss(["action": "addReaction"])
}
controller.onToggle = { [weak self] emoji in
self?.channel.invokeMethod("toggleReaction", arguments: [
"requestId": data["requestId"] as? String ?? "", "emoji": emoji,
])
}
controller.modalPresentationStyle = .pageSheet
if let presentation = controller.sheetPresentationController {
presentation.overrideTraitCollection = UITraitCollection(userInterfaceStyle: controller.overrideUserInterfaceStyle)
presentation.detents = [.medium(), .large()]
presentation.prefersGrabberVisible = true
presentation.prefersScrollingExpandsWhenScrolledToEdge = true
}
controller.overrideUserInterfaceStyle = data["dark"] as? Bool == true ? .dark : .light
pendingResult = result
requestID = data["requestId"] as? String
presented = controller
controller.presentationController?.delegate = self
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
parent.present(controller, animated: !UIAccessibility.isReduceMotionEnabled)
}
private func dismiss(_ value: [String: String]) {
guard pendingResult != nil, terminalValue == nil else { return }
terminalValue = value
guard let presented else { complete(value); return }
let completion: () -> Void = { [weak self] in
guard let self, self.presented === presented else { return }
self.complete(value)
}
presented.dismiss(animated: !UIAccessibility.isReduceMotionEnabled, completion: completion)
}
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
guard presented === presentationController.presentedViewController else { return }
complete([:])
}
private func complete(_ value: [String: String]) {
let result = pendingResult
let terminal = terminalValue ?? value
terminalValue = nil
pendingResult = nil
presented = nil
requestID = nil
result?(terminal)
}
}