Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 58 additions & 29 deletions lib/features/base/base_mailbox_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import 'package:tmail_ui_user/features/mailbox/presentation/extensions/presentat
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_actions.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_categories.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_categories_expand_mode.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_collection.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_node.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_tree.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_tree_builder.dart';
Expand All @@ -62,6 +63,7 @@ typedef OnMoveFolderContentActionCallback = void Function(
);
typedef DeleteMailboxActionCallback = void Function(PresentationMailbox mailbox);
typedef AllowSubaddressingActionCallback = void Function(MailboxId, Map<String, List<String>?>?, MailboxActions);
typedef OnUpdateMailboxCollectionCallback = MailboxCollection Function(MailboxCollection);

abstract class BaseMailboxController extends BaseController
with ExpandFolderTriggerScrollableMixin {
Expand All @@ -87,40 +89,62 @@ abstract class BaseMailboxController extends BaseController

List<PresentationMailbox> allMailboxes = <PresentationMailbox>[];

MailboxCollection get currentMailboxCollection => MailboxCollection(
allMailboxes: allMailboxes,
defaultTree: defaultMailboxTree.value,
personalTree: personalMailboxTree.value,
teamMailboxTree: teamMailboxesTree.value,
);

Future<void> buildTree(
List<PresentationMailbox> allMailbox,
{MailboxId? mailboxIdSelected}
) async {
final recordTree = await _treeBuilder.generateMailboxTreeInUI(
List<PresentationMailbox> allMailbox, {
MailboxId? mailboxIdSelected,
OnUpdateMailboxCollectionCallback? onUpdateMailboxCollectionCallback,
}) async {
MailboxCollection mailboxCollection =
await _treeBuilder.generateMailboxTreeInUI(
allMailboxes: allMailbox,
currentDefaultTree: defaultMailboxTree.value,
currentPersonalTree: personalMailboxTree.value,
currentTeamMailboxTree: teamMailboxesTree.value,
currentCollection: currentMailboxCollection,
mailboxIdSelected: mailboxIdSelected,
);
defaultMailboxTree.firstRebuild = true;
personalMailboxTree.firstRebuild = true;
teamMailboxesTree.firstRebuild = true;
defaultMailboxTree.value = recordTree.defaultTree;
personalMailboxTree.value = recordTree.personalTree;
teamMailboxesTree.value = recordTree.teamMailboxTree;
allMailboxes = recordTree.allMailboxes;

if (onUpdateMailboxCollectionCallback != null) {
mailboxCollection = onUpdateMailboxCollectionCallback(mailboxCollection);
}

updateMailboxTree(mailboxCollection: mailboxCollection);
}

Future<void> refreshTree(List<PresentationMailbox> allMailbox) async {
final recordTree = await _treeBuilder.generateMailboxTreeInUIAfterRefreshChanges(
Future<void> refreshTree(
List<PresentationMailbox> allMailbox, {
OnUpdateMailboxCollectionCallback? onUpdateMailboxCollectionCallback,
}) async {
MailboxCollection mailboxCollection =
await _treeBuilder.generateMailboxTreeInUIAfterRefreshChanges(
allMailboxes: allMailbox,
currentDefaultTree: defaultMailboxTree.value,
currentPersonalTree: personalMailboxTree.value,
currentTeamMailboxTree: teamMailboxesTree.value,
currentCollection: currentMailboxCollection,
);
defaultMailboxTree.firstRebuild = true;
personalMailboxTree.firstRebuild = true;
teamMailboxesTree.firstRebuild = true;
defaultMailboxTree.value = recordTree.defaultTree;
personalMailboxTree.value = recordTree.personalTree;
teamMailboxesTree.value = recordTree.teamMailboxTree;
allMailboxes = allMailbox;

if (onUpdateMailboxCollectionCallback != null) {
mailboxCollection = onUpdateMailboxCollectionCallback(mailboxCollection);
}

updateMailboxTree(mailboxCollection: mailboxCollection);
}

void updateMailboxTree({
required MailboxCollection mailboxCollection,
bool isRefreshTrigger = true,
}) {
if (isRefreshTrigger) {
defaultMailboxTree.firstRebuild = true;
personalMailboxTree.firstRebuild = true;
teamMailboxesTree.firstRebuild = true;
}
defaultMailboxTree.value = mailboxCollection.defaultTree;
personalMailboxTree.value = mailboxCollection.personalTree;
teamMailboxesTree.value = mailboxCollection.teamMailboxTree;
allMailboxes = mailboxCollection.allMailboxes;
}

void syncAllMailboxWithDisplayName(BuildContext context) {
Expand Down Expand Up @@ -675,10 +699,15 @@ abstract class BaseMailboxController extends BaseController
}
}

void autoCreateVirtualFolder(bool isAINeedsActionEnabled) {
addFavoriteFolderToMailboxList();
bool get isAINeedsActionEnabled => false;
Comment thread
tddang-linagora marked this conversation as resolved.

MailboxCollection updateMailboxCollection(MailboxCollection mailboxCollection) {
MailboxCollection updated = addFavoriteFolderToMailboxList(
mailboxCollection: mailboxCollection,
);
if (isAINeedsActionEnabled) {
addActionRequiredFolder();
updated = addActionRequiredFolder(mailboxCollection: updated);
}
return updated;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@ import 'package:model/mailbox/presentation_mailbox.dart';
import 'package:tmail_ui_user/features/base/base_mailbox_controller.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/list_mailbox_node_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/presentation_mailbox_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_collection.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_node.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_tree.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart';

extension HandleActionRequiredTabExtension on BaseMailboxController {
void addActionRequiredFolder() {
MailboxCollection addActionRequiredFolder({
required MailboxCollection mailboxCollection,
}) {
final folder = _buildActionRequiredFolder();
_addToDefaultMailboxTree(folder);
_addToAllMailboxes(folder);
return mailboxCollection.copyWith(
defaultTree: _addToDefaultMailboxTree(
folder: folder,
currentDefaultTree: mailboxCollection.defaultTree,
),
allMailboxes: _addToAllMailboxes(
folder: folder,
currentAllMailboxes: mailboxCollection.allMailboxes,
),
);
}

PresentationMailbox _buildActionRequiredFolder() {
Expand All @@ -23,43 +34,72 @@ extension HandleActionRequiredTabExtension on BaseMailboxController {
);
}

void _addToDefaultMailboxTree(PresentationMailbox folder) {
final root = defaultMailboxTree.value.root;
MailboxTree _addToDefaultMailboxTree({
required PresentationMailbox folder,
required MailboxTree currentDefaultTree,
}) {
final root = currentDefaultTree.root;
final children = List<MailboxNode>.from(root.childrenItems ?? []);
if (children.any((node) => node.item.id == folder.id)) {
return currentDefaultTree;
}

children.insertAfterStarredOrInbox(MailboxNode(folder));

defaultMailboxTree.value = MailboxTree(
root.copyWith(children: children),
);
return MailboxTree(root.copyWith(children: children));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

void _addToAllMailboxes(PresentationMailbox folder) {
if (_allMailboxesContains(folder.id)) return;
allMailboxes.add(folder);
List<PresentationMailbox> _addToAllMailboxes({
required PresentationMailbox folder,
required List<PresentationMailbox> currentAllMailboxes,
}) {
if (_allMailboxesContains(
id: folder.id,
currentAllMailboxes: currentAllMailboxes,
)) {
return currentAllMailboxes;
}
return [...currentAllMailboxes, folder];
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

bool _allMailboxesContains(MailboxId id) {
return allMailboxes.any((mailbox) => mailbox.id == id);
bool _allMailboxesContains({
required MailboxId id,
required List<PresentationMailbox> currentAllMailboxes,
}) {
return currentAllMailboxes.any((mailbox) => mailbox.id == id);
}

void removeActionRequiredFolder() {
final folder = PresentationMailbox.actionRequiredFolder;
_removeFromDefaultMailboxTree(folder.id);
_removeFromAllMailboxes(folder.id);
MailboxCollection removeActionRequiredFolder({
required MailboxCollection mailboxCollection,
}) {
return mailboxCollection.copyWith(
defaultTree: _removeFromDefaultMailboxTree(
folderId: PresentationMailbox.actionRequiredFolder.id,
currentDefaultTree: mailboxCollection.defaultTree,
),
allMailboxes: _removeFromAllMailboxes(
folderId: PresentationMailbox.actionRequiredFolder.id,
currentAllMailboxes: mailboxCollection.allMailboxes,
),
);
}

void _removeFromDefaultMailboxTree(MailboxId folderId) {
final root = defaultMailboxTree.value.root;
MailboxTree _removeFromDefaultMailboxTree({
required MailboxId folderId,
required MailboxTree currentDefaultTree,
}) {
final root = currentDefaultTree.root;
final children = List<MailboxNode>.from(root.childrenItems ?? [])
..removeWhere((node) => node.item.id == folderId);

defaultMailboxTree.value = MailboxTree(
root.copyWith(children: children),
);
return MailboxTree(root.copyWith(children: children));
}

void _removeFromAllMailboxes(MailboxId folderId) {
allMailboxes.removeWhere((mailbox) => mailbox.id == folderId);
List<PresentationMailbox> _removeFromAllMailboxes({
required MailboxId folderId,
required List<PresentationMailbox> currentAllMailboxes,
}) {
return currentAllMailboxes
.where((mailbox) => mailbox.id != folderId)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,65 @@ import 'package:model/mailbox/presentation_mailbox.dart';
import 'package:tmail_ui_user/features/base/base_mailbox_controller.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/list_mailbox_node_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/presentation_mailbox_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_collection.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_node.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_tree.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart';

extension HandleFavoriteTabExtension on BaseMailboxController {
void addFavoriteFolderToMailboxList() {
MailboxCollection addFavoriteFolderToMailboxList({
required MailboxCollection mailboxCollection,
}) {
PresentationMailbox favoriteFolder = PresentationMailbox.favoriteFolder;
if (currentContext != null) {
favoriteFolder = favoriteFolder.copyWith(
displayName: favoriteFolder.getDisplayName(currentContext!),
);
}

_addFavoriteFolderToDefaultMailboxTree(favoriteFolder);
_addFavoriteFolderToAllMailboxes(favoriteFolder);
final newDefaultTree = _addFavoriteFolderToDefaultMailboxTree(
favoriteFolder: favoriteFolder,
defaultTree: mailboxCollection.defaultTree,
);
final newAllMailboxes = _addFavoriteFolderToAllMailboxes(
favoriteFolder: favoriteFolder,
allMailboxes: mailboxCollection.allMailboxes,
);

return mailboxCollection.copyWith(
defaultTree: newDefaultTree,
allMailboxes: newAllMailboxes,
);
}

void _addFavoriteFolderToDefaultMailboxTree(
PresentationMailbox favoriteFolder,
) {
final defaultMailboxNode = defaultMailboxTree.value.root;
List<MailboxNode> currentDefaultFolders =
defaultMailboxNode.childrenItems ?? [];
MailboxTree _addFavoriteFolderToDefaultMailboxTree({
required PresentationMailbox favoriteFolder,
required MailboxTree defaultTree,
}) {
final defaultMailboxNode = defaultTree.root;
Comment thread
hoangdat marked this conversation as resolved.
final currentDefaultFolders =
List<MailboxNode>.from(defaultMailboxNode.childrenItems ?? []);

if (currentDefaultFolders.isEmpty) {
currentDefaultFolders.add(MailboxNode(favoriteFolder));
} else {
currentDefaultFolders.insertAfterInbox(MailboxNode(favoriteFolder));
}

defaultMailboxTree.value = MailboxTree(
return MailboxTree(
defaultMailboxNode.copyWith(children: currentDefaultFolders),
);
}

void _addFavoriteFolderToAllMailboxes(PresentationMailbox favoriteFolder) {
List<PresentationMailbox> _addFavoriteFolderToAllMailboxes({
required PresentationMailbox favoriteFolder,
required List<PresentationMailbox> allMailboxes,
}) {
final alreadyExists = allMailboxes.any(
(mailbox) => mailbox.id == favoriteFolder.id,
);
if (alreadyExists) return;
if (alreadyExists) return allMailboxes;

allMailboxes.add(favoriteFolder);
return [...allMailboxes, favoriteFolder];
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
Loading
Loading