Files
buzz/mobile/lib/features/channels/compose_bar/voice_note.dart
klopez4212 d647c9fd98 Refine iOS composer and transcribe recordings into drafts (#9574)
Refine the iOS composer with 40-point glass controls, native attachment
menus, persistent emoji access, and selected-text formatting. The mic
now transcribes recordings on device into an editable, unsent draft;
Dictation has no five-minute recording cap or timer label, and its
waveform fills the space between the controls. Voice note remains
available in attachments with its five-minute cap. Unsupported devices
fail before recording, and audio is retained until the draft is durably
saved. Stopped dictation remains recoverable across editor teardown;
failed retention and cleanup can be retried. Recovered transcripts are
inserted only once across navigation or restart, preserving live exact
mention bindings.

Preserve selection handles over glass with animated avatars. Reduce the
conversation fade to 100 points above the keyboard and transition
continuously to 180 as the keyboard closes, without a first-frame jump
when opening; Home is unchanged.

Validation: 5,117 mobile tests and 27 crypto tests pass; analysis
passes. Prior native suite on iPhone 16/iOS 26.5: 119 pass, one
pre-existing expected failure, zero unexpected failures. Physical Air
preview approved; simulator selection/dragging and attachment flows
exercised. Fixed the native navigation test's timing race and a
community-cache cleanup test that checked a parent directory before its
asynchronous removal completed. Repository-wide `just ci` previously
stopped in unchanged `buzz-test-client` load-generator tests; new-head
hosted checks are pending.

Snapshots: production composer in an iPhone Air simulator fixture with
fictional conversation text; Photos snapshot retained from the
attachment preview.

| Composer | Attachments | Photos |
| --- | --- | --- |
| ![Keyboard-up
composer](https://github.com/user-attachments/assets/b7c8f0a4-25f8-4ca2-b8ae-11e18655e64a)
| ![Native attachment
menu](https://github.com/user-attachments/assets/5eaf5259-8e04-4da7-93f5-1b2eccab3c3c)
|
![Photos](https://github.com/user-attachments/assets/a6216b7d-5eaf-482c-b587-6052825b88fc)
|

---------

Signed-off-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
Signed-off-by: Tom Brow <tomb@squareup.com>
Signed-off-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
Co-authored-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
Co-authored-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
2026-11-20 16:35:20 +00:00

212 lines
6.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

part of '../compose_bar.dart';
class _ComposerVoiceNote {
const _ComposerVoiceNote({
required this.start,
required this.startTranscription,
required this.transcriptionPanel,
required this.isDictating,
required this.transcription,
required this.onKeyboardHidden,
required this.onDraftIdentityChanged,
required this.insertedTranscriptionId,
required ValueNotifier<bool> isPreparing,
required ValueNotifier<bool> isRecording,
required ValueChanged<VoiceNoteRecording> onRecorded,
}) : _isPreparing = isPreparing,
_isRecording = isRecording,
_onRecorded = onRecorded;
final VoidCallback start;
final VoidCallback startTranscription;
final Widget? transcriptionPanel;
final bool isDictating;
final ComposerTranscription transcription;
final VoidCallback onKeyboardHidden;
final VoidCallback onDraftIdentityChanged;
/// The recording whose transcript the editor's next draft snapshot carries.
final ObjectRef<String?> insertedTranscriptionId;
final ValueNotifier<bool> _isPreparing;
final ValueNotifier<bool> _isRecording;
final ValueChanged<VoiceNoteRecording> _onRecorded;
bool get isPreparing => _isPreparing.value;
bool get isRecording => _isRecording.value;
Widget? get recorder =>
transcriptionPanel ??
(isRecording
? VoiceNoteComposerRecorder(
isDictation: isDictating,
onCancel: () => _isRecording.value = false,
onRecorded: _onRecorded,
onStopping: isDictating ? transcription.stopping : null,
onStopFailed: isDictating ? transcription.stopFailed : null,
)
: transcription.pendingStop.value
? Semantics(liveRegion: true, child: const Text('Saving recording…'))
: null);
}
bool _voiceNoteFullWidth(
_ComposerVoiceNote voiceNote,
List<_PendingAttachment> attachments,
) =>
voiceNote.transcriptionPanel != null ||
voiceNote.transcription.pendingStop.value ||
voiceNote.isPreparing ||
voiceNote.isRecording ||
attachments.any((item) => item.kind == _PendingAttachmentKind.voiceNote);
_ComposerVoiceNote _useComposerVoiceNote({
required BuildContext context,
required WidgetRef ref,
required TextEditingController controller,
required String scope,
required String draftKey,
required ValueNotifier<_InputTool?> inputTool,
required FocusNode focusNode,
required ValueNotifier<bool> isComposerExpanded,
required ValueNotifier<bool> showFormatting,
required ValueNotifier<_AttachmentSurface> attachmentSurface,
required ValueNotifier<String?> uploadError,
required ObjectRef<int> draftRevision,
required ValueNotifier<List<_PendingAttachment>> attachments,
}) {
final owner = useRef(scope)..value = scope;
final isPreparing = useState(false);
final isRecording = useState(false);
final dictating = useState(false);
final availabilityCheck = useRef<Object?>(null);
final insertedTranscriptionId = useRef<String?>(null);
final transcript = useComposerTranscription(
context: context,
ref: ref,
scope: scope,
draftKey: draftKey,
onText: (text, recordingId) {
final existing = controller.text;
final separator =
existing.isEmpty || existing.endsWith(' ') || existing.endsWith('\n')
? ''
: ' ';
final result = '$existing$separator$text';
// The editor's own draft snapshot saves this text, the mentions bound in
// the editor and the recording's identity together, in one write.
insertedTranscriptionId.value = recordingId;
try {
controller.value = TextEditingValue(
text: result,
selection: TextSelection.collapsed(offset: result.length),
);
} finally {
insertedTranscriptionId.value = null;
}
isComposerExpanded.value = true;
focusNode.requestFocus();
},
);
final resetForDraftIdentityChange = useCallback(() {
isPreparing.value = false;
isRecording.value = false;
dictating.value = false;
availabilityCheck.value = null;
}, [isPreparing, isRecording, dictating, availabilityCheck]);
void beginRecording() {
if (!isPreparing.value) return;
isPreparing.value = false;
isRecording.value = true;
}
void start({bool transcribe = false}) {
if (transcript.pendingStop.value ||
transcript.panel != null ||
isPreparing.value ||
isRecording.value) {
return;
}
availabilityCheck.value = null;
dictating.value = transcribe;
if (attachments.value.isNotEmpty) {
uploadError.value = 'A voice note must be the only attachment.';
return;
}
attachmentSurface.value = _AttachmentSurface.closed;
showFormatting.value = false;
isComposerExpanded.value = false;
_dismissComposerKeyboard(focusNode);
if (ref.read(huddleSessionProvider).isInSession) {
uploadError.value = 'Leave the Huddle before recording a voice note.';
return;
}
uploadError.value = null;
draftRevision.value += 1;
isPreparing.value = true;
if (View.of(context).viewInsets.bottom == 0) beginRecording();
}
Future<void> startTranscription() async {
if (availabilityCheck.value != null || transcript.pendingStop.value) return;
final request = Object();
availabilityCheck.value = request;
var available = false;
try {
available = await transcript.isAvailable().timeout(
const Duration(seconds: 5),
);
} catch (_) {
// A missing or unresponsive native recognizer cannot handle dictation.
}
if (!context.mounted || availabilityCheck.value != request) return;
availabilityCheck.value = null;
if (controller.text.isNotEmpty) return;
if (!available) {
uploadError.value =
'Dictation isn’t available on this device or language.';
return;
}
inputTool.value = null;
start(transcribe: true);
}
void complete(VoiceNoteRecording recording) {
draftRevision.value += 1;
uploadError.value = null;
attachments.value = [
...attachments.value,
_PendingAttachment(
file: recording.file,
kind: _PendingAttachmentKind.voiceNote,
deleteAfterUse: true,
duration: recording.duration,
waveform: recording.waveform,
),
];
isRecording.value = false;
}
return _ComposerVoiceNote(
start: start,
startTranscription: () => unawaited(startTranscription()),
transcriptionPanel: transcript.panel,
isDictating: dictating.value,
transcription: transcript,
onKeyboardHidden: beginRecording,
onDraftIdentityChanged: resetForDraftIdentityChange,
insertedTranscriptionId: insertedTranscriptionId,
isPreparing: isPreparing,
isRecording: isRecording,
onRecorded: dictating.value
? (recording) {
transcript.complete(recording);
if (context.mounted && owner.value == scope) {
isRecording.value = false;
}
}
: complete,
);
}