Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(mobile): faster machine poll when all asleep, refresh button

tonye332bc5a3dc1
diff --git a/mobile/lib/phone/machines_tab.dart b/mobile/lib/phone/machines_tab.dart
index 8541949ae..d58752041 100644
--- a/mobile/lib/phone/machines_tab.dart
+++ b/mobile/lib/phone/machines_tab.dart
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
import 'package:lucide_icons_flutter/lucide_icons.dart';
import 'package:harness_mobile/shared/theme/app_theme.dart';
@@ -72,6 +73,14 @@ class MachinesTab extends StatelessWidget {
),
),
),
+ // What a pull on the list does, for whoever does not know to pull — or has
+ // nothing to pull: the empty and failed states draw no list.
+ _RefreshButton(
+ busy:
+ notifier.machinesRefreshing || notifier.machinesLoading,
+ trailing: !large,
+ onPressed: () => unawaited(notifier.retryMachines()),
+ ),
// Large, this is the home screen while no computer is ready — locked, or off —
// and nothing else on it leads to Settings or Sign out. Pushed (small), it came
// FROM Settings, and back is the way there.
@@ -224,3 +233,112 @@ class _Body extends StatelessWidget {
void _open(BuildContext context, MachineState state) =>
openMachineActions(context, notifier, state.machine.machineId);
}
+
+/// The list read again — what a pull does ([AppNotifier.retryMachines]) — as a glyph beside
+/// Settings.
+///
+/// While a read the person asked for is running ([busy]) the glyph turns and a press does nothing;
+/// it keeps its ink rather than greying out, because it is working, not unavailable — the rail's
+/// reload says the same (`AppIconButton.spinning`). The deaf poll's own reads do not turn it: every
+/// few seconds, that would be a glyph that never stops.
+class _RefreshButton extends StatefulWidget {
+ const _RefreshButton({
+ required this.busy,
+ required this.trailing,
+ required this.onPressed,
+ });
+
+ final bool busy;
+
+ /// Last in its row, with no Settings after it (the list pushed from Settings): the glyph sits on
+ /// the right gutter, in Settings' own box. Otherwise it stands left of Settings, which keeps its
+ /// place on the gutter.
+ final bool trailing;
+
+ final VoidCallback onPressed;
+
+ @override
+ State<_RefreshButton> createState() => _RefreshButtonState();
+}
+
+class _RefreshButtonState extends State<_RefreshButton>
+ with SingleTickerProviderStateMixin {
+ /// One turn — the rail's reload's (`AppIconButton`).
+ static const Duration _spinPeriod = Duration(milliseconds: 900);
+
+ late final AnimationController _spin = AnimationController(
+ vsync: this,
+ duration: _spinPeriod,
+ );
+
+ @override
+ void initState() {
+ super.initState();
+ if (widget.busy) _spin.repeat();
+ }
+
+ @override
+ void didUpdateWidget(_RefreshButton oldWidget) {
+ super.didUpdateWidget(oldWidget);
+ if (widget.busy == oldWidget.busy) return;
+ if (widget.busy) {
+ _spin.repeat();
+ } else {
+ // The turn under way lands upright instead of freezing at whatever angle the reply arrived:
+ // a glyph stopped askew reads as stuck.
+ _spin
+ .animateTo(1, duration: _spinPeriod * (1 - _spin.value))
+ .whenComplete(() {
+ if (mounted && !widget.busy) _spin.value = 0;
+ });
+ }
+ }
+
+ @override
+ void dispose() {
+ _spin.dispose();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final tty = Tty.of(context);
+ return Semantics(
+ button: true,
+ enabled: !widget.busy,
+ label: 'Refresh',
+ excludeSemantics: true,
+ child: GestureDetector(
+ key: const ValueKey('machines-refresh'),
+ behavior: HitTestBehavior.opaque,
+ onTap: () {
+ if (widget.busy) return;
+ HapticFeedback.selectionClick();
+ widget.onPressed();
+ },
+ // A thumb's 44. Beside Settings the glyph keeps to the box's right edge: Settings' box
+ // already leaves room left of its own glyph, and centred here the two sat a button apart.
+ child: SizedBox(
+ width: widget.trailing ? 52 : 44,
+ height: 44,
+ child: Padding(
+ padding: EdgeInsets.only(
+ right: widget.trailing ? Tty.origin - 2 : 2,
+ ),
+ child: Align(
+ alignment: Alignment.centerRight,
+ child: RotationTransition(
+ turns: _spin,
+ child: Icon(
+ LucideIcons.refreshCw300,
+ size: 20,
+ color: tty.faint,
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dart
index fa06a7e1d..03f84347c 100644
--- a/mobile/lib/state/app_state.dart
+++ b/mobile/lib/state/app_state.dart
@@ -2368,6 +2368,8 @@ class AppNotifier extends ChangeNotifier {
// beats the cache parse it was dialled ahead of. See [_adoptConnection].
if (isNew) _adoptConnection(state);
}
+ // A machine come or gone changes how often the deaf poll reads — one asleep or several.
+ _retuneDeafPoll();
_autoConnectAndLoadMachines();
// The list that just landed is what the NEXT launch starts from. Never
// awaited: it is a hint for a future run and must not add a disk write to
@@ -6114,6 +6116,7 @@ class AppNotifier extends ChangeNotifier {
unawaited(_pool?.closeMachine(machineId));
machineStates.remove(machineId);
machines.removeWhere((m) => m.machineId == machineId);
+ _retuneDeafPoll();
expandedMachines.remove(machineId);
if (selectedMachineId == machineId) selectedMachineId = null;
if (pendingPairing?.machineId == machineId) pendingPairing = null;
@@ -6683,6 +6686,8 @@ class AppNotifier extends ChangeNotifier {
final machineId = machine.machine.machineId;
final wasOnline = machine.nodeOnline;
machine.nodeOnline = online;
+ // Asleep or awake decides how often a phone with nothing connected reads the list again.
+ if (wasOnline != online) _retuneDeafPoll();
if (!online) {
_markSessionsUnreachable(
@@ -8373,20 +8378,90 @@ class AppNotifier extends ChangeNotifier {
@visibleForTesting
Duration deafMachineListInterval = const Duration(seconds: 20);
+ /// The same read, when the account's ONLY machine is asleep. Switched on, it shows up here on its
+ /// own within this — the computer the person is standing at, waiting to see it come back.
+ @visibleForTesting
+ Duration asleepMachineListInterval = const Duration(seconds: 4);
+
+ /// The same read, when every one of SEVERAL machines is asleep.
+ @visibleForTesting
+ Duration asleepMachinesListInterval = const Duration(seconds: 8);
+
Timer? _deafMachineListTimer;
Future<void>? _machineReread;
+ /// The interval [_deafMachineListTimer] was set to, and the time since — so [_retuneDeafPoll] can
+ /// tell when that interval no longer fits, and how much of it has already gone.
+ Duration? _deafPollInterval;
+ final Stopwatch _deafPollSince = Stopwatch();
+
+ /// How long the deaf poll waits between two reads: [asleepMachineListInterval] when the account's
+ /// one machine is asleep, [asleepMachinesListInterval] when every one of several is, and
+ /// [deafMachineListInterval] otherwise — no machine at all, or one that is on but not connected
+ /// (still connecting, or waiting for its password).
+ ///
+ /// "Asleep" is what its row on the Computers screen says (`phoneMachineStatusOf`): the machine
+ /// reported off. A machine with no state yet counts as not asleep — the slower, safer interval.
+ Duration _deafPollIntervalNow() {
+ if (machines.isEmpty) return deafMachineListInterval;
+ for (final machine in machines) {
+ if (machineStates[machine.machineId]?.nodeOnline != false) {
+ return deafMachineListInterval;
+ }
+ }
+ return machines.length == 1
+ ? asleepMachineListInterval
+ : asleepMachinesListInterval;
+ }
+
void _startDeafPoll() {
if (_disposed || status != AppStatus.authenticated) return;
- _deafMachineListTimer ??= Timer.periodic(
- deafMachineListInterval,
- (_) => _rereadMachinesWhileDeaf(),
- );
+ if (_deafMachineListTimer != null) return;
+ final interval = _deafPollIntervalNow();
+ _setDeafPoll(interval, firstIn: interval);
+ }
+
+ /// One read [firstIn] from now, then one every interval the machines call for at the time of each
+ /// — see [_deafPollIntervalNow]. A timer set again after every read rather than a periodic one,
+ /// because the interval follows the machines.
+ void _setDeafPoll(Duration interval, {required Duration firstIn}) {
+ _deafMachineListTimer?.cancel();
+ _deafPollInterval = interval;
+ _deafPollSince
+ ..reset()
+ ..start();
+ late final Timer timer;
+ timer = Timer(firstIn < Duration.zero ? Duration.zero : firstIn, () {
+ _rereadMachinesWhileDeaf();
+ // Stopped meanwhile — paused, signed out, disposed — or set again already: nothing to do.
+ if (_disposed || !identical(_deafMachineListTimer, timer)) return;
+ final next = _deafPollIntervalNow();
+ _setDeafPoll(next, firstIn: next);
+ });
+ _deafMachineListTimer = timer;
+ }
+
+ /// The machines changed (one went to sleep or woke, or the list itself changed): a poll running at
+ /// an interval that no longer fits is set to the one that does, counting the wait already spent.
+ ///
+ /// ⚠️ Without this the new interval would only take over after the old one ran out: on launch the
+ /// poll can start before the machine list has landed — so at [deafMachineListInterval] — and a
+ /// computer asleep from the start would be read again only after those 20 seconds.
+ void _retuneDeafPoll() {
+ final current = _deafPollInterval;
+ if (_disposed || _deafMachineListTimer == null || current == null) return;
+ final interval = _deafPollIntervalNow();
+ if (interval == current) return;
+ _setDeafPoll(interval, firstIn: interval - _deafPollSince.elapsed);
}
void _stopDeafPoll() {
_deafMachineListTimer?.cancel();
_deafMachineListTimer = null;
+ _deafPollInterval = null;
+ _deafPollSince
+ ..stop()
+ ..reset();
}
/// A phone that hears nothing reads the machine list again: the computer the person is signing in on