Skip to content

Commit 3b5521c

Browse files
loganjLarryclaude
committed
fix(mobile): keep the search spinner for the whole directory refresh
The scoped directory status does not cover a production refresh end to end. It turns loading only after memberships and metadata arrive, and loaded before display names, latest messages and the new list are installed. With fast message and people replies, the spinner stopped and "No results" showed while the refresh was still running. Search again tracks the directory load it started until its future completes. A token, which rebuild drops, fences it, so a load from an earlier community cannot settle a newer one. A failure notice stays hidden while the next load runs. The journey fake now follows production order and checks scope. A new test drives the real ensureDirectoryLoaded -> retryDirectory path with the membership query held at the relay boundary. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Logan Johnson <[email protected]>
1 parent 28a0990 commit 3b5521c

2 files changed

Lines changed: 149 additions & 13 deletions

File tree

‎mobile/lib/features/search/search_provider.dart‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,18 @@ class SearchNotifier extends Notifier<SearchState> {
151151
Timer? _debounce;
152152

153153
// The relay lookups still running for the current query. The spinner shows
154-
// only while one of them, or the directory load, is pending, so the last
154+
// only while one of them, or a directory load, is pending, so the last
155155
// lookup to finish settles it regardless of which results came back empty.
156156
bool _messagesPending = false;
157157
bool _usersPending = false;
158158

159+
// The directory load this search started, until its future completes. The
160+
// shared directory status is not enough: production marks it loading only
161+
// after memberships arrive, and loaded before the new list is installed.
162+
// A rebuild drops it, so a load from an earlier community cannot settle a
163+
// newer one.
164+
Object? _directoryLoad;
165+
159166
// Bumped by every new query, clear and rebuild. A lookup writes its result
160167
// only while its generation is current, so a late reply to an earlier query,
161168
// even one with the same text, cannot settle the current spinner.
@@ -168,6 +175,7 @@ class SearchNotifier extends Notifier<SearchState> {
168175
_generation++;
169176
_messagesPending = false;
170177
_usersPending = false;
178+
_directoryLoad = null;
171179
// Channel rows come from the loaded channel list, which gains open
172180
// channels when the directory arrives. Re-rank whenever either changes;
173181
// the directory status for this community also drives the spinner.
@@ -215,6 +223,7 @@ class SearchNotifier extends Notifier<SearchState> {
215223
bool get _isLoading =>
216224
_messagesPending ||
217225
_usersPending ||
226+
_directoryLoad != null ||
218227
_directoryIs(ChannelDirectoryLoadStatus.loading);
219228

220229
/// Whether this community's directory load is in [status].
@@ -229,12 +238,16 @@ class SearchNotifier extends Notifier<SearchState> {
229238
}
230239

231240
/// Loads the directory again after it failed, like desktop's Retry channels.
232-
void retryChannels() => _loadDirectory();
241+
void retryChannels() {
242+
_loadDirectory();
243+
_refreshChannels();
244+
}
233245

234246
/// Loads the open-channel directory so search also finds open channels the
235-
/// user has not joined, as desktop search does. Its status changes re-rank
236-
/// the channels and settle the spinner.
247+
/// user has not joined, as desktop search does.
237248
void _loadDirectory() {
249+
if (_directoryLoad != null) return;
250+
final load = _directoryLoad = Object();
238251
unawaited(
239252
ref
240253
.read(channelsProvider.notifier)
@@ -243,6 +256,11 @@ class SearchNotifier extends Notifier<SearchState> {
243256
debugPrint(
244257
'[SearchNotifier] channel directory load failed: $error',
245258
);
259+
})
260+
.whenComplete(() {
261+
if (!ref.mounted || _directoryLoad != load) return;
262+
_directoryLoad = null;
263+
_refreshChannels();
246264
}),
247265
);
248266
}
@@ -315,7 +333,10 @@ class SearchNotifier extends Notifier<SearchState> {
315333
ref.read(channelsProvider).value ?? const <Channel>[],
316334
state.query,
317335
),
318-
channelSearchFailed: _directoryIs(ChannelDirectoryLoadStatus.error),
336+
// An earlier failure stays hidden while our next load runs.
337+
channelSearchFailed:
338+
_directoryLoad == null &&
339+
_directoryIs(ChannelDirectoryLoadStatus.error),
319340
isLoading: _isLoading,
320341
);
321342
}

‎mobile/test/features/search/search_channels_journey_test.dart‎

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,93 @@ void main() {
228228
expect(spinner, findsNothing);
229229
expect(failed, findsNothing);
230230
expect(unjoined, findsOneWidget);
231+
232+
// A community switch retires a directory load still in flight. When it
233+
// finishes, it must not settle the new community's search.
234+
final community =
235+
ProviderScope.containerOf(
236+
tester.element(find.byType(SearchPage)),
237+
).read(relayConfigProvider.notifier)
238+
as _Community;
239+
community.switchTo('http://other.test');
240+
await tester.pump();
241+
await tester.enterText(find.byType(TextField), 'ops');
242+
await tester.pump(const Duration(milliseconds: 300));
243+
expect(channels.directoryRequests, 5);
244+
community.switchTo('http://third.test');
245+
await tester.pump();
246+
await tester.enterText(find.byType(TextField), 'dev');
247+
await tester.pump(const Duration(milliseconds: 300));
248+
expect(channels.directoryRequests, 6);
249+
session.searches.removeLast().complete(const []);
250+
actions.searches.removeLast().complete(const []);
251+
channels.finishDirectory();
252+
await tester.pump();
253+
expect(spinner, findsOneWidget);
254+
channels.finishDirectory();
255+
await tester.pump();
256+
expect(spinner, findsNothing);
257+
});
258+
259+
testWidgets('the production directory refresh holds the spinner', (
260+
tester,
261+
) async {
262+
final session = _MembershipHeldSession();
263+
final actions = _HeldUserSearch();
264+
265+
await tester.pumpWidget(
266+
WidgetHelpers.testable(
267+
overrides: [
268+
relayConfigProvider.overrideWith(_Community.new),
269+
myPubkeyProvider.overrideWith((ref) => 'test'),
270+
relaySessionProvider.overrideWith(() => session),
271+
channelActionsProvider.overrideWith((ref) => actions),
272+
channelsProvider.overrideWith(_NoMemberChannels.new),
273+
recentSearchesProvider.overrideWith(_NoRecentSearches.new),
274+
profileProvider.overrideWith(_Profile.new),
275+
],
276+
child: const SearchPage(),
277+
),
278+
);
279+
await tester.pumpAndSettle();
280+
281+
final spinner = find.byType(BuzzLoadingIndicator);
282+
283+
// The real refresh reads memberships before it marks the directory
284+
// loading. Messages and people finishing first must not stop the spinner.
285+
await tester.enterText(find.byType(TextField), '4things');
286+
await tester.pump(const Duration(milliseconds: 300));
287+
session.searches.removeAt(0).complete(const []);
288+
actions.searches.removeAt(0).complete(const []);
289+
await tester.pump();
290+
expect(session.memberships, hasLength(1));
291+
expect(spinner, findsOneWidget);
292+
expect(find.text("No results for '4things'"), findsNothing);
293+
294+
// The refresh fails; search says so and offers a retry.
295+
session.memberships.single.completeError(StateError('relay down'));
296+
await tester.pump();
297+
expect(spinner, findsNothing);
298+
expect(find.byKey(const Key('search-channels-failed')), findsOneWidget);
299+
300+
// Another caller's directory load keeps the spinner on until it ends,
301+
// though this search's own request returns at once.
302+
final container = ProviderScope.containerOf(
303+
tester.element(find.byType(SearchPage)),
304+
);
305+
final status = container.read(channelDirectoryLoadStatusProvider.notifier);
306+
final scope = channelDirectoryScope('http://community.test', 'test');
307+
status.markLoading(scope);
308+
await tester.enterText(find.byType(TextField), 'ops');
309+
await tester.pump(const Duration(milliseconds: 300));
310+
session.searches.removeAt(0).complete(const []);
311+
actions.searches.removeAt(0).complete(const []);
312+
await tester.pump();
313+
expect(session.memberships, hasLength(1));
314+
expect(spinner, findsOneWidget);
315+
status.markError(scope);
316+
await tester.pump();
317+
expect(spinner, findsNothing);
231318
});
232319
}
233320

@@ -246,6 +333,8 @@ Channel _channel(String name, {required bool isMember}) => Channel(
246333
class _Community extends RelayConfigNotifier {
247334
@override
248335
RelayConfig build() => const RelayConfig(baseUrl: 'http://community.test');
336+
337+
void switchTo(String baseUrl) => state = RelayConfig(baseUrl: baseUrl);
249338
}
250339

251340
/// Holds every message search open until the test answers it.
@@ -283,13 +372,13 @@ class _HeldUserSearch implements ChannelActions {
283372

284373
/// Lists member channels, and adds the open directory when it is loaded.
285374
class _DirectoryChannels extends ChannelsNotifier {
286-
Completer<bool>? _directory;
375+
final _directories = <Completer<bool>>[];
287376
var directoryRequests = 0;
288377
Duration? maxAge;
289378

290-
void finishDirectory() => _directory!.complete(true);
379+
void finishDirectory() => _directories.removeAt(0).complete(true);
291380

292-
void failDirectory() => _directory!.complete(false);
381+
void failDirectory() => _directories.removeAt(0).complete(false);
293382

294383
@override
295384
Future<List<Channel>> build() async => [
@@ -305,19 +394,24 @@ class _DirectoryChannels extends ChannelsNotifier {
305394
ref.read(relayConfigProvider).baseUrl,
306395
ref.read(myPubkeyProvider),
307396
);
308-
if (ref.read(channelDirectoryLoadStatusProvider).status ==
309-
ChannelDirectoryLoadStatus.loaded) {
397+
final directory = ref.read(channelDirectoryLoadStatusProvider);
398+
if (directory.scope == scope &&
399+
directory.status == ChannelDirectoryLoadStatus.loaded) {
310400
return;
311401
}
312402
directoryRequests++;
313-
status.markLoading(scope);
314-
if (!await (_directory = Completer<bool>()).future) {
403+
// Like production, the status turns loading only after memberships
404+
// arrive, so it stays as it was while the request is held.
405+
final load = Completer<bool>();
406+
_directories.add(load);
407+
if (!await load.future) {
315408
// Like production retryDirectory: record the error, do not throw.
316409
status.markError(scope);
317410
return;
318411
}
319-
state = AsyncData([...?state.value, _channel('4things', isMember: false)]);
412+
// Production marks the directory loaded before it installs the list.
320413
status.markLoaded(scope);
414+
state = AsyncData([...?state.value, _channel('4things', isMember: false)]);
321415
}
322416
}
323417

@@ -331,3 +425,24 @@ class _Profile extends ProfileNotifier {
331425
Future<UserProfile?> build() async =>
332426
const UserProfile(pubkey: 'test', displayName: 'Test');
333427
}
428+
429+
/// Real channel loading, starting with no memberships.
430+
class _NoMemberChannels extends ChannelsNotifier {
431+
@override
432+
Future<List<Channel>> build() async => [];
433+
}
434+
435+
/// Holds the membership query that starts a real directory refresh.
436+
class _MembershipHeldSession extends _HeldSession {
437+
final memberships = <Completer<List<NostrEvent>>>[];
438+
439+
@override
440+
Future<List<NostrEvent>> queryRelay(
441+
List<NostrFilter> filters, {
442+
Duration timeout = const Duration(seconds: 8),
443+
}) {
444+
final membership = Completer<List<NostrEvent>>();
445+
memberships.add(membership);
446+
return membership.future;
447+
}
448+
}

0 commit comments

Comments
 (0)