fix(market): zone prompt before connection error; keep Save above the nav bar

- A new user's first step (set your area) works offline, so that empty state
  now wins over 'can't reach the servers'
- The sharing-setup sheet wraps in SafeArea(top: false) so the Save button
  isn't clipped by the system navigation bar on edge-to-edge devices
This commit is contained in:
vjrj 2026-07-22 13:03:25 +02:00
parent 49e9a45a7d
commit 3a87e112f9
2 changed files with 318 additions and 214 deletions

View file

@ -100,11 +100,11 @@ class _MarketScreenState extends State<MarketScreen> {
Future<void> _init() async {
// Only needed to flush the outbox; read here (while mounted) to avoid using
// context across the awaits below. Null when there's no outbox (e.g. tests).
final repo =
widget.outbox != null ? context.read<VarietyRepository>() : null;
final repo = widget.outbox != null
? context.read<VarietyRepository>()
: null;
setState(() => _loading = true);
final cubit =
await createOffersCubit(widget.connection, repository: repo);
final cubit = await createOffersCubit(widget.connection, repository: repo);
cubit.setBlockedAuthors(await widget.settings.blockedPubkeys());
cubit.setHiddenOffers(await widget.settings.hiddenOfferKeys());
final area = await widget.settings.areaGeohash();
@ -208,12 +208,18 @@ class _MarketScreenState extends State<MarketScreen> {
await widget.outbox?.remove(ids); // published now, so unpark any duplicates
// count == 0 with lots to share means every relay refused say so plainly
// rather than "shared 0", which reads like success.
messenger.showSnackBar(SnackBar(
content: Text(count == 0 ? t.market.shareFailed : t.market.sharedCount(n: count)),
));
messenger.showSnackBar(
SnackBar(
content: Text(
count == 0 ? t.market.shareFailed : t.market.sharedCount(n: count),
),
),
);
final precision = await widget.settings.searchPrecision();
if (!mounted) return;
await cubit.discover(searchPrefix(area, precision)); // refresh incl. just-shared
await cubit.discover(
searchPrefix(area, precision),
); // refresh incl. just-shared
}
@override
@ -300,6 +306,17 @@ class MarketBody extends StatelessWidget {
final t = context.t;
return BlocBuilder<OffersCubit, OffersState>(
builder: (context, state) {
// A new user's first step is setting a zone — which works offline — so
// that prompt wins over the connection error.
if (!hasArea) {
return _EmptyState(
icon: Icons.place_outlined,
title: t.market.setArea,
body: t.market.setAreaBody,
actionLabel: t.market.setUp,
onAction: onConfigure,
);
}
if (!context.read<OffersCubit>().isOnline) {
// Default community servers exist, so "offline" means unreachable
// offer a retry rather than "set up sharing".
@ -311,15 +328,6 @@ class MarketBody extends StatelessWidget {
onAction: onRetry,
);
}
if (!hasArea) {
return _EmptyState(
icon: Icons.place_outlined,
title: t.market.setArea,
body: t.market.setAreaBody,
actionLabel: t.market.setUp,
onAction: onConfigure,
);
}
if (state.searching && state.offers.isEmpty) {
return Center(
child: Column(
@ -327,8 +335,10 @@ class MarketBody extends StatelessWidget {
children: [
const CircularProgressIndicator(),
const SizedBox(height: 16),
Text(t.market.searching,
style: const TextStyle(color: seedMuted)),
Text(
t.market.searching,
style: const TextStyle(color: seedMuted),
),
],
),
);
@ -372,16 +382,22 @@ class MarketBody extends StatelessWidget {
prefixIcon: const Icon(Icons.search, color: seedMuted),
// Offer to save the current search once it's worth alerting on
// (some text typed or a chip active), never for the bare list.
suffixIcon: savedSearches != null &&
suffixIcon:
savedSearches != null &&
(state.query.trim().isNotEmpty ||
state.hasActiveFilter)
? IconButton(
key: const Key('market.saveSearch'),
icon: const Icon(Icons.bookmark_add_outlined,
color: seedGreen),
icon: const Icon(
Icons.bookmark_add_outlined,
color: seedGreen,
),
tooltip: t.savedSearches.save,
onPressed: () =>
_saveCurrentSearch(context, savedSearches!, state),
onPressed: () => _saveCurrentSearch(
context,
savedSearches!,
state,
),
)
: null,
hintStyle: const TextStyle(color: seedMuted),
@ -433,13 +449,17 @@ class MarketBody extends StatelessWidget {
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(16),
// A trailing spinner row while the next page loads.
itemCount: visible.length + (state.loadingMore ? 1 : 0),
separatorBuilder: (_, _) => const SizedBox(height: 12),
itemCount:
visible.length + (state.loadingMore ? 1 : 0),
separatorBuilder: (_, _) =>
const SizedBox(height: 12),
itemBuilder: (context, i) {
if (i >= visible.length) {
return const Padding(
padding: EdgeInsets.symmetric(vertical: 16),
child: Center(child: CircularProgressIndicator()),
child: Center(
child: CircularProgressIndicator(),
),
);
}
final o = visible[i];
@ -576,7 +596,9 @@ class _OfferCard extends StatelessWidget {
if (mine) ...[
Container(
padding: const EdgeInsets.symmetric(
horizontal: 8, vertical: 3),
horizontal: 8,
vertical: 3,
),
decoration: BoxDecoration(
color: seedGreen,
borderRadius: BorderRadius.circular(20),
@ -600,8 +622,10 @@ class _OfferCard extends StatelessWidget {
children: [
const Icon(Icons.place_outlined, size: 15, color: seedMuted),
const SizedBox(width: 4),
Text(t.market.near,
style: const TextStyle(color: seedMuted, fontSize: 13)),
Text(
t.market.near,
style: const TextStyle(color: seedMuted, fontSize: 13),
),
if (offer.isOrganic) ...[
const SizedBox(width: 10),
const Icon(Icons.eco, size: 15, color: seedGreen),
@ -609,7 +633,8 @@ class _OfferCard extends StatelessWidget {
const Spacer(),
if (offer.type == OfferType.sale && offer.priceAmount != null)
Text(
'${offer.priceAmount} ${offer.priceCurrency ?? ''}'.trim(),
'${offer.priceAmount} ${offer.priceCurrency ?? ''}'
.trim(),
style: const TextStyle(
color: seedOnSurface,
fontWeight: FontWeight.w600,
@ -908,7 +933,11 @@ class _ConfigSheetState extends State<_ConfigSheet> {
Widget build(BuildContext context) {
final t = context.t;
final hasArea = _area.text.trim().isNotEmpty;
return Padding(
// SafeArea keeps the Save button above the system nav bar on edge-to-edge
// devices; the viewInsets padding still lifts it above the keyboard.
return SafeArea(
top: false,
child: Padding(
padding: EdgeInsets.only(
left: 20,
right: 20,
@ -917,7 +946,9 @@ class _ConfigSheetState extends State<_ConfigSheet> {
),
child: _loading
? const SizedBox(
height: 120, child: Center(child: CircularProgressIndicator()))
height: 120,
child: Center(child: CircularProgressIndicator()),
)
: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
@ -935,7 +966,10 @@ class _ConfigSheetState extends State<_ConfigSheet> {
Text(
t.market.setupIntro,
style: const TextStyle(
color: seedMuted, fontSize: 13, height: 1.4),
color: seedMuted,
fontSize: 13,
height: 1.4,
),
),
const SizedBox(height: 18),
// Setting your area from device location is the human path;
@ -948,7 +982,9 @@ class _ConfigSheetState extends State<_ConfigSheet> {
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
child: CircularProgressIndicator(
strokeWidth: 2,
),
)
: const Icon(Icons.my_location, size: 18),
label: Text(t.market.useLocation),
@ -959,16 +995,16 @@ class _ConfigSheetState extends State<_ConfigSheet> {
child: Text(
_locationError!,
style: const TextStyle(
color: Color(0xFFB3261E), fontSize: 12),
color: Color(0xFFB3261E),
fontSize: 12,
),
),
),
const SizedBox(height: 12),
Row(
children: [
Icon(
hasArea
? Icons.check_circle
: Icons.pending_outlined,
hasArea ? Icons.check_circle : Icons.pending_outlined,
size: 18,
color: hasArea ? seedGreen : seedMuted,
),
@ -996,9 +1032,18 @@ class _ConfigSheetState extends State<_ConfigSheet> {
SegmentedButton<int>(
key: const Key('market.range'),
segments: [
ButtonSegment(value: 5, label: Text(t.market.rangeNear)),
ButtonSegment(value: 4, label: Text(t.market.rangeArea)),
ButtonSegment(value: 3, label: Text(t.market.rangeRegion)),
ButtonSegment(
value: 5,
label: Text(t.market.rangeNear),
),
ButtonSegment(
value: 4,
label: Text(t.market.rangeArea),
),
ButtonSegment(
value: 3,
label: Text(t.market.rangeRegion),
),
],
selected: {_precision},
showSelectedIcon: false,
@ -1007,15 +1052,19 @@ class _ConfigSheetState extends State<_ConfigSheet> {
),
const SizedBox(height: 8),
Theme(
data: Theme.of(context)
.copyWith(dividerColor: Colors.transparent),
data: Theme.of(
context,
).copyWith(dividerColor: Colors.transparent),
child: ExpansionTile(
key: const Key('market.advanced'),
tilePadding: EdgeInsets.zero,
childrenPadding: const EdgeInsets.only(bottom: 8),
title: Text(
t.market.advanced,
style: const TextStyle(fontSize: 13, color: seedMuted),
style: const TextStyle(
fontSize: 13,
color: seedMuted,
),
),
children: [
TextField(
@ -1034,16 +1083,23 @@ class _ConfigSheetState extends State<_ConfigSheet> {
child: Text(
t.market.serversLabel,
style: const TextStyle(
fontSize: 13, color: seedMuted),
fontSize: 13,
color: seedMuted,
),
),
),
Padding(
padding: const EdgeInsetsDirectional.only(
top: 2, bottom: 4),
top: 2,
bottom: 4,
),
child: Text(
t.market.serversHelp,
style: const TextStyle(
fontSize: 12, color: seedMuted, height: 1.3),
fontSize: 12,
color: seedMuted,
height: 1.3,
),
),
),
for (final url in _knownRelays)
@ -1096,6 +1152,7 @@ class _ConfigSheetState extends State<_ConfigSheet> {
],
),
),
),
);
}
}

View file

@ -84,8 +84,9 @@ void main() {
findsOneWidget);
});
testWidgets('MarketScreen with no relays configured degrades to offline',
(tester) async {
testWidgets(
'a fresh install (no area, unreachable) asks for the area first, '
'not the connection', (tester) async {
final social = await SocialService.fromRootSeedHex('00' * 32);
final settings = SocialSettings(InMemorySecretStore());
await settings.setRelayUrls(const []); // offline: don't hit the network
@ -94,7 +95,23 @@ void main() {
await tester.pumpAndSettle();
expect(find.text('Seeds near you'), findsOneWidget); // app bar title
expect(find.text('Retry'), findsOneWidget); // can't-reach state offers retry
// Setting a zone works offline and is the first step the connection
// error must not bury it.
expect(find.text('Set your area'), findsOneWidget);
expect(find.text('Retry'), findsNothing);
});
testWidgets('with an area set, unreachable servers degrade to retry',
(tester) async {
final social = await SocialService.fromRootSeedHex('00' * 32);
final settings = SocialSettings(InMemorySecretStore());
await settings.setRelayUrls(const []); // offline: don't hit the network
await settings.setAreaGeohash('ezsn9');
await tester.pumpWidget(_wrapMarket(social, settings));
await tester.pumpAndSettle();
expect(find.text('Retry'), findsOneWidget);
});
testWidgets('the range selector persists the chosen search precision',
@ -148,6 +165,36 @@ void main() {
expect(tester.widget<CheckboxListTile>(find.byKey(firstKey)).value, isTrue);
});
testWidgets('the config sheet Save button stays above the system nav bar',
(tester) async {
// Edge-to-edge Android: a 50-logical-px gesture/nav bar at the bottom.
tester.view.padding = const FakeViewPadding(bottom: 150); // physical px
tester.view.viewPadding = const FakeViewPadding(bottom: 150);
addTearDown(tester.view.reset);
final social = await SocialService.fromRootSeedHex('00' * 32);
final settings = SocialSettings(InMemorySecretStore());
await settings.setRelayUrls(const []); // offline: don't hit the network
await tester.pumpWidget(_wrapMarket(social, settings));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('market.config')));
await tester.pumpAndSettle();
final save = find.byKey(const Key('market.save'));
await tester.ensureVisible(save);
await tester.pumpAndSettle();
final screenHeight = tester.view.physicalSize.height /
tester.view.devicePixelRatio; // 600 on the default test surface
const navBarLogical = 150 / 3.0; // FakeViewPadding is physical px
expect(
tester.getRect(save).bottom,
lessThanOrEqualTo(screenHeight - navBarLogical + 0.1),
reason: 'Save must not sit under the system navigation bar',
);
});
testWidgets('"use my location" fills the area with a coarse geohash',
(tester) async {
final social = await SocialService.fromRootSeedHex('00' * 32);