Skip to content

Commit fa83295

Browse files
authored
chore: move timeline code (#94)
1 parent b95f5a4 commit fa83295

File tree

6 files changed

+53
-27
lines changed

6 files changed

+53
-27
lines changed

lib/chat_room/common/view/chat_room_page.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class _ChatRoomPageState extends State<ChatRoomPage> {
4444
@override
4545
void initState() {
4646
super.initState();
47-
_timelineFuture = widget.room.getTimeline(
47+
_timelineFuture = di<TimelineModel>().loadTimeline(
48+
widget.room,
4849
onNewEvent: () => _roomListKey.currentState?.setState(() {}),
4950
onChange: (i) => _roomListKey.currentState?.setState(() {}),
5051
onInsert: (i) => _roomListKey.currentState?.insertItem(i),

lib/chat_room/timeline/chat_room_timeline_list.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class _ChatRoomTimelineListState extends State<ChatRoomTimelineList> {
5050
@override
5151
void dispose() {
5252
_controller.dispose();
53+
widget.timeline.cancelSubscriptions();
5354
super.dispose();
5455
}
5556

@@ -71,6 +72,10 @@ class _ChatRoomTimelineListState extends State<ChatRoomTimelineList> {
7172
).data ??
7273
[];
7374

75+
watchPropertyValue(
76+
(TimelineModel m) => m.getUpdatingTimeline(widget.timeline.room.id),
77+
);
78+
7479
return Stack(
7580
children: [
7681
NotificationListener<ScrollEndNotification>(

lib/chat_room/timeline/timeline_model.dart

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,33 @@ import '../../common/event_x.dart';
55
import '../../common/logging.dart';
66

77
class TimelineModel extends SafeChangeNotifier {
8+
Future<Timeline> loadTimeline(
9+
Room room, {
10+
void Function(int)? onChange,
11+
void Function(int)? onRemove,
12+
void Function(int)? onInsert,
13+
void Function()? onNewEvent,
14+
void Function()? onUpdate,
15+
String? eventContextId,
16+
int? limit = Room.defaultHistoryCount,
17+
}) async {
18+
// This calls await postLoad() and then creates a new Timeline object
19+
final timeline = await room.getTimeline(
20+
onChange: onChange,
21+
onRemove: onRemove,
22+
onInsert: onInsert,
23+
onNewEvent: onNewEvent,
24+
onUpdate: onUpdate,
25+
eventContextId: eventContextId,
26+
limit: limit,
27+
);
28+
29+
_setTimeline(timeline: timeline);
30+
return timeline;
31+
}
32+
833
Future<void> postTimelineLoad(Timeline timeline) async {
9-
await loadRoomStates(timeline.room);
10-
await loadAllKeysFromRoom(timeline);
34+
_requestKeysForUndecryptableEvents(timeline);
1135
await requestHistory(timeline, historyCount: 500);
1236
await trySetReadMarker(timeline);
1337
}
@@ -71,26 +95,10 @@ class TimelineModel extends SafeChangeNotifier {
7195
}
7296
}
7397

74-
Future<void> loadAllKeysFromRoom(Timeline timeline) async {
98+
// Request the keys for undecryptable events of this timeline
99+
void _requestKeysForUndecryptableEvents(Timeline timeline) {
75100
try {
76-
for (final event
77-
in timeline.events
78-
.toList(growable: false)
79-
.where(
80-
(e) =>
81-
e.isEncryptedAndCouldDecrypt &&
82-
!_hasLoadedKeys.contains(e.eventId),
83-
)) {
84-
await event.requestKey();
85-
await timeline.room.client.encryption?.decryptRoomEvent(
86-
event,
87-
store: event.stateKey != null,
88-
);
89-
_hasLoadedKeys.add(event.eventId);
90-
printMessageInDebugMode(
91-
'Decrypted event ${event.eventId} in room ${timeline.room.getLocalizedDisplayname()}',
92-
);
93-
}
101+
timeline.requestKeys(onlineKeyBackupOnly: false);
94102
} on Exception catch (e, s) {
95103
printMessageInDebugMode(e, s);
96104
}

lib/common/event_x.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ extension EventX on Event {
3232
MatrixLocalizations? i18n,
3333
}) {
3434
i18n ??= i18n = const MatrixDefaultLocalizations();
35+
36+
if (type == EventTypes.RoomPinnedEvents) {
37+
return 'Event was pinned';
38+
}
39+
3540
final maybeSpaceChildOrParentId =
3641
'${stateKey?.isNotEmpty == true && room.client.getRoomById(stateKey!) != null ? room.client.getRoomById(stateKey!)?.getLocalizedDisplayname() : l10n.unavailable}';
3742
if (type == 'm.space.parent') {
@@ -50,8 +55,11 @@ extension EventX on Event {
5055
return calcLocalizedBodyFallback(i18n);
5156
}
5257

53-
bool get showAsSpecialBadge =>
54-
{'m.space.parent', 'm.space.child'}.contains(type);
58+
bool get showAsSpecialBadge => {
59+
'm.space.parent',
60+
'm.space.child',
61+
EventTypes.RoomPinnedEvents,
62+
}.contains(type);
5563

5664
bool get showAsBadge =>
5765
type.contains('m.call.') ||

lib/common/view/theme.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ Color getMonochromeBg({
8080
: darkFactor ?? factor),
8181
);
8282

83-
Color getEventBadgeColor(ThemeData theme, String type) =>
84-
type == 'm.space.parent' || type == 'm.space.child'
83+
Color getEventBadgeColor(ThemeData theme, bool showAsSpecialBadge) =>
84+
showAsSpecialBadge
8585
? theme.colorScheme.primary
8686
: theme.colorScheme.onSurface.withValues(alpha: 0.2);
8787

lib/events/view/chat_message_badge.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:matrix/matrix.dart';
33

4+
import '../../common/event_x.dart';
45
import '../../common/view/build_context_x.dart';
56
import '../../common/view/theme.dart';
67
import '../../common/view/ui_constants.dart';
@@ -33,7 +34,10 @@ class ChatMessageBadge extends StatelessWidget {
3334
Flexible(
3435
child: Badge(
3536
textColor: getEventBadgeTextColor(theme, displayEvent.type),
36-
backgroundColor: getEventBadgeColor(theme, displayEvent.type),
37+
backgroundColor: getEventBadgeColor(
38+
theme,
39+
displayEvent.showAsSpecialBadge,
40+
),
3741
label: ConstrainedBox(
3842
constraints: const BoxConstraints(
3943
maxWidth: 400,

0 commit comments

Comments
 (0)