Skip to content

Commit

Permalink
feat: workaround onResume when popping all
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Sep 20, 2023
1 parent 96c707f commit 829be76
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
71 changes: 67 additions & 4 deletions app_navigator/lib/src/content_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ class ContentNavigatorBloc extends BaseBloc {
return await item.completer.future as T;
}

/*
void transientPop([Object? result]) {
if (_stack.isNotEmpty) {
transientPopItem(_stack.length - 1, result);
}
}*/
/// Pop until a matching path or push it
void popUntilPathOrPush(BuildContext context, ContentPath path) {
if (_routeAwareManager != null) {
Expand All @@ -218,7 +224,49 @@ class ContentNavigatorBloc extends BaseBloc {
}
}

/// Returns true if found
void transientPopUntilPath(BuildContext context, ContentPath path) {
if (_routeAwareManager != null) {
_routeAwareManager!.popLock.synchronized(() {
_popUntilPath(context, path);

/// Late cleanup
routeAwareManager.popLock.synchronized(() {
routeAwareManager.popPaths.clear();
});
});
} else {
_popUntilPath(context, path);
}
}

/// Pop until a matching path or push it
void transientPop(BuildContext context, [Object? result]) {
if (_routeAwareManager != null) {
_routeAwareManager!.popLock.synchronized(() {
routeAwareManager.popTransient = true;
_transientPop(context, result);

/// Late cleanup
routeAwareManager.popLock.synchronized(() {
routeAwareManager.popTransient = false;
});
});
} else {
_transientPop(context, result);
}
}

void _popUntilPathOrPush(BuildContext context, ContentPath path) {
var found =
_popUntilPath(context, path); // print('popUntil($path) found $found');
if (!found) {
pushPath<void>(path);
}
}

/// Returns true if fount
bool _popUntilPath(BuildContext context, ContentPath path) {
var found = false;

Navigator.of(context).popUntil((route) {
Expand All @@ -236,10 +284,12 @@ class ContentNavigatorBloc extends BaseBloc {
}
return false;
});
// print('popUntil($path) found $found');
if (!found) {
pushPath<void>(path);
}
return found;
}

/// Pop last no onResume
void _transientPop(BuildContext context, Object? result) {
Navigator.of(context).pop(result);
}

// Use ContentNavigator.push instead
Expand Down Expand Up @@ -482,15 +532,28 @@ class ContentNavigator extends StatefulWidget {
@override
State<ContentNavigator> createState() => _ContentNavigatorState();

/// Push a path setting.
static Future<T?> push<T>(
BuildContext context, ContentPathRouteSettings rs) async {
return await ContentNavigator.of(context).push<T>(rs);
}

/// Do not trigger onResume()...WIP
static void transientPop(BuildContext context, [Object? result]) {
return ContentNavigator.of(context).transientPop(context, result);
}

/// Pop until a matching path or push it
/// Do not trigger onResume()...WIP
static void popUntilPathOrPush(BuildContext context, ContentPath path) {
ContentNavigator.of(context).popUntilPathOrPush(context, path);
}

/// Pop until a matching path
/// Do not trigger onResume()...WIP
static void transientPopUntilPath(BuildContext context, ContentPath path) {
ContentNavigator.of(context).transientPopUntilPath(context, path);
}
}

class _ContentNavigatorState extends State<ContentNavigator> {
Expand Down
6 changes: 6 additions & 0 deletions app_navigator/lib/src/route_aware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class RouteAwareManager {
RouteObserver<ModalRoute<void>>();

final popLock = Lock();

/// Only valid during pop
var popPaths = <String>[];
var popTransient = false;
}

final routeAwareManager = RouteAwareManager();
Expand Down Expand Up @@ -63,6 +66,9 @@ mixin RouteAwareMixin<T extends StatefulWidget> on State<T>

@override
void didPopNext() {
if (routeAwareManager.popTransient) {
return;
}
if (widget is RouteAwareWithPath) {
var needOnResume = true;
var path = (widget as RouteAwareWithPath).contentPath;
Expand Down
2 changes: 1 addition & 1 deletion app_navigator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: tekartik_app_navigator_flutter
description: Experimental app navigator flutter
version: 0.4.6
version: 0.4.7
publish_to: none

environment:
Expand Down

0 comments on commit 829be76

Please sign in to comment.