Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
83 changes: 60 additions & 23 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 @@ -88,39 +90,58 @@ abstract class BaseMailboxController extends BaseController
List<PresentationMailbox> allMailboxes = <PresentationMailbox>[];

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,
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,
);
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.teamTree;
allMailboxes = mailboxCollection.allMailboxes;
}

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

void autoCreateVirtualFolder(bool isAINeedsActionEnabled) {
addFavoriteFolderToMailboxList();
bool get isAINeedsActionEnabled => false;

MailboxCollection autoCreateVirtualFolder({
required MailboxCollection mailboxCollection,
}) {
MailboxCollection newMailboxCollection = addFavoriteFolderToMailboxList(
mailboxCollection: mailboxCollection,
);
if (isAINeedsActionEnabled) {
addActionRequiredFolder();
newMailboxCollection = addActionRequiredFolder(
mailboxCollection: newMailboxCollection,
);
}

return newMailboxCollection;
}

MailboxCollection updateMailboxCollection(
MailboxCollection mailboxCollection,
) {
return autoCreateVirtualFolder(mailboxCollection: mailboxCollection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,29 @@ 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);
final newDefaultTree = _addToDefaultMailboxTree(
folder: folder,
currentDefaultTree: mailboxCollection.defaultTree,
);
final newAllMailboxes = _addToAllMailboxes(
folder: folder,
currentAllMailboxes: mailboxCollection.allMailboxes,
);

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

PresentationMailbox _buildActionRequiredFolder() {
Expand All @@ -23,24 +37,37 @@ 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 ?? []);

children.insertAfterStarredOrInbox(MailboxNode(folder));

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

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;
}
currentAllMailboxes.add(folder);
return currentAllMailboxes;
}

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,42 @@ 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;
MailboxTree _addFavoriteFolderToDefaultMailboxTree({
required PresentationMailbox favoriteFolder,
required MailboxTree defaultTree,
}) {
final defaultMailboxNode = defaultTree.root;
List<MailboxNode> currentDefaultFolders =
defaultMailboxNode.childrenItems ?? [];

Expand All @@ -32,17 +47,22 @@ extension HandleFavoriteTabExtension on BaseMailboxController {
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;
}
}
Loading
Loading