@@ -3,7 +3,9 @@ import 'dart:convert';
3
3
4
4
import 'package:back_button_interceptor/back_button_interceptor.dart' ;
5
5
import 'package:core/core.dart' ;
6
+ import 'package:cozy/cozy_config_manager/cozy_config_manager.dart' ;
6
7
import 'package:dartz/dartz.dart' ;
8
+ import 'package:debounce_throttle/debounce_throttle.dart' ;
7
9
import 'package:email_recovery/email_recovery/email_recovery_action.dart' ;
8
10
import 'package:email_recovery/email_recovery/email_recovery_action_id.dart' ;
9
11
import 'package:flutter/material.dart' ;
@@ -292,6 +294,8 @@ class MailboxDashBoardController extends ReloadableController
292
294
bool _isFirstSessionLoad = false ;
293
295
DeepLinksManager ? _deepLinksManager;
294
296
StreamSubscription <DeepLinkData ?>? _deepLinkDataStreamSubscription;
297
+ StreamSubscription <html.PopStateEvent >? _popStateEventStreamSubscription;
298
+ Debouncer <html.PopStateEvent ?>? _popStateDebouncer;
295
299
296
300
final StreamController <Either <Failure , Success >> _progressStateController =
297
301
StreamController <Either <Failure , Success >>.broadcast ();
@@ -345,6 +349,7 @@ class MailboxDashBoardController extends ReloadableController
345
349
}
346
350
_registerStreamListener ();
347
351
BackButtonInterceptor .add (onBackButtonInterceptor, name: AppRoutes .dashboard);
352
+ registerCozyPopState ();
348
353
WidgetsBinding .instance.addPostFrameCallback ((_) async {
349
354
await applicationManager.initUserAgent ();
350
355
});
@@ -1910,6 +1915,7 @@ class MailboxDashBoardController extends ReloadableController
1910
1915
1911
1916
void goToSettings () async {
1912
1917
closeMailboxMenuDrawer ();
1918
+ unregisterCozyPopState ();
1913
1919
BackButtonInterceptor .removeByName (AppRoutes .dashboard);
1914
1920
final result = await push (
1915
1921
AppRoutes .settings,
@@ -1920,6 +1926,7 @@ class MailboxDashBoardController extends ReloadableController
1920
1926
);
1921
1927
1922
1928
BackButtonInterceptor .add (onBackButtonInterceptor, name: AppRoutes .dashboard);
1929
+ registerCozyPopState ();
1923
1930
1924
1931
if (result is Tuple2 ) {
1925
1932
if (result.value1 is VacationResponse ) {
@@ -1978,6 +1985,7 @@ class MailboxDashBoardController extends ReloadableController
1978
1985
}
1979
1986
1980
1987
void goToVacationSetting () async {
1988
+ unregisterCozyPopState ();
1981
1989
BackButtonInterceptor .removeByName (AppRoutes .dashboard);
1982
1990
final result = await push (
1983
1991
AppRoutes .settings,
@@ -1989,6 +1997,7 @@ class MailboxDashBoardController extends ReloadableController
1989
1997
);
1990
1998
1991
1999
BackButtonInterceptor .add (onBackButtonInterceptor, name: AppRoutes .dashboard);
2000
+ registerCozyPopState ();
1992
2001
1993
2002
if (result is Tuple2 ) {
1994
2003
if (result.value1 is VacationResponse ) {
@@ -2927,6 +2936,18 @@ class MailboxDashBoardController extends ReloadableController
2927
2936
bool _navigateToScreen () {
2928
2937
log ('MailboxDashBoardController::_navigateToScreen: dashboardRoute: $dashboardRoute ' );
2929
2938
switch (dashboardRoute.value) {
2939
+ case DashboardRoutes .threadDetailed:
2940
+ if (PlatformInfo .isMobile) {
2941
+ if (currentContext != null && canBack (currentContext! )) {
2942
+ return false ;
2943
+ } else {
2944
+ clearSelectedEmail ();
2945
+ return true ;
2946
+ }
2947
+ } else {
2948
+ clearSelectedEmail ();
2949
+ return true ;
2950
+ }
2930
2951
case DashboardRoutes .emailDetailed:
2931
2952
if (PlatformInfo .isMobile) {
2932
2953
if (currentContext != null && canBack (currentContext! )) {
@@ -3265,6 +3286,7 @@ class MailboxDashBoardController extends ReloadableController
3265
3286
_notificationManager.closeStream ();
3266
3287
_fcmService.closeStream ();
3267
3288
applicationManager.releaseUserAgent ();
3289
+ unregisterCozyPopState ();
3268
3290
BackButtonInterceptor .removeByName (AppRoutes .dashboard);
3269
3291
_identities = null ;
3270
3292
outboxMailbox = null ;
@@ -3275,6 +3297,28 @@ class MailboxDashBoardController extends ReloadableController
3275
3297
_currentEmailState = null ;
3276
3298
_isFirstSessionLoad = false ;
3277
3299
twakeAppManager.setHasComposer (false );
3300
+ _popStateEventStreamSubscription? .cancel ();
3278
3301
super .onClose ();
3279
3302
}
3303
+
3304
+ Future <void > registerCozyPopState () async {
3305
+ final isInsideCozy = await CozyConfigManager ().isInsideCozy;
3306
+ if (! isInsideCozy) return ;
3307
+
3308
+ _popStateDebouncer = Debouncer (
3309
+ const Duration (milliseconds: 100 ),
3310
+ initialValue: null ,
3311
+ onChanged: (value) {
3312
+ onBackButtonInterceptor (false , RouteInfo ());
3313
+ },
3314
+ );
3315
+ _popStateEventStreamSubscription = html.window.onPopState.listen ((event) {
3316
+ _popStateDebouncer? .value = event;
3317
+ });
3318
+ }
3319
+
3320
+ void unregisterCozyPopState () {
3321
+ _popStateDebouncer? .cancel ();
3322
+ _popStateEventStreamSubscription? .cancel ();
3323
+ }
3280
3324
}
0 commit comments