hex/ios/HEXMobile/Feedback/FeedbackImage.swift
Ryan Vogel 21eed53fab iOS: Send feedback form that posts to Bellhop
Adds a Send feedback form to the Hex app (never the keyboard) that sends one
report to Bellhop, Anomaly's feedback inbox, following blhp.app/docs/feedback's
"Building a feedback form" checklist:

- Entry points: a Send feedback row on Home's Privacy policy / Support /
  Acknowledgements card, and one at the end of Settings › Experiments. Both
  are absent in a build without a report key.
- The form: Problem / Idea / Question / Other, an optional subject, the
  message, pictures (Hex's own key window drawn just before the form opens,
  removable with one tap; more from the permission-free photo picker, up to
  5), an optional email, Send anonymously (hides the email and drops email
  and install id), and what is sent in plain words. Limits are checked
  before sending (message 20,000, subject 200, 5 pictures, 10 MiB each,
  20 MiB in all); every picture is re-encoded as JPEG at 80% with its
  longest side at most 2048 px (HEIC included).
- Sending: multipart/form-data with a payload JSON part and screenshot
  parts, an Idempotency-Key made at the tap and reused for every retry,
  visible upload progress, network failures and 5xx retried 4 times with
  1/2/4 s backoff, 409 request_pending and 429 retried after Retry-After,
  other 4xx never retried. 401 and 403 feedback_disabled are logged as a
  fault and shown as "unavailable"; 413, 415, 429 and offline get kind
  words. A thank-you says, when the answer has email "pending", that a
  reply comes by email and the address needs confirming once.
- Payload: message, subject, category, app, device (model identifier,
  BCP 47 locale), metadata (screen, dictation_language, speech_model),
  email and a random install id kept in Hex's UserDefaults. No user, no
  logs, nothing Hex keeps on the phone.
- Report key: BELLHOP_REPORT_KEY in HEXMobile/Feedback.xcconfig (empty in
  git), set by a gitignored Feedback.local.xcconfig or by ci_post_clone.sh
  from Xcode Cloud's BELLHOP_REPORT_KEY secret, read at run time from
  Info.plist's BellhopReportKey. No key lives in Swift.
- Debug-only launch arguments (--feedback-log-request, --feedback-key,
  --feedback-endpoint, --feedback-drop-first-response,
  --feedback-broken-image) for simulator proof of each path.
- Simplified Chinese for every new string; docs in
  ios/docs/settings-and-home.md.

Verified on the Studio's iPhone 17 Pro simulator (iOS 27.2) through the
form itself: a send with picture and email got 201 #3, email "pending";
an anonymous send got 201 #4, email "none", with no email or install_id in
the payload; a dropped first answer was retried with the same key and got
the same #5; a wrong key got one 401 and "unavailable"; a refused
connection was tried 4 times (0, +1, +3, +7 s) with one key, then said
offline; a half-cut picture got one 415. The keyboard appex has no
Bellhop key or address. code-shape.py --check passes. No unit test: no
existing HEXMobileTests file covers the app's networking.
2026-11-23 15:02:51 -05:00

43 lines
1.9 KiB
Swift

import PhotosUI
import SwiftUI
import UIKit
/// The pictures a feedback can carry: Hex's own window, drawn just before the form opens (never
/// the whole screen), and photos the person picks, all re-encoded as JPEG at about 80% with the
/// longest side at most 2048 px.
enum FeedbackImage {
/// Hex's key window as it is now, or nil.
@MainActor
static func keyWindow() -> UIImage? {
let window = UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap(\.windows)
.first(where: \.isKeyWindow)
guard let window, window.bounds.width > 0 else { return nil }
let format = UIGraphicsImageRendererFormat(for: window.traitCollection)
return UIGraphicsImageRenderer(bounds: window.bounds, format: format).image { _ in
window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
}
}
/// `image` shrunk and encoded, or nil when it can't be.
nonisolated static func jpeg(_ image: UIImage) -> Data? {
let pixels = CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale)
let longest = max(pixels.width, pixels.height)
guard longest > 0 else { return nil }
let factor = min(1, Feedback.imageSide / longest)
let target = CGSize(width: (pixels.width * factor).rounded(), height: (pixels.height * factor).rounded())
let format = UIGraphicsImageRendererFormat.default()
format.scale = 1
format.opaque = true
let drawn = UIGraphicsImageRenderer(size: target, format: format).image { _ in
image.draw(in: CGRect(origin: .zero, size: target))
}
return drawn.jpegData(compressionQuality: Feedback.jpegQuality)
}
/// A picked photo (HEIC, PNG, anything UIImage reads) as a JPEG.
nonisolated static func jpeg(fromPicked data: Data) -> Data? {
UIImage(data: data).flatMap(jpeg)
}
}