Create a fun

This commit is contained in:
Benoit Marty 2020-05-05 00:30:49 +02:00
parent fcee85a682
commit db77e7b817
1 changed files with 60 additions and 51 deletions

View File

@ -64,60 +64,69 @@ class MergedHeaderItemFactory @Inject constructor(private val avatarRenderer: Av
} else if (!event.canBeMerged() || (nextEvent?.root?.getClearType() == event.root.getClearType() && !addDaySeparator)) { } else if (!event.canBeMerged() || (nextEvent?.root?.getClearType() == event.root.getClearType() && !addDaySeparator)) {
null null
} else { } else {
val prevSameTypeEvents = items.prevSameTypeEvents(currentPosition, 2) buildMembershipEventsMergedSummary(currentPosition, items, event, eventIdToHighlight, requestModelBuild, callback)
if (prevSameTypeEvents.isEmpty()) { }
null }
} else {
var highlighted = false private fun buildMembershipEventsMergedSummary(currentPosition: Int,
val mergedEvents = (prevSameTypeEvents + listOf(event)).asReversed() items: List<TimelineEvent>,
val mergedData = ArrayList<BasedMergedItem.Data>(mergedEvents.size) event: TimelineEvent,
mergedEvents.forEach { mergedEvent -> eventIdToHighlight: String?,
if (!highlighted && mergedEvent.root.eventId == eventIdToHighlight) { requestModelBuild: () -> Unit,
highlighted = true callback: TimelineEventController.Callback?): MergedMembershipEventsItem_? {
} val prevSameTypeEvents = items.prevSameTypeEvents(currentPosition, 2)
val senderAvatar = mergedEvent.senderAvatar return if (prevSameTypeEvents.isEmpty()) {
val senderName = mergedEvent.getDisambiguatedDisplayName() null
val data = BasedMergedItem.Data( } else {
userId = mergedEvent.root.senderId ?: "", var highlighted = false
avatarUrl = senderAvatar, val mergedEvents = (prevSameTypeEvents + listOf(event)).asReversed()
memberName = senderName, val mergedData = ArrayList<BasedMergedItem.Data>(mergedEvents.size)
localId = mergedEvent.localId, mergedEvents.forEach { mergedEvent ->
eventId = mergedEvent.root.eventId ?: "" if (!highlighted && mergedEvent.root.eventId == eventIdToHighlight) {
) highlighted = true
mergedData.add(data)
} }
val mergedEventIds = mergedEvents.map { it.localId } val senderAvatar = mergedEvent.senderAvatar
// We try to find if one of the item id were used as mergeItemCollapseStates key val senderName = mergedEvent.getDisambiguatedDisplayName()
// => handle case where paginating from mergeable events and we get more val data = BasedMergedItem.Data(
val previousCollapseStateKey = mergedEventIds.intersect(mergeItemCollapseStates.keys).firstOrNull() userId = mergedEvent.root.senderId ?: "",
val initialCollapseState = mergeItemCollapseStates.remove(previousCollapseStateKey) avatarUrl = senderAvatar,
?: true memberName = senderName,
val isCollapsed = mergeItemCollapseStates.getOrPut(event.localId) { initialCollapseState } localId = mergedEvent.localId,
if (isCollapsed) { eventId = mergedEvent.root.eventId ?: ""
collapsedEventIds.addAll(mergedEventIds)
} else {
collapsedEventIds.removeAll(mergedEventIds)
}
val mergeId = mergedEventIds.joinToString(separator = "_") { it.toString() }
val attributes = MergedMembershipEventsItem.Attributes(
isCollapsed = isCollapsed,
mergeData = mergedData,
avatarRenderer = avatarRenderer,
onCollapsedStateChanged = {
mergeItemCollapseStates[event.localId] = it
requestModelBuild()
},
readReceiptsCallback = callback
) )
MergedMembershipEventsItem_() mergedData.add(data)
.id(mergeId)
.leftGuideline(avatarSizeProvider.leftGuideline)
.highlighted(isCollapsed && highlighted)
.attributes(attributes)
.also {
it.setOnVisibilityStateChanged(MergedTimelineEventVisibilityStateChangedListener(callback, mergedEvents))
}
} }
val mergedEventIds = mergedEvents.map { it.localId }
// We try to find if one of the item id were used as mergeItemCollapseStates key
// => handle case where paginating from mergeable events and we get more
val previousCollapseStateKey = mergedEventIds.intersect(mergeItemCollapseStates.keys).firstOrNull()
val initialCollapseState = mergeItemCollapseStates.remove(previousCollapseStateKey)
?: true
val isCollapsed = mergeItemCollapseStates.getOrPut(event.localId) { initialCollapseState }
if (isCollapsed) {
collapsedEventIds.addAll(mergedEventIds)
} else {
collapsedEventIds.removeAll(mergedEventIds)
}
val mergeId = mergedEventIds.joinToString(separator = "_") { it.toString() }
val attributes = MergedMembershipEventsItem.Attributes(
isCollapsed = isCollapsed,
mergeData = mergedData,
avatarRenderer = avatarRenderer,
onCollapsedStateChanged = {
mergeItemCollapseStates[event.localId] = it
requestModelBuild()
},
readReceiptsCallback = callback
)
MergedMembershipEventsItem_()
.id(mergeId)
.leftGuideline(avatarSizeProvider.leftGuideline)
.highlighted(isCollapsed && highlighted)
.attributes(attributes)
.also {
it.setOnVisibilityStateChanged(MergedTimelineEventVisibilityStateChangedListener(callback, mergedEvents))
}
} }
} }