Init mailbox feature add presentation layer
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import 'package:built_collection/built_collection.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:model/mailbox/presentation_mailbox.dart';
|
||||
|
||||
class MailboxNode with EquatableMixin{
|
||||
static PresentationMailbox _root = PresentationMailbox(MailboxId(Id('root')));
|
||||
|
||||
PresentationMailbox item;
|
||||
late BuiltList<MailboxNode> childrenItems;
|
||||
|
||||
factory MailboxNode.root() => MailboxNode(_root);
|
||||
|
||||
bool hasChildren() => childrenItems.isNotEmpty;
|
||||
|
||||
MailboxNode(this.item, {List<MailboxNode> child = const []}) {
|
||||
this.childrenItems = BuiltList(child);
|
||||
}
|
||||
|
||||
MailboxNode? addChild(PresentationMailbox mailbox) {
|
||||
if (_validateChild(mailbox)) {
|
||||
final node = MailboxNode(mailbox);
|
||||
_addChildNode(node);
|
||||
return node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void _addChildNode(MailboxNode node) {
|
||||
childrenItems = (ListBuilder<MailboxNode>(childrenItems)
|
||||
..add(node))
|
||||
.build();
|
||||
}
|
||||
|
||||
bool _validateChild(PresentationMailbox mailbox) {
|
||||
if (mailbox.parentId == item.id) {
|
||||
return true;
|
||||
}
|
||||
if (item == _root) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [item, childrenItems];
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:model/mailbox/presentation_mailbox.dart';
|
||||
|
||||
import 'mailbox_node.dart';
|
||||
|
||||
class MailboxTree with EquatableMixin {
|
||||
MailboxNode root;
|
||||
MailboxTree(this.root);
|
||||
|
||||
MailboxNode? findNode(MailboxId? mailboxId) {
|
||||
var result;
|
||||
final queue = ListQueue<MailboxNode>();
|
||||
queue.addLast(root);
|
||||
while (queue.isNotEmpty && mailboxId != null) {
|
||||
final currentNode = queue.removeFirst();
|
||||
if (mailboxId == currentNode.item.id) {
|
||||
result = currentNode;
|
||||
break;
|
||||
}
|
||||
currentNode.childrenItems.forEach((child) {
|
||||
queue.addLast(child);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
MailboxNode? insertNode(PresentationMailbox mailbox) {
|
||||
if (mailbox.parentId == null) {
|
||||
return root.addChild(mailbox);
|
||||
}
|
||||
|
||||
final foundParent = findNode(mailbox.parentId);
|
||||
return foundParent != null
|
||||
? foundParent.addChild(mailbox)
|
||||
: root.addChild(mailbox);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [root];
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:built_collection/built_collection.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:model/mailbox/presentation_mailbox.dart';
|
||||
|
||||
import 'mailbox_node.dart';
|
||||
import 'mailbox_tree.dart';
|
||||
|
||||
class TreeBuilder {
|
||||
Future<MailboxTree> generateMailboxTree(List<PresentationMailbox> mailboxesList) async {
|
||||
final Map<MailboxId, BuiltList<PresentationMailbox>> mapNotFoundParentMailbox = HashMap();
|
||||
|
||||
final tree = MailboxTree(MailboxNode.root());
|
||||
mailboxesList.forEach((mailbox) {
|
||||
if (mailbox.hasParentId()) {
|
||||
final parentId = mailbox.parentId!;
|
||||
final foundParent = tree.findNode(parentId);
|
||||
if (foundParent != null) {
|
||||
_addDescendant(tree, mailbox, maybeDescendant: mapNotFoundParentMailbox);
|
||||
} else {
|
||||
final siblingTrees = mapNotFoundParentMailbox[mailbox.parentId];
|
||||
if (siblingTrees != null) {
|
||||
mapNotFoundParentMailbox.addAll({
|
||||
parentId: (ListBuilder<PresentationMailbox>(siblingTrees)..add(mailbox)).build()
|
||||
});
|
||||
} else {
|
||||
mapNotFoundParentMailbox.addAll({parentId: (ListBuilder<PresentationMailbox>()..add(mailbox)).build()});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_addDescendant(tree, mailbox, maybeDescendant: mapNotFoundParentMailbox);
|
||||
}
|
||||
});
|
||||
mapNotFoundParentMailbox.forEach((_, listMailbox) {
|
||||
listMailbox.forEach((mailbox) {
|
||||
_addDescendant(tree, mailbox);
|
||||
});
|
||||
});
|
||||
return tree;
|
||||
}
|
||||
|
||||
MailboxNode? _addDescendant(
|
||||
MailboxTree tree,
|
||||
PresentationMailbox mailboxChild,
|
||||
{Map<MailboxId, BuiltList<PresentationMailbox>>? maybeDescendant}
|
||||
) {
|
||||
final queue = ListQueue<PresentationMailbox>();
|
||||
queue.addLast(mailboxChild);
|
||||
while (queue.isNotEmpty) {
|
||||
final current = queue.removeFirst();
|
||||
tree.insertNode(current);
|
||||
maybeDescendant?.remove(current.id)?.forEach((child) {
|
||||
queue.addLast(child);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user