-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TW-1519: Automatically synchronize pin unpin (#1531)
- Loading branch information
Showing
5 changed files
with
223 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
lib/presentation/extensions/event_update_extension.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import 'package:collection/collection.dart'; | ||
import 'package:matrix/matrix.dart'; | ||
|
||
typedef OnPinnedMessageUpdated = void Function({ | ||
required bool isInitial, | ||
required bool isUnpin, | ||
String? eventId, | ||
}); | ||
|
||
extension EventUpdateExtension on EventUpdate { | ||
String? get eventId { | ||
final id = content['event_id']; | ||
if (id != null) { | ||
return id; | ||
} | ||
return null; | ||
} | ||
|
||
bool get isPinnedEventsHasChanged { | ||
return content['type'] == EventTypes.RoomPinnedEvents; | ||
} | ||
|
||
bool _isPinnedListChanged( | ||
Map<String, dynamic> content, | ||
Map<String, dynamic> prevContent, | ||
) { | ||
final pinnedList = content['pinned']; | ||
final prevPinnedList = prevContent['pinned']; | ||
|
||
return pinnedList != null || prevPinnedList != null; | ||
} | ||
|
||
void updatePinnedMessage({ | ||
required OnPinnedMessageUpdated onPinnedMessageUpdated, | ||
}) { | ||
if (!isPinnedEventsHasChanged) return; | ||
final content = this.content['content']; | ||
final unsignedContent = this.content['unsigned']; | ||
final prevContent = | ||
unsignedContent != null ? unsignedContent['prev_content'] : null; | ||
if (content != null && prevContent != null) { | ||
if (!_isPinnedListChanged(content, prevContent)) return; | ||
|
||
final pinnedList = content['pinned']; | ||
final prevPinnedList = prevContent['pinned']; | ||
|
||
if (pinnedList.length == prevPinnedList.length) return; | ||
|
||
if (pinnedList.length > prevPinnedList.length) { | ||
_handlePinnedListIncreased( | ||
pinnedList, | ||
prevPinnedList, | ||
onPinnedMessageUpdated, | ||
); | ||
} else if (pinnedList.length < prevPinnedList.length) { | ||
_handlePinnedListDecreased( | ||
pinnedList, | ||
prevPinnedList, | ||
onPinnedMessageUpdated, | ||
); | ||
} | ||
} | ||
} | ||
|
||
void _handlePinnedListIncreased( | ||
List<dynamic> pinnedList, | ||
List<dynamic> prevPinnedList, | ||
OnPinnedMessageUpdated onPinnedMessageUpdated, | ||
) { | ||
if (prevPinnedList.isEmpty) { | ||
return onPinnedMessageUpdated( | ||
isInitial: true, | ||
isUnpin: false, | ||
); | ||
} | ||
final eventId = pinnedList.firstWhereOrNull( | ||
(event) => !prevPinnedList.contains(event), | ||
); | ||
return onPinnedMessageUpdated( | ||
isInitial: false, | ||
isUnpin: false, | ||
eventId: eventId, | ||
); | ||
} | ||
|
||
void _handlePinnedListDecreased( | ||
List<dynamic> pinnedList, | ||
List<dynamic> prevPinnedList, | ||
OnPinnedMessageUpdated onPinnedMessageUpdated, | ||
) { | ||
if (pinnedList.isEmpty) { | ||
return onPinnedMessageUpdated( | ||
isInitial: false, | ||
isUnpin: true, | ||
); | ||
} | ||
final eventId = prevPinnedList.firstWhereOrNull( | ||
(event) => !pinnedList.contains(event), | ||
); | ||
return onPinnedMessageUpdated( | ||
isInitial: false, | ||
isUnpin: true, | ||
eventId: eventId, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1551,7 +1551,7 @@ packages: | |
description: | ||
path: "." | ||
ref: "twake-supported-0.22.6" | ||
resolved-ref: "599c37af897625f936bdab98d56d3deef789c171" | ||
resolved-ref: "40bab63ea8895015aed935bd3fb6c279c6fe294c" | ||
url: "[email protected]:linagora/matrix-dart-sdk.git" | ||
source: git | ||
version: "0.22.6" | ||
|