mirror of
https://github.com/block/buzz.git
synced 2026-11-24 10:12:19 +00:00
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 | | --- | --- | --- | |  |  |  | --------- 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>
261 lines
8.8 KiB
Dart
261 lines
8.8 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import '../relay/relay.dart';
|
|
import '../theme/theme_provider.dart';
|
|
|
|
const _draftsPrefsKey = 'compose_drafts_v1';
|
|
const _maxDrafts = 50;
|
|
|
|
/// A locally persisted, unsent composer draft.
|
|
///
|
|
/// Mobile has no durable relay-backed draft store (desktop keeps drafts
|
|
/// locally too), so drafts are device-local by design: the inbox Drafts
|
|
/// filter reflects what this device's composer has in flight.
|
|
@immutable
|
|
class ComposeDraft {
|
|
/// `<channelId>` for channel composers, `<channelId>:<threadHeadId>` for
|
|
/// thread composers.
|
|
final String key;
|
|
final String channelId;
|
|
final String? threadHeadId;
|
|
final String text;
|
|
|
|
/// Recording whose transcript is included in this same persisted snapshot.
|
|
final String? transcriptionId;
|
|
|
|
/// Literal picker labels bound to exact keys, never authorization metadata.
|
|
/// An empty key/value is a malformed-record tombstone; send must refuse it.
|
|
final Map<String, String> mentionKeys;
|
|
final int updatedAt; // unix seconds
|
|
|
|
const ComposeDraft({
|
|
required this.key,
|
|
required this.channelId,
|
|
required this.threadHeadId,
|
|
required this.text,
|
|
required this.updatedAt,
|
|
this.mentionKeys = const {},
|
|
this.transcriptionId,
|
|
});
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
'key': key,
|
|
'channel_id': channelId,
|
|
if (threadHeadId != null) 'thread_head_id': threadHeadId,
|
|
'text': text,
|
|
if (transcriptionId != null) 'transcription_id': transcriptionId,
|
|
'mention_keys': mentionKeys,
|
|
'updated_at': updatedAt,
|
|
};
|
|
|
|
static ComposeDraft? fromJson(Object? raw) {
|
|
if (raw is! Map<String, dynamic>) return null;
|
|
final key = raw['key'];
|
|
final channelId = raw['channel_id'];
|
|
final text = raw['text'];
|
|
final updatedAt = raw['updated_at'];
|
|
if (key is! String || channelId is! String || text is! String) return null;
|
|
if (text.trim().isEmpty) return null;
|
|
final bindings = raw['mention_keys'];
|
|
final mentionKeys = <String, String>{};
|
|
if (raw.containsKey('mention_keys')) {
|
|
if (bindings is! Map<String, dynamic>) {
|
|
mentionKeys[''] = '';
|
|
} else {
|
|
final labels = <String>{};
|
|
for (final entry in bindings.entries) {
|
|
if (!labels.add(entry.key.toLowerCase())) mentionKeys[''] = '';
|
|
final value = entry.value;
|
|
mentionKeys[entry.key] =
|
|
entry.key.isNotEmpty &&
|
|
value is String &&
|
|
RegExp(r'^[0-9a-fA-F]{64}$').hasMatch(value)
|
|
? value.toLowerCase()
|
|
: '';
|
|
}
|
|
}
|
|
}
|
|
return ComposeDraft(
|
|
key: key,
|
|
channelId: channelId,
|
|
threadHeadId: raw['thread_head_id'] is String
|
|
? raw['thread_head_id'] as String
|
|
: null,
|
|
mentionKeys: Map.unmodifiable(mentionKeys),
|
|
text: text,
|
|
transcriptionId: raw['transcription_id'] is String
|
|
? raw['transcription_id'] as String
|
|
: null,
|
|
updatedAt: updatedAt is int ? updatedAt : 0,
|
|
);
|
|
}
|
|
}
|
|
|
|
String composeDraftKey(String channelId, {String? threadHeadId}) =>
|
|
threadHeadId == null ? channelId : '$channelId:$threadHeadId';
|
|
|
|
/// SharedPreferences-backed store of unsent composer drafts, newest first.
|
|
///
|
|
/// Drafts are plaintext, so persistence is namespaced by community (relay)
|
|
/// and account pubkey: switching community or account rebuilds this provider
|
|
/// against that identity's own store and can never surface another
|
|
/// identity's draft text.
|
|
class ComposeDraftsNotifier extends Notifier<List<ComposeDraft>> {
|
|
late String _prefsKey;
|
|
Future<bool> _pendingWrite = Future.value(true);
|
|
|
|
@override
|
|
List<ComposeDraft> build() {
|
|
// Rebuild (and re-read the identity-scoped store) whenever the active
|
|
// community or the derived account pubkey changes.
|
|
final config = ref.watch(relayConfigProvider);
|
|
final pubkey = ref.watch(myPubkeyProvider) ?? 'anon';
|
|
_prefsKey = _storeKey(config.baseUrl, pubkey);
|
|
_pendingWrite = Future.value(true);
|
|
return _readStore(config, pubkey);
|
|
}
|
|
|
|
static String _storeKey(String origin, String pubkey) =>
|
|
'$_draftsPrefsKey:$origin:$pubkey';
|
|
|
|
List<ComposeDraft> _readStore(RelayConfig config, String pubkey) {
|
|
final prefs = ref.read(savedPrefsProvider);
|
|
final raw = readMigratedPref<String>(
|
|
prefs,
|
|
canonicalKey: _storeKey(config.baseUrl, pubkey),
|
|
legacyKey: _storeKey(config.storedOrigin, pubkey),
|
|
read: prefs.getString,
|
|
write: prefs.setString,
|
|
);
|
|
if (raw == null) return const [];
|
|
try {
|
|
final decoded = jsonDecode(raw);
|
|
if (decoded is! List) return const [];
|
|
final drafts = decoded
|
|
.map(ComposeDraft.fromJson)
|
|
.whereType<ComposeDraft>()
|
|
.toList();
|
|
drafts.sort((a, b) => b.updatedAt.compareTo(a.updatedAt));
|
|
return drafts;
|
|
} catch (_) {
|
|
return const [];
|
|
}
|
|
}
|
|
|
|
/// Adds [text] after any draft at [key] in the store of the community at
|
|
/// [config], which need not be the selected one. Another community's
|
|
/// store is written without being loaded here, so its text never appears
|
|
/// in this community. Completes once it's saved.
|
|
Future<void> append({
|
|
required RelayConfig config,
|
|
required String key,
|
|
required String channelId,
|
|
String? threadHeadId,
|
|
required String text,
|
|
}) {
|
|
final pubkey = pubkeyFromNsec(config.nsec) ?? 'anon';
|
|
final selected = _storeKey(config.baseUrl, pubkey) == _prefsKey;
|
|
final drafts = selected ? state : _readStore(config, pubkey);
|
|
final existing = drafts.where((d) => d.key == key).firstOrNull;
|
|
final existingText = existing?.text.trimRight() ?? '';
|
|
final draft = ComposeDraft(
|
|
key: key,
|
|
channelId: channelId,
|
|
threadHeadId: threadHeadId,
|
|
text: existingText.isEmpty ? text : '$existingText\n\n$text',
|
|
mentionKeys: existing?.mentionKeys ?? const {},
|
|
transcriptionId: existing?.transcriptionId,
|
|
updatedAt: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
|
);
|
|
final next = [draft, ...drafts.where((d) => d.key != key)];
|
|
final kept = next.length > _maxDrafts ? next.sublist(0, _maxDrafts) : next;
|
|
if (!selected) return _write(_storeKey(config.baseUrl, pubkey), kept);
|
|
state = List.unmodifiable(kept);
|
|
return _write(_prefsKey, kept);
|
|
}
|
|
|
|
/// Persist [text] for the composer identified by [key]. Empty text removes
|
|
/// the draft.
|
|
void save({
|
|
required String key,
|
|
required String channelId,
|
|
String? threadHeadId,
|
|
required String text,
|
|
Map<String, String> mentionKeys = const {},
|
|
String? transcriptionId,
|
|
}) {
|
|
if (text.trim().isEmpty) {
|
|
remove(key);
|
|
return;
|
|
}
|
|
final existing = state.where((d) => d.key == key).firstOrNull;
|
|
final insertion = transcriptionId ?? existing?.transcriptionId;
|
|
if (existing?.text == text &&
|
|
existing?.transcriptionId == insertion &&
|
|
mapEquals(existing?.mentionKeys, mentionKeys)) {
|
|
return;
|
|
}
|
|
final draft = ComposeDraft(
|
|
key: key,
|
|
channelId: channelId,
|
|
threadHeadId: threadHeadId,
|
|
text: text,
|
|
mentionKeys: Map.unmodifiable(mentionKeys),
|
|
transcriptionId: insertion,
|
|
updatedAt: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
|
);
|
|
final next = [draft, ...state.where((d) => d.key != key)];
|
|
_persist(next.length > _maxDrafts ? next.sublist(0, _maxDrafts) : next);
|
|
}
|
|
|
|
void remove(String key) {
|
|
if (!state.any((d) => d.key == key)) return;
|
|
_persist([...state.where((d) => d.key != key)]);
|
|
}
|
|
|
|
/// Read text and selected identities from the same persisted snapshot.
|
|
ComposeDraft? draftFor(String key) =>
|
|
state.where((d) => d.key == key).firstOrNull;
|
|
|
|
String? textFor(String key) => draftFor(key)?.text;
|
|
|
|
/// Confirms the draft snapshot reached storage before dropping its source.
|
|
Future<bool> ensurePersisted() async {
|
|
final scope = _prefsKey;
|
|
await _pendingWrite;
|
|
if (_prefsKey != scope) return false;
|
|
// A rebuilt provider may have read a cache entry from a failed platform
|
|
// write. Confirm the current snapshot durably before releasing its audio.
|
|
return _pendingWrite = _writeDrafts(state);
|
|
}
|
|
|
|
void _persist(List<ComposeDraft> drafts) {
|
|
state = List.unmodifiable(drafts);
|
|
_pendingWrite = _writeDrafts(drafts);
|
|
}
|
|
|
|
Future<bool> _writeDrafts(List<ComposeDraft> drafts) async {
|
|
final prefs = ref.read(savedPrefsProvider);
|
|
try {
|
|
return await prefs.setString(
|
|
_prefsKey,
|
|
jsonEncode([for (final d in drafts) d.toJson()]),
|
|
);
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<void> _write(String prefsKey, List<ComposeDraft> drafts) => ref
|
|
.read(savedPrefsProvider)
|
|
.setString(prefsKey, jsonEncode([for (final d in drafts) d.toJson()]));
|
|
}
|
|
|
|
final composeDraftsProvider =
|
|
NotifierProvider<ComposeDraftsNotifier, List<ComposeDraft>>(
|
|
ComposeDraftsNotifier.new,
|
|
);
|