Skip to content

Commit 5f80ee1

Browse files
browstudio
andauthored
fix(mobile): keep Move to section working after the actions sheet closes (#9814)
Move to section has been failing on mobile: choosing a section, creating a new one, or removing the channel from its section throws a `StateError` ("Using ref when a widget is about to or has been unmounted") and the channel stays where it was. - **Disposed ref.** The section picker opens from the channel actions sheet, which closes first. The picker read and wrote sections through that sheet's `WidgetRef`, which is disposed by the time the user taps a row. It now uses the page's `ProviderContainer`, which it already used to listen for desktop's layout lock. Channel details passed its own `ref` the same way and now goes through the same path. - **New section never filed the channel.** It created the section, then looked it up by name in the provider's state, which local edits only reached after a relay round trip. The notifier now creates the section and files the channel in one call, by the new section's ID, so a second section with the same name can't catch it either. - **Local edits show at once.** `createSection`, `assignChannel` and `unassignChannel` now emit the notifier's state right away, as `deleteSection` already did, so a picked or removed channel moves in the sidebar immediately instead of after the relay echoes the edit. - The New section dialog disposed its text controller while still animating out; the dialog now owns its controller. ### Validation - [Move to section journey](https://github.com/block/buzz/blob/0147d7a0f2f005e47dcb5aec201dc5d3f255846c/mobile/test/features/channels/channel_actions_sheet_test.dart): opens the picker from the actions sheet with the real sections notifier (storage and relay faked) and checks the shown and saved section after picking a section, Remove from section, and New section, including when a section with the new name already exists. - Automated iOS simulator run (iPhone 17 Pro, signed-in Builderlab account): long-press a channel, Move to section, then New section (with a name that already existed), pick an existing section, and Remove from section. Each move showed in the sidebar immediately and no errors were logged. The recording shows real channel names, so it is shared internally rather than here. ### Regressions None expected. The picker reads the same provider from the same root scope (mobile has a single `ProviderScope`), through a handle that outlives the closed sheet. Emitting after local edits rebuilds section listeners once per edit, which Delete section already did. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
1 parent 47b00c0 commit 5f80ee1

5 files changed

Lines changed: 355 additions & 56 deletions

File tree

‎mobile/lib/features/channels/channel_actions_sheet.dart‎

Lines changed: 96 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ class ChannelActionsSheet extends HookConsumerWidget {
263263
close();
264264
await _showMoveSectionSheet(
265265
pageContext,
266-
ref,
267266
channel: channel,
268267
sectionId: sectionId,
269268
);
@@ -616,34 +615,55 @@ Future<void> _confirmAndRun(
616615
}
617616

618617
Future<void> _showMoveSectionSheet(
619-
BuildContext context,
620-
WidgetRef ref, {
618+
BuildContext context, {
621619
required Channel channel,
622620
required String? sectionId,
623621
}) async {
624-
final sections = [...ref.read(channelSectionsProvider).store.sections]
622+
// The actions sheet that opened this picker is already closing, and its
623+
// WidgetRef is disposed with it. Use the page's container throughout.
624+
final container = ProviderScope.containerOf(context, listen: false);
625+
// A notification can switch communities while the picker or name dialog is
626+
// open. Both close then, and only the community that opened them is edited.
627+
final config = container.read(relayConfigProvider);
628+
bool inScope() {
629+
final current = container.read(relayConfigProvider);
630+
return current.storedOrigin == config.storedOrigin &&
631+
current.nsec == config.nsec;
632+
}
633+
634+
void edit(void Function(ChannelSectionsNotifier sections) change) {
635+
if (inScope()) change(container.read(channelSectionsProvider.notifier));
636+
}
637+
638+
final sections = [...container.read(channelSectionsProvider).store.sections]
625639
..sort((a, b) => a.order.compareTo(b.order));
626640
BuildContext? sheet;
627-
// Desktop's unified layout can lock legacy filing while this is open. The
628-
// sheet that opened this picker may already be gone, so listen on the
629-
// page's container.
630-
final container = ProviderScope.containerOf(context, listen: false);
641+
void close() {
642+
final open = sheet;
643+
if (open != null) _closeOwnRoute(open);
644+
}
645+
646+
// Desktop's unified layout can lock legacy filing while this is open.
631647
final lock = container.listen(
632648
channelSectionsProvider.select((state) => state.editable),
633649
(_, editable) {
634-
final open = sheet;
635-
if (!editable && open != null && open.mounted) Navigator.of(open).pop();
650+
if (!editable) close();
636651
},
637652
);
653+
final scope = container.listen(relayConfigProvider, (_, _) {
654+
if (!inScope()) close();
655+
});
638656
await showBuzzModalBottomSheet<void>(
639657
context: context,
640658
showDragHandle: true,
641659
builder: (sheetContext) {
642-
// A lock that landed before this first frame found no sheet to close.
643-
if (sheet == null && !container.read(channelSectionsProvider).editable) {
644-
WidgetsBinding.instance.addPostFrameCallback((_) {
645-
if (sheetContext.mounted) Navigator.of(sheetContext).pop();
646-
});
660+
// A lock or switch that landed before this first frame found no sheet
661+
// to close.
662+
if (sheet == null &&
663+
(!container.read(channelSectionsProvider).editable || !inScope())) {
664+
WidgetsBinding.instance.addPostFrameCallback(
665+
(_) => _closeOwnRoute(sheetContext),
666+
);
647667
}
648668
sheet = sheetContext;
649669
return SafeArea(
@@ -670,29 +690,20 @@ Future<void> _showMoveSectionSheet(
670690
: null,
671691
onTap: () {
672692
Navigator.of(sheetContext).pop();
673-
ref
674-
.read(channelSectionsProvider.notifier)
675-
.assignChannel(channel.id, section.id);
693+
edit((s) => s.assignChannel(channel.id, section.id));
676694
},
677695
),
678696
ListTile(
679697
leading: const Icon(BuzzIcons.folderPlus),
680698
title: const Text('New section…'),
681699
onTap: () async {
682700
Navigator.of(sheetContext).pop();
683-
final name = await _showSectionNameDialog(context);
701+
final name = await _showSectionNameDialog(
702+
context,
703+
inScope: inScope,
704+
);
684705
if (name == null || name.isEmpty) return;
685-
final notifier = ref.read(channelSectionsProvider.notifier);
686-
notifier.createSection(name);
687-
final created = ref
688-
.read(channelSectionsProvider)
689-
.store
690-
.sections
691-
.where((section) => section.name == name.trim())
692-
.lastOrNull;
693-
if (created != null) {
694-
notifier.assignChannel(channel.id, created.id);
695-
}
706+
edit((s) => s.createSection(name, channelId: channel.id));
696707
},
697708
),
698709
if (sectionId != null)
@@ -701,9 +712,7 @@ Future<void> _showMoveSectionSheet(
701712
title: const Text('Remove from section'),
702713
onTap: () {
703714
Navigator.of(sheetContext).pop();
704-
ref
705-
.read(channelSectionsProvider.notifier)
706-
.unassignChannel(channel.id);
715+
edit((s) => s.unassignChannel(channel.id));
707716
},
708717
),
709718
],
@@ -712,29 +721,70 @@ Future<void> _showMoveSectionSheet(
712721
),
713722
);
714723
},
715-
).whenComplete(lock.close);
724+
).whenComplete(() {
725+
lock.close();
726+
scope.close();
727+
});
716728
}
717729

718-
Future<String?> _showSectionNameDialog(BuildContext context) async {
719-
final controller = TextEditingController();
720-
final result = await showBuzzDialog<String>(
721-
context: context,
722-
builder: (dialogContext) => AlertDialog(
730+
/// Closes the route that [context] belongs to, once. Later calls, such as a
731+
/// second community switch, find it already leaving and never pop the page
732+
/// underneath.
733+
void _closeOwnRoute(BuildContext context) {
734+
if (!context.mounted) return;
735+
final route = ModalRoute.of(context);
736+
if (route == null || !route.isActive) return;
737+
final navigator = Navigator.of(context);
738+
if (route.isCurrent) {
739+
navigator.pop();
740+
} else {
741+
navigator.removeRoute(route);
742+
}
743+
}
744+
745+
Future<String?> _showSectionNameDialog(
746+
BuildContext context, {
747+
required bool Function() inScope,
748+
}) => showBuzzDialog<String>(
749+
context: context,
750+
builder: (_) => _SectionNameDialog(inScope: inScope),
751+
);
752+
753+
/// Owns its controller so the field outlives the dialog's exit animation.
754+
class _SectionNameDialog extends HookConsumerWidget {
755+
const _SectionNameDialog({required this.inScope});
756+
757+
/// False once the community that opened the dialog is no longer active.
758+
final bool Function() inScope;
759+
760+
@override
761+
Widget build(BuildContext context, WidgetRef ref) {
762+
final controller = useTextEditingController();
763+
ref.listen(relayConfigProvider, (_, _) {
764+
if (!inScope()) _closeOwnRoute(context);
765+
});
766+
// A switch that landed before this first frame found no dialog to close.
767+
useEffect(() {
768+
if (!inScope()) {
769+
WidgetsBinding.instance.addPostFrameCallback(
770+
(_) => _closeOwnRoute(context),
771+
);
772+
}
773+
return null;
774+
}, const []);
775+
return AlertDialog(
723776
title: const Text('New Section'),
724777
content: TextField(controller: controller, autofocus: true),
725778
actions: [
726779
TextButton(
727-
onPressed: () => Navigator.of(dialogContext).pop(),
780+
onPressed: () => Navigator.of(context).pop(),
728781
child: const Text('Cancel'),
729782
),
730783
FilledButton(
731-
onPressed: () =>
732-
Navigator.of(dialogContext).pop(controller.text.trim()),
784+
onPressed: () => Navigator.of(context).pop(controller.text.trim()),
733785
child: const Text('Create'),
734786
),
735787
],
736-
),
737-
);
738-
controller.dispose();
739-
return result;
788+
);
789+
}
740790
}

‎mobile/lib/features/channels/channel_details_page.dart‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ class ChannelDetailsPage extends HookConsumerWidget {
404404
BuzzIcons.folderInput,
405405
() => _showMoveSectionSheet(
406406
context,
407-
ref,
408407
channel: resolvedChannel,
409408
sectionId: currentSectionId,
410409
),
@@ -687,7 +686,6 @@ class ChannelDetailsPage extends HookConsumerWidget {
687686
onTap: () async {
688687
await _showMoveSectionSheet(
689688
context,
690-
ref,
691689
channel: resolvedChannel,
692690
sectionId: currentSectionId,
693691
);

‎mobile/lib/features/channels/channel_sections/channel_sections_manager.dart‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ class ChannelSectionsManager {
245245
_unsubscribe = null;
246246
}
247247

248-
void createSection(String name) {
248+
/// Files [channelId] in the new section in the same write, when given.
249+
void createSection(String name, {String? channelId}) {
249250
if (!_editable) return;
250251
final maxOrder = _store.sections.fold<int>(
251252
-1,
@@ -258,7 +259,9 @@ class ChannelSectionsManager {
258259
);
259260
_store = ChannelSectionStore(
260261
sections: [..._store.sections, section],
261-
assignments: _store.assignments,
262+
assignments: channelId == null
263+
? _store.assignments
264+
: {..._store.assignments, channelId: section.id},
262265
);
263266
_persist();
264267
markDirty();

‎mobile/lib/features/channels/channel_sections/channel_sections_provider.dart‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ class ChannelSectionsNotifier extends Notifier<ChannelSectionsState> {
101101
);
102102
}
103103

104-
void createSection(String name) => _manager?.createSection(name);
104+
/// Files [channelId] in the new section in the same write, when given.
105+
void createSection(String name, {String? channelId}) =>
106+
_edit((m) => m.createSection(name, channelId: channelId));
105107

106108
void renameSection(String sectionId, String? newName, {String? icon}) =>
107109
_manager?.renameSection(sectionId, newName, icon: icon);
@@ -119,10 +121,19 @@ class ChannelSectionsNotifier extends Notifier<ChannelSectionsState> {
119121
_manager?.moveSectionDown(sectionId);
120122

121123
void assignChannel(String channelId, String sectionId) =>
122-
_manager?.assignChannel(channelId, sectionId);
124+
_edit((m) => m.assignChannel(channelId, sectionId));
123125

124126
void unassignChannel(String channelId) =>
125-
_manager?.unassignChannel(channelId);
127+
_edit((m) => m.unassignChannel(channelId));
128+
129+
/// Applies a local edit and emits it at once, as [deleteSection] does,
130+
/// rather than waiting for the relay to echo it back.
131+
void _edit(void Function(ChannelSectionsManager manager) edit) {
132+
final manager = _manager;
133+
if (manager == null) return;
134+
edit(manager);
135+
_emitManagerState(manager);
136+
}
126137

127138
void _emitManagerState(ChannelSectionsManager manager) {
128139
if (_manager != manager) return;

0 commit comments

Comments
 (0)