From d4c2ffadf2b06d2318174aefebcb4276870ef0e2 Mon Sep 17 00:00:00 2001 From: sherlock Date: Tue, 12 Dec 2023 14:27:55 +0700 Subject: [PATCH] TW-898: update chat and pinned events to handle back from pinned screen --- lib/pages/chat/chat.dart | 18 +++++++++++---- .../pinned_events_controller.dart | 22 +++++++++++++++++-- .../pinned_events_style.dart | 2 ++ .../pinned_events_view.dart | 9 ++++++-- .../event_extension.dart | 9 ++++---- 5 files changed, 48 insertions(+), 12 deletions(-) diff --git a/lib/pages/chat/chat.dart b/lib/pages/chat/chat.dart index 37b786e91e..ba48bbe325 100644 --- a/lib/pages/chat/chat.dart +++ b/lib/pages/chat/chat.dart @@ -786,7 +786,7 @@ class ChatController extends State inputFocus.requestFocus(); } - void scrollToEventIdAndHighlight(String eventId) { + Future scrollToEventIdAndHighlight(String eventId) { return scrollToEventId(eventId, highlight: true); } @@ -868,8 +868,7 @@ class ChatController extends State } } - void scrollToEventId(String eventId, {bool highlight = false}) async { - if (timeline == null) return; + Future scrollToEventId(String eventId, {bool highlight = false}) async { final eventIndex = timeline!.events.indexWhere((e) => e.eventId == eventId); if (eventIndex == -1) { timeline = null; @@ -896,7 +895,7 @@ class ChatController extends State ); if (highlight) { await scrollController.highlight( - index, + index + 1, ); } setState(() {}); @@ -1466,6 +1465,17 @@ class ChatController extends State } } + void handlePopBackFromPinnedScreen(Object? popResult) async { + Logs().d( + "PinnedEventsController()::handlePopBack(): popResult: $popResult", + ); + if (popResult is Event) { + scrollToEventIdAndHighlight(popResult.eventId); + } else if (popResult is List) { + pinnedEventsController.handlePopBack(popResult); + } + } + @override Widget build(BuildContext context) { return ChatView(this, key: widget.key); diff --git a/lib/pages/chat/chat_pinned_events/pinned_events_controller.dart b/lib/pages/chat/chat_pinned_events/pinned_events_controller.dart index 83deda952e..bf5b07a6e6 100644 --- a/lib/pages/chat/chat_pinned_events/pinned_events_controller.dart +++ b/lib/pages/chat/chat_pinned_events/pinned_events_controller.dart @@ -38,8 +38,11 @@ class PinnedEventsController { String? eventId, }) async { await Future.delayed(_timeDelayGetPinnedMessage); - _pinnedEventsSubscription = - getPinnedMessageInteractor.execute(room: room).listen((event) { + _pinnedEventsSubscription = getPinnedMessageInteractor + .execute( + room: room, + ) + .listen((event) { getPinnedMessageNotifier.value = event; event.fold((_) => null, (success) { if (success is ChatGetPinnedEventsSuccess) { @@ -96,6 +99,21 @@ class PinnedEventsController { ); } + void handlePopBack(Object? popResult) { + Logs().d( + "PinnedEventsController()::handlePopBack(): popResult: $popResult", + ); + if (popResult is List) { + final room = popResult.first?.room; + if (room != null) { + getPinnedMessageAction( + room: room, + isInitial: true, + ); + } + } + } + void jumpToCurrentMessage( List pinnedEvents, { String? eventId, diff --git a/lib/pages/chat/chat_pinned_events/pinned_events_style.dart b/lib/pages/chat/chat_pinned_events/pinned_events_style.dart index b8d937558d..6a95e402e7 100644 --- a/lib/pages/chat/chat_pinned_events/pinned_events_style.dart +++ b/lib/pages/chat/chat_pinned_events/pinned_events_style.dart @@ -2,6 +2,8 @@ import 'package:flutter/material.dart'; class PinnedEventsStyle { static const double maxHeight = 48; + static const double maxWidthContextItem = 100; + static const double minWidthContextItem = 80; static const double minHeightIndicator = 12; static const double maxWidthIndicator = 2.0; static const double maxLengthIndicator = 5.0; diff --git a/lib/pages/chat/chat_pinned_events/pinned_events_view.dart b/lib/pages/chat/chat_pinned_events/pinned_events_view.dart index e9d2569420..4af83795eb 100644 --- a/lib/pages/chat/chat_pinned_events/pinned_events_view.dart +++ b/lib/pages/chat/chat_pinned_events/pinned_events_view.dart @@ -130,14 +130,19 @@ class PinnedEventsView extends StatelessWidget { margin: PinnedEventsStyle.marginPinIcon, paddingAll: PinnedEventsStyle.paddingPinIcon, - onTap: () { - context.pushChild( + onTap: () async { + final popResult = + await context.pushChild( 'pinnedmessages', extra: PinnedEventsArgument( pinnedEvents: data.pinnedEvents, timeline: controller.timeline, ), ); + controller + .handlePopBackFromPinnedScreen( + popResult, + ); }, ), ], diff --git a/lib/utils/matrix_sdk_extensions/event_extension.dart b/lib/utils/matrix_sdk_extensions/event_extension.dart index cde3d4315d..7242334d37 100644 --- a/lib/utils/matrix_sdk_extensions/event_extension.dart +++ b/lib/utils/matrix_sdk_extensions/event_extension.dart @@ -174,14 +174,15 @@ extension LocalizedBody on Event { } final pinnedEventIds = room.pinnedEventIds; pinnedEventIds.remove(eventId); - String? message; + String? responseMessage; try { - message = await room.setPinnedEvents(pinnedEventIds); + responseMessage = await room.setPinnedEvents(pinnedEventIds); } catch (e) { - Logs().e('$e'); + Logs().e('EventExtension::unpin(): $e'); return false; } - if (message.startsWith('\$')) { + // If it's start with $, it means it's a eventId + if (responseMessage.startsWith('\$')) { return true; } return false;