TF-4384 Fix page not found 404 error when refreshing label views (#4390)

This commit is contained in:
Dat Vu
2026-03-21 17:23:18 +07:00
committed by GitHub
parent 8d82eac16f
commit 8a9901a0a4
13 changed files with 220 additions and 21 deletions
@@ -82,6 +82,7 @@ import 'package:tmail_ui_user/features/email/presentation/utils/email_action_rea
import 'package:tmail_ui_user/features/email/presentation/utils/email_utils.dart';
import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/action/mailbox_ui_action.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/presentation_mailbox_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/action/download_ui_action.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/handle_download_attachment_extension.dart';
@@ -1069,18 +1070,19 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
void _replaceBrowserHistory() {
if (PlatformInfo.isWeb) {
final selectedMailboxId = mailboxDashBoardController.selectedMailbox.value?.id;
final selectedMailbox = mailboxDashBoardController.selectedMailbox.value;
final isSearchRunning = mailboxDashBoardController.searchController.isSearchEmailRunning;
RouteUtils.replaceBrowserHistory(
title: isSearchRunning
? 'SearchEmail'
: 'Mailbox-${selectedMailboxId?.id.value}',
: selectedMailbox?.browserRouteTitle ?? '',
url: RouteUtils.createUrlWebLocationBar(
AppRoutes.dashboard,
router: NavigationRouter(
mailboxId: isSearchRunning
? null
: selectedMailboxId,
: selectedMailbox?.browserRouteMailboxId,
labelId: selectedMailbox?.labelId,
dashboardType: isSearchRunning
? DashboardType.search
: DashboardType.normal,
@@ -2,6 +2,7 @@ import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
import 'package:dartz/dartz.dart' hide State;
import 'package:flutter/material.dart' hide State;
import 'package:get/get.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
@@ -38,6 +39,8 @@ class LabelController extends BaseController with LabelContextMenuMixin {
final labels = <Label>[].obs;
final labelListExpandMode = Rx(ExpandMode.EXPAND);
final isLabelSettingEnabled = RxBool(false);
final isLabelsLoaded = RxBool(false);
final GlobalKey labelAppBarKey = GlobalKey();
GetAllLabelInteractor? _getAllLabelInteractor;
CreateNewLabelInteractor? _createNewLabelInteractor;
@@ -72,11 +75,17 @@ class LabelController extends BaseController with LabelContextMenuMixin {
isLabelSettingEnabled.value = false;
isLabelSettingEnabled.refresh();
_clearLabelData();
setLabelLoaded();
}
}
void setLabelLoaded() {
isLabelsLoaded.value = true;
}
void _clearLabelData() {
labels.clear();
isLabelsLoaded.value = false;
}
void injectLabelsBindings() {
@@ -114,7 +123,11 @@ class LabelController extends BaseController with LabelContextMenuMixin {
}
void getAllLabels(AccountId accountId) {
if (_getAllLabelInteractor == null) return;
if (_getAllLabelInteractor == null) {
labels.value = [];
setLabelLoaded();
return;
}
consumeState(_getAllLabelInteractor!.execute(accountId));
}
@@ -173,6 +186,9 @@ class LabelController extends BaseController with LabelContextMenuMixin {
if (isEnabled) {
injectLabelsBindings();
getAllLabels(accountId);
} else {
_clearLabelData();
setLabelLoaded();
}
}
@@ -181,6 +197,7 @@ class LabelController extends BaseController with LabelContextMenuMixin {
if (success is GetAllLabelSuccess) {
labels.value = success.labels..sortByAlphabetically();
setCurrentLabelState(success.newState);
setLabelLoaded();
} else if (success is CreateNewLabelSuccess) {
_handleCreateNewLabelSuccess(success);
} else if (success is GetLabelSettingStateSuccess) {
@@ -198,12 +215,14 @@ class LabelController extends BaseController with LabelContextMenuMixin {
void handleFailureViewState(Failure failure) {
if (failure is GetAllLabelFailure) {
labels.value = [];
setLabelLoaded();
} else if (failure is CreateNewLabelFailure) {
_handleCreateNewLabelFailure(failure);
} else if (failure is GetLabelSettingStateFailure) {
isLabelSettingEnabled.value = false;
isLabelSettingEnabled.refresh();
_clearLabelData();
setLabelLoaded();
} else if (failure is EditLabelFailure) {
handleEditLabelFailure(failure);
} else if (failure is DeleteALabelFailure) {
@@ -368,6 +368,7 @@ abstract class BaseMailboxView extends GetWidget<MailboxController>
final countLabels = labelController.labels.length;
return LabelsBarWidget(
key: labelController.labelAppBarKey,
imagePaths: controller.imagePaths,
isDesktop: isDesktop,
height: isDesktop ? 48 : 40,
@@ -1,6 +1,7 @@
import 'package:core/presentation/resources/image_paths.dart';
import 'package:core/utils/platform_info.dart';
import 'package:flutter/material.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:labels/model/label.dart';
import 'package:tmail_ui_user/features/labels/presentation/extensions/handle_label_action_type_extension.dart';
import 'package:tmail_ui_user/features/labels/presentation/models/label_action_type.dart';
@@ -64,4 +65,22 @@ extension HandleLabelActionTypeExtension on MailboxDashBoardController {
),
);
}
Label? getLabelById(Id labelId) {
return labelController.labels
.where((label) => label.id == labelId)
.firstOrNull;
}
void scrollToLabelListView() {
final context = labelController.labelAppBarKey.currentContext;
if (context == null) return;
Scrollable.ensureVisible(
context,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut,
alignment: 0.5,
);
}
}
@@ -0,0 +1,104 @@
import 'dart:ui';
import 'package:core/utils/app_logger.dart';
import 'package:get/get_rx/src/rx_workers/rx_workers.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/handle_label_action_type_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/mailbox_controller.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/presentation_label_mailbox.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/labels/handle_logic_label_extension.dart';
import 'package:tmail_ui_user/main/routes/app_routes.dart';
import 'package:tmail_ui_user/main/routes/navigation_router.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
extension HandleNavigationExtension on MailboxController {
void handleLabelNavigation(NavigationRouter router, Id labelId) {
if (!_isLabelCapabilityValid()) {
_navigateToUnknown();
return;
}
if (!_isLabelsReady()) {
_waitForLabelsLoaded(() {
_handleLabelNavigationInternal(router, labelId);
});
return;
}
_handleLabelNavigationInternal(router, labelId);
}
bool _isLabelCapabilityValid() {
return mailboxDashBoardController.isLabelCapabilitySupported;
}
bool _isLabelsReady() {
return mailboxDashBoardController.labelController.isLabelsLoaded.value;
}
void _waitForLabelsLoaded(VoidCallback onLoaded) {
isLabelsLoadedWorker?.dispose();
isLabelsLoadedWorker = null;
final observable = mailboxDashBoardController.labelController.isLabelsLoaded;
if (observable.value) {
onLoaded();
return;
}
isLabelsLoadedWorker = ever(
observable,
(isLoaded) {
try {
if (!isLoaded) return;
_completeLabelsLoaded(onLoaded);
} catch (e) {
logWarning('HandleNavigationExtension::_waitForLabelsLoaded: Exception: $e');
}
},
);
}
void _completeLabelsLoaded(VoidCallback onLoaded) {
final worker = isLabelsLoadedWorker;
if (worker == null) return;
isLabelsLoadedWorker = null;
worker.dispose();
if (!isClosed) {
onLoaded();
}
}
void _handleLabelNavigationInternal(
NavigationRouter router,
Id labelId,
) {
final matchedLabel = mailboxDashBoardController.getLabelById(labelId);
if (matchedLabel == null) {
_navigateToUnknown();
return;
}
final labelMailbox = PresentationLabelMailbox.initial(matchedLabel);
if (router.emailId != null) {
openEmailInsideMailboxFromLocationBar(
labelMailbox,
router.emailId!,
);
} else {
openMailboxFromLocationBar(labelMailbox);
}
mailboxDashBoardController.scrollToLabelListView();
}
void _navigateToUnknown() {
clearNavigationRouter();
popAndPush(AppRoutes.unknownRoutePage);
}
}
@@ -1,8 +1,11 @@
import 'package:core/presentation/resources/image_paths.dart';
import 'package:flutter/material.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/mail/email/keyword_identifier.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:labels/extensions/label_extension.dart';
import 'package:model/extensions/mailbox_id_extension.dart';
import 'package:model/extensions/presentation_mailbox_extension.dart';
import 'package:model/mailbox/presentation_mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/presentation_label_mailbox.dart';
@@ -160,4 +163,13 @@ extension PresentationMailboxExtension on PresentationMailbox {
bool get isCacheable => !isVirtualFolder && this is! PresentationLabelMailbox;
bool get isLabelMailbox => this is PresentationLabelMailbox;
String get browserRouteTitle => isLabelMailbox
? 'Label-${(this as PresentationLabelMailbox).label.id?.value}'
: 'Mailbox-${mailboxId?.asString}';
Id? get labelId =>
isLabelMailbox ? (this as PresentationLabelMailbox).label.id : null;
MailboxId? get browserRouteMailboxId => isLabelMailbox ? null : mailboxId;
}
@@ -70,6 +70,7 @@ import 'package:tmail_ui_user/features/mailbox/domain/usecases/subscribe_mailbox
import 'package:tmail_ui_user/features/mailbox/domain/usecases/subscribe_multiple_mailbox_interactor.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/action/mailbox_ui_action.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/handle_action_required_tab_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/handle_navigation_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/presentation_mailbox_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/mixin/mailbox_widget_mixin.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_actions.dart';
@@ -133,6 +134,7 @@ class MailboxController extends BaseMailboxController
MailboxId? _newFolderId;
NavigationRouter? _navigationRouter;
WebSocketQueueHandler? _webSocketQueueHandler;
Worker? isLabelsLoadedWorker;
final _openMailboxEventController = StreamController<OpenMailboxViewEvent>();
StreamSubscription? _openMailboxEventStreamSubscription;
@@ -198,6 +200,8 @@ class MailboxController extends BaseMailboxController
_openMailboxEventController.close();
mailboxListScrollController.dispose();
_webSocketQueueHandler?.dispose();
isLabelsLoadedWorker?.dispose();
isLabelsLoadedWorker = null;
super.onClose();
}
@@ -787,7 +791,9 @@ class MailboxController extends BaseMailboxController
}
break;
case DashboardType.normal:
if (_navigationRouter!.mailboxId != null) {
if (_navigationRouter!.labelId != null) {
handleLabelNavigation(_navigationRouter!, _navigationRouter!.labelId!);
} else if (_navigationRouter!.mailboxId != null) {
final matchedMailboxNode = findMailboxNodeById(_navigationRouter!.mailboxId!);
if (matchedMailboxNode != null) {
if (_navigationRouter!.emailId != null) {
@@ -813,6 +819,11 @@ class MailboxController extends BaseMailboxController
}
}
void openEmailInsideMailboxFromLocationBar(
PresentationMailbox presentationMailbox,
EmailId emailId
) => _openEmailInsideMailboxFromLocationBar(presentationMailbox, emailId);
void _openEmailInsideMailboxFromLocationBar(
PresentationMailbox presentationMailbox,
EmailId emailId
@@ -822,15 +833,19 @@ class MailboxController extends BaseMailboxController
_clearNavigationRouter();
}
void openMailboxFromLocationBar(PresentationMailbox presentationMailbox) =>
_openMailboxFromLocationBar(presentationMailbox);
void _openMailboxFromLocationBar(PresentationMailbox presentationMailbox) {
mailboxDashBoardController.setSelectedMailbox(presentationMailbox);
if (PlatformInfo.isWeb) {
RouteUtils.replaceBrowserHistory(
title: 'Mailbox-${presentationMailbox.mailboxId?.id.value}',
title: presentationMailbox.browserRouteTitle,
url: RouteUtils.createUrlWebLocationBar(
AppRoutes.dashboard,
router: NavigationRouter(
mailboxId: presentationMailbox.mailboxId,
mailboxId: presentationMailbox.browserRouteMailboxId,
labelId: presentationMailbox.labelId,
dashboardType: DashboardType.normal
)
)
@@ -868,6 +883,8 @@ class MailboxController extends BaseMailboxController
_clearNavigationRouter();
}
void clearNavigationRouter() => _clearNavigationRouter();
void _clearNavigationRouter() {
_navigationRouter = null;
}
@@ -1279,13 +1296,14 @@ class MailboxController extends BaseMailboxController
}
void _replaceBrowserHistory() {
log('MailboxController::_replaceBrowserHistory:selectedMailbox: ${selectedMailbox?.id}');
final currentMailbox = selectedMailbox;
log('MailboxController::_replaceBrowserHistory:selectedMailbox: ${currentMailbox?.id.asString}');
if (PlatformInfo.isWeb && Get.currentRoute.startsWith(AppRoutes.dashboard)) {
final selectedMailboxId = selectedMailbox?.id;
final route = RouteUtils.createUrlWebLocationBar(
AppRoutes.dashboard,
router: NavigationRouter(
mailboxId: selectedMailboxId,
mailboxId: currentMailbox?.browserRouteMailboxId,
labelId: currentMailbox?.labelId,
searchQuery: mailboxDashBoardController.searchController.isSearchEmailRunning
? mailboxDashBoardController.searchController.searchQuery
: null,
@@ -1295,7 +1313,7 @@ class MailboxController extends BaseMailboxController
)
);
RouteUtils.replaceBrowserHistory(
title: 'Mailbox-${selectedMailboxId?.id.value}',
title: currentMailbox?.browserRouteTitle ?? '',
url: route
);
}
@@ -3064,7 +3064,7 @@ class MailboxDashBoardController extends ReloadableController
void _replaceBrowserHistory({Uri? uri}) {
log('MailboxDashBoardController::_replaceBrowserHistory:uri: $uri');
if (PlatformInfo.isWeb) {
final selectedMailboxId = selectedMailbox.value?.id;
final currentMailbox = selectedMailbox.value;
final selectedEmailId = selectedEmail.value?.id;
final isSearchRunning = searchController.isSearchEmailRunning;
String title = '';
@@ -3073,7 +3073,7 @@ class MailboxDashBoardController extends ReloadableController
} else if (isSearchRunning) {
title = 'SearchEmail';
} else {
title = 'Mailbox-${selectedMailboxId?.asString}';
title = currentMailbox?.browserRouteTitle ?? '';
}
RouteUtils.replaceBrowserHistory(
title: title,
@@ -3083,7 +3083,8 @@ class MailboxDashBoardController extends ReloadableController
emailId: selectedEmail.value?.id,
mailboxId: isSearchRunning
? null
: selectedMailboxId,
: currentMailbox?.browserRouteMailboxId,
labelId: currentMailbox?.labelId,
dashboardType: isSearchRunning
? DashboardType.search
: DashboardType.normal,
@@ -39,6 +39,7 @@ import 'package:tmail_ui_user/features/email/domain/state/move_to_mailbox_state.
import 'package:tmail_ui_user/features/email/presentation/action/email_ui_action.dart';
import 'package:tmail_ui_user/features/email/presentation/utils/email_utils.dart';
import 'package:tmail_ui_user/features/home/data/exceptions/session_exceptions.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/presentation_mailbox_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/model/mailbox_actions.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/model/recent_search.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/state/get_all_recent_search_latest_state.dart';
@@ -895,12 +896,14 @@ class SearchEmailController extends BaseController
mailboxDashBoardController.searchController.disableAllSearchEmail();
mailboxDashBoardController.dispatchRoute(DashboardRoutes.thread);
if (PlatformInfo.isWeb) {
final currentMailbox = mailboxDashBoardController.selectedMailbox.value;
RouteUtils.replaceBrowserHistory(
title: 'Mailbox-${mailboxDashBoardController.selectedMailbox.value?.id.id.value}',
title: currentMailbox?.browserRouteTitle ?? '',
url: RouteUtils.createUrlWebLocationBar(
AppRoutes.dashboard,
router: NavigationRouter(
mailboxId: mailboxDashBoardController.selectedMailbox.value?.id,
mailboxId: currentMailbox?.browserRouteMailboxId,
labelId: currentMailbox?.labelId,
dashboardType: DashboardType.normal
)
)
@@ -5,6 +5,7 @@ import 'package:model/email/presentation_email.dart';
import 'package:model/extensions/presentation_email_extension.dart';
import 'package:model/mailbox/presentation_mailbox.dart';
import 'package:tmail_ui_user/features/email/presentation/extensions/email_extension.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/extensions/presentation_mailbox_extension.dart';
import 'package:tmail_ui_user/features/thread/domain/model/search_query.dart';
import 'package:tmail_ui_user/features/thread_detail/domain/model/email_in_thread_detail_info.dart';
import 'package:tmail_ui_user/main/routes/app_routes.dart';
@@ -48,7 +49,10 @@ extension ListPresentationEmailExtensions on List<PresentationEmail> {
AppRoutes.dashboard,
router: NavigationRouter(
emailId: currentEmail.id,
mailboxId: isSearchEmailRunning ? null : selectedMailbox?.id,
mailboxId: isSearchEmailRunning
? null
: selectedMailbox?.browserRouteMailboxId,
labelId: selectedMailbox?.labelId,
searchQuery: isSearchEmailRunning ? searchQuery : null,
dashboardType: isSearchEmailRunning ? DashboardType.search : DashboardType.normal
)
@@ -1469,7 +1469,8 @@ class ThreadController extends BaseController with EmailActionController {
AppRoutes.dashboard,
router: NavigationRouter(
emailId: email.id,
mailboxId: mailboxContain.mailboxId,
mailboxId: mailboxContain.browserRouteMailboxId,
labelId: mailboxContain.labelId,
dashboardType: DashboardType.normal
)
));
+8 -1
View File
@@ -1,5 +1,6 @@
import 'package:equatable/equatable.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/email/email_address.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
@@ -14,6 +15,7 @@ enum DashboardType {
class NavigationRouter with EquatableMixin {
final EmailId? emailId;
final MailboxId? mailboxId;
final Id? labelId;
final DashboardType dashboardType;
final SearchQuery? searchQuery;
final String? routeName;
@@ -36,7 +38,11 @@ class NavigationRouter with EquatableMixin {
this.accountMenuItem = AccountMenuItem.none,
this.cc,
this.bcc,
});
this.labelId,
}) : assert(
!(mailboxId != null && labelId != null),
'NavigationRouter accepts either mailboxId or labelId, not both.',
);
factory NavigationRouter.initial() => NavigationRouter();
@@ -53,5 +59,6 @@ class NavigationRouter with EquatableMixin {
accountMenuItem,
cc,
bcc,
labelId,
];
}
+10 -2
View File
@@ -20,6 +20,7 @@ abstract class RouteUtils {
static const String paramID = 'id';
static const String paramType = 'type';
static const String paramContext = 'context';
static const String paramLabelId = 'labelId';
static const String paramQuery = 'q';
static const String paramRouteName = 'routeName';
static const String paramMailtoAddress = 'mailtoAddress';
@@ -60,7 +61,9 @@ abstract class RouteUtils {
}
servicePath = servicePath.withQueryParameters([
StringQueryParameter(paramType, router.dashboardType.name),
if (router.mailboxId != null)
if (router.labelId != null)
StringQueryParameter(paramLabelId, router.labelId!.value)
else if (router.mailboxId != null)
StringQueryParameter(paramContext, router.mailboxId!.id.value),
if (router.searchQuery != null)
StringQueryParameter(paramQuery, router.searchQuery!.value),
@@ -124,6 +127,7 @@ abstract class RouteUtils {
final idParam = parameters[paramID];
final typeParam = parameters[paramType];
final contextPram = parameters[paramContext];
final labelIdPram = parameters[paramLabelId];
final queryParam = parameters[paramQuery];
final routeName = parameters[paramRouteName];
final mailtoAddress = parameters[paramMailtoAddress];
@@ -133,7 +137,10 @@ abstract class RouteUtils {
final body = parameters[paramBody];
final emailId = idParam != null ? EmailId(Id(idParam)) : null;
final mailboxId = contextPram != null ? MailboxId(Id(contextPram)) : null;
final labelId = labelIdPram != null ? Id(labelIdPram) : null;
final mailboxId = labelId == null && contextPram != null
? MailboxId(Id(contextPram))
: null;
final searchQuery = queryParam != null ? SearchQuery(queryParam) : null;
final dashboardType = DashboardType.values.firstWhereOrNull((type) => type.name == typeParam) ?? DashboardType.normal;
final settingType = AccountMenuItem.values.firstWhereOrNull((type) => type.getAliasBrowser() == typeParam) ?? AccountMenuItem.none;
@@ -147,6 +154,7 @@ abstract class RouteUtils {
return NavigationRouter(
emailId: emailId,
mailboxId: mailboxId,
labelId: labelId,
searchQuery: searchQuery,
dashboardType: dashboardType,
routeName: routeName,