Keep chat reading position during sync, selection and app switches
- Room chats: keep the visible message in place when synced messages arrive, don't pull a scrolling reader back to the bottom, and keep loaded older pages while the chat is open. - Selecting messages no longer jumps to the newest message. - Focus returning after a sheet, dialog or app switch no longer counts as opening the keyboard.
This commit is contained in:
1 parent
34ea0cb7ca
commit
ffbb61bc3d
11 files changed
+1431
-10
No files matched your search
@@ -0,0 +1,92 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/helpers/chat_scroll_anchor.dart';
|
||||
|
||||
/// A reversed chat list of rows with uneven heights. [add] appends newer
|
||||
/// rows, which land at index 0 below everything else.
|
||||
class _Harness {
|
||||
final controller = ScrollController();
|
||||
final anchor = ChatScrollAnchor();
|
||||
var items = List.generate(100, (i) => 'm$i');
|
||||
late StateSetter _setState;
|
||||
|
||||
Widget build() => MaterialApp(
|
||||
home: Scaffold(
|
||||
body: StatefulBuilder(
|
||||
builder: (context, setState) {
|
||||
_setState = setState;
|
||||
final rows = items.reversed.toList();
|
||||
return AnchoredChatListView.builder(
|
||||
scrollAnchor: anchor,
|
||||
reverse: true,
|
||||
controller: controller,
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: rows.length,
|
||||
itemBuilder: (context, i) => SizedBox(
|
||||
height: 40.0 + (int.parse(rows[i].substring(1)) % 5) * 20,
|
||||
child: Text(rows[i]),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
void add(int count, {bool anchored = true}) {
|
||||
_setState(() {
|
||||
final start = items.length;
|
||||
items = [...items, for (var i = 0; i < count; i++) 'n${start + i}'];
|
||||
if (anchored) anchor.rowsInsertedBelow(count);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final count in [1, 3, 30, 300]) {
|
||||
testWidgets('$count rows added below keep the visible rows in place', (
|
||||
tester,
|
||||
) async {
|
||||
final harness = _Harness();
|
||||
addTearDown(harness.controller.dispose);
|
||||
await tester.pumpWidget(harness.build());
|
||||
harness.controller.jumpTo(2000);
|
||||
await tester.pump();
|
||||
final row = find.text('m72');
|
||||
expect(row, findsOneWidget);
|
||||
final before = tester.getTopLeft(row).dy;
|
||||
|
||||
harness.add(count);
|
||||
await tester.pump();
|
||||
|
||||
expect(row, findsOneWidget);
|
||||
expect(tester.getTopLeft(row).dy, moreOrLessEquals(before));
|
||||
expect(harness.controller.position.pixels, greaterThan(2000));
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('without the anchor the rows on screen move', (tester) async {
|
||||
final harness = _Harness();
|
||||
addTearDown(harness.controller.dispose);
|
||||
await tester.pumpWidget(harness.build());
|
||||
harness.controller.jumpTo(2000);
|
||||
await tester.pump();
|
||||
final before = tester.getTopLeft(find.text('m72')).dy;
|
||||
|
||||
harness.add(3, anchored: false);
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.getTopLeft(find.text('m72')).dy, isNot(before));
|
||||
});
|
||||
|
||||
testWidgets('a list at the bottom is left at the bottom', (tester) async {
|
||||
final harness = _Harness();
|
||||
addTearDown(harness.controller.dispose);
|
||||
await tester.pumpWidget(harness.build());
|
||||
|
||||
harness.add(3, anchored: false);
|
||||
await tester.pump();
|
||||
|
||||
expect(harness.controller.position.pixels, 0);
|
||||
expect(find.text('n102'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/helpers/composer_focus_intent.dart';
|
||||
|
||||
void _setLifecycle(WidgetTester tester, List<AppLifecycleState> states) {
|
||||
for (final state in states) {
|
||||
tester.binding.handleAppLifecycleStateChanged(state);
|
||||
}
|
||||
}
|
||||
|
||||
const _away = [
|
||||
AppLifecycleState.inactive,
|
||||
AppLifecycleState.hidden,
|
||||
AppLifecycleState.paused,
|
||||
];
|
||||
const _back = [
|
||||
AppLifecycleState.hidden,
|
||||
AppLifecycleState.inactive,
|
||||
AppLifecycleState.resumed,
|
||||
];
|
||||
|
||||
void main() {
|
||||
testWidgets('focus gained while in the foreground is user focus', (
|
||||
tester,
|
||||
) async {
|
||||
final intent = ComposerFocusIntent();
|
||||
addTearDown(intent.dispose);
|
||||
|
||||
expect(intent.isUserFocusGain(true), isTrue);
|
||||
expect(intent.isUserFocusGain(false), isFalse);
|
||||
expect(intent.isUserFocusGain(true), isTrue);
|
||||
});
|
||||
|
||||
testWidgets('focus lost while away and given back on resume is not', (
|
||||
tester,
|
||||
) async {
|
||||
final intent = ComposerFocusIntent();
|
||||
addTearDown(intent.dispose);
|
||||
|
||||
expect(intent.isUserFocusGain(true), isTrue);
|
||||
_setLifecycle(tester, _away.sublist(0, 1));
|
||||
expect(intent.isUserFocusGain(false), isFalse);
|
||||
_setLifecycle(tester, [..._away.sublist(1), ..._back]);
|
||||
// The restore lands before the resume microtask clears the flag.
|
||||
expect(intent.isUserFocusGain(true), isFalse);
|
||||
// The next tap counts again.
|
||||
expect(intent.isUserFocusGain(false), isFalse);
|
||||
expect(intent.isUserFocusGain(true), isTrue);
|
||||
});
|
||||
|
||||
testWidgets('focus not restored on resume does not swallow the next tap', (
|
||||
tester,
|
||||
) async {
|
||||
final intent = ComposerFocusIntent();
|
||||
addTearDown(intent.dispose);
|
||||
|
||||
_setLifecycle(tester, _away.sublist(0, 1));
|
||||
expect(intent.isUserFocusGain(false), isFalse);
|
||||
_setLifecycle(tester, [..._away.sublist(1), ..._back]);
|
||||
await tester.pump(); // Resume microtasks run; nothing restored focus.
|
||||
|
||||
expect(intent.isUserFocusGain(true), isTrue);
|
||||
});
|
||||
|
||||
testWidgets('focus given back when a sheet or dialog closes is not', (
|
||||
tester,
|
||||
) async {
|
||||
final intent = ComposerFocusIntent();
|
||||
addTearDown(intent.dispose);
|
||||
|
||||
expect(intent.isUserFocusGain(true), isTrue);
|
||||
expect(intent.isUserFocusGain(false, coveredByRoute: true), isFalse);
|
||||
expect(intent.isUserFocusGain(true), isFalse);
|
||||
// Losing focus with nothing on top, then tapping, counts again.
|
||||
expect(intent.isUserFocusGain(false), isFalse);
|
||||
expect(intent.isUserFocusGain(true), isTrue);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user