interleave<T> method
function to mix the incoming and outgoing messages by timestamp
Implementation
Future<List<Message>> interleave<T>(List a, List b) async {
List result = [];
final ita = a.iterator;
final itb = b.iterator;
bool hasa = ita.moveNext();
bool hasb = itb.moveNext();
Message valueA, valueB;
while (hasa | hasb) {
if (hasa && hasb) {
valueA = Message.fromJson(ita.current);
if (valueA.contentType == MessageContentType.image) {
valueA.imageData = await getImage(valueA.message ?? '');
}
valueB = Message.fromJson(itb.current);
valueB.type = MessageType.incoming;
if (valueB.contentType == MessageContentType.image) {
valueB.imageData = await getImage(valueB.message ?? '');
}
if ((valueA.time ?? 0) > (valueB.time ?? 0)) {
result.add(valueA);
hasa = ita.moveNext();
} else {
result.add(valueB);
hasb = itb.moveNext();
}
} else if (hasa) {
valueA = Message.fromJson(ita.current);
if (valueA.contentType == MessageContentType.image) {
valueA.imageData = await getImage(valueA.message ?? '');
}
result.add(valueA);
while (hasa = ita.moveNext()) {
valueA = Message.fromJson(ita.current);
if (valueA.contentType == MessageContentType.image) {
valueA.imageData = await getImage(valueA.message ?? '');
}
result.add(valueA);
}
} else if (hasb) {
valueB = Message.fromJson(itb.current);
valueB.type = MessageType.incoming;
if (valueB.contentType == MessageContentType.image) {
valueB.imageData = await getImage(valueB.message ?? '');
}
result.add(valueB);
while (hasb = itb.moveNext()) {
valueB = Message.fromJson(itb.current);
valueB.type = MessageType.incoming;
if (valueB.contentType == MessageContentType.image) {
valueB.imageData = await getImage(valueB.message ?? '');
}
result.add(valueB);
}
}
}
List<Message> finalResult = List<Message>.from(result);
return finalResult;
}