Skip to content

Commit

Permalink
TW-898: update chat and pinned events to handle back from pinned screen
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn authored and hoangdat committed Dec 20, 2023
1 parent 04103d6 commit d4c2ffa
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
18 changes: 14 additions & 4 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ class ChatController extends State<Chat>
inputFocus.requestFocus();
}

void scrollToEventIdAndHighlight(String eventId) {
Future<void> scrollToEventIdAndHighlight(String eventId) {
return scrollToEventId(eventId, highlight: true);
}

Expand Down Expand Up @@ -868,8 +868,7 @@ class ChatController extends State<Chat>
}
}

void scrollToEventId(String eventId, {bool highlight = false}) async {
if (timeline == null) return;
Future<void> scrollToEventId(String eventId, {bool highlight = false}) async {
final eventIndex = timeline!.events.indexWhere((e) => e.eventId == eventId);
if (eventIndex == -1) {
timeline = null;
Expand All @@ -896,7 +895,7 @@ class ChatController extends State<Chat>
);
if (highlight) {
await scrollController.highlight(
index,
index + 1,
);
}
setState(() {});
Expand Down Expand Up @@ -1466,6 +1465,17 @@ class ChatController extends State<Chat>
}
}

void handlePopBackFromPinnedScreen(Object? popResult) async {
Logs().d(
"PinnedEventsController()::handlePopBack(): popResult: $popResult",
);
if (popResult is Event) {
scrollToEventIdAndHighlight(popResult.eventId);
} else if (popResult is List<Event?>) {
pinnedEventsController.handlePopBack(popResult);
}
}

@override
Widget build(BuildContext context) {
return ChatView(this, key: widget.key);
Expand Down
22 changes: 20 additions & 2 deletions lib/pages/chat/chat_pinned_events/pinned_events_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -96,6 +99,21 @@ class PinnedEventsController {
);
}

void handlePopBack(Object? popResult) {
Logs().d(
"PinnedEventsController()::handlePopBack(): popResult: $popResult",
);
if (popResult is List<Event?>) {
final room = popResult.first?.room;
if (room != null) {
getPinnedMessageAction(
room: room,
isInitial: true,
);
}
}
}

void jumpToCurrentMessage(
List<Event?> pinnedEvents, {
String? eventId,
Expand Down
2 changes: 2 additions & 0 deletions lib/pages/chat/chat_pinned_events/pinned_events_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions lib/pages/chat/chat_pinned_events/pinned_events_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
},
),
],
Expand Down
9 changes: 5 additions & 4 deletions lib/utils/matrix_sdk_extensions/event_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d4c2ffa

Please sign in to comment.