mirror of
https://github.com/block/buzz.git
synced 2026-11-30 21:21:48 +00:00
## Summary When creating a channel fails, the iOS New channel sheet printed the Dart error's `toString()` under the form: an exception class name, the relay's machine prefix, or an event id. The text also sat about 20 points right of centre and could run off the right edge. This makes both creation sheets say what went wrong in a plain sentence, and lays the iOS error out properly.  **Wording.** A new `createChannelErrorMessage` (`create_channel_error.dart`) is used by the native iOS sheet and the Flutter sheet: | Failure | Before | After | |---|---|---| | Name is taken | `Exception: invalid: an open channel named "general" already exists` | `An open channel named "general" already exists` | | No connection | `Bad state: Relay session is not connected` | `Couldn’t create channel. No connection.` | | Relay silent for 8 seconds | `TimeoutException: Event <64-char id> not acknowledged within 0:00:08.000000` | `Not confirmed. The relay didn't respond.` | | Connection dropped while waiting | raw error text | `Not confirmed. Connection lost.` | | Any other `StateError` | `Bad state: …` | the message without `Bad state:` | The connection sentences follow the composer and reactions: once the event has gone out, they say "not confirmed" instead of "try again", because the relay may have created the channel. `relayErrorMessage` itself is unchanged. **Layout (iOS).** - The error label was the table's `tableFooterView`, framed at x 20 with the width reduced by 40. `UITableView` keeps a table footer's x and overwrites its width, so centred text landed 20 points right and long text ran off the edge. - The error is now the last section's footer: centred within the same margins as the field headings, wrapping inside them, with UIKit's own group spacing above it so it starts at the same height as before. - After a failed Create the sheet scrolls the error into view. Previously it could sit below the fold in landscape or at large text sizes, so a failure showed nothing. **CI.** `scripts/test-mobile-create-channel-accessibility.sh` existed but ran in no lane. It is now in the native lane and in the `mobile` and `mobile-native` path filters. ### Related issue N/A. No existing issue or open PR found for this. Related: #8807 fixed the taken-name text in the Flutter sheet; the native iOS sheet from #8942 did not use that code. ### Testing - `dart format` and `flutter analyze` over `mobile/`, and the affected Dart test files (`create_channel_error_test`, `ios_create_channel_sheet_test`, `channels_page_test`). - `scripts/test-mobile-create-channel-accessibility.sh` at the default and the largest text size, on iPhone and iPad simulators (iOS 27.2). It now asserts the margins, centring, gap, and that a failed Create scrolls the whole error into view, including the three-row Temporary case. - Mutations: reverting either Dart call site, the `StateError` arm, the scroll, the scroll pass count, the centring, or the top gap each fails a test. - Ran the app on an iPhone 17 simulator against a seeded relay for the three failures in the table above (taken name; relay refusing connections; relay accepting but silent). Measured on the frames: the error text's centre is within 0.33 pt of the screen's centre (was about 20 pt right), and its gap below the Visibility group is unchanged at 20.33 pt. Not run: the native assertions on the CI lane's older simulator runtime (this PR's Mobile Swift native job is the first run), VoiceOver, a real device, and the Flutter sheet on an Android device. The screenshot was taken one commit before the head; that last commit changes where the gap value comes from and renders pixel-identically. Known gap, unchanged by this PR: on an iPhone in landscape at the largest text size with the keyboard open, the visible area is shorter than a three-line error, so its first lines stay above the fold. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Matt Toohey <contact@matttoohey.com> Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
309 lines
15 KiB
Swift
309 lines
15 KiB
Swift
import Flutter
|
|
import UIKit
|
|
|
|
/// UIKit owns the complete form, including menus and keyboard presentation.
|
|
final class NativeCreateChannelSheetCoordinator: NSObject, UIAdaptivePresentationControllerDelegate {
|
|
private let channel: FlutterMethodChannel
|
|
private weak var parent: UIViewController?
|
|
private var navigation: UINavigationController?
|
|
private var pending: FlutterResult?
|
|
|
|
init(messenger: FlutterBinaryMessenger, parentViewController: UIViewController?) {
|
|
channel = FlutterMethodChannel(name: "buzz/create_channel_sheet", binaryMessenger: messenger)
|
|
parent = parentViewController
|
|
super.init()
|
|
channel.setMethodCallHandler { [weak self] call, result in
|
|
guard let self else { return }
|
|
guard call.method == "present" else { result(FlutterMethodNotImplemented); return }
|
|
guard self.navigation == nil, let data = call.arguments as? [String: Any],
|
|
let session = data["session"] as? String else {
|
|
result(FlutterError(code: "unavailable", message: "Cannot open New channel.", details: nil))
|
|
return
|
|
}
|
|
var presenter = self.parent ?? UIApplication.shared.connectedScenes
|
|
.compactMap { $0 as? UIWindowScene }.filter { $0.activationState == .foregroundActive }
|
|
.flatMap(\.windows).first(where: \.isKeyWindow)?.rootViewController
|
|
while let next = presenter?.presentedViewController { presenter = next }
|
|
guard let presenter, presenter.view.window != nil, !presenter.isBeingDismissed else {
|
|
result(FlutterError(code: "unavailable", message: "Cannot open New channel.", details: nil))
|
|
return
|
|
}
|
|
let form = NativeCreateChannelViewController(configuration: data)
|
|
form.onClose = { [weak self] in self?.close() }
|
|
form.onCreate = { [weak self, weak form] values in
|
|
self?.channel.invokeMethod("create", arguments: values.merging(["session": session]) { _, new in new }) { response in
|
|
guard let self, let form else { return }
|
|
if (response as? [String: Any])?["success"] as? Bool == true {
|
|
self.close()
|
|
} else {
|
|
let error = (response as? [String: Any])?["error"] as? String ?? "Couldn't create channel. Please try again."
|
|
form.showError(error)
|
|
}
|
|
}
|
|
}
|
|
let navigation = UINavigationController(rootViewController: form)
|
|
navigation.overrideUserInterfaceStyle = data["dark"] as? Bool == true ? .dark : .light
|
|
if UIDevice.current.userInterfaceIdiom == .pad { navigation.modalPresentationStyle = .formSheet }
|
|
navigation.sheetPresentationController?.detents = [.large()]
|
|
self.navigation = navigation
|
|
self.pending = result
|
|
presenter.present(navigation, animated: !UIAccessibility.isReduceMotionEnabled) {
|
|
navigation.presentationController?.delegate = self
|
|
}
|
|
}
|
|
}
|
|
|
|
private func close() {
|
|
guard let navigation else { return }
|
|
navigation.dismiss(animated: !UIAccessibility.isReduceMotionEnabled) { [weak self] in self?.finish() }
|
|
}
|
|
|
|
private func finish() {
|
|
navigation = nil
|
|
let result = pending
|
|
pending = nil
|
|
result?(nil)
|
|
}
|
|
|
|
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) { finish() }
|
|
}
|
|
|
|
final class NativeCreateChannelViewController: UITableViewController, UITextViewDelegate, UITextFieldDelegate {
|
|
var onClose: (() -> Void)?
|
|
var onCreate: (([String: Any]) -> Void)?
|
|
private let name = UITextField()
|
|
private let details = UITextView()
|
|
private let detailsPlaceholder = UILabel()
|
|
private let pageColor: UIColor
|
|
private let rowColor: UIColor
|
|
private var temporary = false
|
|
private var visibility = "open"
|
|
private var ttl = 604800
|
|
private var busy = false
|
|
private var error: String?
|
|
private let expiry: [(String, Int)] = [
|
|
("30 minutes", 1800), ("1 hour", 3600), ("6 hours", 21600), ("12 hours", 43200),
|
|
("1 day", 86400), ("3 days", 259200), ("7 days", 604800), ("14 days", 1209600), ("30 days", 2592000),
|
|
]
|
|
|
|
init(configuration: [String: Any]) {
|
|
pageColor = Self.color(configuration["pageColor"]) ?? .systemGroupedBackground
|
|
rowColor = Self.color(configuration["rowColor"]) ?? .secondarySystemGroupedBackground
|
|
super.init(style: .insetGrouped)
|
|
title = "New channel"
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
tableView.backgroundColor = pageColor
|
|
tableView.keyboardDismissMode = .interactive
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
tableView.estimatedRowHeight = 56
|
|
name.placeholder = "release-notes"
|
|
name.accessibilityLabel = "Channel name"
|
|
name.font = .preferredFont(forTextStyle: .body)
|
|
name.adjustsFontForContentSizeCategory = true
|
|
name.autocapitalizationType = .none
|
|
name.autocorrectionType = .no
|
|
name.spellCheckingType = .no
|
|
name.returnKeyType = .next
|
|
name.delegate = self
|
|
name.addTarget(self, action: #selector(nameChanged), for: .editingChanged)
|
|
details.font = .preferredFont(forTextStyle: .body)
|
|
details.adjustsFontForContentSizeCategory = true
|
|
details.backgroundColor = .clear
|
|
details.accessibilityLabel = "Description, optional"
|
|
details.delegate = self
|
|
details.isScrollEnabled = true
|
|
detailsPlaceholder.text = "What this channel is for"
|
|
detailsPlaceholder.font = .preferredFont(forTextStyle: .body)
|
|
detailsPlaceholder.adjustsFontForContentSizeCategory = true
|
|
detailsPlaceholder.textColor = .placeholderText
|
|
detailsPlaceholder.numberOfLines = 0
|
|
detailsPlaceholder.isAccessibilityElement = false
|
|
detailsPlaceholder.isUserInteractionEnabled = false
|
|
detailsPlaceholder.translatesAutoresizingMaskIntoConstraints = false
|
|
details.addSubview(detailsPlaceholder)
|
|
NSLayoutConstraint.activate([
|
|
detailsPlaceholder.leadingAnchor.constraint(equalTo: details.leadingAnchor, constant: details.textContainerInset.left + details.textContainer.lineFragmentPadding),
|
|
detailsPlaceholder.trailingAnchor.constraint(equalTo: details.frameLayoutGuide.trailingAnchor, constant: -(details.textContainerInset.right + details.textContainer.lineFragmentPadding)),
|
|
detailsPlaceholder.topAnchor.constraint(equalTo: details.topAnchor, constant: details.textContainerInset.top),
|
|
])
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "xmark"), style: .plain, target: self, action: #selector(cancel))
|
|
navigationItem.leftBarButtonItem?.accessibilityLabel = "Close"
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Create", style: .done, target: self, action: #selector(create))
|
|
NavigationTopEdge.configure(tableView, item: navigationItem)
|
|
updateControls()
|
|
}
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
name.becomeFirstResponder()
|
|
}
|
|
|
|
func textViewDidChange(_ textView: UITextView) { detailsPlaceholder.isHidden = !textView.text.isEmpty }
|
|
|
|
@objc private func nameChanged() { updateControls() }
|
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool { details.becomeFirstResponder(); return false }
|
|
|
|
private func updateControls() {
|
|
navigationController?.isModalInPresentation = busy
|
|
navigationItem.leftBarButtonItem?.isEnabled = !busy
|
|
navigationItem.rightBarButtonItem?.isEnabled = !busy && !(name.text ?? "").trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
|
navigationItem.rightBarButtonItem?.title = busy ? "Creating…" : "Create"
|
|
name.isEnabled = !busy
|
|
details.isEditable = !busy
|
|
for cell in tableView.visibleCells { cell.isUserInteractionEnabled = !busy }
|
|
}
|
|
|
|
@objc private func cancel() { if !busy { onClose?() } }
|
|
@objc private func create() {
|
|
let value = (name.text ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !busy, !value.isEmpty else { return }
|
|
busy = true
|
|
setError(nil)
|
|
updateControls()
|
|
var values: [String: Any] = ["name": value, "description": details.text.trimmingCharacters(in: .whitespacesAndNewlines), "visibility": visibility]
|
|
if temporary { values["ttlSeconds"] = ttl }
|
|
onCreate?(values)
|
|
}
|
|
|
|
func showError(_ error: String) {
|
|
busy = false
|
|
setError(error)
|
|
updateControls()
|
|
// The footer is below the fold in landscape and at large text sizes. The
|
|
// reload left the rows below the fold on their estimated height, so a
|
|
// pass can stop short; it brings more of them on screen for the next.
|
|
// The worst case is one pass per row, plus one.
|
|
for _ in 0...tableView.numberOfRows(inSection: 2) {
|
|
tableView.layoutIfNeeded()
|
|
tableView.scrollRectToVisible(tableView.rectForFooter(inSection: 2), animated: false)
|
|
}
|
|
UIAccessibility.post(notification: .announcement, argument: error)
|
|
}
|
|
|
|
private func setError(_ error: String?) {
|
|
guard self.error != error else { return }
|
|
self.error = error
|
|
guard isViewLoaded else { return }
|
|
UIView.performWithoutAnimation { tableView.reloadSections(IndexSet(integer: 2), with: .none) }
|
|
}
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int { 3 }
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { section == 2 ? (temporary ? 3 : 2) : 1 }
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { section == 0 ? "Name" : section == 1 ? "Description · Optional" : nil }
|
|
|
|
// The error is the last section's footer, so UIKit gives it the headings'
|
|
// margins and rewraps it, and centring it in them centres it in the sheet.
|
|
// A table footer keeps its own x while UIKit stretches it to the table's
|
|
// width, which pushed the text off-centre and off-screen.
|
|
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
|
|
guard section == 2, let error else { return nil }
|
|
let footer = UITableViewHeaderFooterView(reuseIdentifier: nil)
|
|
var content = UIListContentConfiguration.groupedFooter()
|
|
content.text = error
|
|
content.textProperties.color = .systemRed
|
|
content.textProperties.alignment = .center
|
|
// The first group has no footer, so its footer's height is the space UIKit
|
|
// leaves under a group. This puts the text that far below the rows. After
|
|
// a full reload UIKit asks this method for that footer from inside the
|
|
// query; the guard above answers nil, so it goes no deeper.
|
|
content.directionalLayoutMargins.top = tableView.rectForFooter(inSection: 0).height
|
|
footer.contentConfiguration = content
|
|
return footer
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
|
|
// The editable field owns the label; its visual heading is decorative.
|
|
guard section < 2 else { return }
|
|
view.isAccessibilityElement = false
|
|
view.accessibilityElementsHidden = true
|
|
if let header = view as? UITableViewHeaderFooterView {
|
|
header.textLabel?.isAccessibilityElement = false
|
|
header.contentView.accessibilityElementsHidden = true
|
|
}
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
|
|
cell.backgroundColor = rowColor
|
|
cell.selectionStyle = .none
|
|
cell.isUserInteractionEnabled = !busy
|
|
if indexPath.section < 2 {
|
|
let field: UIView = indexPath.section == 0 ? name : details
|
|
field.removeFromSuperview()
|
|
field.translatesAutoresizingMaskIntoConstraints = false
|
|
cell.contentView.addSubview(field)
|
|
NSLayoutConstraint.activate([
|
|
field.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
|
|
field.trailingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.trailingAnchor),
|
|
field.topAnchor.constraint(equalTo: cell.contentView.topAnchor, constant: 12),
|
|
field.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -12),
|
|
field.heightAnchor.constraint(greaterThanOrEqualToConstant: indexPath.section == 0 ? 28 : 96),
|
|
])
|
|
} else {
|
|
let isType = indexPath.row == 0
|
|
let isVisibility = indexPath.row == 1
|
|
let label = isType ? "Channel type" : isVisibility ? "Visibility" : "Delete after inactivity"
|
|
let value = isType ? (temporary ? "Temporary" : "Ongoing") : isVisibility ? (visibility == "open" ? "Public" : "Private") : expiry.first(where: { $0.1 == ttl })?.0 ?? "7 days"
|
|
let button = UIButton(type: .system)
|
|
let titleLabel = UILabel()
|
|
titleLabel.text = label
|
|
titleLabel.font = .preferredFont(forTextStyle: .body)
|
|
titleLabel.adjustsFontForContentSizeCategory = true
|
|
titleLabel.numberOfLines = 0
|
|
titleLabel.isAccessibilityElement = false
|
|
var configuration = UIButton.Configuration.plain()
|
|
configuration.title = value
|
|
configuration.baseForegroundColor = .secondaryLabel
|
|
configuration.image = UIImage(systemName: "chevron.up.chevron.down")
|
|
configuration.imagePlacement = .trailing
|
|
configuration.imagePadding = 12
|
|
configuration.contentInsets = NSDirectionalEdgeInsets(top: 14, leading: 8, bottom: 14, trailing: 16)
|
|
button.configuration = configuration
|
|
button.titleLabel?.adjustsFontForContentSizeCategory = true
|
|
button.setContentHuggingPriority(.required, for: .horizontal)
|
|
button.setContentCompressionResistancePriority(.required, for: .horizontal)
|
|
button.isAccessibilityElement = true
|
|
button.accessibilityLabel = label
|
|
button.accessibilityValue = value
|
|
button.showsMenuAsPrimaryAction = true
|
|
let choices: [(String, String)] = isType ? [("Ongoing", "ongoing"), ("Temporary", "temporary")] : isVisibility ? [("Public", "open"), ("Private", "private")] : expiry.map { ($0.0, String($0.1)) }
|
|
let selected = isType ? (temporary ? "temporary" : "ongoing") : isVisibility ? visibility : String(ttl)
|
|
button.menu = UIMenu(children: choices.map { title, key in
|
|
UIAction(title: title, state: key == selected ? .on : .off) { [weak self] _ in
|
|
guard let self, !self.busy else { return }
|
|
if isType { self.temporary = key == "temporary" }
|
|
else if isVisibility { self.visibility = key }
|
|
else if let seconds = Int(key) { self.ttl = seconds }
|
|
self.tableView.reloadSections(IndexSet(integer: 2), with: .none)
|
|
}
|
|
})
|
|
// Only the trailing value is lifted into UIKit's menu preview.
|
|
let row = UIStackView(arrangedSubviews: [titleLabel, UIView(), button])
|
|
row.spacing = 12
|
|
row.alignment = .center
|
|
row.translatesAutoresizingMaskIntoConstraints = false
|
|
cell.contentView.addSubview(row)
|
|
NSLayoutConstraint.activate([
|
|
row.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor, constant: 16),
|
|
row.trailingAnchor.constraint(equalTo: cell.contentView.trailingAnchor),
|
|
row.topAnchor.constraint(equalTo: cell.contentView.topAnchor),
|
|
row.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor),
|
|
button.heightAnchor.constraint(greaterThanOrEqualToConstant: 48),
|
|
])
|
|
}
|
|
return cell
|
|
}
|
|
|
|
private static func color(_ value: Any?) -> UIColor? {
|
|
guard let number = value as? NSNumber else { return nil }
|
|
let argb = number.uint32Value
|
|
return UIColor(red: CGFloat((argb >> 16) & 255) / 255, green: CGFloat((argb >> 8) & 255) / 255, blue: CGFloat(argb & 255) / 255, alpha: CGFloat((argb >> 24) & 255) / 255)
|
|
}
|
|
}
|