Skip to content

Commit 3e9205c

Browse files
klopez4212Terp
andcommitted
Fix onboarding recovery and avatar draft review feedback
Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz> Signed-off-by: kenny lopez <[email protected]>
1 parent c8898f1 commit 3e9205c

12 files changed

Lines changed: 328 additions & 30 deletions

‎mobile/ios/Runner.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
F84380010000000000000001 /* ProfileSetupRecoveryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F84380010000000000000002 /* ProfileSetupRecoveryTests.swift */; };
1011
BBAA00000000000000000011 /* IdentityFilePlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBAA00000000000000000012 /* IdentityFilePlugin.swift */; };
1112
BBAA00000000000000000001 /* HostedHpkeBackup.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBAA00000000000000000002 /* HostedHpkeBackup.swift */; };
1213
DAAB00000000000000000001 /* NativeTabler.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAAB00000000000000000002; };
@@ -100,6 +101,7 @@
100101
/* End PBXCopyFilesBuildPhase section */
101102

102103
/* Begin PBXFileReference section */
104+
F84380010000000000000002 /* ProfileSetupRecoveryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileSetupRecoveryTests.swift; sourceTree = "<group>"; };
103105
BBAA00000000000000000012 /* IdentityFilePlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdentityFilePlugin.swift; sourceTree = "<group>"; };
104106
BBAA00000000000000000002 /* HostedHpkeBackup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HostedHpkeBackup.swift; sourceTree = "<group>"; };
105107
DAAB00000000000000000002 /* NativeTabler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeTabler.swift; sourceTree = "<group>"; };
@@ -206,6 +208,7 @@
206208
isa = PBXGroup;
207209
children = (
208210
331C807B294A618700263BE5 /* RunnerTests.swift */,
211+
F84380010000000000000002 /* ProfileSetupRecoveryTests.swift */,
209212
AB1200132F40400100A17E01 /* NativeMessageImageLoaderTests.swift */,
210213
AB1200132F40400100A17E09 /* IdentityFilePluginTests.swift */,
211214
BZZ0000000000000000002C /* BuzzCommunicationNotificationTests.swift */,
@@ -615,6 +618,7 @@
615618
buildActionMask = 2147483647;
616619
files = (
617620
331C808B294A63AB00263BE5 /* RunnerTests.swift in Sources */,
621+
F84380010000000000000001 /* ProfileSetupRecoveryTests.swift in Sources */,
618622
AB1200032F40400100A17E01 /* NativeMessageImageLoaderTests.swift in Sources */,
619623
AB1200032F40400100A17E09 /* IdentityFilePluginTests.swift in Sources */,
620624
BZZ0000000000000000002B /* BuzzCommunicationNotificationTests.swift in Sources */,

‎mobile/ios/Runner/NativeCommunityEntry.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ private final class NativeCommunityEntryController: UIViewController, UITextFiel
317317
self.setBusy(false)
318318
self.status.text = result["error"] as? String
319319
self.status.isHidden = self.status.text == nil
320+
if let error = self.status.text {
321+
UIAccessibility.post(notification: .announcement, argument: error)
322+
}
320323
self.navigationController?.sheetPresentationController?.invalidateDetents()
321324
if result["joined"] as? Bool == true {
322325
self.input.text = nil

‎mobile/ios/Runner/NativeProfileSetup.swift‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ final class NativeProfileSetupCoordinator: NSObject, UIAdaptivePresentationContr
1818
private var previousDetent: UISheetPresentationController.Detent.Identifier?
1919
private var editorAvatarRect: CGRect?
2020
private var avatarDraft: [String: Any]?
21+
private let scheduleEditorWatchdog: (@escaping () -> Void) -> Void
2122
private let startEditorEngine: (FlutterEngine) -> Bool
2223
private let configureEditorEngine: (FlutterEngine) -> Void
2324

2425

25-
init(messenger: FlutterBinaryMessenger, startEditorEngine: @escaping (FlutterEngine) -> Bool = {
26+
init(messenger: FlutterBinaryMessenger, scheduleEditorWatchdog: @escaping (@escaping () -> Void) -> Void = {
27+
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: $0)
28+
}, startEditorEngine: @escaping (FlutterEngine) -> Bool = {
2629
$0.run(withEntrypoint: "profileAvatarEditorMain", libraryURI: "package:buzz/features/profile/profile_avatar_sheet_app.dart")
2730
}, configureEditorEngine: @escaping (FlutterEngine) -> Void) {
31+
self.scheduleEditorWatchdog = scheduleEditorWatchdog
2832
self.startEditorEngine = startEditorEngine
2933
self.configureEditorEngine = configureEditorEngine
3034
channel = FlutterMethodChannel(name: "buzz/profile_setup", binaryMessenger: messenger)
@@ -96,10 +100,12 @@ final class NativeProfileSetupCoordinator: NSObject, UIAdaptivePresentationContr
96100
}
97101
let editorChannel = FlutterMethodChannel(name: "buzz/profile_avatar_sheet", binaryMessenger: engine.binaryMessenger)
98102
avatarChannel = editorChannel
103+
var ready = false
99104
var finishing = false
100-
editorChannel.setMethodCallHandler { [weak self, weak controller] call, result in
101-
guard let self, self.session == session, let controller else { result(nil); return }
105+
editorChannel.setMethodCallHandler { [weak self, weak controller, weak engine] call, result in
106+
guard let self, let engine, self.session == session, self.avatarEngine === engine, let controller else { result(nil); return }
102107
if call.method == "ready", let rect = call.arguments as? [String: NSNumber], let editor = self.avatarController {
108+
ready = true
103109
let target = CGRect(x: rect["x"]?.doubleValue ?? 0, y: rect["y"]?.doubleValue ?? 0, width: rect["width"]?.doubleValue ?? 220, height: rect["height"]?.doubleValue ?? 220)
104110
self.editorAvatarRect = target
105111
let destination = editor.view.convert(target, to: navigation.view)
@@ -177,9 +183,13 @@ final class NativeProfileSetupCoordinator: NSObject, UIAdaptivePresentationContr
177183
navigation.pushViewController(editor, animated: false)
178184
if let cover { cover.frame = navigation.view.bounds; navigation.view.addSubview(cover); transitionCover = cover }
179185
if let (snapshot, frame) = sourceAvatar { snapshot.frame = frame; navigation.view.addSubview(snapshot); transitionAvatar = snapshot }
180-
// A failed editor frame must not leave an opaque snapshot blocking dismissal.
181-
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self, weak engine] in
182-
guard let self, let engine, self.session == session, self.avatarEngine === engine, self.transitionCover === cover else { return }; self.clearTransition()
186+
// Recover the whole sheet when Dart never delivers the editor's first frame.
187+
scheduleEditorWatchdog { [weak self, weak engine, weak controller] in
188+
guard let self, let engine, self.session == session,
189+
self.avatarEngine === engine, !ready, !finishing else { return }
190+
navigation.setNavigationBarHidden(false, animated: false)
191+
navigation.popViewController(animated: false)
192+
self.restoreProfileSheet(controller)
183193
}
184194
navigation.interactivePopGestureRecognizer?.isEnabled = false
185195

@@ -345,6 +355,7 @@ private final class NativeProfileSetupController: UIViewController, UITextFieldD
345355
guard let self else { return }
346356
if response["saved"] as? Bool == true { self.finish?("saved"); return }
347357
self.status.text = "Could not save your profile. Try again or skip."; self.status.isHidden = false
358+
UIAccessibility.post(notification: .announcement, argument: self.status.text)
348359
self.name.isEnabled = true; self.controls.forEach { $0.isEnabled = true }; self.updateName(); self.navigationController?.isModalInPresentation = false; self.navigationItem.rightBarButtonItem?.isEnabled = true; self.submit.configuration?.showsActivityIndicator = false
349360
}
350361
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import Flutter
2+
import UIKit
3+
import XCTest
4+
5+
@testable import Buzz
6+
7+
final class ProfileSetupRecoveryTests: XCTestCase {
8+
@MainActor
9+
func testStartedEditorWithoutReadyRestoresProfileAndAllowsRetry() async throws {
10+
let messenger = ProfileSetupTestMessenger()
11+
let parent = UIViewController()
12+
let window = UIWindow(frame: UIScreen.main.bounds)
13+
window.windowScene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first
14+
window.rootViewController = parent
15+
window.makeKeyAndVisible()
16+
defer { window.isHidden = true }
17+
var watchdogs: [() -> Void] = []
18+
weak var engine: FlutterEngine?
19+
let coordinator = NativeProfileSetupCoordinator(messenger: messenger,
20+
scheduleEditorWatchdog: { watchdogs.append($0) },
21+
startEditorEngine: {
22+
engine = $0
23+
// Start the normal app entry point: it never sends avatar-editor ready.
24+
return $0.run()
25+
}, configureEditorEngine: { _ in })
26+
messenger.invoke("present", arguments: ["session": 81])
27+
let nav = try XCTUnwrap(parent.presentedViewController as? UINavigationController)
28+
if let transition = nav.transitionCoordinator {
29+
await withCheckedContinuation { continuation in
30+
transition.animate(alongsideTransition: nil) { _ in continuation.resume() }
31+
}
32+
}
33+
let profile = try XCTUnwrap(nav.viewControllers.first)
34+
let avatar = try XCTUnwrap(descendants(profile.view).compactMap { $0 as? UIButton }
35+
.first { $0.accessibilityLabel == "Add profile photo" })
36+
let pan = UIPanGestureRecognizer()
37+
let disabledPan = UIPanGestureRecognizer()
38+
disabledPan.isEnabled = false
39+
nav.view.addGestureRecognizer(pan)
40+
nav.view.addGestureRecognizer(disabledPan)
41+
nav.interactivePopGestureRecognizer?.isEnabled = true
42+
for detent in [UISheetPresentationController.Detent.Identifier("profile"), .large] {
43+
autoreleasepool {
44+
nav.sheetPresentationController?.selectedDetentIdentifier = detent
45+
avatar.sendActions(for: .touchUpInside)
46+
XCTAssertNotNil(engine)
47+
XCTAssertTrue(nav.topViewController is FlutterViewController)
48+
XCTAssertFalse(pan.isEnabled)
49+
XCTAssertFalse(nav.interactivePopGestureRecognizer?.isEnabled ?? true)
50+
XCTAssertTrue(nav.isModalInPresentation)
51+
XCTAssertTrue(nav.isNavigationBarHidden)
52+
if watchdogs.count > 1 {
53+
// An old attempt's watchdog cannot tear down the successful retry.
54+
watchdogs[0]()
55+
XCTAssertTrue(nav.topViewController is FlutterViewController)
56+
XCTAssertNotNil(engine)
57+
}
58+
watchdogs.last?()
59+
XCTAssertTrue(nav.topViewController === profile)
60+
XCTAssertFalse(nav.isNavigationBarHidden)
61+
XCTAssertEqual(nav.sheetPresentationController?.detents.map(\.identifier), [.init("profile"), .large])
62+
XCTAssertEqual(nav.sheetPresentationController?.selectedDetentIdentifier, detent)
63+
XCTAssertTrue(pan.isEnabled)
64+
XCTAssertFalse(disabledPan.isEnabled)
65+
XCTAssertTrue(nav.interactivePopGestureRecognizer?.isEnabled ?? false)
66+
XCTAssertFalse(nav.isModalInPresentation)
67+
XCTAssertTrue(avatar.isEnabled)
68+
XCTAssertFalse(avatar.isHidden)
69+
}
70+
// UIKit releases popped controllers at the end of its autorelease turn.
71+
let released = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in engine == nil }, object: nil)
72+
await fulfillment(of: [released], timeout: 2)
73+
}
74+
XCTAssertEqual(watchdogs.count, 2)
75+
withExtendedLifetime(coordinator) {}
76+
nav.dismiss(animated: false)
77+
}
78+
79+
@MainActor
80+
private func descendants(_ view: UIView) -> [UIView] {
81+
view.subviews.flatMap { [$0] + descendants($0) }
82+
}
83+
}
84+
85+
private final class ProfileSetupTestMessenger: NSObject, FlutterBinaryMessenger {
86+
private var handler: FlutterBinaryMessageHandler?
87+
func send(onChannel channel: String, message: Data?) {}
88+
func send(onChannel channel: String, message: Data?, binaryReply callback: FlutterBinaryReply?) { callback?(nil) }
89+
func setMessageHandlerOnChannel(_ channel: String, binaryMessageHandler handler: FlutterBinaryMessageHandler?) -> FlutterBinaryMessengerConnection {
90+
self.handler = handler
91+
return 1
92+
}
93+
func cleanUpConnection(_ connection: FlutterBinaryMessengerConnection) { handler = nil }
94+
func invoke(_ method: String, arguments: [String: Any]) {
95+
let codec = FlutterStandardMethodCodec.sharedInstance()
96+
handler?(codec.encode(FlutterMethodCall(methodName: method, arguments: arguments))) { _ in }
97+
}
98+
}

‎mobile/lib/features/profile/animated_avatar_capture.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ class AnimatedAvatarCapture extends HookConsumerWidget {
4646
required this.onPrepareChanged,
4747
this.initialFrames = const [],
4848
this.disposalBarrier,
49+
this.previewKey,
4950
});
5051

52+
/// Identifies the visible avatar for the native sheet transition.
53+
final GlobalKey? previewKey;
54+
5155
/// The vertical space available to the capture surface.
5256
final double height;
5357

@@ -427,6 +431,7 @@ class AnimatedAvatarCapture extends HookConsumerWidget {
427431
.toDouble();
428432
},
429433
child: SizedBox.square(
434+
key: previewKey,
430435
dimension: _animatedAvatarPreviewSize,
431436
child: Stack(
432437
fit: StackFit.expand,
@@ -585,6 +590,7 @@ class AnimatedAvatarCapture extends HookConsumerWidget {
585590
key: const ValueKey('animated-avatar-capture-preview'),
586591
dimension: capturePreviewSize,
587592
child: Stack(
593+
key: previewKey,
588594
fit: StackFit.expand,
589595
children: [
590596
Padding(

‎mobile/lib/features/profile/profile_avatar_editor.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ class ProfileAvatarEditor extends HookConsumerWidget {
432432
onPrepareChanged: onAnimatedPrepareChanged,
433433
) ??
434434
AnimatedAvatarCapture(
435+
previewKey: previewKey,
435436
height: modeHeight,
436437
onPrepareChanged: onAnimatedPrepareChanged,
437438
disposalBarrier: cameraDisposal.value,

‎mobile/lib/features/profile/profile_setup_avatar_editor.dart‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ class ProfileSetupAvatarEditor extends HookConsumerWidget {
7070
busy.value = true;
7171
error.value = false;
7272
try {
73-
final result = mode.value == ProfileAvatarMode.animated
74-
? await prepare.value?.call()
73+
final result =
74+
mode.value == ProfileAvatarMode.animated &&
75+
prepare.value != null
76+
? await prepare.value!()
7577
: draft.value;
7678
if (context.mounted && result != null) {
7779
if (onFinished != null) {
@@ -103,6 +105,7 @@ class ProfileSetupAvatarEditor extends HookConsumerWidget {
103105
)
104106
: IconButton(
105107
onPressed: close,
108+
tooltip: 'Back to profile',
106109
icon: const Icon(Icons.arrow_back),
107110
),
108111
actions: [
@@ -151,6 +154,7 @@ class ProfileSetupAvatarEditor extends HookConsumerWidget {
151154
onModeChanged: (value) {
152155
mode.value = value;
153156
draft.value = null;
157+
prepare.value = null;
154158
},
155159
onDraftChanged: (value) => draft.value = value,
156160
onAnimatedPrepareChanged: (value) {

0 commit comments

Comments
 (0)