Files
buzz/mobile/lib/shared/widgets/text_editor.dart
klopez4212 14e5ea7cc4 Mobile: refine section filters and ordering in Messages (#9796)
## Summary

Give Messages sections a common options sheet for sorting, filtering and
ordering. Active sections let people choose a time window. Channel and
DM sections can reveal filtered rows in place with **Show more** and
collapse them with **Show fewer**. Reordering saves a device-local order
for the current account and community.

Failed activity lookups keep affected channels visible. The recovery
toast offers Retry and dismisses when activity loads. On iOS the sheet
dims the Messages header along with the list.

### Related work

#9707 explores a count-based disclosure. This change uses the section’s
unread or activity filter.

### iOS snapshots

| Section options with an active filter | Reorder sections |
| --- | --- |
| ![Section options with a 30-day activity
window](https://github.com/user-attachments/assets/4b0fd306-f7ba-4466-aeca-18627bc05c57)
| ![Reorder sheet with draggable Solo, project, Team and Direct messages
rows](https://github.com/user-attachments/assets/3a3f1429-6334-4c8e-a535-630ae860376c)
|

### Rename a section

Tap the emoji beside the name to open the picker. Set saves both fields,
including an emoji-only change.

![Rename section with a new emoji
selected](https://github.com/user-attachments/assets/840bf926-f29f-435e-87de-2470d32ac64c)

### Activity recovery and section order

Unfiled Solo chats support local **Show more** and **Show fewer**,
including offline. Failed activity lookups retry automatically up to
three times with jittered backoff while Messages is visible and
connected. **Retry** stays available after that budget is exhausted. The
notice dismisses on recovery.

Reorder supports drag handles and keyboard arrow keys. Saving preserves
the position of an empty Starred section and checks for a newer order.
Each signing identity has its own saved order per relay.

### Testing

Exercised filtering, reorder and dismissal in the iOS simulator. The
preview was also tested on iPhone Air. At `6135e314f3`, Flutter analysis
and the full pre-push mobile checks passed (5,613 tests passed, 71
skipped). Widget regressions cover offline Solo disclosure, retry
exhaustion, route visibility, keyboard reorder, large text, and editor
cancellation.

---------

Signed-off-by: kenny lopez <klopez4212@gmail.com>
Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
2026-11-20 17:20:08 +00:00

178 lines
5.5 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.

import 'package:buzz/shared/widgets/feedback_snackbar.dart';
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import '../theme/theme.dart';
import 'modal_presentation.dart';
import 'ios_text_editor.dart';
/// Opens the shared native iOS text form, with a Flutter sheet fallback.
Future<void> showTextEditor({
required BuildContext context,
required String title,
required String initialValue,
required String hintText,
required Future<void> Function(String value) onSave,
bool multiline = false,
bool requireNonEmpty = false,
String keyPrefix = 'text-editor',
bool Function(Object error)? shouldRetryOnError,
}) async {
if (defaultTargetPlatform == TargetPlatform.iOS) {
final sheetTheme = utilitySurfaceThemeData(Theme.of(context));
try {
await IosTextEditor.presentUntilSaved(
title: title,
initialValue: initialValue,
placeholder: hintText,
multiline: multiline,
requireNonEmpty: requireNonEmpty,
brightness: sheetTheme.brightness,
pageBackgroundColor: sheetTheme.colorScheme.surface,
containerBackgroundColor:
sheetTheme.colorScheme.surfaceContainerHighest,
onSave: onSave,
shouldRetryOnError: shouldRetryOnError,
canPresent: () =>
context.mounted && (ModalRoute.of(context)?.isCurrent ?? true),
onSaveError: () {
if (context.mounted && (ModalRoute.of(context)?.isCurrent ?? true)) {
_showSaveError(context);
}
},
);
return;
} on MissingPluginException {
// Previews and older builds retain the complete Flutter fallback.
} on PlatformException {
// A temporary native presentation failure should not block editing.
}
}
if (!context.mounted) return;
await showBuzzModalBottomSheet<void>(
context: context,
title: title,
isScrollControlled: true,
requestFocus: true,
builder: (_) => _TextEditSheet(
initialValue: initialValue,
hintText: hintText,
multiline: multiline,
requireNonEmpty: requireNonEmpty,
onSave: onSave,
shouldRetryOnError: shouldRetryOnError,
keyPrefix: keyPrefix,
),
);
}
void _showSaveError(BuildContext context) {
ScaffoldMessenger.of(context).showFeedbackSnackBar(
const SnackBar(content: Text('Couldn’t save changes')),
kind: ToastKind.error,
);
}
class _TextEditSheet extends HookWidget {
const _TextEditSheet({
required this.initialValue,
required this.hintText,
required this.multiline,
required this.requireNonEmpty,
required this.onSave,
this.shouldRetryOnError,
required this.keyPrefix,
});
final String keyPrefix;
final String initialValue;
final String hintText;
final bool multiline;
final bool requireNonEmpty;
final Future<void> Function(String value) onSave;
final bool Function(Object error)? shouldRetryOnError;
@override
Widget build(BuildContext context) {
final controller = useTextEditingController(text: initialValue);
useListenable(controller);
final isSaving = useState(false);
final error = useState<String?>(null);
final trimmed = controller.text.trim();
final hasChanges =
trimmed != initialValue.trim() &&
(!requireNonEmpty || trimmed.isNotEmpty);
Future<void> save() async {
if (!hasChanges || isSaving.value) return;
isSaving.value = true;
error.value = null;
try {
await onSave(controller.text);
if (context.mounted) Navigator.of(context).pop();
} catch (saveError) {
if (!context.mounted) return;
if (shouldRetryOnError?.call(saveError) == false) {
Navigator.of(context).pop();
return;
}
error.value = 'Couldn’t save changes';
} finally {
if (context.mounted) isSaving.value = false;
}
}
return SafeArea(
top: false,
child: SingleChildScrollView(
padding: EdgeInsets.fromLTRB(
Grid.gutter,
Grid.xxs,
Grid.gutter,
MediaQuery.viewInsetsOf(context).bottom + Grid.xs,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextField(
key: ValueKey('$keyPrefix-input'),
controller: controller,
autofocus: true,
enabled: !isSaving.value,
minLines: multiline ? 4 : 1,
maxLines: multiline ? 6 : 1,
textCapitalization: TextCapitalization.sentences,
textInputAction: multiline
? TextInputAction.newline
: TextInputAction.done,
onSubmitted: multiline ? null : (_) => unawaited(save()),
decoration: InputDecoration(hintText: hintText),
),
if (error.value != null) ...[
const SizedBox(height: Grid.xxs),
Text(
error.value!,
style: context.textTheme.bodySmall?.copyWith(
color: context.colors.error,
),
),
],
const SizedBox(height: Grid.xs),
FilledButton(
key: ValueKey('$keyPrefix-save'),
onPressed: hasChanges && !isSaving.value ? save : null,
child: Text(isSaving.value ? 'Saving…' : 'Save'),
),
],
),
),
);
}
}