Files
meshcore-open/lib/helpers/chat_jump.dart
T
zjs81 5ea44d6ce9 Tester feedback: routing, bulk select, notifications, map crash
Routing
- Auto routing picks routes by Thompson sampling over faded delivery
  stats (24 h half-life, capped evidence), weighted by what the radio
  currently hears; retries prefer a different route; last attempt floods.
- Route failures are held until a message's fate is known, so an offline
  contact no longer wrecks its route history; failing routes are only
  deleted after a week without a delivery.
- Direct (zero-hop) path is a rotation candidate (#643) and a new Direct
  mode in the routing sheet; history routes are used for repeater logins.
- Remove the route-weight sliders the new chooser no longer uses.

Chats
- Bulk selection in channel chats (spam / not spam / delete / copy) and
  in the Filtered messages list.
- Tapping a search result jumps to the message.
- Swipe-to-reply ignores vertical scrolls.
- Active conversation follows route lifecycle, so covered chats count
  unread again.

Notifications
- Tapping a notification opens its conversation.
- Per-conversation coalescing and one alert per queued-message sync.
- Monochrome status-bar icon; background service stops when the app is
  swiped away; stale notifications cleared on radio or slot change.

Other
- Online map: opaque links and painted dots for dense areas to avoid an
  Impeller GPU crash; expandable hop list on path maps.
- Contact share: clipboard import of shared contacts, "Add again".
- Android: enableEdgeToEdge and scrolling-screenshot support.
- Translations for all new strings; fix mistranslated routing labels.
2026-11-08 23:19:15 -07:00

128 lines
4.6 KiB
Dart

import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
/// Scrolls a lazily built chat list until the row at [targetIndex] is built,
/// then animates it to the middle of the viewport. Returns whether it got
/// there.
///
/// A `ListView.builder` only builds rows near the viewport and only estimates
/// the extent of the rest, so a target far away has no widget yet. This jumps
/// to a proportional estimate, then reads which rows the list actually built
/// and steps toward the target until it is among them. Works for reversed
/// chat lists too: it only compares list indices, never pixel directions.
Future<bool> scrollListToIndex({
required ScrollController controller,
required int targetIndex,
required int itemCount,
required bool Function() isMounted,
}) async {
if (!controller.hasClients || itemCount <= 0) return false;
if (targetIndex < 0 || targetIndex >= itemCount) return false;
// First guess: the target's share of the estimated extent.
final start = controller.position;
final guess = start.maxScrollExtent * ((targetIndex + 0.5) / itemCount);
controller.jumpTo(guess.clamp(start.minScrollExtent, start.maxScrollExtent));
var pinnedFrames = 0;
for (var attempt = 0; attempt < 120; attempt++) {
await WidgetsBinding.instance.endOfFrame;
if (!isMounted() || !controller.hasClients) return false;
final position = controller.position;
final sliver = _findListSliver(position);
if (sliver == null) return false;
final child = _childAt(sliver, targetIndex);
if (child != null) {
final viewport = RenderAbstractViewport.maybeOf(child);
if (viewport == null) return false;
final reveal = viewport.getOffsetToReveal(child, 0.5).offset;
await controller.animateTo(
reveal.clamp(position.minScrollExtent, position.maxScrollExtent),
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
return true;
}
final first = sliver.firstChild;
final last = sliver.lastChild;
if (first == null || last == null) return false;
final lowest = sliver.indexOf(first);
final highest = sliver.indexOf(last);
// Step by the rows still to go times the measured height of the rows
// built around here, so the walk converges even when distant rows are
// much taller or shorter than the ones the extent estimate was based on.
// Larger indices sit further along the scroll axis.
final rowsToGo = targetIndex > highest
? targetIndex - highest
: targetIndex < lowest
? targetIndex - lowest
: 0;
final step = rowsToGo == 0
? 0.0
: rowsToGo * _averageExtent(sliver, position.axis) +
rowsToGo.sign * position.viewportDimension * 0.5;
final next = position.pixels + step;
final clamped = next.clamp(
position.minScrollExtent,
position.maxScrollExtent,
);
if (clamped == position.pixels) {
// Pinned at an end with the target still beyond it. The extent is an
// estimate that grows as rows get built, so only give up once it has
// stopped growing for a few frames. Otherwise wait a frame for layout.
if (next != clamped && ++pinnedFrames >= 3) return false;
continue;
}
pinnedFrames = 0;
controller.jumpTo(clamped);
}
return false;
}
/// Mean main-axis size of the rows [sliver] has built.
double _averageExtent(RenderSliverMultiBoxAdaptor sliver, Axis axis) {
var total = 0.0;
var count = 0;
var child = sliver.firstChild;
while (child != null) {
if (child.hasSize) {
total += axis == Axis.vertical ? child.size.height : child.size.width;
count++;
}
child = sliver.childAfter(child);
}
return count == 0 ? 50 : total / count;
}
RenderBox? _childAt(RenderSliverMultiBoxAdaptor sliver, int index) {
var child = sliver.firstChild;
while (child != null) {
if (sliver.indexOf(child) == index) return child;
child = sliver.childAfter(child);
}
return null;
}
/// The list sliver inside the scrollable that [position] belongs to.
RenderSliverMultiBoxAdaptor? _findListSliver(ScrollPosition position) {
final context = position.context.storageContext;
if (context is! Element) return null;
RenderSliverMultiBoxAdaptor? found;
void visit(Element element) {
if (found != null) return;
final renderObject = element is RenderObjectElement
? element.renderObject
: null;
if (renderObject is RenderSliverMultiBoxAdaptor) {
found = renderObject;
return;
}
element.visitChildren(visit);
}
context.visitChildren(visit);
return found;
}