initialize method
Initializes this collection from startingPoint.
Implementation
Future<void> initialize() async {
if (_isDisposed) return;
if (_isInitialized) {
throw InvalidInitializationException();
}
sbLog.i(StackTrace.current, 'initialize()');
_isInitialized = true;
_initializeParams.inclusive = true;
final messages =
await _chat.apiClient.send<List<BaseMessage>>(ChannelMessagesGetRequest(
_chat,
channelType: ChannelType.group,
channelUrl: _channel.channelUrl,
params: _initializeParams.toJson(),
timestamp: _startingPoint,
));
if (_isDisposed) return;
if (messages.isNotEmpty) {
final oldestMessage =
_initializeParams.reverse ? messages.last : messages.first;
final latestMessage =
_initializeParams.reverse ? messages.first : messages.last;
_oldestMessage = oldestMessage;
_latestMessage = latestMessage;
final maxCreatedAt = messages
.reduce((current, next) =>
current.createdAt > next.createdAt ? current : next)
.createdAt;
if (_startingPoint > maxCreatedAt) {
_hasPrevious = messages.length == _initializeParams.previousResultSize;
_hasNext = false;
} else if (_startingPoint == 0) {
_hasPrevious = false;
_hasNext = messages.length == _initializeParams.nextResultSize;
} else {
final previousMessages =
messages.where((e) => e.createdAt < _startingPoint).toList();
final nextMessages =
messages.where((e) => e.createdAt > _startingPoint).toList();
_hasPrevious = previousMessages.length -
_getExistedMessageCountInMessageList(previousMessages) ==
_initializeParams.previousResultSize;
_hasNext = nextMessages.length -
_getExistedMessageCountInMessageList(nextMessages) ==
_initializeParams.nextResultSize;
}
} else {
_hasPrevious = false;
_hasNext = false;
}
if (messages.isNotEmpty) {
_chat.collectionManager.sendEventsToMessageCollection(
messageCollection: this,
groupChannel: _channel,
eventSource: CollectionEventSource.messageInitialize,
sendingStatus: SendingStatus.succeeded,
addedMessages: messages,
isReversedAddedMessages: _initializeParams.reverse,
);
}
}