Merge branch 'krille/read-marker' into 'main'

fix: Set read marker only on user interaction

See merge request famedly/fluffychat!747
This commit is contained in:
Krille Fear 2022-02-17 20:39:04 +00:00
commit 6f7ea70a50
2 changed files with 291 additions and 278 deletions

View File

@ -147,6 +147,7 @@ class ChatController extends State<Chat> {
if (!mounted) { if (!mounted) {
return; return;
} }
setReadMarker();
if (scrollController.position.pixels == if (scrollController.position.pixels ==
scrollController.position.maxScrollExtent && scrollController.position.maxScrollExtent &&
timeline!.events.isNotEmpty && timeline!.events.isNotEmpty &&
@ -201,8 +202,8 @@ class ChatController extends State<Chat> {
if (timeline == null) { if (timeline == null) {
timeline = await room!.getTimeline(onUpdate: updateView); timeline = await room!.getTimeline(onUpdate: updateView);
if (timeline!.events.isNotEmpty) { if (timeline!.events.isNotEmpty) {
// ignore: unawaited_futures
if (room!.markedUnread) room!.markUnread(false); if (room!.markedUnread) room!.markUnread(false);
setReadMarker();
} }
// when the scroll controller is attached we want to scroll to an event id, if specified // when the scroll controller is attached we want to scroll to an event id, if specified
@ -220,15 +221,24 @@ class ChatController extends State<Chat> {
} }
filteredEvents = timeline!.getFilteredEvents(unfolded: unfolded); filteredEvents = timeline!.getFilteredEvents(unfolded: unfolded);
timeline!.requestKeys(); timeline!.requestKeys();
if ((room!.hasNewMessages || room!.notificationCount > 0) && return true;
}
Future<void>? _setReadMarkerFuture;
void setReadMarker([_]) {
if (_setReadMarkerFuture == null &&
(room!.hasNewMessages || room!.notificationCount > 0) &&
timeline != null && timeline != null &&
timeline!.events.isNotEmpty && timeline!.events.isNotEmpty &&
Matrix.of(context).webHasFocus) { Matrix.of(context).webHasFocus) {
Logs().v('Set read marker...');
// ignore: unawaited_futures // ignore: unawaited_futures
timeline!.setReadMarker(); _setReadMarkerFuture = timeline!.setReadMarker().then((_) {
_setReadMarkerFuture = null;
});
room!.client.updateIosBadge(); room!.client.updateIosBadge();
} }
return true;
} }
@override @override
@ -900,6 +910,7 @@ class ChatController extends State<Chat> {
} }
void onInputBarChanged(String text) { void onInputBarChanged(String text) {
setReadMarker();
if (text.endsWith(' ') && matrix!.hasComplexBundles) { if (text.endsWith(' ') && matrix!.hasComplexBundles) {
final clients = currentRoomBundle; final clients = currentRoomBundle;
for (final client in clients) { for (final client in clients) {

View File

@ -156,295 +156,297 @@ class ChatView extends StatelessWidget {
redirector.stopRedirection(); redirector.stopRedirection();
} }
}, },
child: StreamBuilder( child: GestureDetector(
stream: controller.room!.onUpdate.stream onTapDown: controller.setReadMarker,
.rateLimit(const Duration(milliseconds: 250)), child: StreamBuilder(
builder: (context, snapshot) => FutureBuilder<bool>( stream: controller.room!.onUpdate.stream
future: controller.getTimeline(), .rateLimit(const Duration(milliseconds: 250)),
builder: (BuildContext context, snapshot) { builder: (context, snapshot) => FutureBuilder<bool>(
return Scaffold( future: controller.getTimeline(),
appBar: AppBar( builder: (BuildContext context, snapshot) {
actionsIconTheme: IconThemeData( return Scaffold(
color: controller.selectedEvents.isEmpty appBar: AppBar(
? null actionsIconTheme: IconThemeData(
: Theme.of(context).colorScheme.primary, color: controller.selectedEvents.isEmpty
? null
: Theme.of(context).colorScheme.primary,
),
leading: controller.selectMode
? IconButton(
icon: const Icon(Icons.close),
onPressed: controller.clearSelectedEvents,
tooltip: L10n.of(context)!.close,
color: Theme.of(context).colorScheme.primary,
)
: UnreadBadgeBackButton(roomId: controller.roomId!),
titleSpacing: 0,
title: ChatAppBarTitle(controller),
actions: _appBarActions(context),
), ),
leading: controller.selectMode floatingActionButton: controller.showScrollDownButton &&
? IconButton( controller.selectedEvents.isEmpty
icon: const Icon(Icons.close), ? Padding(
onPressed: controller.clearSelectedEvents, padding: const EdgeInsets.only(bottom: 56.0),
tooltip: L10n.of(context)!.close, child: FloatingActionButton(
color: Theme.of(context).colorScheme.primary, onPressed: controller.scrollDown,
foregroundColor:
Theme.of(context).textTheme.bodyText2!.color,
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
mini: true,
child: Icon(Icons.arrow_downward_outlined,
color: Theme.of(context).primaryColor),
),
) )
: UnreadBadgeBackButton(roomId: controller.roomId!), : null,
titleSpacing: 0, backgroundColor: Theme.of(context).colorScheme.surface,
title: ChatAppBarTitle(controller), body: DropTarget(
actions: _appBarActions(context), onDragDone: controller.onDragDone,
), onDragEntered: controller.onDragEntered,
floatingActionButton: controller.showScrollDownButton && onDragExited: controller.onDragExited,
controller.selectedEvents.isEmpty child: Stack(
? Padding( children: <Widget>[
padding: const EdgeInsets.only(bottom: 56.0), if (Matrix.of(context).wallpaper != null)
child: FloatingActionButton( Image.file(
onPressed: controller.scrollDown, Matrix.of(context).wallpaper!,
foregroundColor: width: double.infinity,
Theme.of(context).textTheme.bodyText2!.color, height: double.infinity,
backgroundColor: fit: BoxFit.cover,
Theme.of(context).scaffoldBackgroundColor, ),
mini: true, SafeArea(
child: Icon(Icons.arrow_downward_outlined, child: Column(
color: Theme.of(context).primaryColor), children: <Widget>[
), TombstoneDisplay(controller),
) PinnedEvents(controller),
: null, Expanded(
backgroundColor: Theme.of(context).colorScheme.surface, child: GestureDetector(
body: DropTarget( onTap: controller.clearSingleSelectedEvent,
onDragDone: controller.onDragDone, child: Builder(
onDragEntered: controller.onDragEntered, builder: (context) {
onDragExited: controller.onDragExited, if (snapshot.hasError) {
child: Stack( SentryController.captureException(
children: <Widget>[ snapshot.error,
if (Matrix.of(context).wallpaper != null) StackTrace.current,
Image.file( );
Matrix.of(context).wallpaper!, }
width: double.infinity, if (controller.timeline == null) {
height: double.infinity, return const Center(
fit: BoxFit.cover, child: CircularProgressIndicator
), .adaptive(strokeWidth: 2),
SafeArea( );
child: Column( }
children: <Widget>[
TombstoneDisplay(controller),
PinnedEvents(controller),
Expanded(
child: GestureDetector(
onTap: controller.clearSingleSelectedEvent,
child: Builder(
builder: (context) {
if (snapshot.hasError) {
SentryController.captureException(
snapshot.error,
StackTrace.current,
);
}
if (controller.timeline == null) {
return const Center(
child:
CircularProgressIndicator.adaptive(
strokeWidth: 2),
);
}
// create a map of eventId --> index to greatly improve performance of // create a map of eventId --> index to greatly improve performance of
// ListView's findChildIndexCallback // ListView's findChildIndexCallback
final thisEventsKeyMap = <String, int>{}; final thisEventsKeyMap = <String, int>{};
for (var i = 0; for (var i = 0;
i < controller.filteredEvents.length; i < controller.filteredEvents.length;
i++) { i++) {
thisEventsKeyMap[controller thisEventsKeyMap[controller
.filteredEvents[i].eventId] = i; .filteredEvents[i].eventId] = i;
} }
return ListView.custom( return ListView.custom(
padding: EdgeInsets.only( padding: EdgeInsets.only(
top: 16, top: 16,
bottom: 4, bottom: 4,
left: horizontalPadding, left: horizontalPadding,
right: horizontalPadding, right: horizontalPadding,
), ),
reverse: true, reverse: true,
controller: controller.scrollController, controller: controller.scrollController,
keyboardDismissBehavior: PlatformInfos keyboardDismissBehavior: PlatformInfos
.isIOS .isIOS
? ScrollViewKeyboardDismissBehavior ? ScrollViewKeyboardDismissBehavior
.onDrag .onDrag
: ScrollViewKeyboardDismissBehavior : ScrollViewKeyboardDismissBehavior
.manual, .manual,
childrenDelegate: childrenDelegate:
SliverChildBuilderDelegate( SliverChildBuilderDelegate(
(BuildContext context, int i) { (BuildContext context, int i) {
return i == return i ==
controller.filteredEvents controller.filteredEvents
.length + .length +
1 1
? controller.timeline! ? controller.timeline!
.isRequestingHistory .isRequestingHistory
? const Center( ? const Center(
child: child:
CircularProgressIndicator CircularProgressIndicator
.adaptive( .adaptive(
strokeWidth: strokeWidth:
2), 2),
) )
: controller.canLoadMore : controller.canLoadMore
? Center( ? Center(
child: OutlinedButton( child:
style: OutlinedButton(
OutlinedButton style:
.styleFrom( OutlinedButton
backgroundColor: .styleFrom(
Theme.of( backgroundColor:
context) Theme.of(
.scaffoldBackgroundColor, context)
.scaffoldBackgroundColor,
),
onPressed: controller
.requestHistory,
child: Text(L10n.of(
context)!
.loadMore),
), ),
onPressed: controller )
.requestHistory, : Container()
child: Text(L10n.of( : i == 0
context)! ? Column(
.loadMore), mainAxisSize:
), MainAxisSize.min,
) children: [
: Container() SeenByRow(controller),
: i == 0 TypingIndicators(
? Column( controller),
mainAxisSize: ],
MainAxisSize.min, )
children: [ : AutoScrollTag(
SeenByRow(controller),
TypingIndicators(
controller),
],
)
: AutoScrollTag(
key: ValueKey(controller
.filteredEvents[i - 1]
.eventId),
index: i - 1,
controller: controller
.scrollController,
child: Swipeable(
key: ValueKey(controller key: ValueKey(controller
.filteredEvents[ .filteredEvents[
i - 1] i - 1]
.eventId), .eventId),
background: index: i - 1,
const Padding( controller: controller
padding: EdgeInsets .scrollController,
.symmetric( child: Swipeable(
horizontal: key: ValueKey(controller
12.0), .filteredEvents[
child: Center( i - 1]
child: Icon(Icons .eventId),
.reply_outlined), background:
const Padding(
padding: EdgeInsets
.symmetric(
horizontal:
12.0),
child: Center(
child: Icon(Icons
.reply_outlined),
),
), ),
), direction:
direction: SwipeDirection
SwipeDirection .endToStart,
.endToStart, onSwipe: (direction) =>
onSwipe: (direction) => controller.replyAction(
controller.replyAction( replyTo: controller
replyTo: controller .filteredEvents[
.filteredEvents[ i - 1]),
i - 1]), child: Message(
child: Message( controller.filteredEvents[
controller.filteredEvents[ i - 1],
i - 1], onInfoTab: controller
onInfoTab: controller .showEventInfo,
.showEventInfo, onAvatarTab: (Event event) =>
onAvatarTab: showModalBottomSheet(
(Event event) => context:
showModalBottomSheet( context,
context: builder: (c) =>
UserBottomSheet(
user: event
.sender,
outerContext:
context, context,
builder: onMention: () => controller
(c) => .sendController
UserBottomSheet( .text += '${event.sender.mention} ',
user: event
.sender,
outerContext:
context,
onMention: () => controller
.sendController
.text += '${event.sender.mention} ',
),
), ),
unfold: controller ),
.unfold, unfold: controller
onSelect: controller .unfold,
.onSelectMessage, onSelect: controller
scrollToEventId: .onSelectMessage,
(String eventId) => scrollToEventId:
controller.scrollToEventId( (String eventId) =>
eventId), controller.scrollToEventId(
longPressSelect: eventId),
controller longPressSelect:
.selectedEvents controller
.isEmpty, .selectedEvents
selected: controller .isEmpty,
.selectedEvents selected: controller
.any((e) => e.eventId == controller.filteredEvents[i - 1].eventId), .selectedEvents
timeline: controller.timeline!, .any((e) => e.eventId == controller.filteredEvents[i - 1].eventId),
nextEvent: i < controller.filteredEvents.length ? controller.filteredEvents[i] : null), timeline: controller.timeline!,
), nextEvent: i < controller.filteredEvents.length ? controller.filteredEvents[i] : null),
); ),
}, );
childCount: },
controller.filteredEvents.length + childCount:
2, controller.filteredEvents.length +
findChildIndexCallback: (key) => 2,
controller.findChildIndexCallback( findChildIndexCallback: (key) =>
key, thisEventsKeyMap), controller.findChildIndexCallback(
), key, thisEventsKeyMap),
); ),
}, );
)), },
), )),
if (controller.room!.canSendDefaultMessages &&
controller.room!.membership == Membership.join)
Container(
margin: EdgeInsets.only(
bottom: bottomSheetPadding,
left: bottomSheetPadding,
right: bottomSheetPadding,
),
constraints: const BoxConstraints(
maxWidth: FluffyThemes.columnWidth * 2.5),
alignment: Alignment.center,
child: Material(
borderRadius: const BorderRadius.only(
bottomLeft:
Radius.circular(AppConfig.borderRadius),
bottomRight:
Radius.circular(AppConfig.borderRadius),
),
elevation: 6,
shadowColor: Theme.of(context)
.secondaryHeaderColor
.withAlpha(100),
clipBehavior: Clip.hardEdge,
color: Theme.of(context)
.appBarTheme
.backgroundColor,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const ConnectionStatusHeader(),
ReactionsPicker(controller),
ReplyDisplay(controller),
ChatInputRow(controller),
ChatEmojiPicker(controller),
],
),
),
), ),
], if (controller.room!.canSendDefaultMessages &&
), controller.room!.membership == Membership.join)
), Container(
if (controller.dragging) margin: EdgeInsets.only(
Container( bottom: bottomSheetPadding,
color: Theme.of(context) left: bottomSheetPadding,
.scaffoldBackgroundColor right: bottomSheetPadding,
.withOpacity(0.9), ),
alignment: Alignment.center, constraints: const BoxConstraints(
child: const Icon( maxWidth: FluffyThemes.columnWidth * 2.5),
Icons.upload_outlined, alignment: Alignment.center,
size: 100, child: Material(
borderRadius: const BorderRadius.only(
bottomLeft:
Radius.circular(AppConfig.borderRadius),
bottomRight:
Radius.circular(AppConfig.borderRadius),
),
elevation: 6,
shadowColor: Theme.of(context)
.secondaryHeaderColor
.withAlpha(100),
clipBehavior: Clip.hardEdge,
color: Theme.of(context)
.appBarTheme
.backgroundColor,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const ConnectionStatusHeader(),
ReactionsPicker(controller),
ReplyDisplay(controller),
ChatInputRow(controller),
ChatEmojiPicker(controller),
],
),
),
),
],
), ),
), ),
], if (controller.dragging)
Container(
color: Theme.of(context)
.scaffoldBackgroundColor
.withOpacity(0.9),
alignment: Alignment.center,
child: const Icon(
Icons.upload_outlined,
size: 100,
),
),
],
),
), ),
), );
); },
}, ),
), ),
), ),
); );