import 'dart:collection'; import 'package:collection/collection.dart'; import 'package:dartz/dartz.dart'; import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart'; import 'package:model/model.dart'; import 'mailbox_node.dart'; import 'mailbox_tree.dart'; class TreeBuilder { Future generateMailboxTree(List mailboxesList) async { final Map mailboxDictionary = HashMap(); final tree = MailboxTree(MailboxNode.root()); for (var mailbox in mailboxesList) { mailboxDictionary[mailbox.id] = MailboxNode(mailbox); } for (var mailbox in mailboxesList) { final parentId = mailbox.parentId; final parentNode = mailboxDictionary[parentId]; final node = mailboxDictionary[mailbox.id]; if (node != null) { if (parentNode != null) { parentNode.addChildNode(node); parentNode.childrenItems?.sortByCompare( (node) => node.item.name, (name, other) => name?.compareAlphabetically(other) ?? -1 ); } else { tree.root.addChildNode(node); tree.root.childrenItems?.sortByCompare( (node) => node.item.name, (name, other) => name?.compareAlphabetically(other) ?? -1 ); } } } return tree; } Future>> generateMailboxTreeInUI( List allMailboxes, {MailboxId? mailboxIdSelected} ) async { final Map mailboxDictionary = HashMap(); final defaultTree = MailboxTree(MailboxNode.root()); final folderTree = MailboxTree(MailboxNode.root()); List listAllMailboxes = []; for (var mailbox in allMailboxes) { if (mailbox.id == mailboxIdSelected) { mailboxDictionary[mailbox.id] = MailboxNode( mailbox.withMailboxSate(MailboxState.deactivated), nodeState: MailboxState.deactivated); } else { mailboxDictionary[mailbox.id] = MailboxNode(mailbox); } } for (var mailbox in allMailboxes) { final parentId = mailbox.parentId; final parentNode = mailboxDictionary[parentId]; final node = mailboxDictionary[mailbox.id]; if (node != null) { if (parentNode != null) { if (parentNode.nodeState == MailboxState.deactivated) { node.updateItem(mailbox.withMailboxSate(MailboxState.deactivated)); node.updateNodeState(MailboxState.deactivated); parentNode.addChildNode(node); listAllMailboxes.add(node.item); } else { parentNode.addChildNode(node); listAllMailboxes.add(node.item); } parentNode.childrenItems?.sortByCompare( (node) => node.item.name, (name, other) => name?.compareAlphabetically(other) ?? -1 ); } else { listAllMailboxes.add(node.item); var tree = mailbox.hasRole() ? defaultTree : folderTree; tree.root.addChildNode(node); tree.root.childrenItems?.sortByCompare( (node) => node.item.name, (name, other) => name?.compareAlphabetically(other) ?? -1 ); } } } defaultTree.root.childrenItems?.sort((thisMailbox, thatMailbox) => thisMailbox.compareTo(thatMailbox)); return Tuple3(defaultTree, folderTree, listAllMailboxes); } Future> generateMailboxTreeInUIAfterRefreshChanges( List allMailboxes, MailboxTree defaultTreeBeforeChanges, MailboxTree folderTreeBeforeChanges, ) async { final Map mailboxDictionary = HashMap(); final newDefaultTree = MailboxTree(MailboxNode.root()); final newFolderTree = MailboxTree(MailboxNode.root()); for (var mailbox in allMailboxes) { final mailboxNodeBeforeChanges = defaultTreeBeforeChanges.findNode((node) => node.item.id == mailbox.id) ?? folderTreeBeforeChanges.findNode((node) => node.item.id == mailbox.id); if (mailboxNodeBeforeChanges != null) { mailboxDictionary[mailbox.id] = MailboxNode( mailbox, expandMode: mailboxNodeBeforeChanges.expandMode, selectMode: mailboxNodeBeforeChanges.selectMode); } else { mailboxDictionary[mailbox.id] = MailboxNode(mailbox); } } for (var mailbox in allMailboxes) { final parentId = mailbox.parentId; final parentNode = mailboxDictionary[parentId]; final node = mailboxDictionary[mailbox.id]; if (node != null) { if (parentNode != null) { parentNode.addChildNode(node); parentNode.childrenItems?.sortByCompare( (node) => node.item.name, (name, other) => name?.compareAlphabetically(other) ?? -1 ); } else { var tree = mailbox.hasRole() ? newDefaultTree : newFolderTree; tree.root.addChildNode(node); tree.root.childrenItems?.sortByCompare( (node) => node.item.name, (name, other) => name?.compareAlphabetically(other) ?? -1 ); } } } newDefaultTree.root.childrenItems?.sort((thisMailbox, thatMailbox) => thisMailbox.compareTo(thatMailbox)); return Tuple2(newDefaultTree, newFolderTree); } }